I have a project that involves two ESP8266 and a NEO-6M GPS module. I want to send the GPS data to the other microcontroller using ESP-NOW, however it fails to send the data although it displays the coordinates. I just want to figure out how can I send the data through ESP-NOW. I have tried putting the two microcontrollers near each other, however, the status still displays as fail. The sender code is
#include <TinyGPS++.h>
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <SoftwareSerial.h>
SoftwareSerial ss(5, 4);
TinyGPSPlus gps;
int baud = 9600;
uint8_t receiverMAC[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
typedef struct struct_message {
float latitude;
float longitude;
float altitude;
} struct_message;
struct_message myData;
void setup() {
Serial.begin(baud);
ss.begin(baud);
WiFi.mode(WIFI_STA);
delay(100);
if (esp_now_init() != 0) {
Serial.println("ESP-NOW initialization failed!");
while (true);
}
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
if (esp_now_add_peer(receiverMAC, ESP_NOW_ROLE_SLAVE, 1, NULL, 0) != 0) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
while (ss.available() > 0) {
if (gps.encode(ss.read())) {
if (gps.location.isValid()) {
myData.latitude = gps.location.lat();
myData.longitude = gps.location.lng();
myData.altitude = gps.altitude.meters();
displayInfo();
sendData();
}
}
}
esp_now_register_send_cb(OnDataSent);
}
void OnDataSent(uint8_t * mac_addr, uint8_t sendStatus) {
Serial.print("ESP-NOW Send Status: ");
if (sendStatus == 0) {
Serial.println("Delivery success");
}
else {
Serial.println("Delivery fail");
}
}
void displayInfo() {
Serial.print("Latitude: ");
Serial.println(myData.latitude, 6);
Serial.print("Longitude: ");
Serial.println(myData.longitude, 6);
Serial.print("Altitude: ");
Serial.println(myData.altitude, 2);
}
void sendData() {
delay(100);
uint8_t result = esp_now_send(receiverMAC, (uint8_t *) &myData, sizeof(myData));
if (result != 0) {
Serial.println("Error sending data");
}
}
While the receiver code for the other microcontroller is shown below
#include <ESP8266WiFi.h>
#include <espnow.h>
typedef struct struct_message {
float latitude;
float longitude;
float altitude;
} struct_message;
struct_message myData;
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
Serial.println("Data received!");
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Latitude: ");
Serial.println(myData.latitude, 6);
Serial.print("Longitude: ");
Serial.println(myData.longitude, 6);
Serial.print("Altitude: ");
Serial.println(myData.altitude, 2);
}
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
}
WiFi.mode(WIFI_AP);,WiFi.disconnect();. 2) For ESP-Now Transmitter, you don't need to register the callback repeatedly in the loop, the lineesp_now_register_send_cb(OnDataSent);should be moved to setup(). 3) for the sendData(), remove theuint8_t result =and the if statement, the sendData status is provided in the callback function.WiFi.disconnect();to the transmitter code to make sure it does not connected to WiFi. 2)Are you matching thereceiverMAC[]with the actual mac address of your receiver board mac address. 3) You don't have to use the actual hardware mac address of the receiver board, you could also assigned a Locally Administered mac address to the receiver and make sure you use it on both the transmitter and the receiver as shown in my example code.