Send GPS data to your Traccar server the easy way! 💨
- Supports OsmAnd (GET) and JSON (POST) protocols 🌐
- Works on Arduino/ESP32 and similar platforms ⚙️
- Simple API with smart optional fields ✅
- Arduino Library Manager: search for "TraccarClient" and install (after publication)
- Manual: copy this folder to
Documents/Arduino/libraries/TraccarClient
The library provides a TraccarClient class and a TraccarPosition struct.
TraccarClientmanages host, port, deviceId, data sending and debugTraccarPositiondescribes the GPS fix and extra data (all optional)
Main methods send data in three formats:
sendOsmAnd(...)sends OsmAnd-formatted query via GETsendJson(...)sends JSON via POSTsendOsmAndForm(...)sendsapplication/x-www-form-urlencodedvia POST
All methods return true when the HTTP code is 200. You can read the actual HTTP code by passing an optional int* pointer.
- Connect to WiFi (ESP32/ESP8266 or Arduino with networking)
- Configure the client and deviceId
- Fill a
TraccarPositionand send
OsmAnd GET example:
#include <WiFi.h>
#include <TraccarClient.h>
const char* ssid = "YOUR_SSID";
const char* pass = "YOUR_PASS";
TraccarClient client("http://your.traccar.server", 5055, "device001");
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print('.'); }
client.setDebug(true); // log to Serial
client.setBasePath("/"); // optional, default "/"
client.setTimeoutMs(4000); // optional
TraccarPosition p;
p.latitude = 41.9028; // Rome 🇮🇹
p.longitude = 12.4964;
p.speedKmh = 50.0; // km/h (converted to knots by protocol)
p.validFlag = 1; // 1 valid fix, 0 invalid, -1 omit
int httpCode = 0;
bool ok = client.sendOsmAnd(p, &httpCode);
Serial.printf("OsmAnd sent: %s (HTTP %d)\n", ok ? "OK" : "FAIL", httpCode);
}
void loop() {}JSON POST example:
TraccarPosition p;
p.latitude = 41.9028;
p.longitude = 12.4964;
p.timestampMs = 0; // 0 = let the server decide, or set epoch ms
int httpCode = 0;
bool ok = client.sendJson(p, &httpCode);Form POST (x-www-form-urlencoded) example:
TraccarPosition p;
p.latitude = 41.9028;
p.longitude = 12.4964;
bool ok = client.sendOsmAndForm(p);You can also build the OsmAnd URL without sending (useful for debug):
String url = client.buildOsmAndUrl(p);
Serial.println(url);TraccarClient(const String& hostUrl, uint16_t port, const String& deviceId)void setHost(const String& hostUrl)void setPort(uint16_t port)void setDeviceId(const String& deviceId)void setBasePath(const String& basePath)// default "/"void setDebug(bool enabled)// log to Serialvoid setTimeoutMs(uint16_t ms)// HTTP connect timeoutbool sendOsmAnd(const TraccarPosition& pos, int* outHttpCode = nullptr) constbool sendJson(const TraccarPosition& pos, int* outHttpCode = nullptr) constbool sendOsmAndForm(const TraccarPosition& pos, int* outHttpCode = nullptr) constString buildOsmAndUrl(const TraccarPosition& pos) const
latitude(double, degrees) – useNANto omitlongitude(double, degrees) – useNANto omitaltitudeMeters(double, meters) –NANto omitspeedKmh(double, km/h) –NANto omitheadingDeg(double, 0..360) –NANto omithdop(double) –NANto omitaccuracyMeters(double, meters) –NANto omittimestampMs(uint64_t, epoch ms) –0lets the server set itbatteryPercent(int, 0..100) –-1to omitvalidFlag(int, -1/0/1) –-1to omitcharging(bool) – used only ifbatteryPercent >= 0driverUniqueId(String) – optionalcell(String) – format "mcc,mnc,lac,cellId[,signalStrength]"wifi(String) – one or more entries like "mac,-70" separated by;eventName(String) – e.g. "motionchange"activityType(String) – e.g. "still","walking","in_vehicle"odometer(double, meters) –NANto omit
Important notes:
- Speed is converted to knots for OsmAnd; by default standard rounding is used. Define
TRACCAR_SPEED_ROUND_DOWN=1to always round down. deviceIdis required for all formats.basePathis usually/(use it if your server expects a path).
- Send methods return
truewhen the HTTP code is200 - Pass
int* outHttpCodeto read the exact HTTP response code - With
setDebug(true)the library prints URLs/bodies and codes toSerial
architectures=*(Arduino/ESP32/ESP8266, etc.)- Requires network connectivity and
HTTPClienton supported platforms
Valerio Fantozzi — iamvaleriofantozzi@gmail.com
If you like this library, please leave a ⭐️ on the repository!