#include <WiFi.h>
const char *ssid = "wifi";
const char *password = "pass";
// GPS UART
HardwareSerial GPS(2); // UART2
// Standard NMEA TCP port
WiFiServer server(10110);
// Allow multiple clients
WiFiClient clients[5];
String line;
void setup() {
Serial.begin(115200);
// GPS on GPIO21 (RX), GPIO22 (TX)
GPS.begin(9600, SERIAL_8N1, 21, 22);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected: ");
Serial.println(WiFi.localIP());
server.begin();
server.setNoDelay(true);
Serial.println("NMEA TCP server started on port 10110");
}
void loop() {
// Accept new clients
if (server.hasClient()) {
for (int i = 0; i < 5; i++) {
if (!clients[i] || !clients[i].connected()) {
if (clients[i])
clients[i].stop();
clients[i] = server.available();
clients[i].setNoDelay(true);
Serial.printf("Client %d connected\n", i);
break;
}
}
// No room
WiFiClient reject = server.available();
reject.stop();
}
// Read GPS
while (GPS.available()) {
char c = GPS.read();
// Echo to USB serial
Serial.write(c);
line += c;
if (c == '\n') {
// Send complete NMEA sentence to all clients
for (int i = 0; i < 5; i++) {
if (clients[i] && clients[i].connected()) {
clients[i].print(line);
if (clients[i].getWriteError()) {
clients[i].stop();
}
}
}
line.clear();
// Prevent runaway if malformed data
if (line.length() > 256)
line.clear();
}
}
}
apt install gpsd gpsd-clients
nc ESP-IP 10110
#OR
gpsd tcp://192.168.1.150:10110
#lauch
xgps