If ESPNow is used to transfer data from several ESP32’s (Slaves) to another ESP32 (Master) that then forward the data over WiFi to a server (e.g. a MQTT broker) this configuration works only if the channel used for ESPNow is the same channel used for the WiFi connection. If ESPNow channel and WiFi AP channel are different, the ESPNow connection is not working. Example code for Slaves in ESPNow examples – Slave And here an example (adapted from ESPNow examples – Master) for an ESP32 master that receives data from slaves and forwards them to an MQTT broker:

/** ESPNOW - Basic communication - Slave Date: 26th September 2017 Author: Arvind Ravulavaru <https://github.com/arvindr21> */ #include <esp_now.h> #include <WiFi.h> #include <MQTTClient.h> #define CHANNEL 1 /** MQTT client class to access mqtt broker */ MQTTClient mqttClient(2560); /** MQTT broker URL */ static const char * mqttBroker = "xx.xx.xx.xx"; /** MQTT connection id */ static const char * mqttID = "ESP32"; /** MQTT user name */ static const char * mqttUser = "esp32"; /** MQTT password */ static const char * mqttPwd = "PASSWORD"; /** WiFi client class to receive messages from mqtt broker */ WiFiClient mqttReceiver; void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len); // Init ESP Now with fallback void InitESPNow() { if (esp_now_init() == ESP_OK) { Serial.println("ESPNow Init Success"); } else { Serial.println("ESPNow Init Failed"); ESP.restart(); } } // config AP SSID void configDeviceAP() { String Prefix = "Slave:"; String Mac = WiFi.macAddress(); String SSID = Prefix + Mac; String Password = "123456789"; bool result = WiFi.softAP(SSID.c_str(), Password.c_str(), CHANNEL, 0); if (!result) { Serial.println("AP Config failed."); } else { Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID)); } } void setup() { Serial.begin(115200); Serial.println("ESPNow/Basic/Slave Example"); //Set device in AP mode to begin with WiFi.mode(WIFI_AP_STA); WiFi.begin("MYWIFI", "MYWIFIPW"); // configure device AP mode configDeviceAP(); // This is the mac address of the Slave in AP Mode Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress()); // Start connection to MQTT broker // Connect to MQTT broker mqttClient.begin(mqttBroker, mqttReceiver); Serial.println("Connecting to MQTT broker"); int connectTimeout = 0; while (!mqttClient.connect(mqttID, mqttUser, mqttPwd)) { delay(100); connectTimeout++; if (connectTimeout > 10) { // Wait for 1 seconds to connect Serial.println("Can't connect to MQTT broker"); ESP.restart(); } } Serial.println("Connected to MQTT"); // Init ESPNow with a fallback logic InitESPNow(); // Once ESPNow is successfully Init, we will register for recv CB to // get recv packer info. esp_now_register_recv_cb(OnDataRecv); } // callback when data is recv from Master void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Last Packet Recv from: "); Serial.println(macStr); Serial.print("Last Packet Recv Data: "); Serial.println(*data); Serial.println(""); mqttClient.publish("/debug","[INFO] Last Packet Recv from: " + tftMsg); mqttClient.publish("/debug","[INFO] Last Packet Recv Data: " + tftMsg); recvMsgNum++; } void loop() { // Chill } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 /** ESPNOW - Basic communication - Slave Date: 26th September 2017 Author: Arvind Ravulavaru <https://github.com/arvindr21> */ #include <esp_now.h> #include <WiFi.h> #include <MQTTClient.h> #define CHANNEL 1 /** MQTT client class to access mqtt broker */ MQTTClient mqttClient ( 2560 ) ; /** MQTT broker URL */ static const char * mqttBroker = "xx.xx.xx.xx" ; /** MQTT connection id */ static const char * mqttID = "ESP32" ; /** MQTT user name */ static const char * mqttUser = "esp32" ; /** MQTT password */ static const char * mqttPwd = "PASSWORD" ; /** WiFi client class to receive messages from mqtt broker */ WiFiClient mqttReceiver ; void OnDataRecv ( const uint8_t * mac_addr , const uint8_t * data , int data_len ) ; // Init ESP Now with fallback void InitESPNow ( ) { if ( esp_now_init ( ) == ESP_OK ) { Serial . println ( "ESPNow Init Success" ) ; } else { Serial . println ( "ESPNow Init Failed" ) ; ESP . restart ( ) ; } } // config AP SSID void configDeviceAP ( ) { String Prefix = "Slave:" ; String Mac = WiFi . macAddress ( ) ; String SSID = Prefix + Mac ; String Password = "123456789" ; bool result = WiFi . softAP ( SSID . c_str ( ) , Password . c_str ( ) , CHANNEL , 0 ) ; if ( ! result ) { Serial . println ( "AP Config failed." ) ; } else { Serial . println ( "AP Config Success. Broadcasting with AP: " + String ( SSID ) ) ; } } void setup ( ) { Serial . begin ( 115200 ) ; Serial . println ( "ESPNow/Basic/Slave Example" ) ; //Set device in AP mode to begin with WiFi . mode ( WIFI_AP_STA ) ; WiFi . begin ( "MYWIFI" , "MYWIFIPW" ) ; // configure device AP mode configDeviceAP ( ) ; // This is the mac address of the Slave in AP Mode Serial . print ( "AP MAC: " ) ; Serial . println ( WiFi . softAPmacAddress ( ) ) ; // Start connection to MQTT broker // Connect to MQTT broker mqttClient . begin ( mqttBroker , mqttReceiver ) ; Serial . println ( "Connecting to MQTT broker" ) ; int connectTimeout = 0 ; while ( ! mqttClient . connect ( mqttID , mqttUser , mqttPwd ) ) { delay ( 100 ) ; connectTimeout ++ ; if ( connectTimeout > 10 ) { // Wait for 1 seconds to connect Serial . println ( "Can't connect to MQTT broker" ) ; ESP . restart ( ) ; } } Serial . println ( "Connected to MQTT" ) ; // Init ESPNow with a fallback logic InitESPNow ( ) ; // Once ESPNow is successfully Init, we will register for recv CB to // get recv packer info. esp_now_register_recv_cb ( OnDataRecv ) ; } // callback when data is recv from Master void OnDataRecv ( const uint8_t * mac_addr , const uint8_t * data , int data_len ) { char macStr [ 18 ] ; snprintf ( macStr , sizeof ( macStr ) , "%02x:%02x:%02x:%02x:%02x:%02x" , mac_addr [ 0 ] , mac_addr [ 1 ] , mac_addr [ 2 ] , mac_addr [ 3 ] , mac_addr [ 4 ] , mac_addr [ 5 ] ) ; Serial . print ( "Last Packet Recv from: " ) ; Serial . println ( macStr ) ; Serial . print ( "Last Packet Recv Data: " ) ; Serial . println ( * data ) ; Serial . println ( "" ) ; mqttClient . publish ( "/debug" , "[INFO] Last Packet Recv from: " + tftMsg ) ; mqttClient . publish ( "/debug" , "[INFO] Last Packet Recv Data: " + tftMsg ) ; recvMsgNum ++ ; } void loop ( ) { // Chill }