ESP8266 -> ESP32 ESP-NOW message

Hey all.

I am running ESP8266 with Arduino framework, which acts as a ESPNOW message transmitter. Then I am running ESP32 with ESP-IDF, which listens to broadcast messages.

I can send and receive messages when sending and receiving on two ESP 8266s, but when I go cross-platform, it’s a problem and nothing pops up on the ESP32.

Any idea why this is happening or what could be wrong? Is it even possible to send the messages between 2 different chips?

Thank you.

Per How to connect ESP32 and ESP8266 using ESP-Now protocol that’s possible. Try using the source code there to verify it?

Hmm, it does seem to capture the message when the ESP32 is running Arduino.h.

BUT, when I use ESP-IDF on the ESP32, it does not register any messages :frowning:

#include <stdio.h>
#include <string.h>
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_now.h"
#include "driver/gpio.h"
#include "nvs_flash.h"

static const char *TAG = "example";

struct message_payload
{
  int id;
  int state;
  int index;
};

message_payload myMessage;

void onDataReceiver(const uint8_t *mac, const uint8_t *incomingData, int len)
{
  ESP_LOGI(TAG, "Message received.");
  // We don't use mac to verify the sender
  // Let us transform the incomingData into our message structure
  memcpy(&myMessage, incomingData, sizeof(myMessage));
  ESP_LOGI(TAG, "=== Data ===");
  ESP_LOGI(TAG, "Mac address: %02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  ESP_LOGI(TAG, "ID: %d", myMessage.id);
  ESP_LOGI(TAG, "State: %d", myMessage.state);
  ESP_LOGI(TAG, "Index: %d", myMessage.index);

  gpio_set_level(GPIO_NUM_2, 1);
  vTaskDelay(100 / portTICK_RATE_MS);
  gpio_set_level(GPIO_NUM_2, 0);
}

extern "C" void app_main()
{
  ESP_ERROR_CHECK(nvs_flash_init());
  ESP_ERROR_CHECK(esp_netif_init());
  ESP_ERROR_CHECK(esp_event_loop_create_default());
  ESP_ERROR_CHECK(esp_event_loop_init(NULL, NULL));

  esp_netif_create_default_wifi_sta();

  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

  gpio_pad_select_gpio(GPIO_NUM_2);
  gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);

  // Get Mac Address
  uint8_t mac_addr[6];
  ESP_ERROR_CHECK(esp_wifi_get_mac(WIFI_IF_STA, mac_addr));
  ESP_LOGI(TAG, "Mac Address: %02X:%02X:%02X:%02X:%02X:%02X", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  ESP_LOGI(TAG, "ESP32 ESP-Now Broadcast");

  // Initialize ESP-NOW
  ESP_ERROR_CHECK(esp_now_init());
  ESP_ERROR_CHECK(esp_now_register_recv_cb(onDataReceiver));

  while (1)
  {
    // Put your main code here, to run repeatedly
    vTaskDelay(1000 / portTICK_RATE_MS);
  }
}

That sucks :confused:

If it’s an ESP-IDF specific problem, please seek help at Issues · espressif/esp-idf · GitHub

Ended up being a wrong wifi channel!

esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 11, NULL, 0);

I thought this was enough to set it, but turns out you need to call this as well:

wifi_set_channel(11);

Good to know :smiley: