How can I achieve the same on ESP8266?
On an ESP32 I do initialize ESPnow with
Code: Select all
tcpip_adapter_init();
esp_event_loop_init(espNowEvtHandler, NULL);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
esp_wifi_set_storage(WIFI_STORAGE_RAM);
esp_wifi_set_mode(WIFI_MODE_AP);
esp_wifi_start();
/// \todo Decide if we use Long Range WiFi settings
esp_wifi_set_channel(SCENT_CHANNEL, WIFI_SECOND_CHAN_ABOVE);
esp_wifi_set_protocol(WIFI_IF_AP, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR);
esp_now_init();
// Register sent callback
esp_now_register_send_cb(espNowSendCb);
// Register received callback
esp_now_register_recv_cb(espNowRcvdCb);
// Add broadcast address as peer
/** MAC address used for broadcast messages */
uint8_t broadCastMac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_peer_info_t *peer = (esp_now_peer_info_t *)malloc(sizeof(esp_now_peer_info_t));
memset(peer, 0, sizeof(esp_now_peer_info_t));
peer->channel = SCENT_CHANNEL;
peer->ifidx = ESP_IF_WIFI_AP;
peer->encrypt = false;
memcpy(peer->peer_addr, broadCastMac, ESP_NOW_ETH_ALEN);
esp_now_add_peer(peer);
free(peer);
Then I can send messages as broadcasts (using broadCastMac == FF:FF:FF:FF:FF:FF) and all other ESP32's get the message.
Code: Select all
esp_now_send(broadCastMac, data, len);
When receiving the broadcast, the other ESP32's add the senders MAC to the peer list with
Code: Select all
void ICACHE_FLASH_ATTR espNowRcvdCb(const uint8_t *mac_addr, const uint8_t *data, int data_len)
{
if (esp_now_is_peer_exist(mac_addr) == false)
{
esp_now_peer_info_t *peer = (esp_now_peer_info_t *)malloc(sizeof(esp_now_peer_info_t));
memset(peer, 0, sizeof(esp_now_peer_info_t));
peer->channel = SCENT_CHANNEL;
peer->ifidx = ESP_IF_WIFI_AP;
peer->encrypt = true;
memcpy(peer->peer_addr, mac_addr, ESP_NOW_ETH_ALEN);
esp_now_add_peer(peer);
free(peer);
}
After that I can send unicast message between each of the ESP32's.
If I use similar code on ESP8266 to initialize ESPnow and send messages to broadCastMac FF:FF:FF:FF:FF:FF nothing happens and the ESP32's don't receive the message.