ESP-NOW with ESP8266 - Two way communication test done
2025.04.11 05:20
ESP-NOW with ESP8266 - Two way communication test done with my own data type transmitted back and force.
Test coding done :
===========================================================
/* Original code is from Rui Santos' humidity & temperature sensor monitor code
His original source is at https://RandomNerdTutorials.com/esp-now-two-way-communication-esp8266-nodemcu/
My code is simplified version to fit my own purpose.
In this code, the sensor section is yet done
*/
#include <ESP8266WiFi.h>
#include <espnow.h>
// #include <Adafruit_Sensor.h>
// REPLACE WITH THE MAC Address of your receiver (My Net Module attached to PC)
//uint8_t broadcastAddress[] = {0xEC, 0xFA, 0xBC, 0xA7, 0xAB, 0x6D}; // NETWORK'S MAC Address
uint8_t broadcastAddress[] = {0xEC, 0xFA, 0xBC, 0xA7, 0xAE, 0xBF}; // COROLLA'S MAC Address
//COROLLA'S MAC Address: EC:FA:BC:A7:AE:BF
//HONDA'S MAC Address: 3C:71:BF:3A:D1:3D
//CAMRY'S MAC Address:
//NETWORK'S MAC Address: EC:FA:BC:A7:AB:6D
// Digital pin connected to the relay to turn on/off 12V LED
#define BLINKING_12V_LED 2
// Which auto station is this code will be uploaded to (Sender's Station ID) :
#define STATION_ID 0 // NETWORK NODE
//#define STATION_ID 1 // MY TOYOTA COROLLA
// #define STATION_ID 2 // MY HONDA PILOT
// #define STATION_ID 3 // MY TOYOTA CAMRY
#define STATE_OK 1000 // Peaceful
#define STATE_HOOD_OPEN 1001 // Car's front panel is open
#define STATE_TILTED 1002 // Car is lifted from one side (Possibly Tire or CAT Robbery)
#define STATE_SHAKING 1003 // Car is violently shaking from one side
#define STATE_TILTED_SHAKING 1004 // Tire or CAT Robbery Situation
// Define variables to store incoming readings
int incomingStation;
int incomingStatus;
// message sending every 1 seconds
const long interval = 1000;
unsigned long previousMillis = 0; // will store last time message was updated
// Previous Built-in LED blining status
bool prevBuiltInLED = 0;
// Variable to store if sending data was successful
String success;
//Structure example to send data must match the receiver structure
typedef struct struct_message {
int station;
int status;
} struct_message;
// Create a struct_message called StatusReading to hold car status reading
struct_message StatusReading;
// Create a struct_message to hold incoming car status reading
struct_message incomingReadings;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
}
// Callback when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
if (len > 0) { // if there was any data received, reverse built-in LED (Blink)
if (prevBuiltInLED == 0) {
prevBuiltInLED = 1;
digitalWrite(LED_BUILTIN, LOW);
} else {
prevBuiltInLED = 0;
digitalWrite(LED_BUILTIN, HIGH);
};
};
incomingStation = incomingReadings.station;
incomingStatus = incomingReadings.status;
}
void getReadings(){
// Do your sensor reading and analysis here.
// Build up the data.
}
void printIncomingReadings(){
// Display Readings in Serial Monitor
Serial.println("INCOMING READINGS");
Serial.print("Station: ");
Serial.print(incomingStation);
Serial.print(" Status: ");
Serial.println(incomingStatus);
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Initialize the LED_BUILTIN pin as a debug output
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BLINKING_12V_LED, OUTPUT);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set ESP-NOW Role
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// 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_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you updated the Status values
previousMillis = currentMillis;
getReadings(); // Now it doesn't do anything (No sensor code is done yet)
//Set values to send
StatusReading.station = STATION_ID;
StatusReading.status = STATE_OK; // for now it is fixed, no sensor is yet attached
// Send message via ESP-NOW
esp_now_send(broadcastAddress, (uint8_t *) &StatusReading, sizeof(StatusReading));
// Print incoming readings
printIncomingReadings();
}
}
Comment 0
No. | Subject | Author | Date | Views |
---|---|---|---|---|
10 | I asked Google's Gemini for parsing code. | me | 2025.04.17 | 82 |
9 |
ESP-NOW without Long Range & External Antenna is terrible!
![]() | me | 2025.04.16 | 81 |
» |
ESP-NOW with ESP8266 - Two way communication test done
![]() | me | 2025.04.11 | 86 |
7 |
Retry on PWM from Radxa ZERO 3W for servo control
![]() | me | 2025.03.25 | 124 |
6 | Problems found on I2C communication | me | 2025.03.22 | 132 |
5 | radxa zero 3w running an app on booting | me | 2025.03.13 | 142 |
4 |
Basic Communication between ZERO 3W <==> Pro Mini via I2C
![]() | me | 2025.03.13 | 144 |
3 | Important Links | me | 2025.03.12 | 164 |
2 | To-Do List as of Mar. 12, 2025 | me | 2025.03.12 | 159 |
1 |
Mar. 12, 2025 Status
![]() | me | 2025.03.12 | 159 |