SIM900 keeps sending SMS when input is HIGH/LOW !!!

Hey guys, so i’m working on this project and I dont quite know how to program the SIM900 so that it’ll only send SMS once everytime the input goes HIGH or LOW.

Right now, when the input is HIGH, it doesn’t stop sending sms. Same for when the input is low.

Please help me guys. I’m getting really frustrated. I’m not exactly a pro when it comes to programming.

I want it to only send SMS once everytime the input goes HIGH or LOW.

Your prompt reply will be highly appreciated.

Thank You.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9);

void setup() {
  mySerial.begin(9600);
  pinMode(7, INPUT);   // SPD
  pinMode(13, OUTPUT); // LED

  digitalWrite(13, LOW);
}

void loop() {

  if(digitalRead(7) == HIGH) {
    digitalWrite(13, HIGH);
    mySerial.println("AT");
    updateSerial();
    mySerial.println("AT+CMGF=1"); // Text Mode
    updateSerial();
    mySerial.println("AT+CMGS=\"+xxxxxxxxxx\"");// H/P No.
    mySerial.print("SPD OK");
    updateSerial();
    mySerial.write(26); 
  }
  else{
    digitalWrite(13, LOW);
    mySerial.println("AT");//Handshaking with SIM900
    updateSerial();
    mySerial.println("AT+CMGF=1"); // Configuring Text Mode
    updateSerial();
    mySerial.println("AT+CMGS=\"+xxxxxxxxxxxx\"");// H/P No.
    updateSerial();
    mySerial.print("SPD ALARM");
    updateSerial();
    mySerial.write(26); 
  }
}

  void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());
  }
}

pinMode(7, INPUT_PULLUP) maybe?

I tried that it doesn't work. I keep getting sms every .5 seconds. How do i program it so that I'll only receive sms once when input is high or low. Basically, im plugging in a surge protection device to the base. When i plug it in, it should send SMS once saying "SPD ok". When i plug it out, it should send SMS once saying " SPD Alarm". However, thats not the case. I'm getting sms nonstop. :frowning:

Basically, im plugging in a surge protection device to the base. When i plug it in, it should send SMS once saying “SPD ok”. When i plug it out, it should send SMS once saying " SPD Alarm".

What exactly is connected to pin7? How is it wired? Are the grounds connected?

To send one message when the pin goes from LOW to HIGH and a different message when the pin goes from HIGH to LOW you will basically have two conditional statements based on the change of state of pin 7

Here is the idea, not the actual code

pinState = digitalRead(7);

if(pinState ==HIGH and lastPinState==LOW)
{ //send one message}
if(pinState ==LOW and lastPinState ==HIGH)
{//send the other message}

lastPinState = pinState

You may also want to take a look at the StateChangeDetection example in the ide File>examples>02Digital>StateChangeDetection for some ideas as well.

cattledog:
What exactly is connected to pin7? How is it wired? Are the grounds connected?

To send one message when the pin goes from LOW to HIGH and a different message when the pin goes from HIGH to LOW you will basically have two conditional statements based on the change of state of pin 7

Here is the idea, not the actual code

pinState = digitalRead(7);

if(pinState ==HIGH and lastPinState==LOW)
{ //send one message}
if(pinState ==LOW and lastPinState ==HIGH)
{//send the other message}

lastPinState = pinState






You may also want to take a look at the StateChangeDetection example in the ide File>examples>02Digital>StateChangeDetection for some ideas as well.

My code works, the only issue is that it doesn't stop sending SMS while pin 7 is high or low. How do code it so that the sms is sent only once when pin 7 is high or when pin 7 is low. I want the code to constantly detect changes in terms of HIGH and LOW but only send 1 sms everytime the input state changes.

to answer your question, Pin 7 is connected to a surge protection device...and yes, grounds are connected. basically just to test continuity. when there is no continuity, i want the SIM900 to send 1 sms only..not repeatedly. And when there is continuity..also send 1 sms only. So everytime i plug out the SPD, send 1 SMS to notify the status, when i plug in, also send 1 SMS to notify the status. Right now it constantly sends sms of the status. I want to limit the number of sms being sent to only 1 whenever the status changes.

Ok guys, so im using this code now. And When pin 7 goes HIGH, I’m getting ONE SMS. just the way i wanted it to work. But, when pin 7 goes LOW, im not getting that ONE sms “SPD ALARM”.

Can someone tell me whats wrong with my coding?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(8, 9);
const int spdpin = 7;
const int ledpin = 13;
int buttonstate = 0;
int lastbuttonstate = 0;

void setup() {
  pinMode(spdpin, INPUT);   // SPD
  pinMode(ledpin, OUTPUT); // LED
  mySerial.begin(9600);
   
}

void loop() {

buttonstate = digitalRead(spdpin);

if (buttonstate != lastbuttonstate){

  if(buttonstate == HIGH) {
    digitalWrite(13, HIGH);
    mySerial.println("AT");
    updateSerial();
    mySerial.println("AT+CMGF=1"); // Text Mode
    updateSerial();
    mySerial.println("AT+CMGS=\"+xxxxxxxxxxxxx\"");// H/P No.
    updateSerial();
    mySerial.print("SPD OK");
    updateSerial();
    mySerial.write(26); 
  }
  
  else{
    digitalWrite(13, LOW);
    mySerial.println("AT");
    updateSerial();
    mySerial.println("AT+CMGF=1"); // Text Mode
    updateSerial();
    mySerial.println("AT+CMGS=\"+xxxxxxxxxxxx\"");// H/P No.
    updateSerial();
    mySerial.print("SPD ALARM");
    updateSerial();
    mySerial.write(26); 
  }

 delay(50);
}
lastbuttonstate = buttonstate;
}

void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());
  }
}

Do you have a pull down resistor on pin 7 to keep it from floating when the surge protector is disconnected?

You could use the internal pull up resistor enabled with the pin mode INPUT_PULLUP, and then reverse the logic of which message is sent when.