Testing out ESP-NOW with Wifi and I'm having errors with the channel. Both receiver and sender are in channel 1 but I kept seeing an error from the sender saying "E (383127) ESPNOW: Peer channel is not equal to the home channel, send fail!"
Is there anything missing from setting channel to make it work and send data?
Sender code:
/* Sender */
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h> // Include this library for esp_wifi_set_channel
// Define the structure for the message
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} struct_message;
// Define the receiver's MAC address (replace with the correct address)
uint8_t receiverAddress[] = {0xE8, 0x6B, 0xEA, 0xCF, 0xE3, 0xF0}; // Replace with the correct MAC address
// Create a message structure
struct_message myData;
// Channel for ESP-NOW
const int CHANNEL = 1;
// Wi-Fi credentials
const char* ssid = "HOMEWIFI"; //This is a dummy ssid
const char* password = "password"; //This is a dummy password
void setup() {
Serial.begin(115200); // Initialize serial communication
// Initialize Wi-Fi in STA mode
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Check if Wi-Fi is connected
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP()); // Print the IP address assigned to the ESP32
// Set the Wi-Fi channel
esp_wifi_set_channel(CHANNEL, WIFI_SECOND_CHAN_NONE);
Serial.print("Wi-Fi Channel set to: ");
Serial.println(CHANNEL);
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register the send callback function
esp_now_register_send_cb(OnDataSent);
// Initialize peer information
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(peerInfo)); // Clear the peerInfo structure
memcpy(peerInfo.peer_addr, receiverAddress, 6); // Copy the receiver's MAC address
peerInfo.channel = CHANNEL;
peerInfo.encrypt = false;
// Add peer to the ESP-NOW peer list
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
Serial.println("Peer added successfully");
}
void loop() {
// Prepare data to send
strcpy(myData.a, "Hello from sender");
myData.b = random(1, 20);
myData.c = 1.2;
myData.d = false;
// Send data via ESP-NOW
esp_err_t result = esp_now_send(receiverAddress, (uint8_t *)&myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
} else {
Serial.println("Error sending the data");
}
delay(2000);
}
// Callback function when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
Receiver code:
/* Receiver */
#include <esp_now.h>
#include <WiFi.h>
#include <esp_wifi.h> // Include this library for esp_wifi_set_channel
#include <WebServer.h> // Include WebServer library
// Define the structure for the message
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} struct_message;
// Create a message structure to hold incoming data
struct_message incomingData;
// Channel for ESP-NOW
const int CHANNEL = 1;
// Create a web server object on port 80
WebServer server(80);
// Variable to hold received data as a string for web display
String receivedData = "";
// Wi-Fi credentials
const char* ssid = "HOMEWIFI"; //This is a dummy ssid
const char* password = "password"; //This is a dummy password
void setup() {
Serial.begin(115200); // Initialize serial communication at a baud rate of 115200
// Initialize Wi-Fi in STA (station) mode
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); // Connect to the Wi-Fi network
// Check if Wi-Fi is connected
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.println(WiFi.localIP()); // Print the IP address assigned to the ESP32
// Set the Wi-Fi channel to match the ESP-NOW communication channel
esp_wifi_set_channel(CHANNEL, WIFI_SECOND_CHAN_NONE); // Set the Wi-Fi channel
Serial.print("Wi-Fi Channel set to: ");
Serial.println(CHANNEL);
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register the receive callback function
esp_now_register_recv_cb(OnDataRecv);
// Start the web server
server.on("/", handleRoot); // Define the root URL handler
server.begin(); // Start the web server
Serial.println("Web server started");
}
void loop() {
server.handleClient(); // Handle incoming web requests
}
// Callback function when data is received via ESP-NOW
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
// Cast the incoming data to the structure type
struct_message* data = (struct_message*)incomingData;
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("String: ");
Serial.println(data->a); // Correctly access struct members
Serial.print("Int: ");
Serial.println(data->b);
Serial.print("Float: ");
Serial.println(data->c);
Serial.print("Bool: ");
Serial.println(data->d);
// Store received data as a string for web display
receivedData = "String: " + String(data->a) + "\n" +
"Int: " + String(data->b) + "\n" +
"Float: " + String(data->c) + "\n" +
"Bool: " + String(data->d);
// Print sender's MAC address
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.print("From MAC address: ");
Serial.println(macStr);
}
// Handle root URL
void handleRoot() {
server.send(200, "text/plain", receivedData); // Send the received data as a plain text response
}