#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 ++; } } if(LED2_MOD == "fade") { if(LED2_LOOP >= LED2_LOOPCOUNT) { fade_led_2(); LED2_LOOP = 0; } else { LED2_LOOP ++; } } 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 ++; } } if(LED1_MODE_STEEP == 1) { LED1_RED = 255; LED1_GREEN = 0; LED1_BLUE --; if(LED1_BLUE <= 0) { LED1_MODE_STEEP ++; } } if(LED1_MODE_STEEP == 2) { LED1_RED = 255; LED1_GREEN ++; LED1_BLUE = 0; if(LED1_GREEN >= 255) { LED1_MODE_STEEP ++; } } if(LED1_MODE_STEEP == 3) { LED1_RED --; LED1_GREEN = 255; LED1_BLUE = 0; if(LED1_RED <= 0) { LED1_MODE_STEEP ++; } } if(LED1_MODE_STEEP == 4) { LED1_RED = 0; LED1_GREEN = 255; LED1_BLUE ++; if(LED1_BLUE >= 255) { LED1_MODE_STEEP ++; } } if(LED1_MODE_STEEP == 5) { LED1_RED = 0; LED1_GREEN --; 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 = LED2_GREEN + 1; LED2_BLUE = 0; if(LED2_GREEN >= 255) { LED2_MODE_STEEP = LED2_MODE_STEEP + 1; } } if(LED2_MODE_STEEP == 3) { LED2_RED = LED2_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; } } }