Sending SMS is become an emotional struggle

I am Totally new here so excuses my lack of knowledge, I've been tying to send a SMS once power is down even though I managed to send a SMS Arduino won't stop sending SMS.
I have rewrite it countless time but still stuck, I really have no idea what is going on.

I simply want to send one SMS once power is down and do nothing when power is back on.

it's a shame this is my second week yet I've been stuck in the same loop. I am posting 3 different set of codes. Sorry I can't separate them anyways

Feel free to suggest, edit Any help is welcome



 
  #include <SoftwareSerial.h>
  SoftwareSerial mySerial(9, 10);
  char msg;

int pushButton (2);
bool SMSSent = false;
  
  void setup()
  { 
    pinMode(2, INPUT_PULLUP);
    pinMode(13, OUTPUT);
    mySerial.begin (9600);                      //Setting the baud rate of GSM Module
    delay(500);
    Serial.begin (9600);                        //Setting the baud rate of Serial Monitor (Arduino)
    delay(500);                                 // Delay to prevent this "************GSM SIM900A BEGIN"
    Serial.println ("GSM MODULE BEGIN");        //To print on serial monitor
    delay(1000);
  
  }

 
  void loop()

{ 
    bool SMSSent = false;
      
      
      if (digitalRead (2)  == LOW);
      
       delay(100);
       SMSSent = !SMSSent;
       mySerial.write(sendSMS(), SMSSent);
      Serial.println("SMS SENT SUCCESSFUL");
      }
      delay(50);
        digitalWrite(13, HIGH);
        delay(100);
        digitalWrite(13, LOW);
        delay(100);
      }   
      else {
        sendSMS();
        delay(1000);
        digitalWrite(13, HIGH);
        delay(800);
        digitalWrite(13, LOW);
        delay(800);
        }
    
  } 



void sendSMS()
  {
    mySerial.println("AT+CMGF=1");               //Sets the GSM Module in Text Mode
    delay(1200);  
    mySerial.println("AT+CMGS=\"+9000000000\"\r"); 
    delay(1200);
    mySerial.println("POWER DOWN");
    delay(200);
    mySerial.println((char)26);                 
    delay(1200);
  }

 
 

  #include <SoftwareSerial.h>
  SoftwareSerial mySerial(9, 10);
  char msg;


  
  void setup()
  { 
    pinMode(2, INPUT_PULLUP);
    pinMode(13, OUTPUT);
    mySerial.begin (9600);                      //Setting the baud rate of GSM Module
    delay(500);
    Serial.begin (9600);                        //Setting the baud rate of Serial Monitor (Arduino)
    delay(500);                                 // Delay to prevent this "************GSM SIM900A BEGIN"
    Serial.println ("GSM MODULE BEGIN");        //To print on serial monitor
    delay(1000);
  }

 
  void loop()

{ 
    digitalRead(2);
      bool SMSSent = false;
      if (SMSSent = true) 
       {while (2 == HIGH)
      Serial.println("SMS SENT SUCCESSFUL");
      
      delay(50);
        digitalWrite(13, HIGH);
        delay(100);
        digitalWrite(13, LOW);
        delay(100);
      }   
      else {
        sendSMS();
        delay(1000);
        digitalWrite(13, HIGH);
        delay(800);
        digitalWrite(13, LOW);
        delay(800);
        }
    
  } 



void sendSMS()
  {
    mySerial.println("AT+CMGF=1");               //Sets the GSM Module in Text Mode
    delay(1200);  
    mySerial.println("AT+CMGS=\"+9000000000\"\r"); 
    delay(1200);
    mySerial.println("POWER DOWN");
    delay(200);
    mySerial.println((char)26);                 
    delay(1200);
  }
  


 
 
  #include <SoftwareSerial.h>
  SoftwareSerial mySerial(9, 10);
  char msg;


  
  void setup()
  { 
    pinMode(2, INPUT_PULLUP);
    pinMode(13, OUTPUT);
    mySerial.begin (9600);                      //Setting the baud rate of GSM Module
    delay(500);
    Serial.begin (9600);                        //Setting the baud rate of Serial Monitor (Arduino)
    delay(500);                                 // Delay to prevent this "************GSM SIM900A BEGIN"
    Serial.println ("GSM MODULE BEGIN");        //To print on serial monitor
    delay(1000);
  }

 
  void loop()

  { 
    int SMSSent;
    int pushButton = digitalRead(2);
    
    Serial.println(pushButton);
    delay(1000);
    if (pushButton == HIGH)
    {
      sendSMS();
    }
    if (SMSSent == 0)
    {
      sendSMS();
      SMSSent = 1;
      Serial.println("SMS SENT SUCCESSFUL");
      digitalWrite(13, HIGH);
      delay(200);
      digitalWrite(13, LOW);
      delay(200);
      
    } else {
        digitalWrite(13, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        delay(500); }
    
  } 



void sendSMS()
  {
    mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    delay(1000);  
    mySerial.println("AT+CMGS=\"+90000000000\"\r"); 
    delay(1000);
    mySerial.println("POWER DOWN");	
    delay(100);
    mySerial.println((char)26);
    delay(1000);
  }

Read that again…

use == is for comparison not = which is assignment but since you initialized the variable to false just before - the compiler would Throw that code away as the test will never be true

You might also need to read about variable scope: https://www.arduino.cc/reference/en/language/variables/variable-scope-qualifiers/scope/

1 Like

Thank you for taking your time to reply. but it's still the same

Ok.. so I'm only looking at the 3rd version you posted.

I don't really understand what you are trying to do here...

The code just prior to this sends the SMS when the button is pressed. But in this if statement you send it again because SMSSent is 0?

Then because you declare SMSSent at the top of loop it gets set to zero again, and so will keep sending.

If you move this statement

... to the top of the program... you will avoid that problem... but you will still send 2 SMS messages.

I'm not sure why you need the if statement at all?

This will not compile

void loop()

{
  bool SMSSent = false;


  if (digitalRead(2) == LOW)
    ;

  delay(100);
  SMSSent = !SMSSent;
  mySerial.write(sendSMS(), SMSSent);
  Serial.println("SMS SENT SUCCESSFUL");
}
delay(50);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
else {
  sendSMS();
  delay(1000);
  digitalWrite(13, HIGH);
  delay(800);
  digitalWrite(13, LOW);
  delay(800);
}
}

In below, SMSSent is not initialised and can have any value.

void loop()

{
  int SMSSent;

What are the chances in below that SMSSent equals true?

void loop()

{
  digitalRead(2);
  bool SMSSent = false;
  if (SMSSent = true) {

Please (in future) don't place the three individual sketches in one code block. One code block per sketch would be more useful.

It does not matter what you are trying to do, to do it once is always the same and requires an understanding of loop. Loop is the most important concept, try to think in a looping manner when you code.

In loop “do x” means to “do x” again and again and again. The logic is simple if you want to do it only once. You just need to have conditional statements that remember it has been done. So in pseudocode:

If stateX is 0 (== comparator here)
Do x and Change stateX to 1

Now loop checks if stateX is 0 (which will be true the first time as, when you declare it globally you initialise it to 0). Because it is true the if statement code is run. The code changes it to 1 so the if statement will no longer be true the second time through loop.

Problems you can face;
If you declare stateX as 0 in loop then every time through loop it will reset to 0 and the code in the if statement will always run.
If you use the = instead of the == you will be changing rather than comparing the variable.
You declare a variable in the wrong scope {} and so it is not visible to the block of code you want to use it in (see global and local variables and scope)

Try always to run through your code the way the microcontroller does, so in loop you need to check through at least twice to see what you are doing.

Never code 2 things at once eg buttons and your sms once only code. Code each individually in its own sketch and get it working how you want before combining. That way you know where bugs are and things are less complex. Study the important concepts of arduino:
Loop
Millis timing
State machines
Edge detection
Functions
Arrays

1 Like

Do what?

If you remove the entire if statement the sms will only be sent when you push the button. Isn’t that what you want?

Do you think this is valid one? Something is not clicking right here

 
 
  #include <SoftwareSerial.h>
  SoftwareSerial mySerial(9, 10);
  char msg;
  int SMSSent;

  
  void setup()
  { 
    pinMode(2, INPUT_PULLUP);
    pinMode(13, OUTPUT);
    mySerial.begin (9600);                      //Setting the baud rate of GSM Module
    delay(500);
    Serial.begin (9600);                        //Setting the baud rate of Serial Monitor (Arduino)
    delay(500);                                 // Delay to prevent this "************GSM SIM900A BEGIN"
    Serial.println ("GSM MODULE BEGIN");        //To print on serial monitor
    delay(1000);
  }

 
  void loop()

  { 
    
    int pushButton = digitalRead(2);
    
    Serial.println(pushButton);
    delay(1000);

    if (SMSSent == 0)
    {
      sendSMS();
      SMSSent = 1;
      Serial.println("SMS SENT SUCCESSFUL");
      
      digitalWrite(13, HIGH);         /* LED blink */
      delay(200);
      digitalWrite(13, LOW);
      delay(200);
      
    } else {
        digitalWrite(13, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        delay(500); }
    
  } 



void sendSMS()
  {
    mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    delay(1000); 
    mySerial.println("AT+CMGS=\"+00000000000\"\r"); 
    delay(1000);
    mySerial.println("POWER IS DOWN");
    delay(100);
    mySerial.println((char)26);
    delay(1000);
  }
  

Please stop deleting your posts. It destroys the continuity of the replies.

1 Like

Hello, would you mind explaining here why it is not working

 
 
  #include <SoftwareSerial.h>
  SoftwareSerial mySerial(9, 10);
  char msg;
  int SMSSent;

  
  void setup()
  { 
    pinMode(2, INPUT_PULLUP);
    pinMode(13, OUTPUT);
    mySerial.begin (9600);                      //Setting the baud rate of GSM Module
    delay(500);
    Serial.begin (9600);                        //Setting the baud rate of Serial Monitor (Arduino)
    delay(500);                                 // Delay to prevent this "************GSM SIM900A BEGIN"
    Serial.println ("GSM MODULE BEGIN");        //To print on serial monitor
    delay(1000);
  }

 
  void loop()

{ (SMSSent == 0);
    
    int pushButton = digitalRead(2);
    
    Serial.println(pushButton);
    delay(1000);
    if (pushButton == HIGH)
  {
      sendSMS();
      SMSSent = !SMSSent;
      SMSSent = 1;
      Serial.println("SMS SENT SUCCESSFUL");
      
      digitalWrite(13, HIGH);         /* LED blink */
      delay(200);
      digitalWrite(13, LOW);
      delay(200);
  }   else {
    SMSSent = 1;
    !SMSSent;
  }

}
      
    
 
void sendSMS()
  {
    mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    delay(1000);  
    mySerial.println("AT+CMGS=\"+00000000000\"\r"); 
    delay(1000);
    mySerial.println("POWER DOWN");
    delay(100);
    mySerial.println((char)26);
    delay(1000);
  }
  

I can't comment yet on the entire sketch. But this line must be wrong, as it does absolutely nothing.

post your latest version of the code

what do you expect from those 2 assignments?

I was trying to say if the SMS has already been sent do not sent anymore sms

Here is the latest version. bear in mind you'll be confused here coz I am trying all sort of things just to make it right. you can ignore all the garbage and related to only things that matters.

 
  #include <SoftwareSerial.h>
  SoftwareSerial mySerial(9, 10);
  char msg;
  int SMSSent;

  
  void setup()
  { 
    pinMode(2, INPUT_PULLUP);
    pinMode(13, OUTPUT);
    mySerial.begin (9600);                      //Setting the baud rate of GSM Module
    delay(500);
    Serial.begin (9600);                        //Setting the baud rate of Serial Monitor (Arduino)
    delay(500);                                 // Delay to prevent this "************GSM SIM900A BEGIN"
    Serial.println ("GSM MODULE BEGIN");        //To print on serial monitor
    delay(1000);
  }

 
  void loop()
  
{ SMSSent == 0;
  pinMode(2, INPUT_PULLUP);
    
    int pushButton = digitalRead(2);
    
    Serial.print(pushButton);
    delay(1000);
    if (pushButton == HIGH)
  {
      sendSMS();
      SMSSent = !SMSSent;
      SMSSent = 1;
      Serial.println("SMS SENT SUCCESSFUL");
      delay(1000);
      
      
      digitalWrite(13, HIGH);         /* LED blink */
      delay(200);
      digitalWrite(13, LOW);
      delay(200);
  
  }

}
      
    
 
void sendSMS()
  {
    mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    delay(1000);  
    mySerial.println("AT+CMGS=\"+971585997391\"\r"); 
    delay(1000);
    mySerial.println("sim900a sms");
    delay(100);
    mySerial.println((char)26);
    delay(1000);
  }


Oh. What matters?

Just to be clear, are you saying that the lines referred to in reply #14 don't matter? Or do they matter?

I removed below two line it doesn't make any difference.

Here how the codes looks now


 
 
  #include <SoftwareSerial.h>
  SoftwareSerial mySerial(9, 10);
  char msg;
  int SMSSent;

  
  void setup()
  { 
    pinMode(2, INPUT_PULLUP);
    pinMode(13, OUTPUT);
    mySerial.begin (9600);                      //Setting the baud rate of GSM Module
    delay(500);
    Serial.begin (9600);                        //Setting the baud rate of Serial Monitor (Arduino)
    delay(500);                                 // Delay to prevent this "************GSM SIM900A BEGIN"
    Serial.println ("GSM MODULE BEGIN");        //To print on serial monitor
    delay(1000);
  }

 
  void loop()
  
{ SMSSent == 0;
  pinMode(2, INPUT_PULLUP);
    
    int pushButton = digitalRead(2);
    
    Serial.print(pushButton);
    delay(1000);
    if (pushButton == HIGH)
  {
      sendSMS();

      Serial.println("SMS SENT SUCCESSFUL");
      delay(1000);

      
      
      digitalWrite(13, HIGH);         /* LED blink */
      delay(200);
      digitalWrite(13, LOW);
      delay(200);
  
  } 

}
      
    
 
void sendSMS()
  {
    mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
    delay(1000);  
    mySerial.println("AT+CMGS=\"+971585997391\"\r"); 
    delay(1000);
    mySerial.println("sim900a sms");
    delay(100);
    mySerial.println((char)26);
    delay(1000);
  }
  

something like this:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
char msg;
bool SmsAlreadySent = false;
const byte pushButtonPin = 2;
const byte ledPin = 13;

void sendSMS() {
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);
  mySerial.println("AT+CMGS=\"+971585997391\"\r");
  delay(1000);
  mySerial.println("sim900a sms");
  delay(100);
  mySerial.println((char)26);
  delay(1000);
  SmsAlreadySent = true;
}

void setup() {
  pinMode(pushButtonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  Serial.begin (9600);                        //Setting the baud rate of Serial Monitor (Arduino)
  mySerial.begin (9600);                      //Setting the baud rate of GSM Module
  Serial.println ("GSM MODULE BEGIN");        //To print on serial monitor
}


void loop() {
  int pushButtonStatus = digitalRead(pushButtonPin);
  Serial.print(pushButtonStatus);
  if ( not SmsAlreadySent && (pushButtonStatus == LOW)) { // if we have not sent the SMS and the button is being pushed (LOW means pushed as the pin is set as INPUT_PULLUP)
    sendSMS();
    Serial.println("SMS SENT SUCCESSFULLY");
    digitalWrite(ledPin, HIGH);         /* LED blink */
    delay(200);
    digitalWrite(ledPin, LOW);
    delay(200);
  }
}

you set your bool variable to true when the SMS is sent and You never reset it (it will work then only once until you reboot the arduino)

I can see that you have a problem with C syntax. Just as the other line I pointed out. There is no remedy except to study C.

Sure, remove the engine of the car, to make the car work.

➜ programming is not working well if you play guess work... go follow a C++ tutorial

Oh I get you. Thank you for taking time to help.

Note that I add a delay as follow.

void loop() {
  int pushButtonStatus = digitalRead(pushButtonPin);
  delay(1000);
  Serial.print(pushButtonStatus);
  delay(1000);
  if ( not SmsAlreadySent && (pushButtonStatus == LOW)) { // if we have not sent the SMS and the button has been pushed
    sendSMS();

Results as below

16:44:04.205 -> 111110SMS SENT SUCCESSFULLY

16:44:19.769 -> 0000000001111110000000011110000000111100000000001110000000

No offence- even though Arduino was able to detect switch input it does not send the SMS again.