Hello,
I am using the official GSM shield with an official Mega 2560. I have the board sending and received texts, and I am trying to work on getting an LED to turn on when I send a specific word in a text; currently that word is “Test”. My issue is nothing occurs when I send anything to the board. It receives the text, but no change is made in the LED.
I have verified that the LED is wired correctly, by having it turn on with a different code. I think my issue has an easy fix, but I cannot find it. Thank you in advance!
My code:
// include the GSM library
#include <GSM.h>
// PIN Number for the SIM
#define PINNUMBER ""
// initialize the library instances
GSM gsmAccess(true);
GSM_SMS sms;
// Array to hold the number a SMS is retreived from
char senderNumber[20];
int pin8 = 0;
void setup()
{
pinMode(8, OUTPUT); // LED
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("SMS Messages Receiver");
// connection state
boolean notConnected = false;
// Start GSM connection
while (notConnected)
Serial.println("A");
{
// Serial.println("B");
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
notConnected = false;
else
{
// Serial.println("C");
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
Serial.println("Waiting for messages");
// Serial.println("D");
}
void loop()
{
// Serial.println("E");
char c;
Serial.print("Pin 8 = ");
Serial.println(pin8);
if (pin8 == 1){
digitalWrite(8, HIGH);
}
else if (pin8 == 0){
digitalWrite(8, LOW);
}
// If there are any SMSs available()
if (sms.available())
{
Serial.println("Message received from:");
// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);
// An example of message disposal
// Any messages starting with # should be discarded
if (sms.peek() == '#')
{
Serial.println("Discarded SMS");
sms.flush();
}
// Read message bytes and print them
while (c = sms.read())
Serial.print(c);
if (c == 'Test'){
pin8 = 1;
Serial.println("Inside if");
Serial.println(pin8);
}
else if (c == 'Off'){
pin8 = 0;
Serial.println("Inside else if");
Serial.println(pin8);
}
Serial.println("\nEND OF MESSAGE");
// Delete message from modem memory
sms.flush();
Serial.println("MESSAGE DELETED");
}
delay(1000);
}
My issue is in here:
while (c = sms.read())
Serial.print(c);
if (c == 'Test'){
pin8 = 1;
Serial.println("Inside if");
Serial.println(pin8);
}
else if (c == 'Off'){
pin8 = 0;
Serial.println("Inside else if");
Serial.println(pin8);
}
My serial output showing the issue (only showing the relevant part):
Pin 8 = 0
AT+CMGL="REC UNREAD",1%13%
46 52>%13%%10%OK%13%%10%
Pin 8 = 0
AT+CMGL="REC UNREAD",1%13%
69 20>%13%%10%+CMGL: 1,"REC UNREAD","+XXXXXXXXXXX","","2015/03/18 17:17:41-16"%13%%10%Got%13%%10%%13%%10%OK%13%%10%
Message received from:
+XXXXXXXXXXX
Test
END OF MESSAGE
AT+CMGD=1%13%
20 26>%13%%10%OK%13%%10%
MESSAGE DELETED
Pin 8 = 0
AT+CMGL="REC UNREAD",1%13%
26 32>%13%%10%OK%13%%10%
Pin 8 = 0
AT+CMGL="REC UNREAD",1%13%
32 38>%13%%10%OK%13%%10%
Pin 8 = 0
AT+CMGL="REC UNREAD",1%13%
38 44>%13%%10%OK%13%%10%
Pin 8 = 0