#include #include // Replace xxxxxxxxxx with your cell phone number where you want text messages arrive. String msg = String(""); // EN: Set to 1 when the next GPRS shield message will contains the SMS message LiquidCrystal lcd(2, 3, 10, 11, 12, 13); // Pinout, RS=2, E=3, DB4=10, DB5=11, DB6=12, DB7=13. int numRows = 2; // Number of rows of the LCD. int numCols = 16; // Number of Columns of the LCD. int SmsContentFlag = 0; //control pins of relay. int relay_a=4; int relay_b=5; int relay_c=6; int relay_d=7; int val = 0; //variable to store the value read from button void setup() { Serial.begin(9600); lcd.begin(numRows, numCols); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Arduino *"); lcd.setCursor(0,1); lcd.print("* Professor *"); delay(2000); Serial.begin(19200); // the GPRS baud rate // Encender el Modulo GPRS: pinMode(9, OUTPUT); // Pin 9 "PowerKey" of SIM900 shield. digitalWrite(9,LOW); delay(1000); digitalWrite(9,HIGH); delay(2000); digitalWrite(9,LOW); delay(2000); lcd.begin(numRows, numCols); lcd.clear(); lcd.setCursor(0,0); lcd.print("* SIM900 *"); lcd.setCursor(0,1); lcd.print("* Module - ON *"); //************************ // Initialize PINs pinMode( 4, OUTPUT ); pinMode( 5, OUTPUT ); pinMode( 6, OUTPUT ); pinMode( 7, OUTPUT ); digitalWrite( 4, LOW ); digitalWrite( 5, LOW ); digitalWrite( 6, LOW ); digitalWrite( 7, LOW ); Serial.println( "AT+CMGF=1" ); // SMS Text mode active delay(200); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); } void loop() { char SerialInByte; if(Serial.available()) { SerialInByte = (unsigned char)Serial.read(); delay(5); // ------------------------------------------------------------------- // 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); } } } // EN: Make action based on the content of the SMS. // Notice than SMS content is the result of the processing of several GPRS shield messages. void ProcessSms( String sms ){ //************************************************************************************ if( sms.indexOf("Ona") >= 0 ){ digitalWrite( relay_a, HIGH ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Relay #1 *"); lcd.setCursor(0,1); lcd.print("* Activated! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("Relay #1 = ON"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //*********************************************************************************** } if( sms.indexOf("Onb") >= 0 ){ digitalWrite( relay_b, HIGH ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Relay #2 *"); lcd.setCursor(0,1); lcd.print("* Activated! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("Relay #2 = ON"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //*********************************************************************************** } if( sms.indexOf("Onc") >= 0 ){ digitalWrite( relay_c, HIGH ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Relay #3 *"); lcd.setCursor(0,1); lcd.print("* Activated! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("Relay #3 = ON"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //************************************************************************************ } if( sms.indexOf("Ond") >= 0 ){ digitalWrite( relay_d, HIGH ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Relay #4 *"); lcd.setCursor(0,1); lcd.print("* Activated! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("Relay #4 = ON"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //************************************************************************************ } if( sms.indexOf("Allon") >= 0 ){ // ENCIENDE TODOS LOS LEDS digitalWrite( relay_a, HIGH ); digitalWrite( relay_b, HIGH ); digitalWrite( relay_c, HIGH ); digitalWrite( relay_d, HIGH ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* All Relays *"); lcd.setCursor(0,1); lcd.print("* Activated *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("All relays = ON"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //************************************************************************************* } if( sms.indexOf("Offa") >= 0 ){ digitalWrite( relay_a, LOW ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Relay #1 *"); lcd.setCursor(0,1); lcd.print("* Desable! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("Relay #1 = OFF"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //************************************************************************************* } if( sms.indexOf("Offb") >= 0 ){ digitalWrite( relay_b, LOW ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Relay #2 *"); lcd.setCursor(0,1); lcd.print("* Desable! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("Relay #2 = OFF"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //************************************************************************************* } if( sms.indexOf("Offc") >= 0 ){ digitalWrite( relay_c, LOW ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Relay #3 *"); lcd.setCursor(0,1); lcd.print("* Desable! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("Relay #3 = OFF"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //************************************************************************************* } if( sms.indexOf("Offd") >= 0 ){ digitalWrite( relay_d, LOW ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* Relay #4 *"); lcd.setCursor(0,1); lcd.print("* Desable! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("Relay #4 = OFF"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //************************************************************************************* } if( sms.indexOf("Alloff") >= 0 ){ // APAGAR TODOS LOS LEDS digitalWrite( relay_a, LOW ); digitalWrite( relay_b, LOW ); digitalWrite( relay_c, LOW ); digitalWrite( relay_d, LOW ); lcd.clear(); lcd.setCursor(0,0); lcd.print("* All relays *"); lcd.setCursor(0,1); lcd.print("* Desable! *"); Serial.println( "AT+CMGD=1,4" ); // Delete all SMS message in the SIM900 memory. delay(200); Serial.print("AT+CMGF=1\r"); // SMS Text mode active delay(500); Serial.println("AT+CMGS=\"xxxxxxxxxx\""); // send the SMS the number delay(500); Serial.println("All relays = OFF"); // the SMS body delay(500); Serial.println((char)26); // end of message command 1A (hex) delay(500); Serial.println(); //************************************************************************************** } } // EN: Request Text Mode for SMS messaging void GprsTextModeSMS(){ Serial.println( "AT+CMGF=1" ); } void GprsReadSmsStore( String SmsStorePos ){ Serial.print( "AT+CMGR=" ); Serial.println( SmsStorePos ); } // EN: Clear the GPRS shield message buffer void ClearGprsMsg(){ msg = ""; } // EN: interpret the GPRS shield message and act appropiately void ProcessGprsMsg() { 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 == 1 ){ // Serial.println( "*** SMS MESSAGE CONTENT ***" ); // Serial.println( msg ); // Serial.println( "*** END OF SMS MESSAGE ***" ); ProcessSms( msg ); } ClearGprsMsg(); // EN: Always clear the flag SmsContentFlag = 0; }