From: Nils Forssén Date: Fri, 6 Mar 2026 23:38:47 +0000 (+0100) Subject: initial X-Git-Url: https://gitweb.forssennils.se/sitemap.xml?a=commitdiff_plain;ds=inline;p=disco_box.git initial --- fb61791e737c6a90488ebd310451d696b852873f diff --git a/source/Blink.txt b/source/Blink.txt new file mode 100644 index 0000000..0626334 --- /dev/null +++ b/source/Blink.txt @@ -0,0 +1 @@ +Turn an LED on and off. \ No newline at end of file diff --git a/source/source.ino b/source/source.ino new file mode 100644 index 0000000..3ba4c23 --- /dev/null +++ b/source/source.ino @@ -0,0 +1,316 @@ +/* + Filename: esp32_ws2812_effects.ino + Description: Demonstrates rainbow, color wipe, solid color, and breathing effects using WS2812 LED with ESP32. + Author: www.oceanlabz.in + Modification: 1/4/2025 +*/ + + #include +#include +#include +#include +#include + +#define PIN 18 // Use a safe digital GPIO pin on ESP32 +#define NUM_LEDS 150 // Number of WS2812 LEDs +#define DELAY_TIME 10 // Delay for Rainbow effect +#define BREATH_SPEED 10 // Speed for breathing effect + +Adafruit_NeoPixel strip(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800); + +const char ssid[] = "Pelle Sladdlös"; +const char pass[] = ""; + +const char broker[] = "192.168.0.110"; +const char topic_enable[] = "/disco_box/enable"; +const char topic_animation[] = "/disco_box/animation"; +const char topic_max_brightness[] = "/disco_box/max_brightness"; +const char topic_anim_speed[] = "/disco_box/anim_speed"; +const char topic_color[] = "/disco_box/color"; + +const int port = 1883; + +const uint32_t WHITE = strip.Color(255, 255, 255); +const uint32_t OFF_WHITE = strip.Color(242, 140, 40); +const uint32_t BLUE = strip.Color(0, 0, 255); +const uint32_t CYAN = strip.Color(0, 255, 255); +const uint32_t YELLOW = strip.Color(255, 255, 0); +const uint32_t VIOLET = strip.Color(127,0,255); + +const uint32_t MAGENTA = strip.Color(255, 0, 255); +const uint32_t INIDIGO = strip.Color(86,5,145); + + +const uint32_t GREEN = strip.Color(0, 255, 0); +const uint32_t RED = strip.Color(255, 0, 0); +const uint32_t ORANGE = strip.Color(255, 165, 0); + +const uint32_t OFF = strip.Color(0,0,0); + +WiFiClient wifiClient; +MqttClient mqttClient(wifiClient); + +bool disabled = true; +char current_anim[64] = {0}; +uint32_t current_color = OFF; +float current_max_brightness = 0; +float current_anim_speed = 0; + + + +void setup() { + strip.begin(); + strip.show(); // Initialize all pixels to 'off' + + Serial.begin(115200); + delay(1000); + + WiFi.mode(WIFI_STA); //Optional + WiFi.begin(ssid, pass); + Serial.println("\nConnecting"); + + while(WiFi.status() != WL_CONNECTED){ + Serial.print("."); + delay(100); + } + Serial.println("\nConnected to the WiFi network"); + + + Serial.print("Attempting to connect to the MQTT broker: "); + Serial.println(broker); + + if (!mqttClient.connect(broker, port)) { + Serial.print("MQTT connection failed! Error code = "); + Serial.println(mqttClient.connectError()); + + while (1); + } + + Serial.println("You're connected to the MQTT broker!"); + Serial.println(); + + // set the message receive callback + mqttClient.onMessage(onMqttMessage); + + Serial.println("Subscribing to topics!"); + + // subscribe to a topic + mqttClient.subscribe(topic_enable); + mqttClient.subscribe(topic_animation); + mqttClient.subscribe(topic_max_brightness); + mqttClient.subscribe(topic_anim_speed); + mqttClient.subscribe(topic_color); +} + +void onMqttMessage(int messageSize) { + + auto topic = mqttClient.messageTopic(); + + char message[64]; + int index = 0; + + while (mqttClient.available() && index < 64 - 1) + { + message[index++] = (char)mqttClient.read(); + } + + message[index] = '\0'; // Null-terminate the C-string + + if (topic == topic_enable) + { + Serial.println("enabled case"); + disabled = (strcmp(message, "0") == 0); + } + else if (topic == topic_animation) + { + Serial.println("animation case"); + strcpy(current_anim, message); + } + else if (topic == topic_max_brightness) + { + Serial.println("max_brightness case"); + current_max_brightness = atof(message); + } + else if (topic == topic_anim_speed) + { + Serial.println("anim_speed case"); + current_anim_speed = atof(message); + } + else if (topic == topic_color) + { + if (strcmp(message, "White") == 0) + { + current_color = WHITE; + } + else if (strcmp(message, "Off_White") == 0) + { + current_color = OFF_WHITE; + } + else if (strcmp(message, "Blue") == 0) + { + current_color = BLUE; + } + else if (strcmp(message, "Cyan") == 0) + { + current_color = CYAN; + } + else if (strcmp(message, "Yellow") == 0) + { + current_color = YELLOW; + } + else if (strcmp(message, "Violet") == 0) + { + current_color = VIOLET; + } + else if (strcmp(message, "Indigo") == 0) + { + current_color = INIDIGO; + } + else if (strcmp(message, "Magenta") == 0) + { + current_color = MAGENTA; + } + else if (strcmp(message, "Green") == 0) + { + current_color = GREEN; + } + else if (strcmp(message, "Red") == 0) + { + current_color = RED; + } + else if (strcmp(message, "Orange") == 0) + { + current_color = ORANGE; + } + else + { + Serial.println("Bad color not defined!"); + current_color = OFF; + } + } + else + { + Serial.println("default case"); + } +} + +void loop() { + if (disabled) + { + Serial.println("DISABLED!"); + staticEffect(OFF); + mqttClient.poll(); + delay(100); + } + else if (strcmp(current_anim, "Wave") == 0) + { + colorWipe(300 / (current_anim_speed + 1)); + } + else if (strcmp(current_anim, "Strobe") == 0) + { + strobeEffect(1000 / (current_anim_speed + 1)); + } + else if (strcmp(current_anim, "Static") == 0) + { + staticEffect(current_color); + mqttClient.poll(); + delay(100); + + } + else if (strcmp(current_anim, "Breath") == 0) + { + breathingEffect(100 / (current_anim_speed + 1)); + } + else + { + Serial.println("Unknown animation"); + } +} + + + +// Wipe a color across the LED +void colorWipe(int wait) { + strip.setBrightness((int) current_max_brightness * 255 / 100); + for (int i = 0; i < NUM_LEDS; i++) { + strip.setPixelColor(i, current_color); + strip.show(); + delay(wait); + mqttClient.poll(); + if (disabled) break; + } + for (int i = 0; i < NUM_LEDS; i++) { + strip.setPixelColor(i, OFF); + strip.show(); + delay(wait); + mqttClient.poll(); + if (disabled) break; + } +} + +void staticEffect(uint32_t color) +{ + // Set brightness (assuming current_max_brightness is 0.0 – 1.0) + strip.setBrightness((int)(current_max_brightness * 255 / 100)); + + // Set all LEDs to current_color + for (int i = 0; i < NUM_LEDS; i++) + { + strip.setPixelColor(i, color); + } + + strip.show(); +} + +// Smooth breathing effect +void breathingEffect(int breathSpeed) { + for (int brightness = 0; brightness <= (int) current_max_brightness * 255 / 100; brightness++) { + for (int i = 0; i < NUM_LEDS; i++) + { + strip.setPixelColor(i, current_color); + } + strip.setBrightness(brightness); + strip.show(); + delay(breathSpeed); + mqttClient.poll(); + if (disabled) break; + } + for (int brightness = (int) current_max_brightness * 255 / 100; brightness >= 0; brightness--) { + for (int i = 0; i < NUM_LEDS; i++) + { + strip.setPixelColor(i, current_color); + } + strip.setBrightness(brightness); + strip.show(); + delay(breathSpeed); + mqttClient.poll(); + if (disabled) break; + } +} + +void strobeEffect(int wait) +{ + int brightness = (int)(current_max_brightness * 255 / 100); + + strip.setBrightness(brightness); + for (int i = 0; i < NUM_LEDS; i++) + { + strip.setPixelColor(i, current_color); + } + strip.show(); + + delay(wait); + mqttClient.poll(); + if (disabled) return; + + // OFF + for (int i = 0; i < NUM_LEDS; i++) + { + strip.setPixelColor(i, OFF); + } + strip.show(); + + delay(wait); + mqttClient.poll(); + if (disabled) return; +} \ No newline at end of file