// uart-slave-green.ino — Node 2 (slave B), XIAO ESP32-S3 // Week 11 — same logic as slave A but NID 2 and green LED. The ESP32-S3 needs // Serial1 remapped to its UART pins (RX = GPIO44, TX = GPIO43). #define MY_NID 2 // this board is NID 2 (green) #define NID_MASTER 0 #define FRAME_START 0xFF #define CMD_LED 0x03 #define LED_PIN 2 // GPIO2 (D1), green LED via 220 ohm to GND #define UART_RX 44 // GPIO44 (D7) #define UART_TX 43 // GPIO43 (D6) - unused here, declared for completeness uint8_t buf[6]; uint8_t idx = 0; void handleFrame() { uint8_t dest = buf[1], src = buf[2], cmd = buf[3], payload = buf[4], chk = buf[5]; if ((uint8_t)(dest ^ src ^ cmd ^ payload) != chk) { Serial.println("RX bad checksum, frame dropped"); return; } if (dest != MY_NID) { Serial.print("RX frame for NID "); Serial.print(dest); Serial.println(" - not me, ignored"); return; } if (cmd == CMD_LED) { digitalWrite(LED_PIN, payload ? HIGH : LOW); Serial.print("RX DEST_NID=2 (me) LED="); Serial.println(payload ? "ON" : "OFF"); } } void setup() { Serial.begin(9600); // USB monitor Serial1.begin(9600, SERIAL_8N1, UART_RX, UART_TX); // UART bus pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); Serial.println("Slave B (NID 2, green LED) ready."); } void loop() { while (Serial1.available()) { uint8_t b = Serial1.read(); if (idx == 0 && b != FRAME_START) continue; // resync: wait for START buf[idx++] = b; if (idx == 6) { idx = 0; handleFrame(); } } }