Hi i'm trying to get GPS data from my SIM808, without use of librarys..
i've found this code it works quite good but i need some improvement becouse some times it gives some
error.. especially if use it with esp32 or mega 2560..
With esp32 it give me an error wich seems to be "application attempted to dereference a NULL pointer".
And with mega it restart the board (i see "serial begin ok" every loop).
Please can someone help me..or if someone have already done something like this i will appreciate so much.. this is the code i'm using
#define mySerial Serial2
char frame[100];
byte GNSSrunstatus;;
byte Fixstatus;
char UTCdatetime[18];
char latitude[11];
char logitude[11];
char altitude[8];
char speedOTG[6];
char course[6];
byte fixmode;
char HDOP[4];
char PDOP[4];
char VDOP[4];
char satellitesinview[2];
char GNSSsatellitesused[2];
char GLONASSsatellitesused[2];
char cn0max[2];
char HPA[6];
char VPA[6];
float latGPS;
float longGPS;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Serial begin ok");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("AT");
mySerial.println("AT+CGNSPWR=1");
delay(10000);
}
void loop() { // run over and over
get_GPS();
delay(10000);
}
int8_t get_GPS() {
int8_t counter, answer;
long previous;
// First get the NMEA string
// Clean the input buffer
while ( mySerial.available() > 0) mySerial.read();
// request Basic string
mySerial.println("AT+CGNSINF"); //sendATcommand("AT+CGNSINF", "AT+CGNSINF\r\n\r\n", 2000);
counter = 0;
answer = 0;
memset(frame, '\0', sizeof(frame)); // Initialize the string
previous = millis();
// this loop waits for the NMEA string
do {
if (mySerial.available() != 0) {
frame[counter] = mySerial.read();
counter++;
// check if the desired answer is in the response of the module
if (strstr(frame, "OK") != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while ((answer == 0) && ((millis() - previous) < 2000));
frame[counter - 3] = '\0';
// Parses the string
strtok_single(frame, ": ");
GNSSrunstatus = atoi(strtok_single(NULL, ","));;// Gets GNSSrunstatus
Fixstatus = atoi(strtok_single(NULL, ",")); // Gets Fix status
strcpy(UTCdatetime, strtok_single(NULL, ",")); // Gets UTC date and time
strcpy(latitude, strtok_single(NULL, ",")); // Gets latitude
strcpy(logitude, strtok_single(NULL, ",")); // Gets longitude
strcpy(altitude, strtok_single(NULL, ",")); // Gets MSL altitude
strcpy(speedOTG, strtok_single(NULL, ",")); // Gets speed over ground
strcpy(course, strtok_single(NULL, ",")); // Gets course over ground
fixmode = atoi(strtok_single(NULL, ",")); // Gets Fix Mode
strtok_single(NULL, ",");
strcpy(HDOP, strtok_single(NULL, ",")); // Gets HDOP
strcpy(PDOP, strtok_single(NULL, ",")); // Gets PDOP
strcpy(VDOP, strtok_single(NULL, ",")); // Gets VDOP
strtok_single(NULL, ",");
strcpy(satellitesinview, strtok_single(NULL, ",")); // Gets GNSS Satellites in View
strcpy(GNSSsatellitesused, strtok_single(NULL, ",")); // Gets GNSS Satellites used
strcpy(GLONASSsatellitesused, strtok_single(NULL, ",")); // Gets GLONASS Satellites used
strtok_single(NULL, ",");
strcpy(cn0max, strtok_single(NULL, ",")); // Gets C/N0 max
strcpy(HPA, strtok_single(NULL, ",")); // Gets HPA
strcpy(VPA, strtok_single(NULL, "\r")); // Gets VPA
//converto stringa in numero per poterla confrontare
longGPS = atof (logitude);
latGPS = atof (latitude);
Serial.println("mia float latitudine");
Serial.println(latGPS,6);
Serial.println("mia float latitudine");
Serial.println(longGPS,6);
Serial.println("UTCdatetime");
Serial.println(UTCdatetime);
Serial.println("latitude");
Serial.println(latitude);
Serial.println("logitude");
Serial.println(logitude);
return answer;
}
/* strtok_fixed - fixed variation of strtok_single */
static char *strtok_single(char *str, char const *delims)
{
static char *src = NULL;
char *p, *ret = 0;
if (str != NULL)
src = str;
if (src == NULL || *src == '\0') // Fix 1
return NULL;
ret = src; // Fix 2
if ((p = strpbrk(src, delims)) != NULL)
{
*p = 0;
src = ++p;
}
else
src += strlen(src);
return ret;
}
this is the string that i get using "AT+CGNSINF"
22:40:55.059 -> AT+CGNSINF
22:40:55.059 -> +CGNSINF: 1,1,20200407204053.000,45.817337,10.957775,699.400,0.24,27.5,1,,1.0,1.3,0.9,,10,9,,,39,,
Thank you very much!