Sie sind auf Seite 1von 3

// Include Software Serial library to communicate with GSM

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
// Configure software serial port
int l1,l2;
SoftwareSerial SIM900(9,10); //9-GSM TX,10-GSM RX
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Variable to store text message


String textMessage;
char t[15];

// Create a variable to store Lamp state


String lampState = "HIGH";

void setup(){
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
pinMode(8,OUTPUT);
digitalWrite(13,HIGH);
digitalWrite(8,HIGH);
Serial.begin(19200);
SIM900.begin(19200);
lcd.begin(16, 2);
lcd.print("STREETLIGHT");
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
// Set module to send SMS data to serial out upon receipt
SIM900.print("AT+CNMI=2,2,0,0,0\r");
delay(100);
}

void loop(){
if(SIM900.available()>0){
textMessage = SIM900.readString();
Serial.print(textMessage);
delay(10);
int i,j=0;
for(i=8;i<=22;i++)
{
t[j]=textMessage[i];
j++;
}
delay(10);
}

if(textMessage.indexOf("a")>=0){
digitalWrite(13,LOW);
l1=1;
lampState = "on";
Serial.println("LED1 set to ON");
lcd.setCursor(0, 1);
lcd.print("LED1 IS ON ");
}
if(textMessage.indexOf("b")>=0){
// Turn off relay and save current state
digitalWrite(13,HIGH);
l1=0;
lampState = "off";
Serial.println("LED1 set to OFF");
lcd.setCursor(0, 1);
lcd.print("LED1 IS OFF ");
}
if(textMessage.indexOf("c")>=0){
digitalWrite(8,LOW);
l2=1;
lampState = "on";
Serial.println("LED2 set to ON");
lcd.setCursor(0, 1);
lcd.print("LED2 IS ON ");
}
if(textMessage.indexOf("d")>=0){
// Turn off relay and save current state
digitalWrite(8,HIGH);
l2=0;
lampState = "off";
Serial.println("LED2 set to OFF");
lcd.setCursor(0, 1);
lcd.print("LED2 IS OFF ");
}
if(textMessage.indexOf("f")>=0){
digitalWrite(8,HIGH);
digitalWrite(13,HIGH);
lampState = "off";
l1=l2=0;
Serial.println("LED1,2 set to OFF");
lcd.setCursor(0, 1);
lcd.print("LED1,2 IS OFF ");
}
if(textMessage.indexOf("e")>=0){
// Turn off relay and save current state
digitalWrite(8,LOW);
digitalWrite(13,LOW);
lampState = "on";
l1=l2=1;
Serial.println("LED1,2 set to ON");
lcd.setCursor(0, 1);
lcd.print("LED1,2 IS ON ");
}
if(textMessage.indexOf("s")>=0){
// Turn off relay and save current state
if(l1==0 && l2==0)
{
textMessage = "LED1,2 IS OFF";
lcd.setCursor(0, 1);
lcd.print("LED1,2 IS OFF ");
}
if(l1==1 && l2==1)
{
textMessage = "LED1,2 IS ON";
lcd.setCursor(0, 1);
lcd.print("LED1,2 IS ON ");
}
if(l1==0 && l2==1)
{
textMessage = "LED1 OFF,LED2 IS ON";
lcd.setCursor(0, 1);
lcd.print("LED1 OFF,LED2 ON");
}
if(l1==1 && l2==0)
{
textMessage = "LED1 ON,LED2 IS OFF";
lcd.setCursor(0, 1);
lcd.print("LED1 ON,LED2 OFF");
}
sendSMS(textMessage);
}
}

// Function that sends SMS


void sendSMS(String message){
// AT command to set SIM900 to SMS mode
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.print('A');
SIM900.print('T');
SIM900.print('+');
SIM900.print('C');
SIM900.print('M');
SIM900.print('G');
SIM900.print('S');
SIM900.print('=');
for(int i=0;i<=14;i++)
{
SIM900.print(t[i]);
delay(10);
}
SIM900.println("\r");
delay(100);
// Send the SMS
SIM900.println(message);
delay(100);

// End AT command with a ^Z, ASCII code 26


SIM900.println((char)26);
delay(100);
SIM900.println();
// Give module time to send SMS
delay(500);
}