Commit abfa4f53 authored by Kencho Wangdi's avatar Kencho Wangdi
Browse files

networking

parent 9590be7d
Pipeline #318849 passed with stage
in 52 seconds
......@@ -89,6 +89,8 @@ I have used the same board which was used during my input and output week.
![search](../images/week11/outputboard.png)
**For this assignment I will control LEDs using ESP32 web server in Arduino IDE.**
and **try to communicate between two ESP32 by ESP NOW Protocol**
......@@ -185,11 +187,285 @@ And it was working fine.
![type:video](../videos/espip.mp4)
## Original Files
Download all the original files from here.
[network files](../files/network.rar)
Initially I have tried to control LED using the IP address from web server but during Global evalution I got a comment saying "You need to communicate between two or more boards with identifiable addresses wired or wireless." Therefore, I will try to communicate between two ESP32 board.
## ESP32 ESP-NOW Two way Communication (Arduino IDE)
**What is ESP-NOW Protocol?**
ESP-NOW is another protocol developed by Espressif, which can perform one way or two way communication between ESP MCU devices without using a Wi-Fi network.
It allows transfer of small peer to peer wireless data packets. Only upto 250 bytes of data can be transferred. the ESP-NOW uses the same 2.4 GHz band as the Wi-Fi but does not need to connect or interfere with the local network connection. It is a fast and convenient communication protocol for the transmission of a smaller amount of data. The ESP-NOW uses the same 2.4 GHz band as the Wi-Fi but does not need to connect or interfere with the local network connection. It is a fast and convenient communication protocol for the transmission of a smaller amount of data.
**ESP-NOW Networking Modes**
We can make the ESP-NOW network in many configurations. We can mix and match ESP32 and ESP8266 devices within the same network. In ESP-NOW network we have to create a initiators and responders.
## Initiators and Responders
A device participating in an ESP-NOW network can be operated in one of two modes.
Initiator – This device initiates the transmission. It will require the MAC address of the receiving device.
Responder – This device receives the transmission.
In unidirectional (half-duplex) mode, the transmitting device is the Initiator and the receiving device is the Responder.
In a 2-way (full-duplex) communications mode, each device is both an Initiator and Responder.
# One-Way Communication OR half-duplex communication.
In one-way communication, the initiator ESP32 transmits data to the Responder ESP32. We can tell if the responder received the message successfully or not.
Image of one way.
## Two- way communication or Full-Duplex communication.
In this communication, both the device can communicate with eachother at the same time.
Image of one way.
## MAC Addresses
A mac address is required to make the communication possible between the initiator and the responder.
A MAC, or Media Access Control, Address is a unique 6-digit hexadecimal number assigned to every device on a network. It is generally burned into the device by the manufacturer, although it is possible to manually set it.
Today we will be working with ESP-NOW, a connectionless protocol developed by Espressif for the ESP32 and ESP08266 microcontrollers. With ESP-NOW we can build a private network without WiFi or a Router.
For one way communication I am going to send some data from initiator to the responder. For that I need to get MAC address.
## MAC Address
Copy the code below and run it on Arduino IDE to get the MAC address
```
// Include WiFi Library
#include "WiFi.h"
void setup() {
// Setup Serial Monitor
Serial.begin(115200);
// Put ESP32 into Station mode
WiFi.mode(WIFI_MODE_STA);
// Print MAC Address to Serial monitor
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
}
void loop() {
}
```
![search](../images/network/mac1.png)
Copy the MAC address and keep it at a safe loction for the later use.
Once your the MAC address it is time for coding. Make sure all the necessary libraries were installed in your arduino ide. If you are going to do the same example of what I have used, I will give all the library links at the end.
## Coding for ESP-NOW – Sending
Mark one ESP as sender and other as receiver.
![search](../images/network/sandr.jpeg)
Since, I am away from lab I am using the commercially available ESP board for this assignment.
For the sender board you have to
- initialize the ESP-NOW library.
- Next, you’ll register your send callback function
```
In the ESP-NOW protocol, there are two callbacks of interest:
1. Sending Data – The esp_now_register_send_cb() callback is called whenever data is sent.
2. Receiving Data – The esp_now_register_rcv_cb() callback is called when data is received.
The callback functions also return some useful data:
- The Sending callback returns the status of the sent data.
- The Receiving callback includes the received data packet.
```
- You need to add a peer device, which is the responder device. You add the peer by specifying its MAC address.
- Finally, you can packetize and send the message.
## **code for ONE-WAY Initiator**
Open a new Arduino IDE page to write the code for one way initiator and save it as ESP32#1.
copy the code below, replace the MAC address with the one you saved earlier and run the sketch.
```
// Include Libraries
#include <esp_now.h>
#include <WiFi.h>
// Variables for test data
int int_value;
float float_value;
bool bool_value = true;
// MAC Address of responder - edit as required
uint8_t broadcastAddress[] = {0x78, 0x21, 0x84, 0x7A, 0xBC, 0x1C};
// Define a data structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} 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 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
// Generate a random integer
int_value = random(1,20);
// Use integer to make a new float
float_value = 1.3 * int_value;
// Invert the boolean value
bool_value = !bool_value;
// Format structured data
strcpy(myData.a, "Kencho network and communication week");
myData.b = int_value;
myData.c = float_value;
myData.d = bool_value;
// 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(2000);
}
```
Once the code is loaded, open the serial monitor and press the EN button to refresh it.
Something like this will be printed. This means the datas are sucessfully transferred to the responder.
![search](../images/network/sender.png)
Now lets prepare the responder by registering the received callback function. In the receive callback function we will capture the incoming message data and pass it to a variable.
## CODE for One-Way Responder
```
// Include Libraries
#include <esp_now.h>
#include <WiFi.h>
// Define a data structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} 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("Character Value: ");
Serial.println(myData.a);
Serial.print("Integer Value: ");
Serial.println(myData.b);
Serial.print("Float Value: ");
Serial.println(myData.c);
Serial.print("Boolean Value: ");
Serial.println(myData.d);
Serial.println();
}
void setup() {
// 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() {
}
```
copy this code and load it in a new sketch on the arduino ide.
Once the code is loaded, open the serial monitor at baud reate 115200. You should receive the data send from the Sender ESP32.
![search](../images/network/receiver.png)
## One-Way communication Files
Download all the One-Way original files from here.
[One-Way original files](../files/oneway.rar)
......
// Include Libraries
#include <esp_now.h>
#include <WiFi.h>
// Define a data structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} 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("Character Value: ");
Serial.println(myData.a);
Serial.print("Integer Value: ");
Serial.println(myData.b);
Serial.print("Float Value: ");
Serial.println(myData.c);
Serial.print("Boolean Value: ");
Serial.println(myData.d);
Serial.println();
}
void setup() {
// 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() {
}
// Include Libraries
#include <esp_now.h>
#include <WiFi.h>
// Variables for test data
int int_value;
float float_value;
bool bool_value = true;
// MAC Address of responder - edit as required
uint8_t broadcastAddress[] = {0x78, 0x21, 0x84, 0x7A, 0xBC, 0x1C};
// Define a data structure
typedef struct struct_message {
char a[32];
int b;
float c;
bool d;
} 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 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
// Generate a random integer
int_value = random(1,20);
// Use integer to make a new float
float_value = 1.3 * int_value;
// Invert the boolean value
bool_value = !bool_value;
// Format structured data
strcpy(myData.a, "Kencho Networking and Communication week");
myData.b = int_value;
myData.c = float_value;
myData.d = bool_value;
// 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(2000);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment