Networking and Communications¶
Goals¶
For the group work this week, we have to:
- send a message between two projects.
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(){}
pic of mac address
include ¶
include ¶
// — 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); }
same script goes to esp #2 but with the esp32 1’s mac address.