Just wondering. I seem to get a lot of delivery fails easily 50%+. I've looked at a lot of similar questions but don't seen them doing anything different from what I am doing.
I think the code is ok, must be some interference somewhere. So how do I get the reliability better?
I also was not able to find an understandable definition of wifi_promiscuous_enable. any thoughts?
If you want to try this:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <espnow.h>
// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x22, 0x22, 0x20, 0x22, 0x27, 0x24};
// 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 = 90000; // send readings timer
unsigned int readingId = 0;
// Insert your SSID
constexpr char WIFI_SSID[] = "xyz";
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);
Serial.println("first printdiag");
WiFi.printDiag(Serial); // Uncomment to verify channel number before
wifi_promiscuous_enable(1);
wifi_set_channel(channel);
wifi_promiscuous_enable(0);
Serial.println("second printdiag");
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
String msgS="arrived at ";
msgS+=millis();
char msg2[30]={" "};
msgS.toCharArray(msg2,29);
strcpy(myData.a, msg2);
myData.b = random(1,20);
myData.c = 1.2;
myData.d = "Hello";
myData.e = false;
Serial.println(msg2);
// Send message via ESP-NOW
esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
lastTime = millis();
}
}