Assignment to be done:
1.Group Assignment:
Send a message between two projects
To be found here
What I have learned from group assignment
In our group assignment, We want to use two esp32 MCU communicate and exhange data via wifi network protocol over esp-now data protocol.
Esp32 is built in bluetooth and wifi which makes it to be suitable to use for this assignment.
What ESP-NOW Protocol?
ESP-NOW is a proprietary communication protocol developed by Espressif, the company that manufactures the ESP8266 and ESP32 microcontrollers. It allows for communication between two or more ESP devices without the need for a Wi-Fi network.
ESP-NOW uses a simple and lightweight message format that consists of a header and a payload. The header includes the sender and receiver MAC addresses and a message type field, while the payload can be up to 250 bytes. The protocol uses a one-to-many communication model, where a sender can broadcast a message to multiple receivers in a single transmission. ESP-NOW also provides strong security features, including message encryption and authentication, to protect against unauthorized access and tampering. Overall, ESP-NOW is a reliable and easy-to-use protocol for building low-power, wireless networks with ESP devices.
RANGE TEST FOR ESP-NOW
The ESP-NOW Documentations such as (Espressif say that the range is about 200 meters in open space but the range can vary according to the area , walls and other obstacles can reduce the range
What is ESP-NOW?
According to (Link here), ESP-NOW is a wireless communication protocol defined by Espressif, which enables the direct, quick and low-power control of smart devices, without the need of a router. ESP-NOW can work with Wi-Fi and Bluetooth LE, and supports the ESP8266, ESP32, ESP32-S and ESP32-C series of SoCs. It’s widely used in smart-home appliances, remote controlling, sensors, etc.
Second I thought about what address to used by those two boards and we found the MAC address of each board through an Arduino sketch to differentiate between the two modules.
Programming process
The programming process started by scanning the MAC address of each board and programming them with a specific message to be send in each board.
codes to scan the mac addres of the boards:
#include< WiFi.h>
void setup() {
Serial.begin(115200);
// initialize WiFi
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// print the MAC address of the ESP32
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
}
void loop() {
// print the MAC address of the ESP32
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
delay(2000);
}
Then the MAC address is displayed in the serial monitor as shown in the images below:
#include< mWiFi.h>
void setup() {
Serial.begin(115200);
// initialize WiFi
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// print the MAC address of the ESP32
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
}
void loop() {
// print the MAC address of the ESP32
Serial.print("MAC address: ");
Serial.println(WiFi.macAddress());
delay(2000);
}
I got the MAC address of the second board as shown with the immage below
#include < esp_now.h >
#include < WiFi.h >
#include < Wire.h >
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0x7C, 0x9E, 0xBD, 0x49, 0x4E, 0x28};
// Define variables to store BME280 readings to be sent
float temperature;
float humidity;
float pressure;
// Define variables to store incoming readings
float incomingTemp;
float incomingHum;
float incomingPres;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
float temp;
float hum;
float pres;
} struct_message;
// Create a struct_message called BME280Readings to hold sensor readings
struct_message BME280Readings;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
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) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemp = incomingReadings.temp;
incomingHum = incomingReadings.hum;
incomingPres = incomingReadings.pres;
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// 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
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() {
getReadings();
delay(1000);
// Set values to send
BME280Readings.temp = temperature;
BME280Readings.hum = humidity;
BME280Readings.pres = pressure;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &BME280Readings, sizeof(BME280Readings));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
// Display Readings in Serial Monitor
Serial.println("INCOMING READINGS");
Serial.print("Temperature: ");
Serial.print(incomingReadings.temp);
Serial.println(" ºC");
Serial.print("Humidity: ");
Serial.print(incomingReadings.hum);
Serial.println(" %");
Serial.print("Pressure: ");
Serial.print(incomingReadings.pres);
Serial.println(" hPa");
Serial.println();
}
void getReadings(){
temperature = 50.1;
humidity = 40;
pressure = 80;
}
The up codes include the message about temperature = 50.1, humidity = 40, and pressure =80 and these data have been embedded directly into the source code(Hard codings) that are being sent to other board.
#include < esp_now.h >
#include < WiFi.h >
#include < Wire.h >
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0x94, 0xE6, 0x86, 0x04, 0x89, 0x3C};
// Define variables to store BME280 readings to be sent
float temperature;
float humidity;
float pressure;
// Define variables to store incoming readings
float incomingTemp;
float incomingHum;
float incomingPres;
// Variable to store if sending data was successful
String success;
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
float temp;
float hum;
float pres;
} struct_message;
// Create a struct_message called BME280Readings to hold sensor readings
struct_message BME280Readings;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
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) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemp = incomingReadings.temp;
incomingHum = incomingReadings.hum;
incomingPres = incomingReadings.pres;
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// 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
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() {
getReadings();
delay(1000);
// Set values to send
BME280Readings.temp = temperature;
BME280Readings.hum = humidity;
BME280Readings.pres = pressure;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &BME280Readings, sizeof(BME280Readings));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
// Display Readings in Serial Monitor
Serial.println("INCOMING READINGS");
Serial.print("Temperature: ");
Serial.print(incomingReadings.temp);
Serial.println(" ºC");
Serial.print("Humidity: ");
Serial.print(incomingReadings.hum);
Serial.println(" %");
Serial.print("Pressure: ");
Serial.print(incomingReadings.pres);
Serial.println(" hPa");
Serial.println();
}
void getReadings(){
temperature = 20.1;
humidity = 99;
pressure = 120;
}
The up codes includes the message about temperature, humidity and pressure that are being sent to other board.The used board has been designed in the previous assignments and its design files can downloaded from here
2023 All Rights Reserved. Designed by Felix NYIRIGIRA