Last active
August 29, 2015 14:17
-
-
Save anerdev/0fd1cf27a8d28e68df8a to your computer and use it in GitHub Desktop.
SIM900/908 split char "time" values. Version using a generic splitting method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The library for the SIM900/908 Shield by Futura Elettronica from gsmlib.org have the char "time" output in this format: "20150226193522.001" | |
This is a sketch for split the char "time" values like this: 2015 (year) 02 (month) 26 (day) 19 (hours) 35 (minutes) 22 (seconds). | |
The part ".001" not needed. | |
This is the version that use a generic splitting method. | |
Thanks to: KenF from arduino.cc forum: http://forum.arduino.cc/index.php?topic=303515 | |
Arduino GSM GPRS and GPS Shield from Futura Elettronica: http://www.open-electronics.org/gsm-gps-shield-for-arduino/ | |
Library for the Shield: http://www.gsmlib.org/ | |
Arduino IDE: 1.0 | |
Library version: 3.0.5 | |
The output in the serial monitor is like this: | |
GSM Shield testing. | |
status=READY | |
status=GPSREADY | |
3D FIXED | |
char time value: 20150319171957.000 | |
hour: 17 | |
minute: 19 | |
second: 57 | |
day: 19 | |
month: 3 | |
year: 2015 | |
latitude:0000.000000 | |
longitude:0000.000000 | |
altitude:0.000000 | |
speed :0.000000 | |
*/ | |
#include "SIM900.h" | |
#include <SoftwareSerial.h> | |
#include "gps.h" | |
GPSGSM gps; | |
char lon[15]; | |
char lat[15]; | |
char alt[15]; | |
char time[20]; | |
char vel[15]; | |
char msg1[5]; | |
char msg2[5]; | |
char stat; | |
char inSerial[20]; | |
int i=0; | |
boolean started=false; | |
int YY; | |
int MM; | |
int DD; | |
int hh; | |
int mm; | |
int ss; | |
void setup() | |
{ | |
//Serial connection. | |
Serial.begin(9600); | |
Serial.println("GSM Shield testing."); | |
//Start configuration of shield with baudrate. | |
//For http uses is raccomanded to use 4800 or slower. | |
if (gsm.begin(2400)){ | |
Serial.println("\nstatus=READY"); | |
gsm.forceON(); //To ensure that SIM908 is not only in charge mode | |
started=true; | |
} | |
else Serial.println("\nstatus=IDLE"); | |
if(started){ | |
//GPS attach | |
if (gps.attachGPS()) | |
Serial.println("status=GPSREADY"); | |
else Serial.println("status=ERROR"); | |
delay(20000); //Time for fixing | |
stat=gps.getStat(); | |
if(stat==1) | |
Serial.println("NOT FIXED"); | |
else if(stat==0) | |
Serial.println("GPS OFF"); | |
else if(stat==2) | |
Serial.println("2D FIXED"); | |
else if(stat==3) | |
Serial.println("3D FIXED"); Serial.println(""); | |
delay(5000); | |
} | |
}; | |
void loop() { | |
serialhwread(); | |
serialswread(); | |
gps.getPar(lon,lat,alt,time,vel); | |
YY = valueFromString(time, 0, 4); | |
MM = valueFromString(time,4,2); | |
DD = valueFromString (time, 6, 2); | |
hh = valueFromString (time, 8, 2); | |
mm = valueFromString (time, 10, 2); | |
ss = valueFromString (time, 12, 2); | |
Serial.print("char time value: "); Serial.println(time); Serial.println(""); | |
Serial.print("hour: "); Serial.println(hh); | |
Serial.print("minute: "); Serial.println(mm); | |
Serial.print("second: "); Serial.println(ss); | |
Serial.println(""); | |
Serial.print("day: "); Serial.println(DD); | |
Serial.print("month: "); Serial.println(MM); | |
Serial.print("year: "); Serial.println(YY); | |
Serial.println(""); | |
Serial.print("latitude: "); Serial.println(lat); | |
Serial.print("longitude: "); Serial.println(lon); | |
Serial.print("altitude: "); Serial.println(alt); | |
Serial.print("speed: "); Serial.println(vel); | |
Serial.println(""); | |
delay(60000); | |
} | |
int valueFromString(char* string,int start, int width) | |
{ | |
int value=0; | |
for( int n=0; n < width; n++ ) | |
value = value * 10 + string[start +n] - '0'; | |
return value; | |
} | |
void serialhwread(){ | |
i=0; | |
if (Serial.available() > 0){ | |
while (Serial.available() > 0) { | |
inSerial[i]=(Serial.read()); | |
delay(10); | |
i++; | |
} | |
inSerial[i]='\0'; | |
if(!strcmp(inSerial,"/END")){ | |
Serial.println("_"); | |
inSerial[0]=0x1a; | |
inSerial[1]='\0'; | |
gsm.SimpleWriteln(inSerial); | |
} | |
//Send a saved AT command using serial port. | |
if(!strcmp(inSerial,"TEST")){ | |
// Serial.println("BATTERY TEST 1"); | |
// gps.getBattInf(msg1,msg2); | |
// Serial.println(msg1); | |
// Serial.println(msg2); | |
// Serial.println("BATTERY TEST 2"); | |
// gps.getBattTVol(msg1); | |
// Serial.println(msg1); | |
stat=gps.getStat(); | |
if(stat==1) | |
Serial.println("NOT FIXED"); | |
else if(stat==0) | |
Serial.println("GPS OFF"); | |
else if(stat==2) | |
Serial.println("2D FIXED"); | |
else if(stat==3) | |
Serial.println("3D FIXED"); | |
} | |
//Read last message saved. | |
if(!strcmp(inSerial,"MSG")){ | |
Serial.println(msg1); | |
} | |
else{ | |
Serial.println(inSerial); | |
gsm.SimpleWriteln(inSerial); | |
} | |
inSerial[0]='\0'; | |
} | |
} | |
void serialswread(){ | |
gsm.SimpleRead(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment