I have a strange problem with my project. I am able to send and receive SMS messages but for some reason not from the same project.
This little one sends a message, and is working properly:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String no = "+38762701893";
String message = "this is some message";
void setup() {
SIM900.begin(19200);
delay(20000);
}
void sendSMS(String number, String mess) {
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"" + number + "\"");
delay(100);
SIM900.println(mess);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
}
void loop() {
sendSMS(no, message);
do {} while (1);
}
However, if I want this same function to work under a little bigger program which receives SMS messages and responds to them, it doesn't work.
This is the complete code of the not working example:
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
String no = "+122333444"; //phone number
String message = "";
char inchar;
int b = 13;
bool state = 0;
void setup()
{
Serial.begin(19200);
SIM900.begin(19200);
delay(5000);
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
Serial.println("Ready...");
SIM900.begin(19200);
delay(500);
pinMode(b, OUTPUT);
}
void sendSMS(String number, String mess) {
SIM900.print("AT+CMGF=1\r");
delay(200);
SIM900.println("AT+CMGS=\"" + number + "\"");
delay(100);
SIM900.println(mess);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(8000);
}
void loop()
{
if (state) {
sendSMS(no, message);
state = 0;
}
if (SIM900.available() > 0) {
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == '#') {
delay(10);
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == 'a') {
delay(10);
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == '0') {
digitalWrite(b, LOW);
message = "something is off";
} else if (inchar == '1') {
digitalWrite(b, HIGH);
message = "something is on";
}
}
state = 1;
}
}
}
This program receives the SMS, turns on the LED, or turns it off, depending of the SMS content, but it won't reply for some reason. Any help is greatly appreciated.