Advertisement
Guest User

AlexaRGBController

a guest
Mar 3rd, 2018
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. #include <Arduino.h>
  2. #ifdef ESP32
  3.     #include <WiFi.h>
  4. #else
  5.     #include <ESP8266WiFi.h>
  6. #endif
  7. #include "fauxmoESP.h"
  8. #include "credentials.sample.h"
  9.  
  10. #define SERIAL_BAUDRATE                 115200
  11. #define REDPIN                           14
  12. #define GREENPIN                         13
  13. #define BLUEPIN                          12
  14. int r, g, b;
  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.  
  45.     // Init serial port and clean garbage
  46.     Serial.begin(SERIAL_BAUDRATE);
  47.     Serial.println();
  48.     Serial.println();
  49.  
  50.     //LED
  51.     pinMode(REDPIN, OUTPUT);
  52.     pinMode(GREENPIN, OUTPUT);
  53.     pinMode(BLUEPIN, OUTPUT);
  54.     analogWriteFreq(500);
  55.     analogWriteRange(100);
  56.     // Wifi
  57.     wifiSetup();
  58.  
  59.     // LED
  60.  
  61.  
  62.     // You can enable or disable the library at any moment
  63.     // Disabling it will prevent the devices from being discovered and switched
  64.     fauxmo.enable(true);
  65.  
  66.     // Add virtual devices
  67.     fauxmo.addDevice("rot");
  68.       fauxmo.addDevice("blau");
  69.       fauxmo.addDevice("grün");
  70.  
  71.     // fauxmoESP 2.0.0 has changed the callback signature to add the device_id,
  72.     // this way it's easier to match devices to action without having to compare strings.
  73.     fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state) {
  74.         Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
  75.           if (state) {
  76.     if(device_id == 0){
  77.       digitalWrite(REDPIN, 100);
  78.       }
  79.     if(device_id == 1){
  80.       digitalWrite(GREENPIN, 100);
  81.       }
  82.     if(device_id == 2){
  83.       digitalWrite(BLUEPIN, 100);
  84.       }
  85.     if(device_id == 3){
  86.     digitalWrite(REDPIN, 100);
  87.     digitalWrite(GREENPIN, 100);
  88.     digitalWrite(BLUEPIN, 100);
  89.     }
  90.   } else {
  91.      digitalWrite(REDPIN, 0);
  92.     digitalWrite(GREENPIN, 0);
  93.     digitalWrite(BLUEPIN, 0);
  94.     if(device_id == 3){
  95.     digitalWrite(REDPIN, 0);
  96.     digitalWrite(GREENPIN, 0);
  97.     digitalWrite(BLUEPIN, 0);
  98.     }
  99.       }
  100.     });
  101.  
  102.     // Callback to retrieve current state (for GetBinaryState queries)
  103.   /*  fauxmo.onGetState([](unsigned char device_id, const char * device_name) {
  104.        
  105.     }); */
  106.  
  107. }
  108.  
  109. void loop() {
  110.  
  111.     // Since fauxmoESP 2.0 the library uses the "compatibility" mode by
  112.     // default, this means that it uses WiFiUdp class instead of AsyncUDP.
  113.     // The later requires the Arduino Core for ESP8266 staging version
  114.     // whilst the former works fine with current stable 2.3.0 version.
  115.     // But, since it's not "async" anymore we have to manually poll for UDP
  116.     // packets
  117.     fauxmo.handle();
  118.  
  119.     static unsigned long last = millis();
  120.     if (millis() - last > 5000) {
  121.         last = millis();
  122.         Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap());
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement