#include #include #include #include #define LED_PIN 5 #define NUM_LEDS 30 #define BRIGHTNESS 64 #define LED_TYPE WS2811 #define COLOR_ORDER GRB CRGB leds[NUM_LEDS]; #ifndef STASSID #define STASSID "MY_WIFI" #define STAPSK "MY_PASSWORD" #endif // udp port and buffer to hold pattern unsigned int localPort = 31337; String incoming = String(1024); int step = 0; bool initialized = false; const char *ssid = STASSID; const char *password = STAPSK; WiFiUDP udp; // state machine variables enum lightStates { CONNECTING, CONNECTED }; int currentState; // initialization void setup() { // initialize leds FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness(16); // set up serial Serial.begin(115200); Serial.print("Starting street light"); // light test and set current state currentState = CONNECTING; red(); yellow(); green(); show(); // set wifi connection WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); } void clear() { for (int a = 0; a < NUM_LEDS; a++) { leds[a] = CRGB::Black; } } void green() { for (int a = 0; a < 10; a++) { leds[a] = CRGB::Green; } } void yellow() { for (int a = 10; a < 20; a++) { //leds[a] = CRGB::Yellow; leds[a].setRGB(255, 192, 0); } } void red() { for (int a = 20; a < 31; a++) { leds[a] = CRGB::Red; } } void show() { FastLED.show(); } void showInitialized() { clear(); green(); show(); delay(250); clear(); show(); delay(250); green(); show(); delay(250); clear(); show(); delay(250); green(); show(); delay(250); clear(); show(); } void blinkAll() { red(); yellow(); green(); show(); delay(250); clear(); show(); delay(250); } bool readPackets() { int packetSize = udp.parsePacket(); if (packetSize) { Serial.println(""); Serial.printf("got %d bypes, from %s", packetSize, udp.remoteIP().toString().c_str()); char buffer[1024]; int len = udp.read(buffer, 1024); if (len > 0) { incoming[len] = 0; } Serial.printf("Contents: %s", buffer); incoming = buffer; return true; } return false; } void showStep() { Serial.printf("new step %s %d", incoming.c_str(), step); String color = String(incoming[step * 2]); String timeout = String(incoming[step * 2 + 1]); step += 1; if (String(incoming[step]) == String("")) { step = 0; } Serial.println(""); Serial.printf("setting color %s with delay %s", color.c_str(), timeout.c_str()); // set color clear(); if (color == "R") { red(); } if (color == "Y") { yellow(); } if (color == "G") { green(); } if (color == "C") { clear(); } if (color == "E") { step = 0; return; } if (color == "1") { FastLED.setBrightness(16); return; } if (color == "2") { FastLED.setBrightness(32); return; } if (color == "3") { FastLED.setBrightness(64); return; } if (color == "4") { FastLED.setBrightness(128); return; } if (color == "5") { FastLED.setBrightness(255); return; } show(); // delay periods if (timeout == "1") { delay(100); return; } if (timeout == "2") { delay(500); return; } if (timeout == "3") { delay(1000); return; } if (timeout == "4") { delay(2500); return; } if (timeout == "5") { delay(5000); return; } return; } void loop() { switch (currentState) { case CONNECTING: while (WiFi.status() != WL_CONNECTED) { blinkAll(); } // show connected Serial.println(""); Serial.println("WiFi connected"); // ubp connected udp.begin(localPort); Serial.printf("IP Address %s:%d\n", WiFi.localIP().toString().c_str(), localPort); currentState = CONNECTED; break; case CONNECTED: if (readPackets()) { initialized = true; step = 0; } if (initialized) { showStep(); } else { yellow(); show(); delay(500); clear(); show(); delay(500); } break; } // check state if (WiFi.status() != WL_CONNECTED) { currentState = CONNECTING; } }