Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <ArduinoOTA.h>
  2. #include <Arduino.h>
  3. #ifdef ESP32
  4. #include <WiFi.h>
  5. #else
  6. #include <ESP8266WiFi.h>
  7. #endif
  8. #include "fauxmoESP.h"
  9.  
  10.  
  11. #include <Ticker.h> //Ticker Library
  12.  
  13. Ticker blinker;
  14.  
  15. #define WIFI_SSID "xxxxxx"
  16. #define WIFI_PASS "xxxxxx"
  17. fauxmoESP fauxmo;
  18.  
  19. // -----------------------------------------------------------------------------
  20.  
  21. #define SERIAL_BAUDRATE 115200
  22.  
  23. #define xPIN D7
  24.  
  25. #define ID_SPRAY "Spray"
  26.  
  27. // -----------------------------------------------------------------------------
  28. // Wifi
  29. // -----------------------------------------------------------------------------
  30.  
  31. void wifiSetup() {
  32.  
  33. // Set WIFI module to STA mode
  34. WiFi.mode(WIFI_STA);
  35.  
  36. // Connect
  37. Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  38. WiFi.begin(WIFI_SSID, WIFI_PASS);
  39.  
  40. // Wait
  41. while (WiFi.status() != WL_CONNECTED) {
  42. Serial.print(".");
  43. delay(100);
  44. }
  45. Serial.println();
  46.  
  47. // Connected!
  48. Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
  49.  
  50. }
  51.  
  52. void xPINon()
  53. {
  54. Serial.printf("Turning on\n");
  55.  
  56. digitalWrite(xPIN, HIGH);
  57.  
  58. blinker.attach(2, xPINoff);
  59. }
  60.  
  61. void xPINoff()
  62. {
  63. digitalWrite(xPIN, LOW);
  64. Serial.printf("Turning off!\n");
  65.  
  66. }
  67.  
  68. void setup() {
  69.  
  70. // Init serial port and clean garbage
  71. Serial.begin(SERIAL_BAUDRATE);
  72. Serial.println();
  73. Serial.println();
  74.  
  75. // LEDs
  76. pinMode(xPIN, OUTPUT);
  77. digitalWrite(xPIN, LOW);
  78.  
  79. // Wifi
  80. wifiSetup();
  81.  
  82. // By default, fauxmoESP creates it's own webserver on the defined port
  83. // The TCP port must be 80 for gen3 devices (default is 1901)
  84. // This has to be done before the call to enable()
  85.  
  86.  
  87. // You have to call enable(true) once you have a WiFi connection
  88. // You can enable or disable the library at any moment
  89. // Disabling it will prevent the devices from being discovered and switched
  90. fauxmo.setPort(80); // required for gen3 devices
  91. fauxmo.enable(true);
  92.  
  93. // You can use different ways to invoke alexa to modify the devices state:
  94. // "Alexa, turn yellow lamp on"
  95. // "Alexa, turn on yellow lamp
  96.  
  97. // Add virtual devices
  98. fauxmo.addDevice(ID_SPRAY);
  99.  
  100.  
  101. fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
  102.  
  103. // Callback when a command from Alexa is received.
  104. // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
  105. // 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).
  106. // Just remember not to delay too much here, this is a callback, exit as soon as possible.
  107. // If you have to do something more involved here set a flag and process it in your main loop.
  108.  
  109. Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
  110.  
  111. // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
  112. // Otherwise comparing the device_name is safer.
  113.  
  114. if (state==1) {
  115.  
  116. blinker.attach(0.5, xPINon);
  117.  
  118. }
  119. else if (state==0) {
  120. digitalWrite(xPIN, LOW);
  121. }
  122.  
  123. });
  124.  
  125. }
  126.  
  127. void loop() {
  128.  
  129. // fauxmoESP uses an async TCP server but a sync UDP server
  130. // Therefore, we have to manually poll for UDP packets
  131. fauxmo.handle();
  132.  
  133.  
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement