Good morning everyone, I'm trying to include in my project a GSM module to send and receive messages. The problem is the following:
I send a message, I receive it and I display it on the serial monitor, so far everything is fine. What I don't understand is why it doesn't execute the instruction inside the if it's as if it can't see it.
I'm using a mega 2560 arduino and I've connected the module on the serial port Tx1 and Rx1.
This is the code:
//-------------------------------
String textMessage;
const int relay = 13;
String lampState = "HIGH";
//-------------------------------
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and SIM800L
Serial1.begin(9600);
Serial.println("Initializing...");
delay(1000);
Serial1.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
Serial1.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
Serial1.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
}
void loop()
{
updateSerial();
if(textMessage.indexOf("ON")>=0){
// Turn on relay and save current state
digitalWrite(relay, LOW);
lampState = "on";
Serial.println("Relay set to ON");
textMessage = "";
}
if(textMessage.indexOf("OFF")>=0){
// Turn off relay and save current state
digitalWrite(relay, HIGH);
lampState = "off";
Serial.println("Relay set to OFF");
textMessage = "";
}
}
void updateSerial()
{
delay(500);
while (Serial.available()) //
{
Serial1.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(Serial1.available())
{
Serial.write(Serial1.read());//Forward what Software Serial received to Serial Port
}
}
and that's what I read in the serial port:
Initializing...
AT
OK
AT+CMGF=1
OK
AT+CNMI=1,2,0,0,0
OK
+CMT: "+--------------","","19/08/06,11:54:02+08"
ON
I deleted my phone number for private reasons.
Can anyone tell me why they don't get into the if and do not do the education?
Thanks a lot to everyone who wants to help me, probably the solution is under my nose but I can't see it... 
Do you want to run the if when the condition is true or false?
if(textMessage.indexOf("ON")>=0)
If I wanted the if's to do the thing with a true, I'd code
if(textMessage.indexOf("ON"))
as a number above >0 would represent a true and 0 is false. To me it looks like your code is looking for a 0 and anything greater then a 0 as being true.
Yes, what I want to do is to go and read the message that I send with the phone and if I wrote ON the led lights up and instead if it says OFF it goes off.
Making your change I get this on the serial port:
Initializing...
AT
OK
AT+CMGF=1
OK
AT+CNMI=1,2,0,0,0
OK
Relay set to ON
Relay set to OFF
Relay set to ON
Relay set to OFF
Relay set to ON
Relay set to OFF
Relay set to ON
Relay set to OFF
Relay set to ON
Relay set to OFF
Relay set to ON
Relay set to OFF
Relay set to ON
Relay set to OFF
Relay set to ON
........
and he goes on like this forever without me doing anything and without me sending him the message.
I need to know what function allows me to make Arduino understand that I wrote ON or OFF on my message and make him react accordingly.
I don't know if indexOf is used to read the words I send in the message and compare them to what I wrote in brackets.
I know it will be an easy thing to do but I don't understand how to do it, does anyone know how to do it and which command should I use? 
Here is some Serial String parsing code that I wrote:
void fParseLIDAR_ReceivedSerial ( void * parameters )
{
String sTmp = "";
sTmp.reserve ( 20 );
String sMessage = "";
sMessage.reserve ( StringBufferSize300 );
for ( ;; )
{
EventBits_t xbit = xEventGroupWaitBits (eg, evtParseLIDAR_ReceivedSerial, pdTRUE, pdTRUE, portMAX_DELAY) ;
xQueueReceive ( xQ_LIDAR_Display_INFO, &sMessage, QueueReceiveDelayTime );
int commaIndex = sMessage.indexOf(',');
sTmp.concat ( sMessage.substring(0, commaIndex) );
sMessage.remove( 0, (commaIndex + 1) ); // chop off begining of message
if ( sTmp == "!" )
{
xSemaphoreGive ( sema_LIDAR_OK );
// Display info from LIDAR
sLIDAR_Display_Info = sMessage;
}
if ( sTmp == "$" )
{
xEventGroupSetBits( eg1, evtResetWatchDogVariables );
}
if ( sTmp == "#")
{
xSemaphoreTake( sema_LIDAR_Alarm, xSemaphoreTicksToWait );
sLIDAR_Alarm_info = sMessage;
xSemaphoreGive( sema_LIDAR_Alarm );
xEventGroupSetBits( eg, evtfLIDAR_Alarm );
}
sTmp = "";
sMessage = "";
xSemaphoreGive( sema_ParseLIDAR_ReceivedSerial );
}
vTaskDelete( NULL );
} // void fParseReceivedSerial ( void * parameters )
. Perhaps it may help.
probably the solution is under my nose but I can't see it..
You don't read the incoming message into the textMessage String.
Idahowalker:
If I wanted the if's to do the thing with a true, I'd code
if(textMessage.indexOf("ON"))
as a number above >0 would represent a true and 0 is false. To me it looks like your code is looking for a 0 and anything greater then a 0 as being true.
indexof() returns -1 if the target string is not present, and returns the offset (which may be zero) if it is found. The test originally posted is fine.
Your proposed test would not work properly at all.
gardner:
indexof() returns -1 if the target string is not present, and returns the offset (which may be zero) if it is found. The test originally posted is fine.
Your proposed test would not work properly at all.
Oi! Thanks for the reminder.
Thanks a lot to everyone for the interest, looking again on the internet I found a simplified program that does what I want.
Unfortunately, I get an error message that I don't understand. Attached I put the code and the error message, maybe someone knows how to fix it ...
Code:
#include <Sim800l.h>
#include <SoftwareSerial.h>
Sim800l Sim800l;
String text;
uint8_t index;
uint8_t LED=13;
String textSms;
void setup(){
pinMode(LED,OUTPUT);
Serial.begin(9600);
Sim800l.begin();
Sim800l.delAllSms();
}
void loop(){
text=Sim800l.readSms(1);
if (text.indexOf("TURNON")!=-1){
Serial.println(text);
Serial.println("LED TURN ON");
digitalWrite(LED,1);
Sim800l.delAllSms();
}
if (text.indexOf("TURNOFF")!=-1){
Serial.println("LED TURN OFF");
digitalWrite(LED,0);
Sim800l.delAllSms();
}
}
Error:
In file included from C:\Users\lucam\OneDrive\Documenti\Cascina\ProvaLetturaSemplificata\ProvaLetturaSemplificata.ino:1:0:
C:\Users\lucam\OneDrive\Documenti\Arduino\libraries\Sim800l-master/Sim800l.h:41:13: error: expected unqualified-id before 'true'
#define LED true // used for indicator led, in case that you don want set to false .
^
C:\Users\lucam\OneDrive\Documenti\Cascina\ProvaLetturaSemplificata\ProvaLetturaSemplificata.ino:6:9: note: in expansion of macro 'LED'
uint8_t LED=13;
^
exit status 1
Error compiling for board Arduino/Genuino Uno.
all the libraries are installed, I do not understand at all...
For whatever reason, known only to its authors, Sim800l.h #defines the macro LED. This basically makes that symbol off limits for your sketch. If you replace
uint8_t LED=13;
With something like:
const uint8_t LED_PIN=13;
you will avoid the conflicting macro definition. I added the const, since I don't think you're planning to move the LED pin around at runtime and the compiler can handle the constant a bit more efficiently.
There are several versions of a sim800l.h library with the same name. Where does yours come from?
However, I believe that there is conflict between how the library and you are treating the on board indicator LED at pin 13.
Try changing your code as follows
//uint8_t LED=13;
uint8_t myLED=13;
and where you write to it
digitalWrite(myLED,1);
digitalWrite(myLED,0);
I downloaded the one that was with the code I published, so I guess it's right.
how do I see if the libraries and code are compatible?