Guest User

Arduino Alexa Code

a guest
Jul 13th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.33 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Adafruit_NeoPixel.h>
  3. #ifdef ESP32
  4.     #include <WiFi.h>
  5. #else
  6.     #include <ESP8266WiFi.h>
  7. #endif
  8. #include "fauxmoESP.h"
  9.  
  10. // Rename the credentials.sample.h file to credentials.h and
  11. // edit it according to your router configuration
  12. #include "credentials.h"
  13.  
  14. fauxmoESP fauxmo;
  15.  
  16. // -----------------------------------------------------------------------------
  17.  
  18. #define SERIAL_BAUDRATE     115200
  19.  
  20. #define LED_PIN    D1
  21. #define LED_COUNT 194
  22.  
  23. // -----------------------------------------------------------------------------
  24. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  25. // -----------------------------------------------------------------------------
  26. // Wifi
  27. // -----------------------------------------------------------------------------
  28.  
  29. int rb = 0;
  30. int rt = 0;
  31. long previousMillis = 0;
  32. long firstPixelHue = 0;
  33. int i = 0;
  34. int a = 0;
  35. int b = 0;
  36. int c = 0;
  37. int hue = 0;
  38. uint32_t color;
  39.  
  40. void wifiSetup() {
  41.  
  42.     // Set WIFI module to STA mode
  43.     WiFi.mode(WIFI_STA);
  44.  
  45.     // Connect
  46.     Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  47.     WiFi.begin(WIFI_SSID, WIFI_PASS);
  48.  
  49.     // Wait
  50.     while (WiFi.status() != WL_CONNECTED) {
  51.         Serial.print(".");
  52.         delay(100);
  53.     }
  54.     Serial.println();
  55.  
  56.     // Connected!
  57.     Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
  58.  
  59. }
  60.  
  61. void setup() {
  62.  
  63.     // Init serial port and clean garbage
  64.     Serial.begin(SERIAL_BAUDRATE);
  65.     Serial.println();
  66.     Serial.println();
  67.  
  68.     // LEDs
  69.     strip.begin();
  70.     strip.show();
  71.     strip.setBrightness(150);
  72.  
  73.     // Wifi
  74.     wifiSetup();
  75.  
  76.     // By default, fauxmoESP creates it's own webserver on the defined port
  77.     // The TCP port must be 80 for gen3 devices (default is 1901)
  78.     // This has to be done before the call to enable()
  79.     fauxmo.createServer(true); // not needed, this is the default value
  80.     fauxmo.setPort(80); // This is required for gen3 devices
  81.  
  82.     // You have to call enable(true) once you have a WiFi connection
  83.     // You can enable or disable the library at any moment
  84.     // Disabling it will prevent the devices from being discovered and switched
  85.     fauxmo.enable(true);
  86.  
  87.     // You can use different ways to invoke alexa to modify the devices state:
  88.     // "Alexa, turn yellow lamp on"
  89.     // "Alexa, turn on yellow lamp
  90.     // "Alexa, set yellow lamp to fifty" (50 means 50% of brightness, note, this example does not use this functionality)
  91.  
  92.     // Add virtual devices
  93.     // fauxmo.addDevice("Bett");
  94.     // fauxmo.addDevice("Weises Bett");
  95.     // fauxmo.addDevice("Rotes Bett");
  96.     // fauxmo.addDevice("Blaues Bett");
  97.     // fauxmo.addDevice("Grünes Bett");
  98.     // fauxmo.addDevice("Gelbes Bett");
  99.     // fauxmo.addDevice("Rosa Bett");
  100.     // fauxmo.addDevice("Türkis Bett");
  101.     // fauxmo.addDevice("Oranges Bett");
  102.     // fauxmo.addDevice("Hellblaues Bett");
  103.     // fauxmo.addDevice("Hellgrünes Bett");
  104.     // fauxmo.addDevice("Hellrotes Bett");
  105.     fauxmo.addDevice("Regenbogenmodus");
  106.     fauxmo.addDevice("Regenbogenreihe");
  107.  
  108.     fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
  109.        
  110.         // Callback when a command from Alexa is received.
  111.         // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
  112.         // 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).
  113.         // Just remember not to delay too much here, this is a callback, exit as soon as possible.
  114.         // If you have to do something more involved here set a flag and process it in your main loop.
  115.        
  116.         Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
  117.  
  118.         // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
  119.         // Otherwise comparing the device_name is safer.
  120.  
  121.         if (strcmp(device_name, "Bett")==0) {
  122.           if(state) {
  123.             strip.fill(strip.Color(255, 255, 255), 0, LED_COUNT);
  124.           } else {
  125.             strip.fill(strip.Color(0, 0, 0), 0, LED_COUNT);
  126.           }
  127.           strip.show();
  128.         } else if (strcmp(device_name, "Weises Bett")==0) {
  129.             strip.fill(strip.Color(255, 255, 255), 0, LED_COUNT);
  130.             strip.show();
  131.         } else if (strcmp(device_name, "Rotes Bett")==0) {
  132.             strip.fill(strip.Color(255, 0, 0), 0, LED_COUNT);
  133.             strip.show();
  134.         } else if (strcmp(device_name, "Blaues Bett")==0) {
  135.             strip.fill(strip.Color(0, 0, 255), 0, LED_COUNT);
  136.             strip.show();
  137.         } else if (strcmp(device_name, "Grünes Bett")==0) {
  138.             strip.fill(strip.Color(0, 255, 0), 0, LED_COUNT);
  139.             strip.show();
  140.         } else if (strcmp(device_name, "Gelbes Bett")==0) {
  141.             strip.fill(strip.Color(255, 255, 0), 0, LED_COUNT);
  142.             strip.show();
  143.         } else if (strcmp(device_name, "Türkis Bett")==0) {
  144.             strip.fill(strip.Color(0, 255, 255), 0, LED_COUNT);
  145.             strip.show();
  146.         } else if (strcmp(device_name, "Rosa Bett")==0) {
  147.             strip.fill(strip.Color(255, 0, 255), 0, LED_COUNT);
  148.             strip.show();
  149.         } else if (strcmp(device_name, "Hellblaues Bett")==0) {
  150.             strip.fill(strip.Color(0, 200, 255), 0, LED_COUNT);
  151.             strip.show();
  152.         } else if (strcmp(device_name, "Hellgrünes Bett")==0) {
  153.             strip.fill(strip.Color(110, 255, 110), 0, LED_COUNT);
  154.             strip.show();
  155.         } else if (strcmp(device_name, "Hellrotes Bett")==0) {
  156.             strip.fill(strip.Color(255, 80, 80), 0, LED_COUNT);
  157.             strip.show();
  158.         } else if (strcmp(device_name, "Regenbogenmodus")==0) {
  159.           if(state) {
  160.             firstPixelHue = 0;
  161.             previousMillis = 0;
  162.             int pixelHue = 0;
  163.             i = 0;
  164.             rt = 0;
  165.             rb = 1;
  166.           } else {
  167.             rb = 0;
  168.             strip.fill(strip.Color(0, 0, 0), 0, LED_COUNT);
  169.             strip.show();
  170.           }
  171.         } else if (strcmp(device_name, "Regenbogenreihe")==0) {
  172.           if(state) {
  173.             firstPixelHue = 0;
  174.             previousMillis = 0;
  175.             a = 0;
  176.             b = 0;
  177.             c = 0;
  178.             hue = 0;
  179.             uint32_t color = 0;
  180.             rb = 0;
  181.             rt = 1;
  182.           } else {
  183.             rt = 0;
  184.             strip.fill(strip.Color(0, 0, 0), 0, LED_COUNT);
  185.             strip.show();
  186.           }
  187.         }
  188.  
  189.  
  190.     });
  191.  
  192. }
  193.  
  194. void loop() {
  195.  
  196.     // fauxmoESP uses an async TCP server but a sync UDP server
  197.     // Therefore, we have to manually poll for UDP packets
  198.     fauxmo.handle();
  199.     if(rb == 1) {
  200.       unsigned long currentMillis = millis();
  201.       if(currentMillis - previousMillis > 10) {
  202.         previousMillis = currentMillis;
  203.         if(firstPixelHue < 5*65536) {
  204.           if(i < strip.numPixels()) {
  205.             int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  206.             strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  207.             i++;
  208.           }
  209.           strip.show();
  210.           firstPixelHue += 256;
  211.         }
  212.       }
  213.     }
  214.     if(rt == 1) {
  215.       if(a < 30) {
  216.         if(b < 3) {
  217.           strip.clear();
  218.           if(c < strip.numPixels()) {
  219.             int hue   = firstPixelHue + c * 65536L / strip.numPixels();
  220.             uint32_t color = strip.gamma32(strip.ColorHSV(hue));
  221.             strip.setPixelColor(c, color);
  222.             c += 3;
  223.           } else {
  224.             strip.show();
  225.             unsigned long currentMillis = millis();
  226.             if(currentMillis - previousMillis > 50) {
  227.               firstPixelHue += 65536 / 90;
  228.               b++;
  229.               c = b;
  230.             }
  231.           }
  232.         } else {
  233.           b = 0;
  234.           a++;
  235.         }
  236.       }
  237.     }
  238.     // This is a sample code to output free heap every 5 seconds
  239.     // This is a cheap way to detect memory leaks
  240.    
  241.    
  242.     // If your device state is changed by any other means (MQTT, physical button,...)
  243.     // you can instruct the library to report the new state to Alexa on next request:
  244.     // fauxmo.setState(ID_YELLOW, true, 255);
  245.  
  246. }
Add Comment
Please, Sign In to add comment