#include <WiFi.h>
#include <WiFiUdp.h>
// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// Define the UDP port
const int udpPort = 1234;
// Create a UDP instance
WiFiUDP udp;
// Pin where the LED is connected
//ledPower = 5v; //Red
const int ledON = 18; // On while board is on - Yellow
const int ledRUNNING = 17; // On while code is running. - Green
const int ledINTERENET = 4; // Internet is connected. - Blue
const int ledSIGNAL = 2; // Internet is connected. - White
const int ledERROR = 0; // Error has occured - Red
const int ledOUTPUT = 33; // Change this to your LED pin
void setup() {
// Set the LED pin as output
pinMode(ledON, OUTPUT);
digitalWrite(ledON, HIGH);
pinMode(ledRUNNING, OUTPUT);
digitalWrite(ledRUNNING, LOW);
pinMode(ledINTERNET, OUTPUT);
digitalWrite(ledINTERNET, LOW);
pinMode(ledSIGNAL, OUTPUT);
digitalWrite(ledSIGNAL, LOW);
pinMode(ledERROR, OUTPUT);
digitalWrite(ledERROR, LOW);
pinMode(ledOUTPUT, OUTPUT);
digitalWrite(ledOUTPUT, LOW);
// Start the Serial communication
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
digitalWrite(ledINTERNET, HIGH);
// Start listening for UDP packets
udp.begin(udpPort);
Serial.printf("Listening for UDP packets on port %d\n", udpPort);
}
void loop() {
digitalWrite(ledRUNNING, HIGH);
// Check if there are any incoming UDP packets
int packetSize = udp.parsePacket();
if (packetSize) {
// Read the packet
char incomingPacket[255];
int len = udp.read(incomingPacket, 255);
if (len > 0) {
incomingPacket[len] = '\0'; // Null-terminate the string
}
// Convert the incoming packet to an integer
int ledIntensity = atoi(incomingPacket);
// Ensure the value is between 0 and 255
if (ledIntensity < 0) ledIntensity = 0;
if (ledIntensity > 255) ledIntensity = 255;
// Set the LED intensity
analogWrite(ledOUTPUT, ledIntensity);
Serial.printf("Received: %d, LED Intensity set to: %d\n", ledIntensity, ledIntensity);
}
// Wait a bit before scanning again
digitalWrite(ledRUNNING, HIGH);
delay(100);
digitalWrite(ledRUNNING, LOW);
}