Skip to content

13. Networking and communications

Group assignment:

  • Send a message between two projects

To see our group assignment click here

Individual assignment:

  • Design, build, and connect wired or wireless node(s) with network or bus addresses

For this week’s mission, I’ve decided to use two communications protocols: ESPNOW and MQTT.

ESPNOW PROTOCOL

PRESENTATION

🌐 Introducing the ESPNOW protocol: The ESPNOW protocol is a wireless data exchange protocol developed by Espressif, the manufacturer of ESP chips. It enables ESP devices to communicate with each other without using a Wi-Fi access point.

Advantages of using the ESPNOW protocol: ESPNOW is a lightweight, efficient protocol offering low latency and low power consumption. It is ideal for applications requiring fast, reliable communication between devices, such as home automation, remote control and sensors.

🛠️ ESPNOW protocol implementation: ESPNOW can be implemented using the Arduino IDE or ESP-IDF (Espressif IoT Development Framework). It is important to note that ESPNOW is a proprietary protocol and is not compatible with non-ESP devices.

ESP-NOW is a wireless communication protocol based on the data-link layer, which reduces the five layers of the OSI model to only one. This way, the data need not be transmitted through the network layer, the transport layer, the session layer, the presentation layer, and the application layer. Also, there is no need for packet headers or unpackers on each layer, which leads to a quick response reducing the delay caused by packet loss in congested networks. Source Monica.im

IMPLEMENTATION

To use the ESPnow protocol, you need to know two things:

  • 1: You need two ESP cards of any type. One is a master (transmitter) and the other a slave (receiver).
  • 2: You need the Mac address of both ESPs.

You can find the Mac address of your ESP in the arduino IDE when you program your ESP. To link the two ESPs, you need to copy the address of one of them into the code you transfer to the other, and vice versa.

For more information, visit the Raspberry Me website.

I. ESP-NOW: Transmitter

Library Includes : These lines include necessary libraries for ESP-NOW communication and Wi-Fi.

#include <esp_now.h>
#include <WiFi.h>

Receiver MAC Address : This line defines the MAC address of the receiver ESP32 module.

uint8_t RxMACaddress[] = {0x94, 0xE6, 0x86, 0x05, 0x86, 0x40};

Structure Definition : This defines a structure TxStruct to hold data to be sent via ESP-NOW, specifically the potentiometer value.

typedef struct TxStruct {
  int potVal; // Potentiometer value
} TxStruct;

Global Variables : This declares a variable sentData of type TxStruct to store data to be sent.

TxStruct sentData;

Callback Function : This function is a callback called when data is sent via ESP-NOW. It prints the delivery status of the sent packet.

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  // Display the send status
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

Setup Function : This function is called once at the beginning of the program. It initializes serial communication, Wi-Fi, ESP-NOW, and the receiver peer for communication.

void setup() {
  // Initialize serial communication
  // Configure Wi-Fi mode as station
  // Initialize ESP-NOW and register callback function for data send
  // Initialize peer info for ESP-NOW communication
  // Add the ESP-NOW receiver
}

Loop Function : This function is called repeatedly in a loop. It reads the potentiometer value, sends data via ESP-NOW, displays the send status, and then waits for a short time before repeating the process.

void loop() {
  // Read potentiometer value
  // Send data via ESP-NOW
  // Display send status
  // Wait for a short time before repeating the loop
}

Each part of the code plays a specific role in setting up ESP-NOW communication and sending data to the receiver ESP32 module.

II. ESP-NOW: Receiver

Library Includes : TThese lines include necessary libraries for ESP-NOW communication, Wi-Fi, and OLED display.

#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

OLED Display Initialization: : This line initializes the OLED display with specific dimensions and communication settings.

Adafruit_SSD1306 display(128, 64, &Wire, -1);

Structure Definition : This defines a structure RxStruct to hold received data, specifically the potentiometer value.

typedef struct RxStruct {
  int potVal; // Potentiometer value
} RxStruct;

Global Variables : This declares a variable receivedData of type RxStruct to store received data.

RxStruct receivedData;

Callback Function : This function is a callback called when data is received via ESP-NOW. It copies the received data to the receivedData structure.

void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  // Copy received data to the structure
  memcpy(&receivedData, incomingData, sizeof(receivedData));
}

Setup Function : This function is called once at the beginning of the program. It initializes serial communication, OLED display, Wi-Fi, and ESP-NOW.

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  // Initialize OLED display
  // Check if display initialization fails and loop indefinitely if it does
  // Configure Wi-Fi mode as station
  // Initialize ESP-NOW and register callback function for data reception
}

Loop Function : This function is called repeatedly in a loop. It updates the OLED display with the received potentiometer value.

void loop() {
  // Update OLED display with received data
  // Clear the display and set text color
  // Print static text and received potentiometer value on the display
}
Each part of the code serves a specific purpose in setting up ESP-NOW communication and displaying received data on the OLED display.

An important observation to consider for this system is the need to ensure that the MAC addresses of the ESP32 transmitter and receiver modules are correctly configured and match in the code. An error in the MAC addresses can lead to communication failure between the modules. It is also essential to verify that both modules are on the same ESP-NOW communication channel and that the transmitted and received information is correctly structured to avoid transmission errors.

FILES


Last update: July 15, 2024