I'm struggling to get an ESP32-slave send data back to an ESP32-master (this works fine with an ESP8266). Master-Code ist basic-master-example-code from Arduino-IDE with an addinitonal callback function for receiving.
Code: Select all
// callback when data is sent from Slave To Master
void onDataRecv(const uint8_t *mac_addr, const uint8_t *r_data, int data_len) {
char macStr[18];
uint8_t inData;
//MAC Adresse slave as info
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]);
memcpy(&inData, r_data, sizeof(inData));
Serial.print ("received : ");
Serial.println(inData);
}
}
Code: Select all
#include <esp_now.h>
#include <WiFi.h>
#define CHANNEL 1
uint8_t data = 0;
// Init ESP Now with fallback
void InitESPNow() {
// WiFi.disconnect();
if (esp_now_init() == ESP_OK) {
Serial.println("ESPNow Init Success");
}
else {
Serial.println("ESPNow Init Failed");
// Retry InitESPNow, add a counte and then restart?
// InitESPNow();
// or Simply Restart
ESP.restart();
}
}
// config AP SSID
void configDeviceAP() {
const char *SSID = "Slave_1";
bool result = WiFi.softAP(SSID, "Slave_1_Password", 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);
// configure device AP mode
configDeviceAP();
// This is the mac address of the Slave in AP Mode
Serial.print("AP MAC: "); Serial.println(WiFi.softAPmacAddress());
Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
// 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 *r_data, int data_len) {
char macStr[18];
uint8_t inData;
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(*r_data);
data++;
int ret = esp_now_send(mac_addr, &data, sizeof(data));
if (ret != ESP_OK ){
Serial.print ("Error sending message!! ");
if (ret == ESP_ERR_ESPNOW_NOT_INIT) Serial.println("ESPNOW is not initialized");
if (ret == ESP_ERR_ESPNOW_ARG) Serial.println("invalid argument");
if (ret == ESP_ERR_ESPNOW_INTERNAL) Serial.println("internal error");
if (ret == ESP_ERR_ESPNOW_NO_MEM) Serial.println("out of memory");
if (ret == ESP_ERR_ESPNOW_NOT_FOUND) Serial.println("peer is not found");
if (ret == ESP_ERR_ESPNOW_IF) Serial.println("current WiFi interface doesn’t match that of peer");
} else {
Serial.print ("send back :");
Serial.println(data);
}
Serial.println("");
}
void loop() {
delay(1500);
}
The code for ESP8266 - working fine - is:Last Packet Recv from: 3c:71:bf:9d:dd:ec
Last Packet Recv Data: 69
Error sending message!! peer is not found
Last Packet Recv from: 3c:71:bf:9d:dd:ec
Last Packet Recv Data: 70
Error sending message!! peer is not found
Code: Select all
#include <ESP8266WiFi.h>
extern "C"{
#include <espnow.h>
}
#define CHANNEL 1
uint8_t data = 0;
void on_receive_data(uint8_t *mac, uint8_t *r_data, uint8_t len) {
char MACmaster[6];
uint8_t inData;
sprintf(MACmaster, "%02X:%02X:%02X:%02X:%02X:%02X",mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
Serial.print ("received from MAC: ");
Serial.println(MACmaster);
memcpy(&inData, r_data, sizeof(inData));
Serial.print ("received : ");
Serial.println(inData);
data++;
esp_now_send(mac, &data, sizeof(data));
Serial.print ("send back :");
Serial.println(data);
Serial.println();
};
void setup() {
Serial.begin(115200); Serial.println();
WiFi.mode(WIFI_AP);
char* SSID = "Slave_1";
bool ret = WiFi.softAP(SSID, "Slave_1_Password", CHANNEL, 0);
if (!ret) {
Serial.println("AP Config failed.");
} else {
Serial.println("AP Config Success. Broadcasting with AP: " + String(SSID));
}
if (esp_now_init()!=0) {
Serial.println("not initialized...");
ESP.restart();
delay(1);
}
Serial.print("AP MAC : " + String (WiFi.softAPmacAddress()) );
Serial.print("STA MAC: " + String (WiFi.macAddress()) );
esp_now_set_self_role(2);
esp_now_register_recv_cb(on_receive_data);
}
void loop() {
delay(1500);
}
Thank you so much
wupperpi