Networking and Communications¶
Goals¶
For the group work this week, we have to:
Send a message between two projects. - Max Negrin, McKinnon Collins, and Yian Hu
To complete the assignment, we will be designing a telegraph using LEDs, buttons, and two ESP32-c3’s. Pressing the button on one ESP32 will light up the LEDs of both ESPs, allowing for transmission of text via Morse Code. Firstly, I have to find a protocol to enable communication between the two devices. I will be using the ESP-NOW protocol, and will follow this tutorial to establish communication, and Claude to generate the actual script that triggers the LEDs.
I used this script to find the MAC adress for each ESP32:
#include "WiFi.h"
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_MODE_STA);
Serial.println(WiFi.macAddress());
}
void loop(){}

Then, I copied the MAC address from that MCU and put it in a google doc and labled it “1”. Then I ran it on the other ESP32, and also copied its MAC address. Now, with both of the addresses, I had Claude generate me the following script, which handles everything. You simply input the other ESP’s MAC address, and it will connect automatically. Then, when there is a signal in on the button’s GPIO, it sends a packet to the other ESP32 to tell it to light up its LED.
#include <WiFi.h>
#include <esp_now.h>
// --- Pin config ---
#define KEY_PIN 5 // Telegraph key (button), 3V3 to pin 5 (active HIGH)
#define LED_PIN 4 // LED, pin 4 to GND
#define BUZZ_PIN 6 // Buzzer, pin 6 to GND
// ESP 2's MAC address
uint8_t peerMAC[] = {0xE4, 0xB3, 0x23, 0xC6, 0xAD, 0x2C};
typedef struct {
bool keyDown;
} TelegraphMsg;
TelegraphMsg outgoing;
TelegraphMsg incoming;
bool lastKeyState = false;
void onReceive(const esp_now_recv_info_t *info, const uint8_t *data, int len) {
if (len == sizeof(TelegraphMsg)) {
memcpy(&incoming, data, sizeof(TelegraphMsg));
digitalWrite(LED_PIN, incoming.keyDown ? HIGH : LOW);
digitalWrite(BUZZ_PIN, incoming.keyDown ? HIGH : LOW);
}
}
void setup() {
Serial.begin(115200);
pinMode(KEY_PIN, INPUT_PULLDOWN); // Pull-down since button connects to 3V3
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZ_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZ_PIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Serial.print("My MAC: ");
Serial.println(WiFi.macAddress());
if (esp_now_init() != ESP_OK) {
Serial.println("ESP-NOW init failed!");
return;
}
esp_now_register_recv_cb(onReceive);
esp_now_peer_info_t peerInfo = {};
memcpy(peerInfo.peer_addr, peerMAC, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
}
}
void loop() {
bool keyDown = (digitalRead(KEY_PIN) == HIGH); // Active HIGH
if (keyDown != lastKeyState) {
lastKeyState = keyDown;
outgoing.keyDown = keyDown;
esp_now_send(peerMAC, (uint8_t *)&outgoing, sizeof(outgoing));
}
delay(10);
}
Now that we got them connected to each other, we could start sending messages in Morse code. Mckinnon was sending me a message, and I had to decode it.
And here is another message, see if you can decode it.
