Arduino Sim800L Relay Control with SMS | Control Home Appliances with Arduino and Sim800L

Spread the love

Below is a sample program that can be used to Control a Relay Module using SMS. This program uses a SIM800L and Arduino Nano. Although it can be used with any Arduino Board.

In this program, the SIM800L is connected to the Arduino Nano’s RX and TX pins, and the relay module is connected to digital pins 4 and 5. The program sets the SIM800L to text mode and enables the receiving of SMS messages. In the loop() function, the program checks if there are any incoming SMS messages and, if so, looks for specific keywords (“#RELAY1 ON”, “#RELAY1 OFF”, “#RELAY2 ON”, “#RELAY2 OFF”) to turn the relays on or off.

You would also need to provide the SIM card to SIM800L for the SMS functionality.

Please note that this is a basic example, and you may need to modify it to suit your specific needs and hardware setup.

 

#include <SoftwareSerial.h>

SoftwareSerial SIM800L(2, 3); // RX, TX

const int relay1 = 4;
const int relay2 = 5;

void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
SIM800L.begin(9600);
delay(3000);
SIM800L.println("AT+CMGF=1"); // set SMS mode to text
delay(1000);
SIM800L.println("AT+CNMI=2,2,0,0,0"); // receive SMS
delay(1000);
}

void loop() {
if (SIM800L.available() &gt; 0) {
String message = SIM800L.readString();
if (message.indexOf("#RELAY1 ON") &gt;= 0) {
digitalWrite(relay1, HIGH);
} else if (message.indexOf("#RELAY1 OFF") &gt;= 0) {
digitalWrite(relay1, LOW);
} else if (message.indexOf("#RELAY2 ON") &gt;= 0) {
digitalWrite(relay2, HIGH);
} else if (message.indexOf("#RELAY2 OFF") &gt;= 0) {
digitalWrite(relay2, LOW);
}
}
}