Arduino core for ESP32 Wiki content

ESPNow and connection to WiFi AP

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
}