Advertisement
Guest User

Untitled

a guest
May 5th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [code]
  2. #include <Arduino.h>
  3. #include <ESP8266WiFi.h>
  4. #include "fauxmoESP.h"
  5. #include "ESPAsyncWebServer.h"
  6. #include <ESPAsyncTCP.h>
  7. #include <Hash.h>
  8.  
  9. #define WIFI_SSID "Bond007"
  10. #define WIFI_PASS "Belsen97"
  11. #define SERIAL_BAUDRATE                 115200
  12.  
  13. fauxmoESP fauxmo;
  14. #define ledPin D3
  15. const int  buttonPin = D2;    // the pin that the pushbutton is attached to
  16. int buttonState = 0;         // current state of the button
  17. int lastButtonState = 0;     // previous state of the button
  18.  
  19. // -----------------------------------------------------------------------------
  20. // Wifi
  21. // -----------------------------------------------------------------------------
  22.  
  23. void wifiSetup() {
  24.  
  25.     // Set WIFI module to STA mode
  26.     WiFi.mode(WIFI_STA);
  27.  
  28.     // Connect
  29.     Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  30.     WiFi.begin(WIFI_SSID, WIFI_PASS);
  31.  
  32.     // Wait
  33.     while (WiFi.status() != WL_CONNECTED) {
  34.         Serial.print(".");
  35.         delay(100);
  36.     }
  37.     Serial.println();
  38.  
  39.     // Connected!
  40.     Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
  41. }
  42.  
  43. void callback(uint8_t device_id, const char * device_name, bool state) {
  44.   Serial.print("Device "); Serial.print(device_name);
  45.   Serial.print(" state: ");
  46.   if (state) {
  47.     Serial.println("ON");
  48.     digitalWrite(ledPin, HIGH);
  49.       delay(500);
  50.       digitalWrite(ledPin, LOW);
  51.      
  52. } else {
  53.       Serial.println("OFF");
  54.       digitalWrite(ledPin, HIGH);
  55.       delay(500);
  56.       digitalWrite(ledPin, LOW);
  57.     }
  58.    
  59.  
  60.    
  61.   }
  62.  
  63.  
  64. void setup() {
  65.     pinMode(ledPin, OUTPUT);
  66.     pinMode(buttonPin, INPUT_PULLUP);
  67.     digitalWrite(ledPin, LOW);
  68.     // Init serial port and clean garbage
  69.     Serial.begin(SERIAL_BAUDRATE);
  70.     Serial.println("FauxMo demo sketch");
  71.     Serial.println("After connection, ask Alexa/Echo to 'turn <devicename> on' or 'off'");
  72.  
  73.     // Wifi
  74.     wifiSetup();
  75.  
  76.     // Fauxmo
  77.     fauxmo.addDevice("PC");
  78.     fauxmo.addDevice("Computer");
  79.     fauxmo.onMessage(callback);
  80. }
  81.  
  82. void loop() {
  83.   fauxmo.handle();
  84.  
  85.   // read the pushbutton input pin:
  86.   buttonState = digitalRead(buttonPin);
  87.  
  88.   // compare the buttonState to its previous state
  89.   if (buttonState != lastButtonState) {
  90.     // if the state has changed, increment the counter
  91.     if (buttonState == LOW) {
  92.       Serial.println("on");
  93.       digitalWrite(ledPin, HIGH);
  94.       delay(500);
  95.       digitalWrite(ledPin, LOW);
  96.     }
  97.     else {
  98.       // if the current state is LOW then the button
  99.       // went from on to off:
  100.       Serial.println("off");
  101.       digitalWrite(ledPin, HIGH);
  102.       delay(500);
  103.       digitalWrite(ledPin, LOW);
  104.     }
  105.     // Delay a little bit to avoid bouncing
  106.     delay(50);
  107.   }
  108.   // save the current state as the last state,
  109.   //for next time through the loop
  110.   lastButtonState = buttonState;
  111. }
  112. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement