docs networking

This commit is contained in:
2025-04-08 11:21:01 +02:00
parent 4ee9a76291
commit 960411167a

View File

@@ -32,3 +32,247 @@ When compiling the main side it worked fine but when compiling the secondary sid
![alt text](image-4.jpg) ![alt text](image-4.jpg)
After that we tried with an SPI screen and there we spent over 2.5 hours attempting to get it to work. Checking the wires multiple times. Trying another screen. Trying different example codes. We had everything correct according to the datasheet and the driver chip that was inside the board.
![alt text](image-5.png)
We also connected the serial monitor to it and read the data from it. It was sending a lot of commands so that was working but it is very hard to figure out if the commands are correct since they are machine instructions. So we ended the day there and got beers and a bar.
## Individual Assignment
I kinda did my individual assignment in the [programming week](../week_4_programming/programming.md) since I was both programming the handheld controller and the flight controller at the same time. During that week I also set up the communication between the 2 devices using ESPNow. ESPNow is a protocol skip a lot of OSI layers thus minimizing the overhead. It doesn't do handshakes so it kind of works like UDP. It's also peer to peer so it doesn't need any network infrastructure making it completely decentralized.
![alt text](image-6.png)
[Source: arduino](https://docs.arduino.cc/tutorials/nano-esp32/esp-now/)
### Encrypted ESPNow
To create secure ESPNow communication I used [this tutorial](https://randomnerdtutorials.com/esp32-esp-now-encrypted-messages/).
The code stays the same except for a few extra variables added. Some things also change with registering peers to
```cpp
// PMK and LMK keys
static const char* PMK_KEY_STR = "REPLACE_WITH_PMK_KEY";
static const char* LMK_KEY_STR = "REPLACE_WITH_LMK_KEY";
// Register the receiver board as peer
memcpy(peerInfo.peer_addr, receiverAddress, 6);
peerInfo.channel = 0;
//Set the receiver device LMK key
for (uint8_t i = 0; i < 16; i++) {
peerInfo.lmk[i] = LMK_KEY_STR[i];
}
// Set PMK key
esp_now_set_pmk((uint8_t *)PMK_KEY_STR);
peerInfo.encrypt = true;
```
The PMK and LMK need to be exactly 16 bytes so to count to amount of bytes I used in my key I used this website. Most of the time you can go for each character is 1 byte. But there are also some online tools available. https://tools.smikkelbakje.nl/text-statistics or https://mothereff.in/byte-counter
They should be the same on each board you wanna communicate with. The only flaw about this protocol is that the devices that receive decrypted messages still interpret them as if they where encrypted and they use the data inside them.
![alt text](image-7.png)
??? Sender example
```cpp
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-esp-now-encrypted-messages/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH THE RECEIVER'S MAC Address
uint8_t receiverAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// PMK and LMK keys
static const char* PMK_KEY_STR = "REPLACE_WITH_PMK_KEY";
static const char* LMK_KEY_STR = "REPLACE_WITH_LMK_KEY";
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int counter;
int x;
int y;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Counter variable to keep track of number of sent packets
int counter;
// Variable to save peerInfo
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");
}
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("There was an error initializing ESP-NOW");
return;
}
// Set PMK key
esp_now_set_pmk((uint8_t *)PMK_KEY_STR);
// Register the receiver board as peer
memcpy(peerInfo.peer_addr, receiverAddress, 6);
peerInfo.channel = 0;
//Set the receiver device LMK key
for (uint8_t i = 0; i < 16; i++) {
peerInfo.lmk[i] = LMK_KEY_STR[i];
}
// Set encryption to true
peerInfo.encrypt = true;
// Add receiver as peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of transmitted packet
esp_now_register_send_cb(OnDataSent);
}
void loop() {
static unsigned long lastEventTime = millis();
static const unsigned long EVENT_INTERVAL_MS = 5000;
if ((millis() - lastEventTime) > EVENT_INTERVAL_MS) {
lastEventTime = millis();
// Set values to send
myData.counter = counter++;
myData.x = random(0,50);
myData.y = random(0,50);
// Send message via ESP-NOW
esp_err_t result = esp_now_send(receiverAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
}
```
??? Receiver example
```cpp
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-esp-now-encrypted-messages/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH YOUR MASTER MAC Address
uint8_t masterMacAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// PMK and LMK keys
static const char* PMK_KEY_STR = "REPLACE_WITH_PMK_KEY";
static const char* LMK_KEY_STR = "REPLACE_WITH_LMK_KEY";
// Structure example to send data
// Must match the sender structure
typedef struct struct_message {
int counter; // must be unique for each sender board
int x;
int y;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Function to print MAC address on Serial Monitor
void printMAC(const uint8_t * mac_addr){
char macStr[18];
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
}
// Callback function executed when data is received
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
Serial.print("Packet received from: ");
printMAC(mac_addr);
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("Packet number: ");
Serial.println(myData.counter);
Serial.print("X: ");
Serial.println(myData.x);
Serial.print("Y: ");
Serial.println(myData.y);
}
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("There was an error initializing ESP-NOW");
return;
}
// Set the PMK key
esp_now_set_pmk((uint8_t *)PMK_KEY_STR);
// Register the master as peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, masterMacAddress, 6);
peerInfo.channel = 0;
// Setting the master device LMK key
for (uint8_t i = 0; i < 16; i++) {
peerInfo.lmk[i] = LMK_KEY_STR[i];
}
// Set encryption to true
peerInfo.encrypt = true;
// Add master as peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}
void loop() {
}
```
## Sources
* https://randomnerdtutorials.com/esp32-esp-now-encrypted-messages/