#include <WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_NeoPixel.h>
// === WiFi Credentials ===
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// === NeoPixel LED Strip Configuration ===
#define NEOPIXEL_PIN 21
#define NUM_PIXELS 8
Adafruit_NeoPixel pixels(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
// === UDP Configuration ===
const int UDP_PORT = 12345; // Choose an arbitrary port
WiFiUDP Udp;
char incomingPacket[255]; // Buffer for incoming data
void setup() {
Serial.begin(115200);
while (!Serial);
pixels.begin();
pixels.clear();
pixels.show();
Serial.println("\nConnecting to WiFi (Secondary Board - UDP Listener)...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nSecondary Board WiFi Connected!");
Serial.print("Secondary Board IP Address: ");
Serial.println(WiFi.localIP());
Udp.begin(UDP_PORT);
Serial.printf("Now listening for UDP packets on port %d\n", UDP_PORT);
} else {
Serial.println("\nFailed to connect Secondary Board to WiFi!");
while (true) {
delay(1000);
}
}
// Flash NeoPixels green briefly to indicate ready
for (int i = 0; i < NUM_PIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 255, 0)); // Green
}
pixels.show();
delay(1000);
pixels.clear();
pixels.show();
}
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize) {
// Read the packet into incomingPacket buffer
int len = Udp.read(incomingPacket, 255);
if (len > 0) {
incomingPacket[len] = 0; // Null-terminate the string
}
Serial.print("Received UDP from ");
Serial.print(Udp.remoteIP());
Serial.print(":");
Serial.print(Udp.remotePort());
Serial.println();
Serial.printf("Packet contents: %s\n", incomingPacket);
// --- Parse and act on received data (simplified for UDP) ---
// In a real scenario, you'd parse JSON or a delimited string
// For this example, let's assume the string directly contains the command
String receivedString = String(incomingPacket);
if (receivedString.indexOf("GAS_ALERT") != -1) {
for (int i = 0; i < NUM_PIXELS; i++) pixels.setPixelColor(i, pixels.Color(255, 100, 0)); // Orange
} else if (receivedString.indexOf("SMOKE_ALERT") != -1) {
for (int i = 0; i < NUM_PIXELS; i++) pixels.setPixelColor(i, pixels.Color(255, 150, 0)); // Lighter Orange/Yellow
} else if (receivedString.indexOf("FIRE_ALERT") != -1) {
for (int i = 0; i < NUM_PIXELS; i++) pixels.setPixelColor(i, pixels.Color(255, 0, 0)); // Red
} else if (receivedString.indexOf("FIRE_EXT_ACTIVE") != -1) {
for (int i = 0; i < NUM_PIXELS; i++) pixels.setPixelColor(i, pixels.Color(0, 0, 255)); // Blue
} else if (receivedString.indexOf("SYSTEM_SAFE") != -1 || receivedString.indexOf("STATUS_UPDATE") != -1) {
for (int i = 0; i < NUM_PIXELS; i++) pixels.setPixelColor(i, pixels.Color(0, 255, 0)); // Green
} else {
for (int i = 0; i < NUM_PIXELS; i++) pixels.setPixelColor(i, 0); // All pixels off
}
pixels.show();
}
}