The first time I tried using the GSM module to send a text, I made the dumb mistake of not turning on the module. So it worked this time. I used the code that Benedetta had emailed us.
A modest next step: turn on a LED with a text. For now, I’m able to receive texts by sending them through the GSM module with CoolTerm, but I can’t receive any messages on the GSM module. I’ll need to figure out how to receive a message, match it, then change the LED pin output to HIGH.
UPDATE:
I attempted again with the white GSM module. I can read SMS with CoolTerm now, but there seems to be a long delay. It takes a few seconds for the messages to show. If I use the command AT+CMGL=”ALL”, only the first part of the first message would display correctly. The rest of the messages are in gibberish. I tried changing the delay that follows Serial.write(inChar); in the code. But this didn’t help.
I also tried to turn the LED on by texting I, and turning it off by texting O. After about 10 secs of texting I, the LED would turn on. But typing O did not work. Also if I change the letter in the code from I to O, the LED would not turn on. Another issue I had was that after testing with my SIM card for a while, I wouldn’t be able to read anymore SMS on that card though I was still able to send SMS out. I had to switch to another SIM card to continue testing and reading SMS.
This is how it worked when I text “I” to turn on the LED:
Here’s the code:
#include <SoftwareSerial.h> | |
SoftwareSerial mySerial(2, 3); | |
char inChar = 0; | |
char message[] = "where r u"; | |
void setup() | |
{ | |
Serial.begin(9600); | |
pinMode(7, OUTPUT); | |
Serial.println("Hello Debug Terminal!"); | |
// set the data rate for the SoftwareSerial port | |
mySerial.begin(9600); | |
//Turn off echo from GSM | |
mySerial.print("ATE0"); | |
mySerial.print("\r"); | |
delay(300); | |
//Set the module to text mode | |
mySerial.print("AT+CMGF=1"); | |
mySerial.print("\r"); | |
delay(500); | |
//Send the following SMS to the following phone number | |
mySerial.print("AT+CMGS=\""); | |
// CHANGE THIS NUMBER! CHANGE THIS NUMBER! CHANGE THIS NUMBER! | |
// 129 for domestic #s, 145 if with + in front of # | |
mySerial.print("3477847015\",129"); | |
mySerial.print("\r"); | |
delay(300); | |
// TYPE THE BODY OF THE TEXT HERE! 160 CHAR MAX! | |
mySerial.print(message); | |
// Special character to tell the module to send the message | |
mySerial.write(0x1A); | |
delay(500); | |
} | |
void loop() // run over and over | |
{ | |
// read sms in | |
if (mySerial.available()){ | |
inChar = mySerial.read(); | |
Serial.write(inChar); | |
delay(5); | |
if (inChar == 'I'){ | |
digitalWrite(7, HIGH); | |
//delay(1000); | |
} else if (inChar == 'O') { | |
digitalWrite(7, LOW); | |
//delay(1000); | |
} | |
} | |
// send sms out | |
if (Serial.available()>0){ | |
mySerial.write(Serial.read()); | |
} | |
} |
Question
Is Serial.write(mySerial.(read)) writing +CMTI: “SM”,sms# each time? Perhaps this is why typing I would turn the LED on – because +CMTI contains the letter I. If this is the case, what is an effective way to check for +CMTI: “SM”,sms#, mySerial.print() the AT commands for reading SMS, skip through the metadata for the SMS, and read the actual SMS message?