Hi Dan,
I have built some security into my code. I have not used the char array as mentioned previuosly, but I will. When i run this code even if i send the correct code to the arduino i.e ?20!4#a1 it keeps executing the sendSMSFAULT function repeatedly. Also why is it runnign that part of the code because the correct code was sent to the arduino to turn on light. Please have a look at the attached code for please:
The sendSMSFAULT should only run if the incoming character does not begin with ? mark
#include <SoftwareSerial.h>
SoftwareSerial SIM900(2, 3); // RX, TX
char incoming_char=0; //Will hold the incoming character from the Serial Port.If the command becomes bigger than 8 bits, try use char16_t
// Pin 8 has an LED connected on most Arduino boards.
int led = 8;
int led13 = 13;
int led_status = 0; // variable to store the led status
/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare variables )-----*/
#define Relay_1 8 // Arduino Digital I/O pin number
#define Relay_2 7
#define Relay_3 6
#define Relay_4 5
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led13, OUTPUT);
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
digitalWrite(led, LOW); // Set led to LOW
digitalWrite(led13, LOW); // Set led to LOW
//-------( Initialize Pins so relays are inactive at reset)----
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.
Serial.println("setup Section");
SIM900.print("AT+CMGF=1\r"); // set SMS mode to text
delay(100);
SIM900.println("AT+CNMI=2,2,0,0,0\r");
SIM900.print("AT+CLIP=1\r"); // turn on caller ID notification
// blurt out contents of new SMS upon receipt to the GSM shield's serial out
delay(100);
//digitalWrite(led, LOW); // Set led to LOW
//digitalWrite(led13, LOW); // Set led to LOW
// Serial.println("AT+CMGD=1,4"); //Delete all SMS in box
}
void sendSMS(byte led_status){ //SEND SMS
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(1000);
SIM900.println("AT + CMGS = \"+277XXXXXXXX\""); // 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();
delay(5000); // give module time to send SMS
}
void sendSMSFAULT(){ //SEND SMS
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(1000);
SIM900.println("AT + CMGS = \"+277XXXXXXX\""); // recipient's mobile number, in international format
delay(1000);
SIM900.println( " SORRY! YOU ARE NOT ALLOWED TO USE THE SYSTEM");
delay(1000);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(1000);
SIM900.println();
delay(5000); // give module time to send SMS
}
// the loop routine runs over and over again forever:
void loop() {
if(SIM900.available() >0)
{
incoming_char=SIM900.read();
if (incoming_char!='?')
{
delay(10);
Serial.println("Discarded SMS");
sendSMSFAULT();
Serial.println("AT+CMGD=1,4");
incoming_char=SIM900.read();
if (incoming_char=='?')
{
delay(10);
incoming_char=SIM900.read();
if (incoming_char=='2')
{
delay(10);
incoming_char=SIM900.read();
if (incoming_char=='0')
{
delay(10);
incoming_char=SIM900.read();
if (incoming_char=='!')
{
delay(10);
incoming_char=SIM900.read();
if (incoming_char=='4')
{
delay(10);
incoming_char=SIM900.read();
if (incoming_char=='#')
{
delay(10);
incoming_char=SIM900.read();
if (incoming_char=='a')
{
delay(10);
incoming_char=SIM900.read();
if (incoming_char=='0')
{
digitalWrite(led, LOW);
digitalWrite(Relay_1, LOW);// set the Relay OFF
delay(1000); // wait for a second
}
else if (incoming_char=='1')
{
digitalWrite(led, HIGH);
digitalWrite(Relay_1, HIGH);// set the Relay ON
delay(1000); // wait for a second
}
else if (incoming_char=='S')
{
digitalRead(led);
led_status=digitalRead(led);
Serial.print(led_status); // prints status on serial terminal
sendSMS(led_status);
}
delay(10);
}
}
}
}
}
}
}
}
}
}
I also included this in the code as you previously advised in the void Setup, however even after the changes, when i upload the code onto the arduino, my LED turns ON and stays ON....
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led13, OUTPUT);
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
digitalWrite(led, LOW); // Set led to LOW
digitalWrite(led13, LOW); // Set led to LOW
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);