Need help to define variable

Hi,

I am trying to control engine based on some sensor data and GSM shield will send sms and also control on off accordingly.

I need some help to define the variable which store incoming sms . Currently program working but printing only in serial monitor (full sketch is attached)

I try to change " char caracter=0; " and make some change in function accordingly but it get slow very late response around 1 minute to arrive the msg.

String caracter ;

String State = "";


void reply() {

  if (SIM900.available() > 0) { //We check if there is data available from the SIM900
    caracter = SIM900.readString(); // We read the data and the weights in the message variable
    Serial.print(caracter); //Prints the incoming data one by one on the serial terminal
   delay(10);}
    if(caracter.indexOf("OFF")>=0){
      Serial.println("-->Sending message");
      State = "Engine off";
       SendingMessage(State);
EngOFF();
      !valve;
    }
     if(caracter.indexOf("ON")>=0) {
      Serial.println("-->Sending message");
      State = "Engine on";
      SendingMessage(State);
EngON();
      valve;
    }
     if(caracter.indexOf("STATE")>=0){
       Serial.println("-->Sending message");
   SendingMessage(State);
    Serial.println("Engine state resquest");
    
  }
  }

void SendingMessage(String State) {
    // AT command to set SIM900 to SMS mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);
  SIM900.println("AT + CMGS = \"+9xxxxxxxxxx\"");//replace by the number to send the message
  delay(100);
  SIM900.println("State:" + State);// Replace with the text to be sent
  delay(100);
  //We finish this command with the substitution character (→) code Ascii 26 for sending the SMS
  SIM900.println((char)26);
  delay(100);
  SIM900.println();
  delay(3000);
}

void MSG() {
    // AT command to set SIM900 to SMS mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);
  SIM900.println("AT + CMGS = \"+9xxxxxxxx\"");//replace by the number to send the message
  delay(100);
  SIM900.println("T:" + TMessage + "*C " + " L:" + LMessage + "% "+ "State Stop");// Replace with the text to be sent
  delay(100);
  //We finish this command with the substitution character (→) code Ascii 26 for sending the SMS
  SIM900.println((char)26);
  delay(100);
  SIM900.println();
  delay(3000);
}

Serial out put is:

Control System Starting
  
  
Signal:      -49     -211     dBm     BER:      -49
   
  
  
⸮⸮
RDY

+CFUN: 1

+CPIN: READY

+CREG: 2
  

Call Ready
  
  
  
Signal:      -35     -183     dBm     BER:      17
SQ: 23,0

OK

+CREG: 1
  
  
 

+CMT: "+9xxxxxxxxxxxxxxxxx","","18/12/01,20:07:11+12"
OFF
-->Sending message
  
AT+CMGF=1
AT + CMGS = "+9xxxxxxxxxxxxxxxxxx"
State:Engine off


  
Signal:      23     -67     dBm     BER:      0
  
  
  

+CMT: "+xxxxxxxxxxxxxxxxxxx","","18/12/01,20:07:31+12"
ON
-->Sending message
  
AT+CMGF=1
AT + CMGS = "+9xxxxxxxxxxxxxxxxxxx"
State:Engine on


 
  
  
Signal:      23     -67     dBm     BER:      0
  
  
  

+CMT: "+9xxxxxxx","","18/12/01,20:07:45+12"
STATE
-->Sending message
Engine state resquest
  
AT+CMGF=1
AT + CMGS = "+9xxxxxxx"
State:Engine on


 
  
  
AT+CMGF=1
AT + CMGS = "+9xxxxxxx0"
T:24*C  L:72% State Stop  
  
Signal:      23     -67     dBm     BER:      0

Engine control.txt (16.7 KB)

It seems like you are already storing the incoming message in the String variable called caracter. What else do you want to do?

Separately, it is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Thanks for your reply.

I can read all the thinks on serial monitor and AT command working fine but not send the SMS. if you can help me ?

I have another way to deal it but the response time is very slow serial print letters one by one.

I change these lines in #1

char caracter = ; // Variable to save the characters incoming messages
String State = "";

void reply(){
  
   if (SIM900.available() > 0) { //We check if there is data available from the SIM900
    caracter = SIM900.read(); // We read the data and the weights in the message variable
    Serial.print(caracter); //Prints the incoming data one by one on the serial terminal
    if (caracter == 'H') {
      Serial.println("-->Sending message");
      digitalWrite(Solenoid, HIGH);
      State = "led on";
      SendingMessage(State);
    }
    if (caracter == 'L') {
      Serial.println("-->Sending message");
      digitalWrite(Solenoid, LOW);
      State = "led off";
      SendingMessage(State);
    }}}


    void SendingMessage(String State) {
  SIM900.println("AT + CMGS = \"+xxxxxxxxxxxxxx\"");//replace by the number to send the message
  delay(200);
  SIM900.println("State:" + State);// Replace with the text to be sent
  delay(200);
  //We finish this command with the substitution character (→) code Ascii 26 for sending the SMS
  SIM900.println((char)26);
  delay(200);
  SIM900.println();
}


void MSG() {
  SIM900.println("AT + CMGS = \"+9xxxxxxxxxxxxx\"");//replace by the number to send the message
  delay(200);
  SIM900.println("Temperature: " + tMessage1 + "*C " + " Tank Level: " + tMessage2 + "%");// Replace with the text to be sent
  delay(200);
  //We finish this command with the substitution character (→) code Ascii 26 for sending the SMS
  SIM900.println((char)26);
  delay(200);
  SIM900.println();
}

Ghulamfarid:
I can read all the thinks on serial monitor and AT command working fine but not send the SMS. if you can help me ?

I have no experience of using an SMS module with an Arduino.

I thought, from your Original Post that your difficulty was with receiving and storing data?

I need some help to define the variable which store incoming sms

...R

Robin2:
I have no experience of using an SMS module with an Arduino.

I thought, from your Original Post that your difficulty was with receiving and storing data?
...R

Yes, I try to manage two different way. After google I learn that in my #2 (sketch) I am storing only 01 character that's why processing is slow as I mention.

char caracter = ; // Variable to save the characters incoming messages
String State = "";

if (SIM900.available() > 0) { //We check if there is data available from the SIM900
   caracter = SIM900.read(); // We read the data and the weights in the message variable

Which is in first post

 if (SIM900.available() > 0) { //We check if there is data available from the SIM900
    caracter = SIM900.readString();

So my main issue is between these two different lines 1st one is storing letter by letter but working and sending SMS with relay. 2nd is reading and printing fine but not send the sms.

Thanks for you reply and I will read more to learn.

So my main issue is between these two different lines 1st one is storing letter by letter but working and sending SMS with relay. 2nd is reading and printing fine but not send the sms.

The reading code is not related to the sending code. Look carefully at this sending function in both codes

 void SendingMessage(String State) {

You can see a difference between the one that sends properly and the one that doesn't

 SIM900.print("AT+CMGF=1\r");