I am absolutely newbie in Arduino field. Currently I am building a simple security system with SIM900 shield. I got stuck with controlling a LED with SMS messages. I managed to control the led by serial commands ( 'b' = on and 'k'=off). But I cannot manage to solve to change the led status by sending text messages to the GSM shield. (Of course SIM card is added. I have already been able to call remote numbers). Could you please help me what should be changed in the code? If I send a word like "arming" in SMS then I would like the led to turn on. I do not use a unique gsm library. Thank you for your help in advance.
Here is the code:
//#include <SoftwareSerial.h>
//SoftwareSerial SIM900(7, 8); // configure software serial port
char inchar; //Will hold the incoming character from the Serial Port.
int led = 13;
void setup()
{
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600);
delay(3000); // give time for GSM module to register on network etc.
Serial.println("AT+CMGF=1\r"); // set SMS mode to text
delay(200);
Serial.println("AT+CSMS=1\r");
delay(200);
Serial.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
Serial.println("AT+CMGD=1,4"); // delete all SMS
}
void loop()
{
if(Serial.available() >0)
{
delay(10);
inchar=Serial.read();
if (inchar=='k')
{
digitalWrite(led, LOW);
}
else if (inchar=='b')
{
digitalWrite(led, HIGH);
}
delay(10);
}
}
it means make sure brackets and lines are aligned properly
Yes I see, thanks for the hint
I worked forward on my project without half success. So far I've managed to send SMS to the GSM shield and the serial monitor print that sent letter ('b') and the led turn on. But if I send the second SMS with the turning off letter ('k') then nothing happens
Any idea pls? Please help me correcting the code. Hard to understand all steps without deep experience.
Thx in advance.
Code is on below.
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8); // configure software serial port
char inchar; //Will hold the incoming character from the Serial Port.
boolean newData = false;
int led = 13;
void setup()
{
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
delay(1000);
SIM900.begin(9600); //Initialize GSM module serial port for communication.
delay(3000); // give time for GSM module to register on network etc.
SIM900.println("AT+CMGF=1\r"); // set SMS mode to text
delay(200);
SIM900.println("AT+CSMS=1\r");
delay(200);
SIM900.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
SIM900.println("AT+CMGD=1,4"); // delete all SMS
delay(2000);
}
void loop()
{
if (SIM900.available() > 0)
{
delay(100);
inchar = SIM900.read();
Serial.print(inchar);
if (inchar == 'k')
{
delay(100);
digitalWrite(led, LOW);
delay(1000);
SIM900.println("AT+CMGF=1\r"); // set SMS mode to text
delay(300);
SIM900.println("AT+CSMS=1\r");
delay(300);
SIM900.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt
delay(300);
SIM900.println("AT+CMGD=1,4"); // delete all SMS
delay(2000);
}
else if (inchar == 'b')
{
delay(100);
digitalWrite(led, HIGH);
delay(1000);
SIM900.println("AT+CMGF=1\r"); // set SMS mode to text
delay(300);
SIM900.println("AT+CSMS=1\r");
delay(300);
SIM900.println("AT+CNMI=2,2,0,0,0"); // set module to send SMS data to serial out upon receipt
delay(300);
SIM900.println("AT+CMGD=1,4"); // delete all SMS
delay(2000);
}
}
}
Your challenge is that your code is full of totally un-necessary delays and you send AT commands to your module to set it up but never read the answer from those AT commands so when you hit the loop you'll likely receive data that might not be SMS related...
have you studied the Serial Input Basics examples listed above? this is the right way to handle a serial port.
Sorry I was out for a few days. Thank you for your advices. I tried to understand the Serial Input Basics but I do not see what exactly I missed.
In the below code there is a read command (inchar = SIM900.read();). I thought that is enough for reading out the character I sent in SMS.
if (SIM900.available() > 0)
{
delay(100);
inchar = SIM900.read();
Serial.print(inchar);
Where else should I use reading commands? In the setup part?
My motion sensor security system works but I would like to turn it on/off remotely with a simple character in SMS. That is why I would need this part to be completed. Until it is not solved I cannot place the security system out. If I can control a led with an SMS then I can control a PIR sensor remotely easily.
write a small code that reads through all the stuff received on the SIM900 when you send a SMS
--> you'll see that before showing you your SMS content there are tons of other characters - something like +CMT (or something equivalent) and after that you have the mobile number of the sender and the date and then at the end is really the body of the SMS message
so basically if you send an SMS with just "k", what you'll receive is a flow of ASCII bytes like
so you need to parse that the right way, detect the +CMT to ensure it's a command of interest, skip the rest of the line (could be just \n or \r and not both - you have to figure that out), get to the new line and here is your command --> hence understanding Serial Input Basics can help