/* //######################################################################################## Mini-Muelltone mit WEMOS D1 Mini via MQTT Hardware: WEMOS D1 Mini V 1.01 30.01.2023 * __________________________________________________________________________ * ########################################################################## * Achtung ! * Achtung ! * Achtung ! * Compiliert mit Arduino-IDE V 1.8.19 * und esp8266-core V2.7.1 ( ab esp-core V 3.0.0 laeuft Sketch auf Fehler!) * ########################################################################## * __________________________________________________________________________ */ #include #include #include // fuer Flashen ueber WLAN ( OTA = Over The Air) #include #define FASTLED_ALLOW_INTERRUPTS 0 // reduces flickering #define LED_PIN 13 //D7 #define NUM_LEDS 8 #define BRIGHTNESS 180 #define COLOR_ORDER RGB CRGB leds[NUM_LEDS]; uint8_t gHue = 0; unsigned long currentMillis = 0; unsigned long previousMillis = 0; int i =0; char Data[1000]; // room for 1000 //######################################################################################## // Hier die eigenen Netzwerk-Parameter eintragen !!! IPAddress ip(xxx, xxx, xxx, xxx); //### statische ip-addresse IPAddress gateway(xxx, xxx, xxx, xxx); //### router IPAddress subnet(255, 255, 255, 0); //### subnet-mask IPAddress dns(xxx, xxx, xxx, xxx); //### dns-server = router const char* ssid = "xxxxxxxxxx"; //### WLAN SSID const char* pwd = "xxxxxxxxxx"; //### WLAN Password const char* mqtt_server = "192.168.192.29"; //### IP-Adresse MQTT-Adapter in ioBroker (auf die eigenen Gegebenheiten anpassen!) int mqtt_port = 1883; //### MQTT-Port in ioBroker (muss evtl. angepasst werden. wenn mehrere MQTT-Broker laufen!) const char* OTA_host = "Mini-Muelltonne"; // Name des virtuellen Ports in der Arduino-IDE, fuer OTA-Flashing //######################################################################################## WiFiClient MiniMuelltonne; PubSubClient client(MiniMuelltonne); //_________________________________________________________________________________________ // Subroutine fuer WLAN-Verbindung void setup_wifi(){ WiFi.config(ip, gateway, subnet, dns); delay(100); WiFi.mode(WIFI_STA); WiFi.begin(); Serial.println(""); Serial.println("___________________________________________________"); Serial.print("Connecting to "); Serial.println(ssid); while (WiFi.status() != WL_CONNECTED) { FastLED.setBrightness(BRIGHTNESS); sinelon(); FastLED.show(); FastLED.delay(1); EVERY_N_MILLISECONDS( 10 ) { gHue++; } // slowly cycle the "base color" through the rainbow Serial.print("."); } Serial.println(""); Serial.print(" OK ---> "); Serial.print("IP: "); Serial.println(WiFi.localIP()); for(int i=0; i<=NUM_LEDS-1; i++){ //alle LEDS aus, wenn WLAN verbunden ist leds[i] = CRGB( 0, 0, 0); FastLED.show(); } } // end of void connectWIFI //_________________________________________________________________________________________ void sinelon(){ fadeToBlackBy( leds, NUM_LEDS, 5); //6 int pos = beatsin16( 20, 0, NUM_LEDS-1 ); //14 leds[pos] += CHSV( gHue, 255, 192); } //_________________________________________________________________________________________ void MQTTCallback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); String ColorName = ""; for (int i = 0; i < length; i++) { ColorName = ColorName + (char)payload[i]; } Serial.print(" Color: "); Serial.print(ColorName); String TopicName = topic; TopicName.replace("javascript/0/MiniMuelltonne/led_",""); int LedIndex = TopicName.toInt(); LedIndex = LedIndex - 1; Serial.println(""); if (ColorName.equals("red")) { leds[LedIndex] = CRGB( 255, 0, 0); } if (ColorName.equals("green")) { leds[LedIndex] = CRGB( 0, 255, 0); } if (ColorName.equals("blue")) { leds[LedIndex] = CRGB( 0, 0, 255); } if (ColorName.equals("tuerkis")) { leds[LedIndex] = CRGB( 0, 255, 255); } if (ColorName.equals("violet")) { leds[LedIndex] = CRGB( 255, 0, 255); } if (ColorName.equals("yellow")) { leds[LedIndex] = CRGB( 255, 110, 0); } if (ColorName.equals("orange")) { leds[LedIndex] = CRGB( 255, 40, 0); } if (ColorName.equals("white")) { leds[LedIndex] = CRGB( 255, 255, 255);} if (ColorName.equals("off")) { leds[LedIndex] = CRGB( 0, 0, 0); } //__________ Gelber Sack gelb blinken ________________________ if ( LedIndex == 0 && ColorName.equals("yellow")) { Data[7] = payload[0]; //y Data[8] = payload[1]; //e Data[9] = payload[2]; //l } else if ( LedIndex == 0 && ColorName.equals("off")) { Data[7] = payload[0]; Data[8] = payload[1]; Data[9] = payload[2]; } //__________ Papiertonne blau blinken ________________________ if ( LedIndex == 1 && ColorName.equals("blue")) { Data[10] = payload[0]; //b Data[11] = payload[1]; //l Data[12] = payload[2]; //u } else if ( LedIndex == 1 && ColorName.equals("off")) { Data[10] = payload[0]; Data[11] = payload[1]; Data[12] = payload[2]; } //__________ Restmuell orange blinken ________________________ if ( LedIndex == 2 && ColorName.equals("orange")) { Data[13] = payload[0]; //o Data[14] = payload[1]; //r Data[15] = payload[2]; //a } else if ( LedIndex == 2 && ColorName.equals("off")) { Data[13] = payload[0]; Data[14] = payload[1]; Data[15] = payload[2]; } //__________ Biomuell gruen blinken ________________________ if ( LedIndex == 3 && ColorName.equals("green")) { Data[0] = payload[0]; //g Data[1] = payload[1]; //r Data[2] = payload[2]; //e } else if ( LedIndex == 3 && ColorName.equals("off")) { Data[0] = payload[0]; //o Data[1] = payload[1]; //f Data[2] = payload[2]; //f } FastLED.show(); } // end of void MQTTCallback //________________________________________________________________________ void reconnect() { while (!client.connected()) { FastLED.setBrightness(BRIGHTNESS); for(int i=0; i<=NUM_LEDS-1; i++){ //alle LEDS leuchten rot, wenn noch nicht mit MQTT verbunden leds[i] = CRGB( 255, 0, 0); FastLED.show(); } Serial.print("Attempting MQTT connection..."); // Create client ID String clientId = "Mini-Muelltonne"; delay(2000); // Attempt to connect if (client.connect(clientId.c_str())) { for(int i=0; i<=NUM_LEDS-1; i++){ //alle LEDS aus, wenn mit MQTT verbunden leds[i] = CRGB( 0, 0, 0); FastLED.show(); } Serial.println("connected"); client.subscribe("javascript/0/MiniMuelltonne/#"); // alle subscriben } else{ Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 6 seconds"); // Wait 6 seconds before retrying delay(6000); } } // end of while } // end of void reconnect //________________________________________________________________________ void setup() { FastLED.addLeds(leds, NUM_LEDS); FastLED.setBrightness(BRIGHTNESS); FastLED.show(); // alle LEDs aus Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, mqtt_port); // mqtt.0 auf Port 1883 in ioBroker client.setCallback(MQTTCallback); ArduinoOTA.setHostname(OTA_host); // fuer Flashen ueber WLAN ArduinoOTA.begin(); // fuer Flashen ueber WLAN } // end of setup //________________________________________________________________________ void loop(){ ArduinoOTA.handle(); //fuer Flashen ueber WLAN //delay(1); if (!client.connected()) { setup_wifi(); reconnect(); } FastLED.setBrightness(0); FastLED.show(); currentMillis = millis(); // aktuelle Zeit bestimmen // __________ Gelber Sack - gelb pulsieren ____________________________ if( Data[7] == 'y' && Data[8] == 'e' && Data[9] == 'l'){ if (currentMillis - previousMillis < 2500 ) { leds[1] = CRGB( 0, 0, 0); leds[2] = CRGB( 0, 0, 0); leds[3] = CRGB( 0, 0, 0); leds[5] = CRGB( 0, 0, 0); leds[6] = CRGB( 0, 0, 0); leds[7] = CRGB( 0, 0, 0); leds[8] = CRGB( 0, 0, 0); leds[0] = CRGB( 255, 110, 0); leds[4] = CRGB( 255, 110, 0); for ( i=0; i<=220; i++){ delay(6); FastLED[0].show(leds, 1, i); FastLED[4].show(leds, 5, i); } for ( i=220; i>0; i--){ delay(6); FastLED[0].show(leds, 1, i); FastLED[4].show(leds, 5, i); } } } // __________ Papiertonne - blau pulsieren ____________________________ if( Data[10] == 'b' && Data[11] == 'l' && Data[12] == 'u'){ if (currentMillis - previousMillis < 2500) { leds[0] = CRGB( 0, 0, 0); leds[2] = CRGB( 0, 0, 0); leds[3] = CRGB( 0, 0, 0); leds[4] = CRGB( 0, 0, 0); leds[6] = CRGB( 0, 0, 0); leds[7] = CRGB( 0, 0, 0); leds[8] = CRGB( 0, 0, 0); leds[1] = CRGB( 0, 0, 255); leds[5] = CRGB( 0, 0, 255); for ( i=25; i<=220; i++){ delay(6); FastLED[1].show(leds, 2, i); FastLED[5].show(leds, 6, i); } for ( i=220; i>25; i--){ delay(6); FastLED[1].show(leds, 2, i); FastLED[5].show(leds, 6, i); } } } // __________ Restmuell - orange pulsieren ____________________________ if( Data[13] == 'o' && Data[14] == 'r' && Data[15] == 'a'){ if (currentMillis - previousMillis < 2500) { leds[0] = CRGB( 0, 0, 0); leds[1] = CRGB( 0, 0, 0); leds[3] = CRGB( 0, 0, 0); leds[4] = CRGB( 0, 0, 0); leds[5] = CRGB( 0, 0, 0); leds[7] = CRGB( 0, 0, 0); leds[8] = CRGB( 0, 0, 0); leds[2] = CRGB( 255, 40, 0); leds[6] = CRGB( 255, 40, 0); for ( i=25; i<=220; i++){ delay(6); FastLED[2].show(leds, 3, i); FastLED[6].show(leds, 7, i); } for ( i=220; i>25; i--){ delay(6); FastLED[2].show(leds, 3, i); FastLED[5].show(leds, 7, i); } } } // __________ Biomuell - gruen pulsieren ____________________________ if( Data[0] == 'g' && Data[1] == 'r' && Data[2] == 'e'){ if (currentMillis - previousMillis < 2500) { leds[0] = CRGB( 0, 0, 0); leds[1] = CRGB( 0, 0, 0); leds[2] = CRGB( 0, 0, 0); leds[4] = CRGB( 0, 0, 0); leds[5] = CRGB( 0, 0, 0); leds[6] = CRGB( 0, 0, 0); leds[8] = CRGB( 0, 0, 0); leds[3] = CRGB( 0, 255, 0); leds[7] = CRGB( 0, 255, 0); for ( i=25; i<=220; i++){ delay(6); FastLED[3].show(leds, 4, i); FastLED[7].show(leds, 8, i); } for ( i=220; i>25; i--){ delay(6); FastLED[3].show(leds, 4, i); FastLED[7].show(leds, 8, i); } } } // __________________________________________________________ if (currentMillis - previousMillis >= 2495) { previousMillis = currentMillis; // Schaltzeitpunkt speichern FastLED.setBrightness(0); for ( i=0; i<=7; i++){ leds[i] = CRGB( 0, 0, 0); FastLED[i].show(leds, i+1, 0); } } delay(1); client.loop(); } // end of loop //________________________________________________________________________