#include <softwareserial.h>
// en: String buffer for the gprs shield message
string msg = string("");
// en: Set to 1 when the next gprs shield message will contains the sms message
int smscontentflag = 0;
//control pins of relay.
Int relay_a=4;
int relay_b=5;
int relay_c=6;
int relay_d=7;
// en: Code pin of the sim card (if applied)
//string sim_pin_code = string( "xxxx" );
void setup()
{
serial.begin(19200); // the gprs baud rate
// 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" );
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 <cr> 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("1") >= 0 ){
digitalwrite( relay_a, high );
serial.println("at+cmgd=1,4\r");
}
if( sms.indexof("3") >= 0 ){
digitalwrite( relay_b, high );
serial.println("at+cmgd=1,4\r");
}
if( sms.indexof("5") >= 0 ){
digitalwrite( relay_c, high );
serial.println("at+cmgd=1,4\r");
}
if( sms.indexof("7") >= 0 ){
digitalwrite( relay_d, high );
serial.println("at+cmgd=1,4\r");
}
if( sms.indexof("2") >= 0 ){
digitalwrite( relay_a, low );
serial.println("at+cmgd=1,4\r");
}
if( sms.indexof("4") >= 0 ){
digitalwrite( relay_b, low );
serial.println("at+cmgd=1,4\r");
}
if( sms.indexof("6") >= 0 ){
digitalwrite( relay_c, low );
serial.println("at+cmgd=1,4\r");
}
if( sms.indexof("8") >= 0 ){
digitalwrite( relay_d, low );
serial.println("at+cmgd=1,4\r");
}
else {
serial.println("at+cmgd=1,4\r");
}
}
// 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;
}