here is the sketch
#include <TinyGPS.h>
#include <stdlib.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
#include <LiquidCrystal595.h> // include the library
LiquidCrystal595 lcd(A0,A1,A2);
// 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 led.
int led_a= 6;
int led_b= 7;
int led_c= 8;
// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );
TinyGPS gps;
int LED = 13;
//Define String
String SD_date_time = "invalid";
String SD_lat = "invalid";
String SD_lon = "invalid";
String SD_spd = "invalid";
String dataString ="";
static void gpsdump(TinyGPS &gps);
static bool feedgps();
static void print_float(float val, float invalid, int len, int prec, int SD_val);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
void setup()
{
/*------------------------------- initialize serial communication --------------------*/
Serial.begin(9600); // initialize
/*-------------------------------------- Initialize PINs ----------------------------*/
pinMode( 6, OUTPUT );
pinMode( 7, OUTPUT );
pinMode( 8, OUTPUT );
digitalWrite( 6, LOW );
digitalWrite( 7, HIGH );
digitalWrite( 8, LOW );
delay(500);
lcd.begin(20, 4);
lcd.setCursor(0,1);
lcd.print(" GSM switch");
delay(5000);
lcd.clear();
/* ----------------Initial AT command for the GSM module -------------------------------*/
Serial.print("AT\r\n"); //AT command
delay(200);
ShowSerialData(); // this will show OK in the terminal
delay(200);
Serial.print("ATE1\r\n"); //echo to arduino then to the uart
delay(200);
ShowSerialData(); // this will show OK in the terminal
delay(200);
Serial.print("AT+CMEE=2\r\n"); //repport any error in readable format
delay(200);
ShowSerialData(); // this will show OK in the terminal
delay(200);
Serial.print("AT+CMGF=1\r\n"); //Because we want to send the SMS in text mode
delay(200);
ShowSerialData(); // this will show OK in the terminal
delay(200);
Serial.print("AT+CNMI=3,3,0,0 \r\n"); // set module to send SMS data to serial out upon receipt without storing it to memory
delay(200);
ShowSerialData(); // this will show OK in the terminal
delay(200);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("waiting sms msg");
}
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 )
{
int retrn = 1;
if( (sms.indexOf("status") >= 0) && (retrn == 1) )
{
digitalWrite( led_a, HIGH );
lcd.setCursor(0,0);
bool newdata = false;
unsigned long start = millis();
bool x;
// Every half second we print an update
if ( (millis() - start < 500) && x == true )
{
if (feedgps())
newdata = true;
delay(500);
x = false;
}
else
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" gsm update");
delay(500);
lcd.clear();
x = true;
}
gpsdump(gps);
lcd.setCursor(0,0);
lcd.print("Lat:");
lcd.print(SD_lat);
lcd.setCursor(0,1);
lcd.print("Lon:");
lcd.print(SD_lon);
lcd.setCursor(0,2);
lcd.print(SD_date_time);
lcd.setCursor(0,3);
lcd.print("Speed:");
lcd.print(SD_spd);
if(SD_date_time != "invalid")
digitalWrite(LED, HIGH);
else
digitalWrite(LED, LOW);
delay(10000);
digitalWrite( led_a, LOW );
retrn = 0;
}
if( sms.indexOf("start") >= 0 )
{
lcd.clear();
digitalWrite( led_b, HIGH );
lcd.print("led a is ON");
delay(200);
lcd.clear();
}
if( sms.indexOf("stop") >= 0 )
{
lcd.clear();
digitalWrite( led_b, LOW );
lcd.print("led a is OFF");
delay(200);
lcd.clear();
}
}
// 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;
}
void ShowSerialData() // read the data of the GSM/GPRS module then echo it to the serial port of arduino
{
while(Serial.available()!=0)
lcd.write(Serial.read());
}
static void gpsdump(TinyGPS &gps)
{
float flat, flon;
unsigned long age, date, time, chars = 0;
gps.f_get_position(&flat, &flon, &age);
print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 7, 8, 1); //LATITUDE
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 8, 2); //LONGITUDE
print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
print_date(gps); //DATE AND TIME
print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 2, 2, 3);
}
static void print_int(unsigned long val, unsigned long invalid, int len)
{
char sz[32];
if (val == invalid)
strcpy(sz, "*******");
else
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len-1] = ' ';
feedgps();
}
static void print_float(float val, float invalid, int len, int prec, int SD_val)
{
char sz[32];
if (val == invalid)
{
strcpy(sz, "*******");
sz[len] = 0;
if (len > 0)
sz[len-1] = ' ';
for (int i=7; i<len; ++i)
sz[i] = ' ';
if(SD_val == 1) SD_lat = sz;
else if(SD_val == 2) SD_lon = sz;
else if (SD_val == 3) SD_spd = sz;
}
else
{
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1);
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i);
}
feedgps();
}
static void print_date(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
if (age == TinyGPS::GPS_INVALID_AGE)
{
SD_date_time = "invalid";
}
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
month, day, year, 8+hour, minute, second);
SD_date_time = sz;
}
print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
feedgps();
}
static bool feedgps()
{
while (mySerial.available())
{
if (gps.encode(mySerial.read()))
return true;
}
return false;
}