Hi All,
I'm a newbie on arduino.I'm doing a project where part of the requirements i have is to be able to turn the lights ON/OFF and get the STATUS of the lights via an SMS. The lights also need to be time controlled activated i.e. The lights must turn ON at 17:00PM and turn OFF at 08:00AM via SMS as well.
For the time controlled activation part i was thicking of sending an SMS with START date (START=17:00) and store this on EEPROM, then do a compare with the RTC
I have managed to get the below code to work with an assistance of a friend. This code seems to only work once for some reason I'm not able to figure out why. Can someone please help me to understand why this is behaving like that.
I also need some assistance on the time controlled actiavtion part, I know I will need to use the RTC for this part.
#include <SoftwareSerial.h>
SoftwareSerial SIM900(2,3); // RX, TX
int pin8 = 8;
int led13 = 13;
char buffer[9]; // eight char plus null terminator
char secret_code[] = "20!4#a1";
char secret_code1[] = "20!4#a0";
char secret_code2[] = "20!4#aS";
char incoming_char=0;
byte buffer_pos=0;
/*-----( Declare Constants )-----*/
#define RELAY_ON 1
#define RELAY_OFF 0
/*-----( Declare variables )-----*/
#define Relay_1 8 // Arduino Digital I/O pin number
#define Relay_2 7
#define Relay_3 6
#define Relay_4 5
void setup()
{
pinMode(pin8, OUTPUT);
pinMode(led13, OUTPUT);
digitalWrite(led13, LOW);
digitalWrite(pin8, LOW); // Set led to LOW
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
Serial.begin(19200); // set the baud rate
SIM900.begin(19200); // for GSM shield
delay(20000); // 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 sendSMS(byte led_status)
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(1000);
SIM900.println("AT + CMGS = \"+27XXXXXXXXXXX\""); // 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();
if (incoming_char=='?')
{
Serial.println("Got a ?");
delay(100);
while (SIM900.available())
{
delay(100);
incoming_char=SIM900.read();
buffer[buffer_pos]=incoming_char;
buffer_pos++;
if (buffer_pos==7) // Already incremented
{
//Print it out
buffer[buffer_pos]='\0';
Serial.write(buffer);
Serial.println();
if (strcmp(buffer,secret_code)==0)
{
Serial.println("It's a match!");
digitalWrite(pin8, HIGH);
int led_status=digitalRead(pin8);
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;
if (strcmp(buffer,secret_code1)==0)
{
Serial.println("It's a match!");
digitalWrite(pin8, LOW);
int led_status=digitalRead(pin8);
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;
if (strcmp(buffer,secret_code2)==0)
{
Serial.println("It's a match!");
int led_status=digitalRead(pin8);
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;
}
}
}
}
}
Any assistance will be highly appreciated.