Hello everyone, I have a question about ESP-NOW.
I am currently building a two way communication between two ESP32 boards.
What the code should do:
The first ESP should create a random Number for the variable called "pod" then send it to the second ESP. When the second ESP recieves the Number he checks if the number he recieved is the same number as a number which is assigned to the second ESP called "pod_ID".
If thats the case he should wait for a touch sensor to detect a signal.
When a signal is detected he should send a message with a random number called "reactiontime" back to the first ESP.
My Problem:
The issue is that i dont want the ESP thats waiting for a message coming in from the other ESP, to just keep on running through the loop().
I tried to solve it with a while loop kinda like:
while(/* no data recieved*/ )
{
//do nothing
}
But the issue with this is that I am not sure what or where I would declare a "recieve status" for a recieved data packet.
I hope the explaination of what my issue is, was understandable.
I would really appreciate any help.
The code for the first ESP is:
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
//define pod number
const int pod_ID = 1;
//Receiver
//uint8_t broadcastAddress[] = {0x08, 0x3A, 0xF2, 0xAC, 0xE0, 0xFC};
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Define variables to be sent
int pod;
// Define variables to store incoming readings
float incoming_reactionTime;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct message_send
{
int rPod;
} message_send;
typedef struct message_in
{
float rTime;
} message_in;
// Create a message to hold incoming readings
message_in incomingReadings;
// Create a struct_message the data to be sent
message_send dataToSend;
//troubleshooting / make peerinfo a global variabel
esp_now_peer_info_t peerInfo;
// 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";
}
}
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)
{
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(macStr);
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incoming_reactionTime = incomingReadings.rTime;
}
void showData()
{
Serial.println("_______________________________");
Serial.println("INCOMING READINGS:");
Serial.print("Pod number: ");
Serial.print(dataToSend.rPod);
Serial.println();
Serial.print("Time: ");
Serial.print(incoming_reactionTime);
Serial.println();
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
//while the serial stream is not open, do nothing
while(!Serial);
//led pin
pinMode(2, OUTPUT);
// 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;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Transmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
//esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop()
{
while(/* no data recieved*/ )
{
//do nothing
}
// Recieve message via ESP-NOW (not sure where this needs to be called)
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &incomingReadings, sizeof(incomingReadings));
//define Variables to be send
dataToSend.rPod = random(1,4);
if(dataToSend.rPod == pod_ID)
{
do{
//turn on LED
}while(touchRead(4) >= 50);
}
//Send message via ESP-NOW
esp_err_t sending = esp_now_send(broadcastAddress, (uint8_t *) &dataToSend, sizeof(dataToSend));
showData();
}
Here is the code for the second ESP:
#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
//define pod number
const int pod_ID = 2;
//Receiver
//uint8_t broadcastAddress[] = {0x9C, 0x9C, 0x1F, 0xE9, 0xC5, 0xFC}; //{0x08, 0x3A, 0xF2, 0xAC, 0xE0, 0xFC}
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Define variables to be sent
float reactionTime;
// Define variables to store incoming readings
int incoming_pod;
// Variable to store if sending data was successful
String success;
//Structure to send data
//Must match the receiver structure
typedef struct message_send
{
float rTime;
} message_send;
typedef struct message_in
{
int rPod;
} message_in;
// Create a struct_message to hold incoming readings
message_in incomingReadings;
// Create a struct_message the data to be sent
message_send dataToSend;
//troubleshooting / make peerinfo a global variabel
esp_now_peer_info_t peerInfo;
// 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";
}
}
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)
{
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(macStr);
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incoming_pod = incomingReadings.rPod;
}
void showData()
{
Serial.println("_______________________________");
Serial.println("INCOMING READINGS:");
Serial.print("Pod number: ");
Serial.print(incoming_pod);
Serial.println();
Serial.print("Time: ");
Serial.print(dataToSend.rTime);
Serial.println();
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
//while the serial stream is not open, do nothing
while(!Serial);
//led pin
pinMode(2, OUTPUT);
// 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;
}
// 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 = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop()
{
while(/* no data recieved*/ )
{
//do nothing
}
// Recieve message via ESP-NOW (not sure where this needs to be called)
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &incomingReadings, sizeof(incomingReadings));
//define Variables to be send
dataToSend.rTime = random(1,4001);
if(incoming_pod == pod_ID)
{
do{
//turn on LED
}while(touchRead(4) >= 50);
}
// Send message via ESP-NOW
esp_err_t sending = esp_now_send(broadcastAddress, (uint8_t *) &dataToSend, sizeof(dataToSend));
showData();
}