I am trying to control some electronics using text messaging. I am using a SIM900 GSM Shield. My problem comes when I am trying input the text message as a string and compare it to another.
I tried to do what I wanted by comparing each character of the message. It is very hard. I got it to work but I hated making changes to my code because it took me several minutes each time just to figure out what I had done. The idea of using string comparison seems a little bit more confusing for me to understand but in the end I am sure it will be worth it. I am having troubles though. For some reason my program is having a hard time recognizing equivalent strings. I kind of this that this is probably because I am declaring the strings wrong. For some reason when I text it "on:" it spits back out on the serial port a whole mess of numbers. Some of them being
111
110
58
These turn out to be the decimal representation of o n : I assume that the reason they are coming in separate is because I must be doing something wrong.
How to I properly declare compare these strings. (see last 10 lines or so of code)
Thanks,
Jake
// Example 55.4
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8);
int sensorPin = A0;
int sensorValue = 0;
void setup()
{
Serial.begin(19200); // for serial monitor
SIM900.begin(19200); // for GSM shield
SIM900power(); // turn on shield
delay(20000); // give time to log on to network.
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0\r");
// blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
}
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(5000);
Serial.println("ready");
}
void loop()
{
// Now we simply display any text that the GSM shield sends out on the serial monitor
if(SIM900.available() >0)
{
String incoming_string=String(SIM900.read()); //Get the character from the cellular serial port.
Serial.println(incoming_string); //Print the incoming character to the terminal.
if(incoming_string=="on:")digitalWrite(13, HIGH);
if(incoming_string=="off:")digitalWrite(13, LOW);
if(incoming_string=="sensor:")
{
sensorValue = analogRead(sensorPin);
}
}
}