i’m trying to that usin this code…but i think there are some errors becouse in the loop sometimes it doesn’t work… i’m using it with esp32… wiring is correct becouse sometimes it works…and cheking
my error on internet it says “application attempted to dereference a NULL pointer”
I’m not really good in programming so please if someone have ever done already some sketch help me
thank you very much!
#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;
}