4. GPS Tracker
Read a NEO-6M from the ESP32’s second hardware UART and publish valid location, speed, and satellite data using PxServ’s required map keys.
Level: Advanced · Time: 60 minutes
Privacy and wiring
Location can be personally and operationally sensitive. Protect the API key, publish only necessary precision and frequency, obtain informed consent, and follow applicable law. This is not a vehicle-safety or emergency device.
- Install TinyGPSPlus.
- GPS TX→GPIO16 (ESP32 RX2), GPS RX→GPIO17 (optional for read-only use).
- Power the module according to its datasheet and share GND.
- Verify UART logic is 3.3 V-compatible.
PxServ Maps expects map/lat, map/long, map/speed, and map/connectedsats.
Complete sketch
#include <PxServ.h>
#include <TinyGPSPlus.h>
const char* WIFI_SSID = "your_wifi_ssid";
const char* WIFI_PASSWORD = "your_wifi_password";
const char* PXSERV_API_KEY = "your_pxserv_api_key";
const int GPS_RX_PIN = 16, GPS_TX_PIN = 17;
const uint32_t GPS_BAUD = 9600;
PxServ client(PXSERV_API_KEY);
TinyGPSPlus gps;
HardwareSerial gpsSerial(1);
unsigned long lastPublishAt = 0;
void publishGps() {
if (!gps.location.isValid()) {
client.setData("gps/status", "waiting-for-fix");
return;
}
client.setData("map/lat", String(gps.location.lat(), 6));
client.setData("map/long", String(gps.location.lng(), 6));
client.setData("map/speed", gps.speed.isValid() ? String(gps.speed.kmph(), 1) : "0");
client.setData(
"map/connectedsats",
gps.satellites.isValid() ? String(gps.satellites.value()) : "0"
);
client.setData("gps/status", "normal");
client.setData("gps/data-age-ms", String(gps.location.age()));
}
void setup() {
Serial.begin(115200);
gpsSerial.begin(GPS_BAUD, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
PxServ::connectWifi(WIFI_SSID, WIFI_PASSWORD);
}
void loop() {
while (gpsSerial.available()) gps.encode(gpsSerial.read());
unsigned long now = millis();
if (now - lastPublishAt >= 10000) {
lastPublishAt = now;
publishGps();
}
if (now > 15000 && gps.charsProcessed() < 10) {
Serial.println("No GPS data; check TX/RX and baud rate");
}
delay(5);
}Test
Place the antenna with a clear view of the sky and allow several minutes for the first fix. Confirm the marker in PxServ Maps. Treat a growing data-age-ms as stale data and reject it beyond a documented limit in production.
Reference: TinyGPSPlus repository
Last updated