He, sorry for my late reply and thanks for your comment.
Did not have much time over the last days to work on the ESP8266 project, whereas I found out that it indeed is somehow connected to the WiFi channel (i.e. my AP was using channel 6, whereas ESP-Now supposedly only (?) works on 1).
I wrote some test code and used the following WiFi and ESP-Now config which in a first test seemed to work, at least all deliveries were a success.
My test code for reference below.
Coordinator:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <espnow.h>
//#include <PubSubClient.h>
#include "network.h"
// WIFI and MQTT
//WiFiClient espClient;
//PubSubClient client(espClient);
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
char a[32];
int b;
float c;
String d;
bool e;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Char: ");
Serial.println(myData.a);
//client.publish("masterbathroom/shower/a", myData.a);
Serial.print("Int: ");
Serial.println(myData.b);
char myDatab[32];
String myDatab_str;
myDatab_str = String(myData.b);
myDatab_str.toCharArray(myDatab, myDatab_str.length() + 1);
//client.publish("masterbathroom/shower/b", myDatab);
Serial.print("Float: ");
Serial.println(myData.c);
Serial.print("String: ");
Serial.println(myData.d);
Serial.print("Bool: ");
Serial.println(myData.e);
Serial.println();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_AP_STA);
// Set device as a Wi-Fi Station
WiFi.begin(ssid, password, channel, bssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Setting as a Wi-Fi Station..");
}
Serial.print("Station IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Wi-Fi Channel: ");
Serial.println(WiFi.channel());
//WiFi.config(staticIP, dns, gateway, subnet);
//WiFi.begin(ssid, password, channel, bssid);
//WiFi.persistent(true);
//WiFi.setAutoConnect(true);
//WiFi.setAutoReconnect(true);
//while (WiFi.status() != WL_CONNECTED) {
//delay(500);
//}
//Connect to MQTT - loop till connected
//client.setServer(mqttServer, mqttPort);
//while (!client.connected()) {
// if (client.connect("WEMOS", mqttUser, mqttPassword)) {
// client.subscribe("masterbathroom/shower");
// } else {
// delay(500);
// }
//}
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
//client.loop();
}
Sender:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <espnow.h>
// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x84, 0xCC, 0xA8, 0xA6, 0x1D, 0x7D};
uint8_t bssid[] = {0x18,0xE8,0x29,0xCC,0xF0,0x4E};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
char a[32];
int b;
float c;
String d;
bool e;
} struct_message;
// Create a struct_message called myData
struct_message myData;
unsigned long lastTime = 0;
unsigned long timerDelay = 2000; // send readings timer
unsigned int readingId = 0;
// Insert your SSID
constexpr char WIFI_SSID[] = "IOT";
int32_t getWiFiChannel(const char *ssid) {
if (int32_t n = WiFi.scanNetworks()) {
for (uint8_t i=0; i<n; i++) {
if (!strcmp(ssid, WiFi.SSID(i).c_str())) {
return WiFi.channel(i);
}
}
}
return 0;
}
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
int32_t channel = getWiFiChannel(WIFI_SSID);
WiFi.printDiag(Serial); // Uncomment to verify channel number before
wifi_promiscuous_enable(1);
wifi_set_channel(channel);
wifi_promiscuous_enable(0);
WiFi.printDiag(Serial); // Uncomment to verify channel change after
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Set values to send
strcpy(myData.a, "THIS IS A CHAR");
myData.b = random(1,20);
myData.c = 1.2;
myData.d = "Hello";
myData.e = false;
// Send message via ESP-NOW
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
lastTime = millis();
}
}