Hi, I have a problem with my code here. The SIM900 was working fine until I changed 64 to 256 in the software serial library while trying to solve this so please help on how to resolve that as well. The main issue though is that the sim900 receives the SMS but it seems like mySerial.available() is never greater than 0. Please help.
The sketch is a bit messy as I have been trying many ways to fix it but here it is.
#include <SoftwareSerial.h>
//Create software serial object to communicate with SIM900
SoftwareSerial mySerial(7, 8); //SIM900 Tx & Rx is connected to Arduino #7 & #8
String textMessage;
const int light1 = 10;
String light1State = "LOW";
void setup()
{
// Set lights as OUTPUT
pinMode(light1, OUTPUT);
// By default the leds is on
digitalWrite(light1, LOW);
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and SIM900
mySerial.begin(115200);
delay(10000);
Serial.println("Initializing...");
delay(10000);
mySerial.println("AT"); //Handshaking with SIM900
updateSerial();
mySerial.println("AT+CMGF=1");
updateSerial();// Configuring TEXT mode
mySerial.println("AT+IPR=115200");
updateSerial();
delay(10000);
mySerial.println("AT+CNMI=2,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
}
void loop()
{
updateSerial();
if(mySerial.available()>0){
mySerial.write(mySerial.read());
textMessage = mySerial.readString();
textMessage.toUpperCase();
// mySerial.println(textMessage);
delay(10);
}
if(textMessage.indexOf("LIGHT1ON")>=0){
// Turn on yellow led and save current state
digitalWrite(light1, HIGH);
light1State = "on";
textMessage = "";
}
if(textMessage.indexOf("LIGHT1OFF")>=0){
// Turn off yellow led and save current state
digitalWrite(light1, LOW);
light1State = "off";
textMessage = "";
}
if(textMessage.indexOf("LIGHT1STATE")>=0){
String message = "light1 is " + light1State;
sendSMS(message);
textMessage = "";
}
}
void updateSerial()
{
delay(500);
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 sendSMS(String message){
// AT command to set SIM900 to SMS mode
Serial.print("AT+CMGF=1\r");
delay(100);
// REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER
// USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS
Serial.println("AT + CMGS = \"xxxxxxxxxxx\"");
delay(100);
// Send the SMS
Serial.println(message);
delay(100);
// End AT command with a ^Z, ASCII code 26
Serial.println((char)26);
delay(100);
Serial.println();
// Give module time to send SMS
delay(5000);
}