1
Arduino: 1.8.19 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (1M SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
esp_now_testing:3:21: fatal error: esp_now.h: No such file or directory
 #include <esp_now.h>
                     ^
compilation terminated.
exit status 1
esp_now.h: No such file or directory
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Couldnt compile for a node mcu esp8266 board. Have installed esp8266 library from boards manager enter image description here

#include <ESP8266WiFi.h>
#include <esp_now.h>

// Variables for test data
int int_value;
float float_value;
bool bool_value = true;

// MAC Address of responder - edit as required
uint8_t broadcastAddress[] = {0x24, 0x6F, 0x28, 0x7A, 0xAE, 0x7C};

// Define a data structure
typedef struct struct_message {
 char a[32];
 int b;
 float c;
 bool d;
} struct_message;

// Create a structured object
struct_message myData;

// Peer info
esp_now_peer_info_t peerInfo;

// Callback function called when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
 Serial.print("\r\nLast Packet Send Status:\t");
 Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup() {
 
 // Set up Serial Monitor
 Serial.begin(115200);

 // Set ESP32 as a Wi-Fi Station
 WiFi.mode(WIFI_STA);

 // Initilize 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);
 
 // Register peer
 memcpy(peerInfo.peer_addr, broadcastAddress, 6);
 peerInfo.channel = 0;  
 peerInfo.encrypt = false;
 
 // Add peer        
 if (esp_now_add_peer(&peerInfo) != ESP_OK){
   Serial.println("Failed to add peer");
   return;
 }
}

void loop() {

 // Create test data

 // Generate a random integer
 int_value = random(1,20);

 // Use integer to make a new float
 float_value = 1.3 * int_value;

 // Invert the boolean value
 bool_value = !bool_value;
 
 // Format structured data
 strcpy(myData.a, "Welcome");
 myData.b = int_value;
 myData.c = float_value;
 myData.d = bool_value;
 
 // Send message via ESP-NOW
 esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
  
 if (result == ESP_OK) {
   Serial.println("Sending confirmed");
 }
 else {
   Serial.println("Sending error");
 }
 delay(2000);
}
1
  • 1
    Where did you get this code from? Although it does load <ESP8266WiFi.h>, the rest of this code seems to be for an ESP32 and not an ESP8266/NodeMCU.
    – StarCat
    Jan 3, 2023 at 14:15

1 Answer 1

-1

To use ESP-NOW with ESP8266 and Arduino IDE, you must use the "espnow.h" header file. In this case, the error codes change and only need to use int values ​​for success(0). esp_now_peer_info_t is not required to add peers. Here is the code:

#include <ESP8266WiFi.h>
#include <espnow.h>

// Variables for test data
int int_value;
float float_value;
bool bool_value = true;

// MAC Address of responder - edit as required
uint8_t broadcastAddress[] = {0x24, 0x6F, 0x28, 0x7A, 0xAE, 0x7C};

// Define a data structure
typedef struct struct_message {
 char a[32];
 int b;
 float c;
 bool d;
} struct_message;

// Create a structured object
struct_message myData;

// Callback function called when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t status) {
 Serial.print("\r\nLast Packet Send Status:\t");
 Serial.println(status == 0 ? "Delivery Success" : "Delivery Fail");
}

void setup() {
 
 // Set up Serial Monitor
 Serial.begin(115200);

 // Set ESP32 as a Wi-Fi Station
 WiFi.mode(WIFI_STA);

 // Initilize ESP-NOW
 if (esp_now_init() != 0) {
   Serial.println("Error initializing ESP-NOW");
   return;
 }

 // Register the send callback
 esp_now_register_send_cb(OnDataSent);
 
 // Add peer        
 if (esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 0, NULL, 0) != 0){
   Serial.println("Failed to add peer");
   return;
 }
}

void loop() {

 // Create test data

 // Generate a random integer
 int_value = random(1,20);

 // Use integer to make a new float
 float_value = 1.3 * int_value;

 // Invert the boolean value
 bool_value = !bool_value;
 
 // Format structured data
 strcpy(myData.a, "Welcome");
 myData.b = int_value;
 myData.c = float_value;
 myData.d = bool_value;
 
 // Send message via ESP-NOW
 int result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
  
 if (result == 0) {
   Serial.println("Sending confirmed");
 }
 else {
   Serial.println("Sending error");
 }
 delay(2000);
}
1
  • 1
    Your description of what needed to be changed is not complete. You have changed more than just the header file.
    – StarCat
    Jan 3, 2023 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.