HI,
I'm doing a 1:N Wi-Fi test with a few ESP32, and the purpose of the test is serial.write() when a certain pin on the master is activated. (but now code are just writing Serial.)Then, as the slave parses the that Serial, the built-in LED must blink.
What's the problem?
master's code
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
//uint8_t broadcastAddress1[] = {0x40, 0x91, 0x51, 0x9A, 0xEB, 0xEC};
//uint8_t broadcastAddress2[] = {0x40, 0x91, 0x51, 0x9A, 0xE5, 0x94};
uint8_t Receiver_Address1[] = {0x40, 0x91, 0x51, 0x9A, 0xEB, 0xEC};
uint8_t Receiver_Address2[] = {0x40, 0x91, 0x51, 0x9A, 0xE5, 0x94};
uint8_t Receiver_Address3[] = {0x84, 0xCC, 0xA8, 0x5E, 0x52, 0x44};
typedef struct struct_message {
int integer;
char character[100];
} struct_message;
struct_message message;
void data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) {
char address[18];
Serial.print("Sent to: ");
snprintf(address, sizeof(address), "%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(address);
Serial.print(" status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(data_sent);
esp_now_peer_info_t peerInfo;
peerInfo.channel = 0;
peerInfo.encrypt = false;
memcpy(peerInfo.peer_addr, Receiver_Address1, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
memcpy(peerInfo.peer_addr, Receiver_Address2, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
memcpy(peerInfo.peer_addr, Receiver_Address3, 6);
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
message.integer = Serial.write(0x61);
strcpy(message.character, "Welcome to Microcontrollerslab! This is test example.");
esp_err_t outcome = esp_now_send(0, (uint8_t *) &message, sizeof(struct_message));
if (outcome == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
delay(2000);
}
slave's code
#include <esp_now.h>
#include <WiFi.h>
//Must match the sender structure
typedef struct struct_message {
int integer;
char character[100];
} struct_message;
struct_message message;
void data_receive(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&message, incomingData, sizeof(message));
Serial.print("Bytes received: ");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(data_receive);
}
void loop() {
//if (n != 0)
//{
//byte l = Serial.read(); //expecying numerical value; so, declare byte type variable
byte p = Serial.available(); //check that a charcater/data byte has come from UNO
if (p != 0)
{
byte l = Serial.read(); //expecying numerical value; so, declare byte type variable
if ( l == 0x61
)
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(500);
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
}
}
refer entirely to this reference. below.
thanks!