GSM pumping station operation

Hello, please help me!
I want to make 2 arduino connection via GSM.
I want that when the button is on and at the same time pin 13 is disconnected send sms until the response that the pump is turned on,
When the SMS response is returned, pin 13 will be turned on and sending SMS will be stopped.
its working but
But if I increase the delay time to more than 200, it cannot perceive the returned sms

fun();
  delay(200);

And in the serial monitor delay(200)

16:50:09.006 -> +CMTI: "SM",1
16:50:09.006 -> 
16:50:09.006 -> start
16:50:10.371 -> AT+CMGR=1
16:50:10.371 -> 
16:50:10.371 -> +CMGR: "REC UNREAD","+xxxxxxxxxxxxx","","23/07/30,16:49:57+16",145,36,0,0,"+99599599997",145,1
16:50:10.439 -> 1
16:50:10.439 -> 
16:50:10.439 -> OK
16:50:10.439 -> 
16:50:10.439 -> 1
16:50:13.607 -> SMS Sent Successfully.
16:50:16.838 -> AT+CMGF=1
16:50:16.838 -> OK
16:50:16.838 -> AT+CMGS="+xxxxxxxxxxxxx"
16:50:16.885 -> > pump start
16:50:16.885 -> +CMGS: 150
16:50:16.885 -> 
16:50:16.885 -> OK

more then 200

16:47:34.760 -> +CMTI: "SM",1
16:47:34.760 -> 
16:47:34.760 -> start
16:47:36.272 -> AT+CMGR=1
16:47:36.272 -> 
16:47:36.272 -> +CMGR: "REC UNREAD","+xxxxxxxxxxxxxx","","23/07/30,
16:47:36.318 -> 
16:47:39.358 -> AT+CMGD=1,4
16:47:39.358 -> 
16:47:39.358 -> OK
16:47:39.358 -> AT+CMGDA= "DEL ALL"
16:47:39.358 -> 
16:47:39.358 -> OK

Please someone help me

Here is the complete code

#include <SoftwareSerial.h>

//sender phone number with country code
const String PHONE = "+xxxxxxxxxxxxx";

//GSM Module RX pin to Arduino 3
//GSM Module TX pin to Arduino 2
#define rxPin 2
#define txPin 3
SoftwareSerial sim800(rxPin, txPin);


#define floaton 6  //Button pin, on the other pin it's wired with GND
#define floatoff 7
#define RELAY_3 12
#define LED 13

bool on;   //Button state
bool off;  //Button state
bool pump;
String smsStatus, senderNumber, receivedDate, msg;
boolean isReply = false;

void setup() {
  pinMode(floaton, INPUT_PULLUP);
  pinMode(floatoff, INPUT_PULLUP);
  pinMode(LED, INPUT_PULLUP);
  digitalWrite(RELAY_3, HIGH);
  digitalWrite(LED, 1);

  delay(7000);

  Serial.begin(9600);
  Serial.println("Arduino serial initialize");

  sim800.begin(9600);
  Serial.println("SIM800L software serial initialize");


  pinMode(RELAY_3, OUTPUT);  //Relay 1
  pinMode(LED, OUTPUT);      //Relay

  smsStatus = "";
  senderNumber = "";
  receivedDate = "";
  msg = "";

  sim800.print("AT+CMGF=1\r");  //SMS text mode
  delay(1000);
  //delete all sms
  sim800.println("AT+CMGD=1,4");
  delay(1000);
  sim800.println("AT+CMGDA= \"DEL ALL\"");
  delay(1000);
}

void loop() {
  //////////////////////////////////////////////////
  while (sim800.available()) {
    parseData(sim800.readString());
  }
  //////////////////////////////////////////////////
  while (Serial.available()) {
    sim800.println(Serial.readString());
  }
  //////////////////////////////////////////////////
  on = digitalRead(floaton);  //We are constantly reading the button State
  off = digitalRead(floatoff);
  pump = digitalRead(LED);




  // Serial.println(pump);
  fun();
  delay(200);




}  //main loop ends


void fun() {
  if ((on == LOW) && (pump == 1)) {
    Serial.println("start");
    //delay(200);
    //SendSMS();
    //delay(1000);
  }

  if ((off == LOW) && (pump == 0)) {
    Serial.println("stop");
    delay(200);
    SendSMS1();
    delay(1000);
  }
}
void SendSMS() {
  sim800.print("AT+CMGF=1\r");
  delay(1000);
  sim800.print("AT+CMGS=\"" + PHONE + "\"\r");
  delay(1000);
  sim800.print("start");
  delay(100);
  sim800.write(0x1A);  //ascii code for ctrl-26 //sim800.println((char)26); //ascii code for ctrl-26
  delay(1000);
  Serial.println("SMS Sent Successfully.");
}
void SendSMS1() {
  sim800.print("AT+CMGF=1\r");
  delay(1000);
  sim800.print("AT+CMGS=\"" + PHONE + "\"\r");
  delay(1000);
  sim800.print("stop");
  delay(100);
  sim800.write(0x1A);  //ascii code for ctrl-26 //sim800.println((char)26); //ascii code for ctrl-26
  delay(1000);
  Serial.println("SMS Sent Successfully.");
}

//***************************************************
void parseData(String buff) {
  Serial.println(buff);



  unsigned int len, index;
  //////////////////////////////////////////////////
  //Remove sent "AT Command" from the response string.
  index = buff.indexOf("\r");
  buff.remove(0, index + 2);
  buff.trim();
  //////////////////////////////////////////////////

  //////////////////////////////////////////////////
  if (buff != "OK") {
    index = buff.indexOf(":");
    String cmd = buff.substring(0, index);
    cmd.trim();

    buff.remove(0, index + 2);

    if (cmd == "+CMTI") {
      //get newly arrived memory location and store it in temp
      index = buff.indexOf(",");
      String temp = buff.substring(index + 1, buff.length());
      temp = "AT+CMGR=" + temp + "\r";
      //get the message stored at memory location "temp"
      sim800.println(temp);
    } else if (cmd == "+CMGR") {
      extractSms(buff);

      if (senderNumber == PHONE) {
        doAction();
        //delete all sms
        sim800.println("AT+CMGD=1,4");
        delay(1000);
        sim800.println("AT+CMGDA= \"DEL ALL\"");
        delay(1000);
      }
    }
    //////////////////////////////////////////////////
  } else {
    //The result of AT Command is "OK"
  }
}

//************************************************************
void extractSms(String buff) {
  unsigned int index;

  index = buff.indexOf(",");
  smsStatus = buff.substring(1, index - 1);
  buff.remove(0, index + 2);

  senderNumber = buff.substring(0, 13);
  buff.remove(0, 19);

  receivedDate = buff.substring(0, 20);
  buff.remove(0, buff.indexOf("\r"));
  buff.trim();

  index = buff.indexOf("\n\r");
  buff = buff.substring(0, index);
  buff.trim();
  msg = buff;
  buff = "";
  msg.toLowerCase();
}

void doAction() {
  Serial.println(msg);
  if (msg == "2") {
    digitalWrite(LED, HIGH);
    Reply("pump stop");
  } else if (msg == "1") {
    digitalWrite(LED, LOW);
    Reply("pump start");
  }

  smsStatus = "";
  senderNumber = "";
  receivedDate = "";
  msg = "";
}

void Reply(String text) {
  sim800.print("AT+CMGF=1\r");
  delay(1000);
  sim800.print("AT+CMGS=\"" + PHONE + "\"\r");
  delay(1000);
  sim800.print(text);
  delay(100);
  sim800.write(0x1A);  //ascii code for ctrl-26 //sim800.println((char)26); //ascii code for ctrl-26
  delay(1000);
  Serial.println("SMS Sent Successfully.");
}

IF you can perceive the returned sms, why do you need ANY delay?

The reason is that it sends a lot of sms at low delay

I need add check SMS before fun()

relay = digitalRead(RELAY);
 


  fun();

All the more reason to not delay!

what is your idea? This doesn't work

If there is no message, test again and again, etc. until you get one.

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