I am trying to set up two ESP32s so that they can be updated via OTA and at the same time send sensor readings using ESP-NOW, from Sender Board to Receiver Board.
For this to work both boards must connect to wifi and simultaneously send messages over ESP-NOW.
I don't mind using a mode where a button is pressed and Wifi is only then enabled but switching wifi on and off seems to have intermittent success.
I have tried setting the channels to the same on both boards but this does not work.
In the example below I have stripped all the OTA code out and the code needed for ESP-NOW and just the line for WIFI enable
WiFi.begin(ssid, password);
which when enabled causes the delivery to fail.
Here is my Sender Code:
#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#define ssid "MYNETWORKSSID"
#define password "mynetworkpassword"
const int sNine = 33;
#define CHAN_AP 2
uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xDC, 0x70, 0x90};
int hall;
String success;
typedef struct struct_message {
int hall;
} struct_message;
struct_message hallReadings;
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status ==0){
success = "Delivery Success :)";
}
else{
success = "Delivery Fail :(";
}
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); // PROBLEMATIC LINE
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
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_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = CHAN_AP;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
// getReadings();
hallReadings.hall = analogRead(sNine);
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &hallReadings, sizeof(hallReadings));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(100);
}
Here is my Receiver code:
#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
const int sNine = 15;
//Wi-Fi channel (must match the gateway wi-fi channel as an access point)
#define CHAN_AP 2
// Define variables to store incoming readings
int incomingHall;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
int hall;
} struct_message;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
void updateDisplay(){
Serial.print(incomingReadings.hall);
}
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
incomingHall = incomingReadings.hall;
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
updateDisplay();
delay(100);
}