Sim800l add relay control using sms

hi guys kinda get stoped in my progaming i need someone to help me with the code to do the following, i already have in my code the use of a push botton to send a sms.
now i want to be able to send a sms and put a relay on and delay 2000 auto off
if possible the code should be able to chose only the white list numbers to put on the relay

thanks
code


SoftwareSerial sim800l(2, 3); // RX,TX for Arduino and for the module it's TXD RXD, they should be inverted

#define button1 7 //Button pin, on the other pin it's wired with GND
#define RELAY_PIN 9
bool button_State; //Button state
bool prev_State  = HIGH;

void setup()
{

 pinMode(button1, INPUT_PULLUP); //The button is always on HIGH level, when pressed it goes LOW
 sim800l.begin(9600);   //Module baude rate, this is on max, it depends on the version
 Serial.begin(9600);   
 delay(1000);
}

void loop()
{
 

 button_State = digitalRead(button1);   //We are constantly reading the button State

if (button_State != prev_State && button_State == LOW) {
 
   Serial.println("botao on");   //Shows this message on the serial monitor
   delay(200);                         //Small delay to avoid detecting the button press many times

SendSMS();                          //And this function is called

}

 if (sim800l.available()){            //Displays on the serial monitor if there's a communication from the module
   Serial.write(sim800l.read()); 
 }prev_State = button_State;
}

void SendSMS()
{
 Serial.println("Sending SMS...");               
 sim800l.print("AT+CMGF=1\r");                   //Set the module to SMS mode
 delay(100);
 sim800l.print("AT+CMGS=\",,,,,\"\r");  //phone number
 delay(500);
 sim800l.print("1");       //mensage send
 delay(500);
 sim800l.print((char)26);
 delay(500);
 sim800l.println();
 Serial.println("Text Sent.");
 delay(500);

}

have a look at receive an SMS to switch on the relay

use code tags (</> button) when posting code

@josecarlossilva

Do this first...

You will get a lot more help.

Done :ok_hand:

Your button code is not correct.
Look at this line:

Where does the value prev_State come from?

Did you try to program it yourself? Show us your efforts

The code works....
I program it myself with the help of some users
Now I can't make a code to work with the sms read and relay high

So could you explain, why do you compare button_State with prev_State?

if (button_State != prev_State && button_State == LOW) {

why do not use just this:

if (button_State == LOW) {

Because the push bottom when press it stays pressed a long time... If I don't use the this part of code if the button stays pressed it will send a lot of messages....

Can you help with the SMS receive and relay high?

Sorry
I was blind and did not see this line in your code:

The forum cannot write the code for you. Start yourself, if there will be problems then ask

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.