Hi, Im trying to host a server with an esp32, in this server I will show gps data (latitude and longitude) thats obtained from another esp32 thats connected to the gps.
The way I want to communicate these 2 esp32 is through ESP NOW but im having trouble both hosting the server and sending the data, I managed to host the server once with my house wi fi but it hasnt worked since, i also have made it host with my phone hot spot, but the gps data never reaches the other esp, it has said sometimes that data tranfers was a success but that never happened when the server was up
Heres the code for the transmitter
////////TRANSMISOR
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include <TinyGPS++.h>
// Define the RX and TX pins for Serial 2 (for GPS module)
#define RXD2 16
#define TXD2 17
#define GPS_BAUD 9600
// MAC Address of the receiver
uint8_t broadcastAddress[] = {0x08, 0xd1, 0xf9, 0xee, 0x5e, 0xc0};
// Structure to send data
typedef struct struct_message {
float latitude;
float longitude;
} struct_message;
struct_message gpsData;
// Create a TinyGPS++ object
TinyGPSPlus gps;
// Create an instance of the HardwareSerial class for Serial 2 (GPS communication)
HardwareSerial gpsSerial(2);
// ESP-NOW callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Last Packet Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Initialize Serial 2 for GPS communication
gpsSerial.begin(GPS_BAUD, SERIAL_8N1, RXD2, TXD2);
Serial.println("GPS module initialized.");
// Set ESP32 as Wi-Fi Station
WiFi.mode(WIFI_STA);
esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE); // Usa el canal 1
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register the send callback
esp_now_register_send_cb(OnDataSent);
// Add receiver's MAC Address as a peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
Serial.println("ESP-NOW initialized, ready to send GPS data.");
}
void loop() {
// Read GPS data from the module
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
}
// If GPS location is updated, send the data via ESP-NOW
if (gps.location.isUpdated()) {
gpsData.latitude = gps.location.lat();
gpsData.longitude = gps.location.lng();
Serial.print("Sending LAT: ");
Serial.print(gpsData.latitude, 6);
Serial.print(" LONG: ");
Serial.println(gpsData.longitude, 6);
// Send the GPS data via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &gpsData, sizeof(gpsData));
if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
}
delay(2000); // Delay before sending the next packet
}
This is what the serial monitor says
17:04:04.574 -> Sent with success
17:04:04.705 -> Last Packet Send Status: Fail
17:04:06.577 -> Sending LAT: latitude LONG: longitude
17:04:06.577 -> Sent with success
17:04:06.577 -> Last Packet Send Status: Fail
17:04:08.588 -> Sending LAT: latitude LONG: longitude
And the receiver code
////RECEPTOR
#include <esp_now.h>
#include <WiFi.h>
#include <WebServer.h>
#include <esp_wifi.h>
// Set up a web server on port 80
WebServer server(80);
// Structure to receive data
typedef struct struct_message {
float latitude;
float longitude;
} struct_message;
// Create a struct_message instance to hold the received data
struct_message receivedData;
// Wi-Fi credentials (replace with your network details)
const char* ssid = "name 2.4GHz";
const char* password = "password";
// Callback when data is received
void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len) {
memcpy(&receivedData, incomingData, sizeof(receivedData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Latitude: ");
Serial.println(receivedData.latitude, 6);
Serial.print("Longitude: ");
Serial.println(receivedData.longitude, 6);
}
// Web server handler for root page
void handleRoot() {
String page = "<html><body>";
page += "<h1>ESP32 GPS Data</h1>";
page += "<p><strong>Latitude:</strong> " + String(receivedData.latitude, 6) + "</p>";
page += "<p><strong>Longitude:</strong> " + String(receivedData.longitude, 6) + "</p>";
page += "</body></html>";
server.send(200, "text/html", page);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Wi-Fi connected.");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP()); // This will print the IP address
// Set the ESP32 as a Wi-Fi Station
WiFi.mode(WIFI_STA);
esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE); // Usa el canal 1
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register the callback function to receive data
esp_now_register_recv_cb(OnDataRecv);
// Start the web server
server.on("/", handleRoot);
server.begin();
Serial.println("Web server started.");
}
void loop() {
// Handle incoming web requests
server.handleClient();
}
Heres a photo of the components if it helps