Hi,
Im a newbie and trying to get my head around the code. I found some code to switch on outputs via a sms and have modified it to suit my needs. It basically should read a string of characters via the serial monitor and enable or disable a output.
The issue I am having is I can see the text come in on the serial monitor and get stuck after the characters are displayed. I have commented in the code where the issue is. Im not sure why its getting stuck at this point. Im only guessing it may be something to do with its buffer position.
Using a UNO, GPRS Sheild V2.0, DFRobot 4 Relay
Any help is appreciated.
#include <SoftwareSerial.h>
SoftwareSerial SIM900(7, 8); // RX, TX
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
char buffer[9]; // eight char plus null terminator
char secret_code[] = "2014#a1";
char secret_code1[] = "20!4#a0";
char secret_code2[] = "20!4#aS";
char incoming_char = 0;
byte buffer_pos = 0;
void setup()
{
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
Serial.begin(19200); // set the baud rate
SIM900power();
SIM900.begin(19200); // for GSM shield
delay(8000); // give time to log on to network.
SIM900.print("ATE0\r");
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.println("AT+CNMI=2,2,0,0,0\r");
delay(100);
Serial.println("Finished Setup Section");
}
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(7000);
}
void sendSMS(byte led_status)
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(1000);
SIM900.println("AT+CSCA =\"+61xxxxxxxxx\""); // Carriers SMS number
delay(1000);
SIM900.println("AT+CMGS =\"+61xxxxxxxxx\""); // recipient's mobile number, in international format
delay(1000);
if (led_status == 0)
{
SIM900.println( " LIGHT STATUS: OFF");
}
else
{
SIM900.println( " LIGHT STATUS: ON");
}
delay(1000);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(1000);
SIM900.println();
Serial.println("Message sent");
delay(5000); // give module time to send SMS
}
void loop()
{
if (SIM900.available() > 0)
{
Serial.println("Reading incoming data");
incoming_char = SIM900.read();
Serial.print(int(incoming_char));
Serial.print(" ");
Serial.println(incoming_char);
if (incoming_char == '?')
{
Serial.println("Got a ?");
delay(100);
buffer_pos = 0;
while (SIM900.available())
{
delay(100);
incoming_char = SIM900.read();
buffer[buffer_pos] = incoming_char;
buffer_pos++;
if (buffer_pos == 8) // Already incremented
{
//Print it out
buffer[buffer_pos] = '\0';
Serial.write(buffer);
Serial.println();
if (strcmp(buffer, secret_code) == 0)
{
// ????GETTING STUCK BEFORE THIS SECTION!!!!!!!!!
Serial.println("It's a match!");
digitalWrite(pin2, HIGH);
int led_status = digitalRead(pin2);
Serial.print ("LED status = ");
Serial.println(led_status); // prints status on serial terminal
// DISABLED sendSMS(led_status);
delay(1000); // wait for a second
}
buffer_pos = 0;
if (strcmp(buffer, secret_code1) == 0)
{
Serial.println("It's a match!");
digitalWrite(pin2, LOW);
int led_status = digitalRead(pin2);
Serial.print ("LED status = ");
Serial.println(led_status); // prints status on serial terminal
// DISABLED sendSMS(led_status);
delay(1000); // wait for a second
}
buffer_pos = 0;
if (strcmp(buffer, secret_code2) == 0)
{
Serial.println("It's a match!");
int led_status = digitalRead(pin2);
Serial.print ("LED status = ");
Serial.println(led_status); // prints status on serial terminal
sendSMS(led_status);
delay(1000); // wait for a second
}
buffer_pos = 0;
}
}
// SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
}
}