ESP-NOW Photoresistor Integration¶
Read a photoresistor on the sender board and blink an LED on the receiver board when the light level drops below 600.
How the Data Flows¶
SENDER RECEIVER
────── ────────
analogRead(A0) → lightVal
lightVal < 600 → myData.d=true
esp_now_send() ────────────────→ OnDataRecv()
myData.d==true → blink LED
The bool d field is the flag — sender sets it, receiver acts on it. Field b carries the raw sensor value for logging.
SENDER — SenderESP.ino¶
Integrate the photoresistor by reading the sensor each loop and packing the value into the struct before sending.
#include <esp_now.h>
#include <WiFi.h>
uint8_t broadcastAddress[] = {0xe4, 0xb3, 0x23, 0xc5, 0x3d, 0xa4};
typedef struct struct_message {
char a[32];
int b; // used for raw lightVal
float c;
bool d; // true = dark (below threshold)
} struct_message;
struct_message myData;
esp_now_peer_info_t peerInfo;
// Photoresistor globals
int lightPin = A0;
int lightVal = 0;
int threshold = 600;
void OnDataSent(const wifi_tx_info_t *tx_info, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Sent OK" : "Send Failed");
}
void setup() {
Serial.begin(115200);
// Photoresistor setup
analogSetAttenuation(ADC_11db);
pinMode(lightPin, INPUT);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_send_cb(OnDataSent);
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
// Read sensor
lightVal = analogRead(lightPin);
Serial.print("Light value: ");
Serial.println(lightVal);
// Pack into struct
strcpy(myData.a, "LIGHT SENSOR");
myData.b = lightVal; // raw reading
myData.c = 1.2;
myData.d = (lightVal < threshold); // true = dark
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) Serial.println("Sent with success");
else Serial.println("Error sending the data");
delay(2000);
}
RECEIVER — RecieverESP.ino¶
Check the bool d flag in the callback and blink the LED if dark, turn it off if bright.
#include <esp_now.h>
#include <WiFi.h>
typedef struct struct_message {
char a[32];
int b;
float c;
bool d; // true = below threshold (dark)
} struct_message;
struct_message myData;
int ledPin = 4; // change to match your LED pin
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Light value received: ");
Serial.println(myData.b);
Serial.print("Dark? ");
Serial.println(myData.d ? "YES - blinking" : "NO - LED off");
if (myData.d) {
// Blink 3 times when dark
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
} else {
digitalWrite(ledPin, LOW);
}
}
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}
void loop() {
// nothing needed here
}
Key Changes Summary¶
| Location | What was added |
|---|---|
| Sender globals | lightPin, lightVal, threshold variables |
Sender setup() |
analogSetAttenuation() and pinMode() for sensor |
Sender loop() |
analogRead(), pack value into myData.b and myData.d |
| Receiver globals | ledPin variable |
Receiver setup() |
pinMode(ledPin, OUTPUT) |
Receiver OnDataRecv() |
Check myData.d, blink or turn off LED |