Week 11 Networking and Communications
๐ Fab Academy: Week X - Networking and Communications
This week for networking I connected 2 ESP32-S3 boards and made them communicate wirelessly.
๐ง I soldered header pins onto the ESP32 boards so that I can attach components onto it.
๐ค Then I worked with ChatGPT to learn how to code and network between the boards.
๐งญ Step 1: Getting the MAC Address
๐ The first step was to get the MAC address for one board.
๐งช It took a couple of tries to get the code just right but then it worked!
The working code:
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // Set to Station mode
delay(1000);
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
}
void loop() {}

๐ฌ I had the MAC address for the receiver.
๐ฃ๏ธ Step 2: Saying Hello
๐ก I used this MAC address to do a simple serial print code that made them say hello to each other, to establish a connection.
Sender Code:
#include <WiFi.h>
#include <esp_now.h>
uint8_t peerAddress[] = {0x74, 0x4D, 0xBD, 0x97, 0x65, 0xFC};
String incomingMsg;
unsigned long lastSent = 0;
void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
incomingMsg = "";
for (int i = 0; i < len; i++) {
incomingMsg += (char)data[i];
}
Serial.print("Received: ");
Serial.println(incomingMsg);
}
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW init failed");
return;
}
esp_now_register_recv_cb(OnDataRecv);
esp_now_register_send_cb(OnDataSent);
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, peerAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (!esp_now_is_peer_exist(peerAddress)) {
esp_now_add_peer(&peerInfo);
}
}
void loop() {
if (millis() - lastSent > 1000) {
const char *msg = "Hello from A";
esp_now_send(peerAddress, (uint8_t *)msg, strlen(msg));
lastSent = millis();
}
}
Reciever code:
#include <WiFi.h>
#include <esp_now.h>
String incomingMsg;
void OnDataRecv(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
incomingMsg = "";
for (int i = 0; i < len; i++) {
incomingMsg += (char)data[i];
}
Serial.print("Received: ");
Serial.println(incomingMsg);
// Add sender as peer before replying
if (!esp_now_is_peer_exist(info->src_addr)) {
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, info->src_addr, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo);
}
const char *reply = "Hello from B";
esp_now_send(info->src_addr, (uint8_t *)reply, strlen(reply));
}
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW init failed");
return;
}
esp_now_register_recv_cb(OnDataRecv);
esp_now_register_send_cb(OnDataSent);
}
void loop() {
// Waiting for incoming messages...
}
Output from this transmission.
๐ก Step 3: Blinking the Onboard LED
๐จ Next, I built upon this code to make an onboard LED blink .
๐งฉ It was a task to figure out which pin the built-in LED was on, but I got it eventually!
Code for a simple LED blink:
#ifndef LED_BUILTIN
#define LED_BUILTIN 38 // fallback if not defined
#endif
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
Development for Final project!
I am going to use ESP32s as my main board for my final project, It will be used for controlling the gas release and trigger the plasma pulse from the glove. I have strated tinkering and I will post updates when I have breakthroughs!
code:
#include <esp_now.h>
#include <WiFi.h>
#include <ESP32Servo.h>
Servo myServo;
typedef struct struct_message {
int xVal;
} struct_message;
struct_message receivedData;
void onReceive(const esp_now_recv_info_t *info, const uint8_t *incomingData, int len) {
memcpy(&receivedData, incomingData, sizeof(receivedData));
int angle = map(receivedData.xVal, 0, 4095, 0, 180);
myServo.write(angle);
Serial.print("Received X: ");
Serial.print(receivedData.xVal);
Serial.print(" | Mapped Angle: ");
Serial.println(angle);
}
void setup() {
Serial.begin(115200);
myServo.attach(0); // D0 = GPIO 0
WiFi.mode(WIFI_STA);
WiFi.disconnect();
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW Init Failed");
return;
}
esp_now_register_recv_cb(&onReceive);
}
void loop() {
// nothing here; waiting for joystick input
}
#include <esp_now.h>
#include <WiFi.h>
#define JOYSTICK_X_PIN 0 // D0 -> GPIO 0
uint8_t receiverMac[] = {0x74, 0x4D, 0xBD, 0x97, 0x65, 0xFC}; // Updated MAC
typedef struct {
int xVal;
} JoystickData;
JoystickData dataToSend;
void setup() {
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
Serial.println("Transmitter MAC: " + WiFi.macAddress());
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW Init Failed");
return;
}
// Register peer
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, receiverMac, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (!esp_now_add_peer(&peerInfo)) {
Serial.println("Failed to add peer");
} else {
Serial.println("Peer added");
}
// Read Joystick X only
pinMode(JOYSTICK_X_PIN, INPUT);
}
void loop() {
dataToSend.xVal = analogRead(JOYSTICK_X_PIN);
esp_now_send(receiverMac, (uint8_t*)&dataToSend, sizeof(dataToSend));
Serial.print("Sent X: ");
Serial.println(dataToSend.xVal);
delay(100); // Adjust as needed
}