I've been trying this one out for a while now and I got a problem with the sms code. It doesn't read incoming sms. I just want to continuously wait for incoming sms so that if the text is correct, the led will light up, otherwise it won't. I can't think what is wrong or missing..
char inchar; //Will hold the incoming character from the Serial Port.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{
// prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600); // for GSM module communication
delay(1000); // give time for GSM module to register on network etc.
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
}
void loop()
{
Serial.println("AT+CMGL=\"REC UNREAD\"");
delay(200);
Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
//If a character comes in from the cellular module...
if(Serial.available() >0)
{
inchar=Serial.read();
if (inchar=='#')
{
delay(10);
inchar=Serial.read();
if (inchar=='a')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
inchar=Serial.read();
if (inchar=='b')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
inchar=Serial.read();
if (inchar=='c')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
inchar=Serial.read();
if (inchar=='d')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
Serial.println("AT+CMGD=1,0");
}
}
}
}
}
Moderator: I split this post off - it seems to have nothing to do with the original topic it was posted under.
AWOL
It would be a lot easier if you moved the sms device to some other pins, and used NewSoftSerial to talk to it. Then, you could use the hardware serial port to send data to a PC, for debugging purposes. Otherwise, you haven't a clue how the sms device is responding to the commands you issue.
I tried this but still doesn't do anything.. I don't even get anything from the serial monitor. I reinstalled the newsoftserial library and restarted the IDE but no luck.
#include <NewSoftSerial.h> //Include the NewSoftSerial library to send serial commands to the cellular module.
char inchar; //Will hold the incoming character from the Serial Port.
NewSoftSerial cell(2,3); //Create a 'fake' serial port. Pin 2 is the Rx pin, pin 3 is the Tx pin.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{ // prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW); //Initialize GSM module serial port for communication.
cell.begin(9600);
delay(1000); // give time for GSM module to register on network etc.
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
cell.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
}
void loop()
{
//If a character comes in from the cellular module...
if(cell.available() >0)
{
inchar=cell.read();
if (inchar=='#') // OK - the start of our command
{
delay(10);
inchar=cell.read();
if (inchar=='a')
{
delay(10);
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='b')
{
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='c')
{
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
inchar=cell.read();
if (inchar=='d')
{
delay(10);
inchar=cell.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
cell.println("AT+CMGD=1,0");
}
}
}
}
}
The reason for using NewSoftSerial is so that it frees up the Serial port for debugging. That means that you are supposed to add some debugging statements.
That means that you are supposed to add some debugging statements.
Can you help me with that?
By the way, I've managed to get the code from the first post working by moving the AT commands from the loop() to the setup(). I am now able to receive and read the sms but only works after resetting the arduino. So everytime I received sms, I have to reset the board in order to read it and to do what it has to do.
char inchar; //Will hold the incoming character from the Serial Port.
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup()
{
// prepare the digital output pins
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
//Initialize GSM module serial port for communication.
Serial.begin(9600); // for GSM module communication
delay(1000); // give time for GSM module to register on network etc.
Serial.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
Serial.println("AT+CMGL=\"REC UNREAD\"");
delay(200);
Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(2000);
}
void loop()
{
//If a character comes in from the cellular module...
if(Serial.available() >0)
{
inchar=Serial.read();
if (inchar=='#')
{
delay(10);
inchar=Serial.read();
if (inchar=='a')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led1, LOW);
}
else if (inchar=='1')
{
digitalWrite(led1, HIGH);
}
delay(10);
inchar=Serial.read();
if (inchar=='b')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led2, LOW);
}
else if (inchar=='1')
{
digitalWrite(led2, HIGH);
}
delay(10);
inchar=Serial.read();
if (inchar=='c')
{
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led3, LOW);
}
else if (inchar=='1')
{
digitalWrite(led3, HIGH);
}
delay(10);
inchar=Serial.read();
if (inchar=='d')
{
delay(10);
inchar=Serial.read();
if (inchar=='0')
{
digitalWrite(led4, LOW);
}
else if (inchar=='1')
{
digitalWrite(led4, HIGH);
}
delay(10);
}
}
Serial.println("AT+CMGD=1,0");
}
}
}
}
}
By the way, I've managed to get the code from the first post working by moving the AT commands from the loop() to the setup(). I am now able to receive and read the sms but only works after resetting the arduino. So everytime I received sms, I have to reset the board in order to read it and to do what it has to do.
It might have something to do with the AT+CNMI command. Try adding another zero to the end, it should have 5 arguments.
Try adding another zero to the end, it should have 5 arguments
Still the same.I want the gsm to continuously wait and read new sms but putting these AT commands inside the loop() doesn't seem to receive and read the new sms at all but the commands are in the setup() it will work but needs to reset the board after receiving sms so that it will execute what's inside the loop(). PLEASE HELP
Serial.println("AT+CMGL=\"REC UNREAD\"");
delay(200);
Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
delay(200);
I'm using SIM900D.When using hyperterminal, both receiving and sending sms is okay . Only in arduino not working.Don't know why. Do you think there's something missing in the code? Putting the AT commands inside the loop() should work out fine right since it works when in setup()?? Don't know what to do now..
If the SMS is automatically displayed to the terminal what is the purpose of the CMGL function? I can probably test it later but if the message goes straight to the terminal and then you issue a AT+CMGL="REC UNREAD" it probably won't show anything because the SMS should now be classified as READ.
I'm assuming you're checking the body of the SMS for some string you've sent it, but can you tell us exactly what is being received? In other words add some Serial prints for debugging.
If the SMS is automatically displayed to the terminal what is the purpose of the CMGL function?
I want the program to read only the new sms therefore only the unread sms which is currently received by the gsm module. And after the program reads it, it will delete it so that there will be no sms in the inbox.When I get this whole thing work, I am going to implement this on my project which is the electronic vault wherein you can open it via sms.
I don't see why not. If you had an extra IO pin available you could just monitor the Ring Indicator pin on the GSM by setting an interrupt on the falling edge. When an SMS is received the pin goes low and then you could send a CMGR to read the message.