Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <Adafruit_NeoPixel.h>
- #ifdef ESP32
- #include <WiFi.h>
- #else
- #include <ESP8266WiFi.h>
- #endif
- #include "fauxmoESP.h"
- // Rename the credentials.sample.h file to credentials.h and
- // edit it according to your router configuration
- #include "credentials.h"
- fauxmoESP fauxmo;
- // -----------------------------------------------------------------------------
- #define SERIAL_BAUDRATE 115200
- #define LED_PIN D1
- #define LED_COUNT 194
- // -----------------------------------------------------------------------------
- Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
- // -----------------------------------------------------------------------------
- // Wifi
- // -----------------------------------------------------------------------------
- int rb = 0;
- int rt = 0;
- long previousMillis = 0;
- long firstPixelHue = 0;
- int i = 0;
- int a = 0;
- int b = 0;
- int c = 0;
- int hue = 0;
- uint32_t color;
- void wifiSetup() {
- // Set WIFI module to STA mode
- WiFi.mode(WIFI_STA);
- // Connect
- Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- // Wait
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(100);
- }
- Serial.println();
- // Connected!
- Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
- }
- void setup() {
- // Init serial port and clean garbage
- Serial.begin(SERIAL_BAUDRATE);
- Serial.println();
- Serial.println();
- // LEDs
- strip.begin();
- strip.show();
- strip.setBrightness(150);
- // Wifi
- wifiSetup();
- // By default, fauxmoESP creates it's own webserver on the defined port
- // The TCP port must be 80 for gen3 devices (default is 1901)
- // This has to be done before the call to enable()
- fauxmo.createServer(true); // not needed, this is the default value
- fauxmo.setPort(80); // This is required for gen3 devices
- // You have to call enable(true) once you have a WiFi connection
- // You can enable or disable the library at any moment
- // Disabling it will prevent the devices from being discovered and switched
- fauxmo.enable(true);
- // You can use different ways to invoke alexa to modify the devices state:
- // "Alexa, turn yellow lamp on"
- // "Alexa, turn on yellow lamp
- // "Alexa, set yellow lamp to fifty" (50 means 50% of brightness, note, this example does not use this functionality)
- // Add virtual devices
- // fauxmo.addDevice("Bett");
- // fauxmo.addDevice("Weises Bett");
- // fauxmo.addDevice("Rotes Bett");
- // fauxmo.addDevice("Blaues Bett");
- // fauxmo.addDevice("Grünes Bett");
- // fauxmo.addDevice("Gelbes Bett");
- // fauxmo.addDevice("Rosa Bett");
- // fauxmo.addDevice("Türkis Bett");
- // fauxmo.addDevice("Oranges Bett");
- // fauxmo.addDevice("Hellblaues Bett");
- // fauxmo.addDevice("Hellgrünes Bett");
- // fauxmo.addDevice("Hellrotes Bett");
- fauxmo.addDevice("Regenbogenmodus");
- fauxmo.addDevice("Regenbogenreihe");
- fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
- // Callback when a command from Alexa is received.
- // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
- // State is a boolean (ON/OFF) and value a number from 0 to 255 (if you say "set kitchen light to 50%" you will receive a 128 here).
- // Just remember not to delay too much here, this is a callback, exit as soon as possible.
- // If you have to do something more involved here set a flag and process it in your main loop.
- Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
- // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
- // Otherwise comparing the device_name is safer.
- if (strcmp(device_name, "Bett")==0) {
- if(state) {
- strip.fill(strip.Color(255, 255, 255), 0, LED_COUNT);
- } else {
- strip.fill(strip.Color(0, 0, 0), 0, LED_COUNT);
- }
- strip.show();
- } else if (strcmp(device_name, "Weises Bett")==0) {
- strip.fill(strip.Color(255, 255, 255), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Rotes Bett")==0) {
- strip.fill(strip.Color(255, 0, 0), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Blaues Bett")==0) {
- strip.fill(strip.Color(0, 0, 255), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Grünes Bett")==0) {
- strip.fill(strip.Color(0, 255, 0), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Gelbes Bett")==0) {
- strip.fill(strip.Color(255, 255, 0), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Türkis Bett")==0) {
- strip.fill(strip.Color(0, 255, 255), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Rosa Bett")==0) {
- strip.fill(strip.Color(255, 0, 255), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Hellblaues Bett")==0) {
- strip.fill(strip.Color(0, 200, 255), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Hellgrünes Bett")==0) {
- strip.fill(strip.Color(110, 255, 110), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Hellrotes Bett")==0) {
- strip.fill(strip.Color(255, 80, 80), 0, LED_COUNT);
- strip.show();
- } else if (strcmp(device_name, "Regenbogenmodus")==0) {
- if(state) {
- firstPixelHue = 0;
- previousMillis = 0;
- int pixelHue = 0;
- i = 0;
- rt = 0;
- rb = 1;
- } else {
- rb = 0;
- strip.fill(strip.Color(0, 0, 0), 0, LED_COUNT);
- strip.show();
- }
- } else if (strcmp(device_name, "Regenbogenreihe")==0) {
- if(state) {
- firstPixelHue = 0;
- previousMillis = 0;
- a = 0;
- b = 0;
- c = 0;
- hue = 0;
- uint32_t color = 0;
- rb = 0;
- rt = 1;
- } else {
- rt = 0;
- strip.fill(strip.Color(0, 0, 0), 0, LED_COUNT);
- strip.show();
- }
- }
- });
- }
- void loop() {
- // fauxmoESP uses an async TCP server but a sync UDP server
- // Therefore, we have to manually poll for UDP packets
- fauxmo.handle();
- if(rb == 1) {
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > 10) {
- previousMillis = currentMillis;
- if(firstPixelHue < 5*65536) {
- if(i < strip.numPixels()) {
- int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
- strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
- i++;
- }
- strip.show();
- firstPixelHue += 256;
- }
- }
- }
- if(rt == 1) {
- if(a < 30) {
- if(b < 3) {
- strip.clear();
- if(c < strip.numPixels()) {
- int hue = firstPixelHue + c * 65536L / strip.numPixels();
- uint32_t color = strip.gamma32(strip.ColorHSV(hue));
- strip.setPixelColor(c, color);
- c += 3;
- } else {
- strip.show();
- unsigned long currentMillis = millis();
- if(currentMillis - previousMillis > 50) {
- firstPixelHue += 65536 / 90;
- b++;
- c = b;
- }
- }
- } else {
- b = 0;
- a++;
- }
- }
- }
- // This is a sample code to output free heap every 5 seconds
- // This is a cheap way to detect memory leaks
- // If your device state is changed by any other means (MQTT, physical button,...)
- // you can instruct the library to report the new state to Alexa on next request:
- // fauxmo.setState(ID_YELLOW, true, 255);
- }
Add Comment
Please, Sign In to add comment