I found an example code that does almost exactly what I want, but it appears to have a glitch. What I need is to extract: LatLong, GPS heading, Speed, SatNumber, DOP, GPStime and make a string to send via sms.
The code extracts some of them, but when it prints everything via serial the old strings get concatenated with the new ones...
This is the code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,2); //software serial instance for GPS/GSM module
String latlongtab[5]; // for the purpose of example let array be global
#define DEBUG true
String state, timegps, latitude, longitude;
void setup() {
mySerial.begin(9600);
Serial.begin(9600); //open serial port
delay(50);
/*We can use old sendData function to print our commands*/
sendData("AT+CGNSPWR=1",1000,DEBUG); //Initialize GPS device
delay(50);
sendData("AT+CGNSSEQ=RMC",1000,DEBUG);
delay(150);
}
void loop() {
sendTabData("AT+CGNSINF",1000,DEBUG); //send demand of gps localization
if(state != 0){
/*we just dont want to print empty signs so we wait until
* gps module will have connection to satelite.
* when whole table returned:
for(int i = 0; i < (sizeof(latlongtab)/sizeof(int)); i++){
Serial.println(latlongtab[i]); // print
}*/
Serial.println("State: "+state+" Time: "+timegps+" Latitude: "+latitude+" Longitude "+longitude);
}else{
Serial.println("GPS initializing");
}
}
void sendTabData(String command, const int timeout, boolean debug){
mySerial.println(command);
long int time = millis();
int i = 0;
while((time+timeout) > millis()){
while(mySerial.available()){
char c = mySerial.read();
if(c != ','){ //read characters until you find comma, if found increment
latlongtab[i]+=c;
delay(100);
}else{
i++;
}
if(i == 5){
delay(100);
goto exitL;
}
}
}exitL:
if(debug){
/*or you just can return whole table,in case if its not global*/
state = latlongtab[1]; //state = recieving data - 1, not recieving - 0
timegps = latlongtab[2];
latitude = latlongtab[3]; //latitude
longitude = latlongtab[4]; //longitude
}
}
String sendData(String command, const int timeout, boolean debug){
String response = "";
mySerial.println(command);
long int time = millis();
int i = 0;
while( (time+timeout) > millis()){
while(mySerial.available()){
char c = mySerial.read();
response+=c;
}
}
if(debug){
Serial.print(response);
}
return response;
}
And here's the serial output:
State: 1 Time: 20181017153156.000 Latitude: -33.207455 Longitude -33.8931
State: 11 Time: 20181017153156.00020181017153202.000 Latitude: -33.207455-33.207472 Longitude -33.8931-33.893
State: 111 Time: 20181017153156.00020181017153202.00020181017153208.000 Latitude: -33.207455-33.207472-33.207473 Longitude -33.8931-33.893-33.893.
The module is a SIM808 GSM/GPS