#include #include SoftwareSerial mySerial(7, 8); String msg = String(""); dht DHT; int SmsContentFlag = 0; #define DHT11_PIN A1 //temperature and humidity sensor pin #include LiquidCrystal lcd(11, 10, 5, 4, 3, 2); int LDRpin = A0; //LDR pin int ldr; int LDRValue; int relay = 6; int check = 0; int fan = 13; int pingpin =12; int readSwitch = 9; //int j; void setup() { mySerial.begin(19200); // the GPRS baud rate Serial.begin(19200); // the GPRS baud rate lcd.begin(16, 2); pinMode(relay, OUTPUT); pinMode( fan, OUTPUT ); pinMode( pingpin, OUTPUT ); pinMode(readSwitch, INPUT); digitalWrite( relay, HIGH ); digitalWrite( fan, LOW ); digitalWrite( pingpin, HIGH ); } void loop() { Serial.println(""); serialData(); Serial.print("check = "); Serial.println(check); intrusion(); delay (100); Serial.print("check1 = "); Serial.println(check); if (check == 1){ sendmesg(); check = 0; delay(3000); lcd.clear(); } lcd.clear(); ldr = analogRead(LDRpin); LDRValue = ldr; int chk = DHT.read11(DHT11_PIN); lcd.setCursor(1,0); lcd.print("LDR VALUE="); lcd.setCursor(11,0); lcd.print(LDRValue); lcd.setCursor(14,0); lcd.print("%"); if(LDRValue >= 15){ lcd.setCursor(1,1); lcd.print("ROOM IS BRIGHT"); }else { lcd.setCursor(2,1); lcd.print("ROOM IS DARK"); } Serial.print("LDR VALUE = "); Serial.print(LDRValue); Serial.println(" Percent"); delay(1500); intrusion(); delay (100); Serial.print("check1 = "); Serial.println(check); if (check == 1){ sendmesg(); check = 0; delay(3000); lcd.clear(); } lcd.clear(); lcd.setCursor(0,0); lcd.print("Temp="); lcd.setCursor(6,0); lcd.print(DHT.temperature); lcd.setCursor(11,0); lcd.print("*C"); Serial.print("Temperature: "); Serial.print(DHT.temperature); Serial.println(" degree C"); lcd.setCursor(0,1); lcd.print("Humid="); lcd.setCursor(7,1); lcd.print(DHT.humidity); lcd.setCursor(12,1); lcd.print("%"); Serial.print("Humidity: "); Serial.print(DHT.humidity); Serial.println(" Percent"); delay(1500); int readState = digitalRead(readSwitch); // print out the state of the button: Serial.print("readState= "); Serial.println(readState); int pingState = digitalRead(pingpin); // print out the state of the button: Serial.print("pingState= "); Serial.println(pingState); while (readState == 0 && pingState == 0){ Serial.println(""); serialData(); lcd.clear(); ldr = analogRead(LDRpin); LDRValue = ldr; int chk = DHT.read11(DHT11_PIN); lcd.setCursor(1,0); lcd.print("LDR VALUE="); lcd.setCursor(11,0); lcd.print(LDRValue); lcd.setCursor(14,0); lcd.print("%"); if(LDRValue >= 15){ lcd.setCursor(1,1); lcd.print("ROOM IS BRIGHT"); }else { lcd.setCursor(2,1); lcd.print("ROOM IS DARK"); } Serial.print("LDR VALUE = "); Serial.print(LDRValue); Serial.println(" Percent"); delay(3000); if(LDRValue <= 15 ){ digitalWrite(relay, LOW); Serial.println("Relay is ON"); } else{ digitalWrite(relay, HIGH); Serial.println("Relay is OFF"); } lcd.clear(); lcd.setCursor(0,0); lcd.print("Temp="); lcd.setCursor(6,0); lcd.print(DHT.temperature); lcd.setCursor(11,0); lcd.print("*C"); Serial.print("Temperature: "); Serial.print(DHT.temperature); Serial.println(" degree C"); lcd.setCursor(0,1); lcd.print("Humid="); lcd.setCursor(7,1); lcd.print(DHT.humidity); lcd.setCursor(12,1); lcd.print("%"); Serial.print("Humidity: "); Serial.print(DHT.humidity); Serial.println(" Percent"); delay(3000); if(DHT.temperature >= 26 ){ digitalWrite(fan, HIGH); Serial.println("FAN is ON"); } else{ digitalWrite(fan, LOW); Serial.println("FAN is OFF"); } int readState = digitalRead(readSwitch); // print out the state of the button: Serial.print("readState= "); Serial.println(readState); if(readState == 1){ digitalWrite(relay, HIGH); digitalWrite(fan, LOW); break; } } } void serialData(){ char SerialInByte; if(Serial.available()) { mySerial.print((unsigned char)Serial.read()); } while(mySerial.available()) { char SerialInByte; SerialInByte = (unsigned char)mySerial.read(); Serial.print( SerialInByte ); // ------------------------------------------------------------------- // EN: Program also listen to the GPRS shield message. // ------------------------------------------------------------------- // EN: If the message ends with then process the message if( SerialInByte == 13 ){ // EN: Store the char into the message buffer ProcessGprsMsg(); } if( SerialInByte == 10 ){ // EN: Skip Line feed } else { // EN: store the current character in the message string buffer msg += String(SerialInByte); } } } void ProcessSms( String sms ){ Serial.print( "ProcessSms for [" ); Serial.print( sms ); Serial.println( "]" ); if( sms.indexOf("von") >= 0 ){ digitalWrite( fan, HIGH ); Serial.println( "FAN IS ON" ); return; } if( sms.indexOf("off") >= 0 ){ digitalWrite( fan, LOW ); Serial.println( "FAN IS OFF" ); return; } if( sms.indexOf("ron") >= 0 ){ digitalWrite( relay, LOW ); Serial.println( "RELAY IS ON" ); return; } if( sms.indexOf("quo") >= 0 ){ digitalWrite( relay, HIGH ); Serial.println( "RELAY IS OFF" ); return; } if( sms.indexOf("active") >= 0 ){ digitalWrite( pingpin, HIGH ); Serial.println( "ACTIVATOR" ); lcd.clear(); lcd.setCursor(0,0);//set cursor (colum by row) indexing from 0 lcd.print("SECURITY SYSTEM"); lcd.setCursor(2,1); lcd.print("ACTIVATED!!"); delay(2000); return; } if( sms.indexOf("status") >= 0 ){ send_status(); delay(7000); Serial.println( "status sent" ); return; } if( sms.indexOf("fes") >= 0 ){ digitalWrite( pingpin, LOW ); Serial.println( "DEACTIVATOR" ); lcd.clear(); lcd.setCursor(0,0);//set cursor (colum by row) indexing from 0 lcd.print("SECURITY SYSTEM"); lcd.setCursor(2,1); lcd.print("DEACTIVATED!!"); delay(2000); return; } sms = ""; } void GprsTextModeSMS(){ mySerial.println( "AT+CMGF=1" ); } void GprsReadSmsStore( String SmsStorePos ){ // Serial.print( "GprsReadSmsStore for storePos " ); // Serial.println( SmsStorePos ); mySerial.print( "AT+CMGR=" ); mySerial.println( SmsStorePos ); } // EN: Clear the GPRS shield message buffer void ClearGprsMsg(){ msg = ""; } // EN: interpret the GPRS shield message and act appropiately void ProcessGprsMsg() { Serial.println(""); Serial.print( "GPRS Message: [" ); Serial.print( msg ); Serial.println( "]" ); if( msg.indexOf( "Call Ready" ) >= 0 ){ Serial.println( "*** GPRS Shield registered on Mobile Network ***" ); GprsTextModeSMS(); } // EN: unsolicited message received when getting a SMS message if( msg.indexOf( "+CMTI" ) >= 0 ){ Serial.println( "*** SMS Received ***" ); // EN: Look for the coma in the full message (+CMTI: "SM",6) // In the sample, the SMS is stored at position 6 int iPos = msg.indexOf( "," ); String SmsStorePos = msg.substring( iPos+1 ); Serial.print( "SMS stored at " ); Serial.println( SmsStorePos ); // EN: Ask to read the SMS store GprsReadSmsStore( SmsStorePos ); } // EN: SMS store readed through UART (result of GprsReadSmsStore request) if( msg.indexOf( "+CMGR:" ) >= 0 ){ // EN: Next message will contains the BODY of SMS SmsContentFlag = 1; // EN: Following lines are essentiel to not clear the flag! ClearGprsMsg(); return; } // EN: +CMGR message just before indicate that the following GRPS Shield message // (this message) will contains the SMS body if( SmsContentFlag == 0 ){ ProcessSms( msg ); } ClearGprsMsg(); // EN: Always clear the flag SmsContentFlag = 0; } void intrusion(){ int readState = digitalRead(readSwitch); // print out the state of the button: Serial.print("readState= "); Serial.println(readState); int pingState = digitalRead(pingpin); // print out the state of the button: Serial.print("pingState= "); Serial.println(pingState); if(readState == 0 && pingState == 1) { lcd.clear(); lcd.setCursor(3,0); lcd.print("ATTENTION"); lcd.setCursor(0,1); lcd.print("INTRUTION @ HOME"); check = 1; delay(4000); //Wait for 3 seconds. } } void send_status(){ lcd.clear(); lcd.setCursor(2,0);//set cursor (colum by row) indexing from 0 lcd.print(" ATTENTION!!"); delay(1000); lcd.setCursor(2,1); lcd.print("SENDING SMS "); String temp; String intru; String humi; String light; ldr = analogRead(LDRpin); LDRValue = ldr; int chk = DHT.read11(DHT11_PIN); if(DHT.temperature >= 26){ temp = String("HIGH"); } if(DHT.temperature >= 16 && DHT.temperature <= 25){ temp = String("NORMAL"); } if(DHT.temperature < 15){ temp = String("LOW"); } if(DHT.humidity >= 53.00){ humi = String("HIGH"); } if(DHT.humidity >= 4.00 && DHT.humidity <= 52.99){ humi = String("NORMAL"); } if(DHT.humidity < 3.99){ humi = String("LOW"); } if(LDRValue >= 15){ light = String("BRIGHT"); } if(LDRValue < 15){ light = String("DARK"); } mySerial.begin(19200); //Default serial port setting for the GPRS modem is 19200bps 8-N-1 mySerial.print("\r"); delay(1000); //wait for a second while the modem sends an "OK" mySerial.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode delay(1000); mySerial.print("AT+CMGS=\"0209206689\"\r"); //Start accepting the text for the message delay(1000); mySerial.print(temp); mySerial.print(" \r"); mySerial.print("ROOM TEMPERATURE= \r"); //The text for the message mySerial.print(DHT.temperature); mySerial.print("*C\r"); mySerial.println("\r"); mySerial.print(humi); mySerial.print(" \r"); mySerial.print("ROOM HUMIDITY= \r"); //The text for the message mySerial.print(DHT.humidity); mySerial.print("%\r"); mySerial.println("\r"); mySerial.print(light); mySerial.print(" \r"); mySerial.print("ROOM LIGHT INTENSITY= \r"); //The text for the message mySerial.print(LDRValue); mySerial.print("%\r"); mySerial.write(0x1A); //Equivalent to sending Ctrl+Z lcd.clear(); lcd.setCursor(5,0);//set cursor (colum by row) indexing from 0 lcd.print("SMS SENT "); lcd.setCursor(2,1); lcd.print("SUCCESSFULLY "); delay(4000); } void sendmesg(){ //mySerial.begin(19200); //Default serial port setting for the GPRS modem is 19200bps 8-N-1. mySerial.print("\r"); delay(1000); //Wait for a second while the modem sends an "OK". mySerial.print("AT+CMGF=1\r"); //Set the SMS in text mode. delay(1000); mySerial.print("AT+CMGS=\"0209206689\"\r"); //Replace this number with the target mobile number. delay(1000); mySerial.print("Hello, there is an intruder in your home. Call the police as soon as possible.\r"); //The text for the message. delay(1000); mySerial.write(0x1A); //Equivalent to sending Ctrl+Z or ending the SMS process. }