Advertisement
Guest User

Alexa LED

a guest
Aug 22nd, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include "fauxmoESP.h"
  4. #include "credentials.h"
  5.  
  6. #define SERIAL_BAUDRATE                 115200
  7. #define REDPIN                           14
  8. #define GREENPIN                         12
  9. #define BLUEPIN                          13
  10.  
  11. #define FADESPEED                        5
  12. int ON, OFF;
  13. int r, g, b;
  14. int state;
  15.  
  16. fauxmoESP fauxmo;
  17.  
  18. // -----------------------------------------------------------------------------
  19. // Wifi
  20. // -----------------------------------------------------------------------------
  21.  
  22. void wifiSetup() {
  23.  
  24.     // Set WIFI module to STA mode
  25.     WiFi.mode(WIFI_STA);
  26.  
  27.     // Connect
  28.     Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  29.     WiFi.begin(WIFI_SSID, WIFI_PASS);
  30.  
  31.     // Wait
  32.     while (WiFi.status() != WL_CONNECTED) {
  33.         Serial.print(".");
  34.         delay(100);
  35.     }
  36.     Serial.println();
  37.  
  38.     // Connected!
  39.     Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
  40.  
  41. }
  42.  
  43. void setup() {
  44.     pinMode(REDPIN, OUTPUT);
  45.     pinMode(GREENPIN, OUTPUT);
  46.     pinMode(BLUEPIN, OUTPUT);
  47.  
  48.     // Init serial port and clean garbage
  49.     Serial.begin(SERIAL_BAUDRATE);
  50.     Serial.println();
  51.     Serial.println();
  52.  
  53.     // Wifi
  54.     wifiSetup();
  55.  
  56.  
  57.     // Fauxmo
  58.     fauxmo.addDevice("Arduino");
  59.  
  60.     // fauxmoESP 2.0.0 has changed the callback signature to add the device_id, this WARRANTY
  61.     // it's easier to match devices to action without having to compare strings.
  62.     fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
  63.         Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
  64.            if (device_id == ON){ //habe hier ein wenig rumprobiert.
  65.    
  66.    // fade from blue to violet
  67.   for (r = 0; r < 256; r++) {
  68.     analogWrite(REDPIN, r);
  69.     delay(FADESPEED);
  70.   }
  71.   // fade from violet to red
  72.   for (b = 255; b > 0; b--) {
  73.     analogWrite(BLUEPIN, b);
  74.     delay(FADESPEED);
  75.   }
  76.   // fade from red to yellow
  77.   for (g = 0; g < 256; g++) {
  78.     analogWrite(GREENPIN, g);
  79.     delay(FADESPEED);
  80.   }
  81.   // fade from yellow to green
  82.   for (r = 255; r > 0; r--) {
  83.     analogWrite(REDPIN, r);
  84.     delay(FADESPEED);
  85.   }
  86.   // fade from green to teal
  87.   for (b = 0; b < 256; b++) {
  88.     analogWrite(BLUEPIN, b);
  89.     delay(FADESPEED);
  90.   }
  91.   // fade from teal to blue
  92.   for (g = 255; g > 0; g--) {
  93.     analogWrite(GREENPIN, g);
  94.     delay(FADESPEED);
  95.      
  96.   }
  97.     }
  98.    
  99.     else (device_id == OFF); {
  100.       digitalWrite(REDPIN, 0);
  101.       digitalWrite(GREENPIN, 0);
  102.       digitalWrite(BLUEPIN, 0);
  103.       }
  104. }
  105.          
  106.          
  107.    
  108.  
  109. );}
  110.  
  111. void loop() {
  112.   int r, g, b;
  113.     // Since fauxmoESP 2.0 the library uses the "compatibility" mode by
  114.     // default, this means that it uses WiFiUdp class instead of AsyncUDP.
  115.     // The later requires the Arduino Core for ESP8266 staging version
  116.     // whilst the former works fine with current stable 2.3.0 version.
  117.     // But, since it's not "async" anymore we have to manually poll for UDP
  118.     // packets
  119.     fauxmo.handle();
  120.  
  121.  
  122.     static unsigned long last = millis();
  123.     if (millis() - last > 5000) {
  124.         last = millis();
  125.         Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement