Hi everyone, as the title says I am getting a delay of about 10 sec which is not great for my application. I am using two Xiao ESP32C3 Modules (both battery powered). One module reads the sensor values and passes it to another using ESP NOW protocol (one way communication). Both modules have an antenna attached to them and are relatively closed to one another (10 meters).
I cannot understand what am I doing wrong. Any help is appreciated. The code for both modules is down below.
Sender:
// Include Libraries
#include <esp_now.h>
#include <WiFi.h>
// Set sensorPins
const int sensorPin = A0;
// Variables for test data
int int_value;
float float_value;
// MAC Address of responder - edit as required
uint8_t broadcastAddress[] = {0x34, 0x85, 0x18, 0x03, 0x45, 0xC8};
// Define a data structure
typedef struct struct_message {
int a;
float b;
} struct_message;
// Create a structured object
struct_message myData;
// Peer info
esp_now_peer_info_t peerInfo;
// Callback function called 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");
}
void setup() {
// Set Pin and resolution xiao has 12bit resolution
pinMode(sensorPin, INPUT);
analogReadResolution(12);
// Set up Serial Monitor
Serial.begin(115200);
// Set ESP32 as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Initilize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register the send callback
esp_now_register_send_cb(OnDataSent);
// Register peer
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;
}
}
void loop() {
// Create test data
int sensorVal = analogRead(sensorPin);
float percentage = (sensorVal/4096.0) * 100.0;
// Format structured data
myData.a = sensorVal;
myData.b = percentage;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sending confirmed");
}
else {
Serial.println("Sending error");
}
delay(125);
}
Receiver:
// Include Libraries
#include <esp_now.h>
#include <WiFi.h>
#define leftLED 10
#define rightLED 9
const int threshold = 2900; // Light intensity threshold
const unsigned long leftLEDinterval = 125; // LED blink variables declaration with millis to avoid delays
//const unsigned long rightLEDinterval = 125; // Uncomment for different blink patterns
unsigned long currentTime = 0;
unsigned long previousTimeLeftLED = 0;
//unsigned long previousTimeRightLED = 0;
int leftLEDstate = LOW;
int rightLEDstate = LOW;
// Define a data structure
typedef struct struct_message {
int a;
float b;
} struct_message;
// Create a structured object
struct_message myData;
// Callback function executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Data received: ");
Serial.println(len);
Serial.print("Raw Sensor Value: ");
Serial.println(myData.a);
Serial.print("Percentage: ");
Serial.println(myData.b);
Serial.println();
}
void setup() {
pinMode(leftLED, OUTPUT);
pinMode(rightLED, OUTPUT);
// Set up Serial Monitor
Serial.begin(115200);
// Set ESP32 as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Initilize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register callback function
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
// Blink on receiver site
unsigned long currentTime = millis();
if (myData.a > threshold)
{
// LED blink logic: LED1 ON && LED2 OFF same rhythm.
if (currentTime - previousTimeLeftLED > leftLEDinterval)
{
previousTimeLeftLED = currentTime;
if (leftLEDstate == LOW)
{
leftLEDstate = HIGH;
rightLEDstate = LOW;
}
else
{
leftLEDstate = LOW;
rightLEDstate = HIGH;
}
digitalWrite(leftLED, leftLEDstate);
digitalWrite(rightLED, rightLEDstate);
}
}
else
{
leftLEDstate = LOW;
rightLEDstate = LOW;
digitalWrite(leftLED, leftLEDstate);
digitalWrite(rightLED, rightLEDstate);
}
}