1

I'm using SoftwareSerial with a SIM808 GSM modulde to detect incoming SMS messages. The problem I'm currently having is that I need to access the phone number from the received SMS so I can send a reply back to that number.

I'm trying to access the contents of the SoftwareSerial and print it to the serial monitor, just so I can see its contents but for some reason, nothing at all is being output. The "Message received" string is being output to the monitor, along with everything else that's needed. It's just the mySerial.read() that won't show.

How can I access the incoming SMS message and get the phone number from it?

Excuse the messy code. It's a work in progress.

 #include <SoftwareSerial.h>
 #include <ADSWeather.h>

SoftwareSerial mySerial(9, 10);

#define ANEMOMETER_PIN 2
#define VANE_PIN 0
#define RAIN_PIN 3


#define CALC_INTERVAL 1000

unsigned long nextCalc;
unsigned long timer;

int windDir;
int windSpeed;
int rainAmmount;
const int switchpin =  7;

ADSWeather ws1(RAIN_PIN, VANE_PIN, ANEMOMETER_PIN); //This should configure all pins correctly

void setup()
{
  pinMode(switchpin, OUTPUT);
  Serial.begin(9600); 
  attachInterrupt(digitalPinToInterrupt(ANEMOMETER_PIN), ws1.countAnemometer, FALLING); //ws1.countAnemometer is the ISR for the anemometer.
  nextCalc = millis() + CALC_INTERVAL;
  
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  delay(100);

  digitalWrite(switchpin, HIGH);
  delay(3000);
  digitalWrite(switchpin, LOW);
        RecieveMessage();
      Serial.write("SMS set to recieve messages");
}


void loop()
{
  
  timer = millis();

  int rainAmmount;
  long windSpeed;
  long windDirection;
  int windGust;
  int sent;

  ws1.update(); //Call this every cycle in your main loop to update all the sensor values

  if(timer > nextCalc)
  {
    
    nextCalc = timer + CALC_INTERVAL;
    rainAmmount = ws1.getRain();
    windSpeed = ws1.getWindSpeed();
    windDirection = ws1.getWindDirection();
    windGust = ws1.getWindGust();

//     windSpeed / 10 will give the interger component of the wind speed
//     windSpeed % 10 will give the fractional component of the wind speed
    Serial.print("Wind speed: ");
    Serial.print(windSpeed);
    //Serial.print('.');
    //Serial.print(windSpeed % 10);
    //Serial.print(" ");

    Serial.print(" Gusting at: ");
    Serial.print(windGust);
    //Serial.print('.');
    //Serial.print(windGust % 10);
    Serial.println("");

    
  }

  
  //if (Serial.available()>0)
  //{
      //RecieveMessage();
     // Serial.write("SMS set to recieve messages");
  //}

//  mySerial.flush();
 // serialFlush();

  //If an message comes in
 if (mySerial.available()>0){
  
  Serial.write("Message received");
  Serial.write(mySerial.read());



  String stringSMSMessage;
  stringSMSMessage = String("Wind speed near St. John's lock: ");
  stringSMSMessage += windSpeed;
  stringSMSMessage += "MPH";
  //stringSMSMessage += '.';
 // stringSMSMessage += windSpeed & 10;
  stringSMSMessage += " Gusting at: ";
  stringSMSMessage += windGust;
    stringSMSMessage += "MPH";
 // stringSMSMessage += '.';
  //stringSMSMessage += windGust % 10;
  stringSMSMessage += ' ';

  //Send the wind speed response.
    mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"07769218312\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println(stringSMSMessage);// The SMS text you want to send
  delay(100);
   mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(10000);
  mySerial.flush();
  serialFlush();
  
}
}

 void SendMessage()
{
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"Phone Number Goes Here\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println("I am SMS from GSM Module");// The SMS text you want to send
  delay(100);
   mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(10000);
  mySerial.flush();
  serialFlush();
  
}


 void RecieveMessage()
{
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
 }

 void serialFlush(){
  while(mySerial.available()>0)  {  
     mySerial.read(); 
    }
}   
3
  • What are you expecting to show up? Serial.write(mySerial.read()); will just send exactly 1 byte/character from the mySerial buffer over Serial. Nothing more. Only 1 byte. You need to read the full message from mySerial. Then you can send that over Serial.
    – chrisl
    Sep 7 '20 at 13:13
  • Why not digging more on AT commands....you can send At command and can get data. electronicsforu.com/resources/cool-stuff-misc/gsm-at-commands Just send the commands and read the response then print to serial monitor.
    – Oggy Saud
    Sep 7 '20 at 13:29
  • Excuse the messy code. It's a work in progress. ... the look of the code is not important ... mostly, it is properly indented, which should keep you from adding code in wrong places .... there is a problem though, there is too much code for the problem you are trying to solve .... there is no reason to have any sendMessage code ... all you need, at this time, is SMS receive code and print received text code ... when that code works, add extract phone number code
    – jsotola
    Sep 7 '20 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.