ESP32 and SIM808 get GPS

ilteo85
Posts: 1
Joined: Tue Mar 24, 2020 3:49 pm

ESP32 and SIM808 get GPS

Postby ilteo85 » Tue Mar 24, 2020 4:27 pm

I'm trying to use SIM808 to get my gps position with my esp32.
I used to do it with Arduino with thise code

Code: Select all

int8_t get_GPS() {

  int8_t counter, answer;
  long previous;

  // First get the NMEA string
  // Clean the input buffer
  while ( Serial2.available() > 0) Serial2.read();
  // request Basic string
  Serial2.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 (Serial2.available() != 0) {
      frame[counter] = Serial2.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 longitudine");
  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;
}

but with ESP32 it works just sometimes and sometimes give me "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)"
I think is becouse of my code..

Please help me to fix it im getting creazy..i've tryed also a lot of library ('TinyGPS, TinyGPS++ ecc) but i didn't get to make them work..
and i prefere to fix this code couse i've already set all my variables in the rest of the code..
(but it's ok also with library if some one has ever used it and know how to do)


Thank you very much and sorry for my english..i'm from Italy :)

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 and SIM808 get GPS

Postby ESP_Sprite » Wed Mar 25, 2020 2:14 pm

Your frame reading code is extremely fragile... what if the module responds with a lot of data instead of 'OK'? Where does the zero terminator get placed in the frame string when there is a timeout? Can strtok_single return NULL if there is no more fields to decode, and what would strcpy do in that case? What I'm saying is: try to implement proper error handling first, and that'll get you much closer to your actual issue.

Who is online

Users browsing this forum: No registered users and 62 guests