#include #include #include #define REDPIN1 5 #define GREENPIN1 4 #define BLUEPIN1 13 #define REDPIN2 12 #define GREENPIN2 14 #define BLUEPIN2 16 #define MQTT_TOPIC_START "led1" #define MQTT_USER "" #define MQTT_PASS "" #define MQTT_BROKER "mqtt.example.com" #define MQTT_CLIENT_ID "LED-Schrank-1" const char* SSID = "WIFISSID"; const char* PSK = "WIDIPASS"; WiFiClient espClient; PubSubClient client(espClient); struct LEDStripe { String MOD = "off"; //Which mode is used "off", "fade", "on" String OLD_MOD = "off"; int SPEED = 3000; int RED = 0; int GREEN = 0; int BLUE = 0; int LOOP = 0; int LOOPCOUNT = 0; int MOD_STEP = 0; }; int Sync = 0; //Sync LED2 on the value of LED1 LEDStripe led1; LEDStripe led2; void connectWiFi() { digitalWrite(LED_BUILTIN, LOW); Serial.println("Start WiFi Connection"); WiFi.begin(SSID, PSK); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } digitalWrite(LED_BUILTIN, HIGH); Serial.println(""); Serial.println("WiFi connected"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } //BLink void blink_short() { digitalWrite(LED_BUILTIN, LOW); delay(500); digitalWrite(LED_BUILTIN, HIGH); delay(500); } void blink_long() { digitalWrite(LED_BUILTIN, LOW); delay(1500); digitalWrite(LED_BUILTIN, HIGH); delay(500); } void blink(int status) { if(status == 1) { blink_short(); blink_short(); blink_short(); blink_short(); } } char* getTopicName(String topic) { String topicName = String(MQTT_TOPIC_START)+String(topic); int str_len = topicName.length() + 1; char cc[str_len]; topicName.toCharArray(cc, str_len); return cc; } void connectMQTT() { digitalWrite(LED_BUILTIN, LOW); Serial.print("Reconnecting..."); if (!client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, getTopicName("/status"), 1, true, "0")) { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" retrying in 5 seconds"); digitalWrite(LED_BUILTIN, HIGH); delay(2000); blink(1); delay(3000); } digitalWrite(LED_BUILTIN, HIGH); client.subscribe(getTopicName("/#")); } //Actions void pulse_white_three_times() { LEDStripe tmpLed(); } //Modes //LED Fade void fade(LEDStripe led) { if(led.MOD_STEP == 0) { led.RED ++; led.GREEN = 0; led.BLUE = 255; if(led.RED >= 255) { led.MOD_STEP++; } } if(led.MOD_STEP == 1) { led.RED = 255; led.GREEN = 0; led.BLUE --; if(led.BLUE <= 0) { led.MOD_STEP++; } } if(led.MOD_STEP == 2) { led.RED = 255; led.GREEN ++; led.BLUE = 0; if(led.GREEN >= 255) { led.MOD_STEP++; } } if(led.MOD_STEP == 3) { led.RED --; led.GREEN = 255; led.BLUE = 0; if(led.RED <= 0) { led.MOD_STEP++; } } if(led.MOD_STEP == 4) { led.RED = 0; led.GREEN = 255; led.BLUE ++; if(led.BLUE >= 255) { led.MOD_STEP++; } } if(led.MOD_STEP == 5) { led.RED = 0; led.GREEN --; led.BLUE = 255; if(led.GREEN <= 0) { led.MOD_STEP = 0; } } } void writeToLedStrips(LEDStripe l1, LEDStripe l2) { analogWrite(REDPIN1, l1.RED); analogWrite(REDPIN2, l2.RED); analogWrite(GREENPIN1, l1.GREEN); analogWrite(GREENPIN2, l2.GREEN); analogWrite(BLUEPIN1, l1.BLUE); analogWrite(BLUEPIN2, l2.BLUE); Serial.println("LED1: "+String(l1.RED)+"|"+String(l1.GREEN)+"|"+String(l1.BLUE)); Serial.println("LED2: "+String(l2.RED)+"|"+String(l2.GREEN)+"|"+String(l2.BLUE)); } void callback(char* topic, byte* payload, unsigned int length) { String message = ""; for (int i=0;i