From 3cf5706eeee85d642fd8a395da255846d023d1bf Mon Sep 17 00:00:00 2001 From: Kekskurse Date: Sun, 12 Jan 2020 02:31:08 +0100 Subject: [PATCH] Stuff --- Readme.md | 20 +++ led-controller.ino | 297 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 317 insertions(+) create mode 100644 Readme.md create mode 100644 led-controller.ino diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..71f45f7 --- /dev/null +++ b/Readme.md @@ -0,0 +1,20 @@ +# LED-Controller +Simple MQTT-LED-Stripe Controller based on https://wiki.keks.cloud/doku.php?id=projects:ledcontroller + +# MQTT Topics +| Topic | Typ | Notice | +|---|---|---| +| led1/status | write-only | Retaines Message and Last will for MQTT Status, 1 = Online, 0 = Offline | +| led1/sync | read-only | Ignore all Setting for LED2 and mirrow LED1 color to the secound stripe, 1 = On, 0 = OFF | +| led1/1/mode | read-only | Set the Mode of LED1 | +| led1/2/mode | read-only | Set the Mode of LED2 | +| led1/1/speed | read-only | How fast the changes of the color will made, bigger number will be slower changes, for LED1 | +| led1/2/speed | read-only | How fast the changes of the color will made, bigger number will be slower changes, for LED2 | +| led1/1/color | read-only | Set Color for LED1, RRRGGGBBB, each color is a int between 000 and 255 | +| led1/2/color | read-only | Set Color for LED2, RRRGGGBBB, each color is a int between 000 and 255 | + +# MODE + +* on = Just one color, color can changed via MQTT +* fade = Fade +* off = Off (color 000000000) diff --git a/led-controller.ino b/led-controller.ino new file mode 100644 index 0000000..a409c34 --- /dev/null +++ b/led-controller.ino @@ -0,0 +1,297 @@ +#include +#include + +#define REDPIN1 5 +#define GREENPIN1 4 +#define BLUEPIN1 13 + +#define REDPIN2 12 +#define GREENPIN2 14 +#define BLUEPIN2 16 + +const char* SSID = "WIFISSID"; +const char* PSK = "WIDIPASS"; +const char* MQTT_BROKER = "mqtt.hostname.de"; +const char* MQTT_USER = ""; +const char* MQTT_PASS = ""; + +String LED1_MOD = "off"; +int LED1_SPEED = 10; +int LED1_RED = 0; +int LED1_GREEN = 0; +int LED1_BLUE = 0; +int LED1_LOOP = 0; +int LED1_LOOPCOUNT = 0; +int LED1_MODE_STEEP = 0; + +String LED2_MOD = "off"; +int LED2_SPEED = 10; +int LED2_RED = 0; +int LED2_GREEN = 0; +int LED2_BLUE = 0; +int LED2_LOOP = 0; +int LED2_LOOPCOUNT = 0; +int LED2_MODE_STEEP = 0; + +int LED_SYNC = 0; + +WiFiClient espClient; +PubSubClient client(espClient); + +void setup() { + //Turn LED ON + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); + + Serial.begin(115200); + Serial.println(); + + //LED Stripe Pins + analogWrite(REDPIN1, 0); + analogWrite(REDPIN2, 0); + analogWrite(GREENPIN1, 0); + analogWrite(GREENPIN2, 0); + analogWrite(BLUEPIN1, 0); + analogWrite(BLUEPIN2, 0); + + //Connecting to WiFi + Serial.print("Connecting to "); + Serial.println(SSID); + + WiFi.begin(SSID, PSK); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + //Prepair MQTT Connection + client.setServer(MQTT_BROKER, 1883); + client.setCallback(callback); + + //Turn LED OFF + digitalWrite(LED_BUILTIN, HIGH); +} + +//Connect to MQTT +void reconnect() { + while (!client.connected()) { + digitalWrite(LED_BUILTIN, LOW); + Serial.print("Reconnecting..."); + if (!client.connect("ESP8266Client-LED-Controller-Schrank", "user", "password", "led1/status", 1, true, "0")) { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" retrying in 5 seconds"); + delay(5000); + } + client.subscribe("led1/#"); + } + client.publish("led1/status", "1", true); + digitalWrite(LED_BUILTIN, HIGH); +} +int loopDebugCount = 0; +void loop() { + if (!client.connected()) { + reconnect(); + } + client.loop(); + + if(LED1_MOD == "off") { + LED1_RED = 0; + LED1_GREEN = 0; + LED1_BLUE = 0; + } + if(LED2_MOD == "off") { + LED2_RED = 0; + LED2_GREEN = 0; + LED2_BLUE = 0; + } + + if(LED1_MOD == "fade") { + if(LED1_LOOP >= LED1_LOOPCOUNT) + { + fade_led_1(); + LED1_LOOP = 0; + } else { + LED1_LOOP = LED1_LOOP + 1; + } + } + if(LED2_MOD == "fade") { + if(LED2_LOOP >= LED2_LOOPCOUNT) + { + fade_led_2(); + LED2_LOOP = 0; + } else { + LED2_LOOP = LED2_LOOP + 1; + } + } + + if(LED_SYNC==1) { + LED2_RED = LED1_RED; + LED2_GREEN = LED1_GREEN; + LED2_BLUE = LED1_BLUE; + } + + analogWrite(REDPIN1, LED1_RED); + analogWrite(REDPIN2, LED2_RED); + analogWrite(GREENPIN1, LED1_GREEN); + analogWrite(GREENPIN2, LED2_GREEN); + analogWrite(BLUEPIN1, LED1_BLUE); + analogWrite(BLUEPIN2, LED2_BLUE); + /*if(loopDebugCount >= 200) { + char cc[20]; + String("LED1:"+String(LED1_RED)+":"+String(LED1_GREEN)+":"+String(LED1_BLUE)).toCharArray(cc, 20); + client.publish("led1/debug", cc); + String("LED2:"+String(LED2_RED)+":"+String(LED2_GREEN)+":"+String(LED2_BLUE)).toCharArray(cc, 20); + client.publish("led1/debug", cc); + loopDebugCount = 0; + } else { + loopDebugCount = loopDebugCount + 1; + }*/ + + + //delay(10); +} +void callback(char* topic, byte* payload, unsigned int length) { + String message = ""; + for (int i=0;i= 255) { + LED1_MODE_STEEP = LED1_MODE_STEEP + 1; + } + } + if(LED1_MODE_STEEP == 1) { + LED1_RED = 255; + LED1_GREEN = 0; + LED1_BLUE = LED1_BLUE - 1; + if(LED1_BLUE <= 0) { + LED1_MODE_STEEP = LED1_MODE_STEEP + 1; + } + } + if(LED1_MODE_STEEP == 2) { + LED1_RED = 255; + LED1_GREEN = LED1_GREEN + 1; + LED1_BLUE = 0; + if(LED1_GREEN >= 255) { + LED1_MODE_STEEP = LED1_MODE_STEEP + 1; + } + } + if(LED1_MODE_STEEP == 3) { + LED1_RED = LED1_RED - 1; + LED1_GREEN = 255; + LED1_BLUE = 0; + if(LED1_RED <= 0) { + LED1_MODE_STEEP = LED1_MODE_STEEP + 1; + } + } + if(LED1_MODE_STEEP == 4) { + LED1_RED = 0; + LED1_GREEN = 255; + LED1_BLUE = LED1_BLUE + 1; + if(LED1_BLUE >= 255) { + LED1_MODE_STEEP = LED1_MODE_STEEP + 1; + } + } + if(LED1_MODE_STEEP == 5) { + LED1_RED = 0; + LED1_GREEN = LED1_GREEN - 1; + LED1_BLUE = 255; + if(LED1_GREEN <= 0) { + LED1_MODE_STEEP = 0; + } + } +} +void fade_led_2() { + if(LED2_MODE_STEEP == 0) { + LED2_RED = LED2_RED + 1; + LED2_GREEN = 0; + LED2_BLUE = 255; + if(LED2_RED >= 255) { + LED2_MODE_STEEP = LED2_MODE_STEEP + 1; + } + } + if(LED2_MODE_STEEP == 1) { + LED2_RED = 255; + LED2_GREEN = 0; + LED2_BLUE = LED2_BLUE - 1; + if(LED2_BLUE <= 0) { + LED2_MODE_STEEP = LED2_MODE_STEEP + 1; + } + } + if(LED2_MODE_STEEP == 2) { + LED2_RED = 255; + LED2_GREEN = LED1_GREEN + 1; + LED2_BLUE = 0; + if(LED2_GREEN >= 255) { + LED2_MODE_STEEP = LED2_MODE_STEEP + 1; + } + } + if(LED2_MODE_STEEP == 3) { + LED2_RED = LED1_RED - 1; + LED2_GREEN = 255; + LED2_BLUE = 0; + if(LED2_RED <= 0) { + LED2_MODE_STEEP = LED2_MODE_STEEP + 1; + } + } + if(LED2_MODE_STEEP == 4) { + LED2_RED = 0; + LED2_GREEN = 255; + LED2_BLUE = LED2_BLUE + 1; + if(LED2_BLUE >= 255) { + LED2_MODE_STEEP = LED2_MODE_STEEP + 1; + } + } + if(LED2_MODE_STEEP == 5) { + LED2_RED = 0; + LED2_GREEN = LED2_GREEN - 1; + LED2_BLUE = 255; + if(LED2_GREEN <= 0) { + LED2_MODE_STEEP = 0; + } + } +}