Arduino build error

fatal error: espnow.h: No such file or directory

ESP-NOW sends small packets between ESP boards with no router. Its header is espnow.h on an ESP8266 and esp_now.h on an ESP32.

  • Build error
  • 2 common causes
The message

What the Arduino IDE prints

Output window
sketch.ino:1:10: fatal error: espnow.h: No such file or directory
    1 | #include <espnow.h>
      |          ^~~~~~~~~~
compilation terminated.
exit status 1

A sketch written for one fails on the other, and on any board without WiFi.

Mokxi's compiler, clang, words it as 'espnow.h' file not found.

Causes

The 2 usual causes with the fix

1The board is not an ESP8266

espnow.h is the ESP8266 header. Choose an ESP8266 board. In Mokxi, ESP-NOW is simulated between the ESP boards on the canvas.

Broken
#include <espnow.h>

void setup() {
  Serial.begin(9600);
}

void loop() {
}
Fixed
#include <ESP8266WiFi.h>
#include <espnow.h>

uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

typedef struct struct_message {
  char a[32];
  int b;
} struct_message;

struct_message myData;

void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  (void)mac_addr;
  Serial.print("Last Packet Send Status: ");
  Serial.println(sendStatus == 0 ? "Delivery success" : "Delivery fail");
}

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
  strcpy(myData.a, "THIS IS A CHAR");
  myData.b = 7;
  esp_now_send(broadcastAddress, (uint8_t *)&myData, sizeof(myData));
  Serial.println("Sent");
}

void loop() {}

Mokxi’s build tab says: espnow.h is the ESP8266 core's ESP-NOW

Try this fix in Mokxi

2An ESP8266 sketch is built for an ESP32

The ESP32 uses esp_now.h, with an underscore, and a different callback signature. Change the include, then the calls, following the ESP32 ESP-NOW example.

  1. Replace #include <espnow.h> with #include <esp_now.h> and #include <WiFi.h>.
  2. Use esp_now_peer_info_t to add a peer, and esp_now_add_peer(&peerInfo).
  3. Open File, Examples, ESP32, ESPNow in the Arduino IDE for the current calls.
FAQ

Questions and answers

Is ESP-NOW the same on ESP32 and ESP8266?

The idea is the same. The header, the callback signatures and the peer calls differ, so code does not carry over unchanged.

Do I need a router for ESP-NOW?

No. The boards talk to each other directly, as long as they use the same WiFi channel.

Can I test ESP-NOW in Mokxi?

Yes. Put two ESP boards on the canvas, and the packets one sends reach the other.