0

I am writing an Arduino code to receive SMS from a SIM800 module and then send the same message back to the sender. My code is as follows:

#include<SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);//Connect to pin 9&10 on GSM module

const byte numChars = 128; //character size
char receivedChars[numChars]; // Store data read from SMS
boolean newData = false; //flag to check if new data has arrived

void setup() {
    Serial.begin(9600);
    mySerial.begin(9600);
    Serial.println("<Arduino is ready>");
    mySerial.println("AT");
    mySerial.println("AT+CMGF=1"); //text mode
    mySerial.println("AT+CNMI=1,2,0,0,0");
    mySerial.println("AT+CSQ"); //Check signal strength
    while (Serial.available()) 
    {
        mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
    }
    while(mySerial.available()) 
    {
        Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
    }
}

void loop() {
    //code for checking sms
    scanSMS();
    //Print if a new data arrived
    showNewData();
    delay(25000);
}

void scanSMS() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '#'; //Start and end markers. Eg. SMS body will be sent as #status# 
    char endMarker = '#';
    char rc;
    while (Serial.available()) 
    {
        mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
    }

    while (mySerial.available() > 0 && newData == false) {
        Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port

        rc = mySerial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }
        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

/*PRINT IF THERE IS A NEW DATA*/
void showNewData() {
    if (newData == true) {
        SendMessage(receivedChars);
        newData = false;
    }
}

/*SEND BACK WHATEVER SMS IS RECEIVED*/
void SendMessage(String receivedChars)
{
    Serial.print("here");
    mySerial.println("AT+CMGS=\"+xxxxxxxxxx\"\r"); // Replace x with mobile number
    mySerial.println("Humidity Alert!");// The SMS text you want to send
    mySerial.print(receivedChars);
    mySerial.println((char)26);// ASCII code of CTRL+Z
}  

The issue is that the module seems to receive only alternate characters instead of the whole message. For example, if I send the text #program#, the Arduino will only print porm or rga. Am I missing something here? Any help is highly appreciated.

Kentaro Okuda
  • 1,557
  • 2
  • 12
  • 16
Monty Swanson
  • 695
  • 16
  • 41

1 Answers1

0

You are calling mySerial.read() twice in the while loop.

while (mySerial.available() > 0 && newData == false) {
      Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
      rc = mySerial.read();

I think that is why you are not seeing the whole string. read reads the first byte in the buffer and advances.

You want to store a returned value and pass it to the write call like this.

while (mySerial.available() > 0 && newData == false) {
    rc = mySerial.read();
    Serial.write(rc);//Forward what Software Serial received to Serial Port
    ....
}
Kentaro Okuda
  • 1,557
  • 2
  • 12
  • 16