Hello!
So, It’s my first time working with arduino and It’s been two days since I keep trying to solve an issue.
I have two SMS modules that need to communicate with each other, and since they are only programmable by text messages they are pretty restrictive with what they can do, so I tought of using an arduino with a gsm module to act as some sort of “translator”.
Let’s call them SMS1, ARDUINO(WITH SIM800L MODULE) AND SMS2.
SMS1 detects the push of a button, sends a message to arduino, wich sends a message to SMS2 to power up the led. I’ve managed to make it work, somehow, but my problem is that if I push the button more than once in a short period of time, the arduino will receive all the messages but will only send a response for the first command to SMS2.
Does anyone have any ideeas about a way to be able to make arduino “keep up” with the incoming messages?
I’ve tried to store the messages in the sim storage and then read only the last one received, but I can’t get it working properly. (I think my way of finding out which is the last SMS received isn’t the best :D)
#include <SoftwareSerial.h>
SoftwareSerial sim800l(12,13);
String command="";
int ok=0;
String nrAutorizat="NUMBER";
String state="OFF";
String lastCommand="OFF";
int lastMessage=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);//start arduino communication
sim800l.begin(9600);//start communication between arduino and sim800l
Serial.println("Initializing...");
delay(1000);
sim800l.println("AT");
updateSerial();
delay(1000);
sim800l.println("AT+CMEE=2");
updateSerial();
delay(1000);
sim800l.println("AT+CPIN=PIN"); //insert PIN
updateSerial();
delay(1000);
sim800l.println("AT+CPIN=?"); //check if PIN ok
updateSerial();
delay(1000);
sim800l.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best
updateSerial();
delay(1000);
sim800l.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
updateSerial();
delay(1000);
sim800l.println("AT+CREG?"); //Check whether it has registered in the network
updateSerial();
delay(1000);
sim800l.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
delay(1000);
sim800l.println("AT+CNMI=2,1,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
delay(1000);
}
void loop() {
if (state != lastCommand){
sendMessage(lastCommand+".1","telNumber");
state=lastCommand;
}
if (lastMessage!=0){
sim800l.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
delay(400);
sim800l.print("AT+CMGR=");
sim800l.print(lastMessage);
sim800l.println("\r\n");
updateSerial();
checkForMessage();
lastMessage=0;
sim800l.println("AT+CMGDA=\"DEL ALL\"\r\n");
updateSerial();
}
checkForMessage();
}
void checkForMessage()
{
while (Serial.available())
{
sim800l.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while (sim800l.available())
{
char character=sim800l.read();
command.concat(character);
if (character=='\n'){
if (command.indexOf("CMTI: \"SM\"")>=0)
{
lastMessage++;
}
if (command.indexOf("ON")>=0 && ok==1){
ok=0;
Serial.println("Porneste: "+command);
lastCommand="ON";
}
else{
if (command.indexOf("OFF")>=0 && ok==1){
ok=0;
lastCommand="OFF";
Serial.println("Opreste: "+command);
}
else {
Serial.println(command);
}
}
if (ok==1){
ok=0;
}
if (command.indexOf(nrAutorizat)>=0){
ok=1;
}
command="";
}
}
}
void sendMessage(String text,String number){
sim800l.println("AT+CMGF=1\r"); // Configuring TEXT mode
updateSerial();
delay(400);
sim800l.println("AT+CMGS=\""+number+"\"\r");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
updateSerial();
delay(400);
sim800l.println(text); //text content
delay(400);
sim800l.write(26);
}
void updateSerial(){
while (Serial.available())
{
sim800l.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while (sim800l.available())
{
Serial.write(sim800l.read());
}
}