Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ArduinoOTA.h>
- #include <Arduino.h>
- #ifdef ESP32
- #include <WiFi.h>
- #else
- #include <ESP8266WiFi.h>
- #endif
- #include "fauxmoESP.h"
- #include <Ticker.h> //Ticker Library
- Ticker blinker;
- #define WIFI_SSID "xxxxxx"
- #define WIFI_PASS "xxxxxx"
- fauxmoESP fauxmo;
- // -----------------------------------------------------------------------------
- #define SERIAL_BAUDRATE 115200
- #define xPIN D7
- #define ID_SPRAY "Spray"
- // -----------------------------------------------------------------------------
- // Wifi
- // -----------------------------------------------------------------------------
- void wifiSetup() {
- // Set WIFI module to STA mode
- WiFi.mode(WIFI_STA);
- // Connect
- Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- // Wait
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(100);
- }
- Serial.println();
- // Connected!
- Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
- }
- void xPINon()
- {
- Serial.printf("Turning on\n");
- digitalWrite(xPIN, HIGH);
- blinker.attach(2, xPINoff);
- }
- void xPINoff()
- {
- digitalWrite(xPIN, LOW);
- Serial.printf("Turning off!\n");
- }
- void setup() {
- // Init serial port and clean garbage
- Serial.begin(SERIAL_BAUDRATE);
- Serial.println();
- Serial.println();
- // LEDs
- pinMode(xPIN, OUTPUT);
- digitalWrite(xPIN, LOW);
- // Wifi
- wifiSetup();
- // By default, fauxmoESP creates it's own webserver on the defined port
- // The TCP port must be 80 for gen3 devices (default is 1901)
- // This has to be done before the call to enable()
- // You have to call enable(true) once you have a WiFi connection
- // You can enable or disable the library at any moment
- // Disabling it will prevent the devices from being discovered and switched
- fauxmo.setPort(80); // required for gen3 devices
- fauxmo.enable(true);
- // You can use different ways to invoke alexa to modify the devices state:
- // "Alexa, turn yellow lamp on"
- // "Alexa, turn on yellow lamp
- // Add virtual devices
- fauxmo.addDevice(ID_SPRAY);
- fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
- // Callback when a command from Alexa is received.
- // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...)
- // 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).
- // Just remember not to delay too much here, this is a callback, exit as soon as possible.
- // If you have to do something more involved here set a flag and process it in your main loop.
- Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
- // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change.
- // Otherwise comparing the device_name is safer.
- if (state==1) {
- blinker.attach(0.5, xPINon);
- }
- else if (state==0) {
- digitalWrite(xPIN, LOW);
- }
- });
- }
- void loop() {
- // fauxmoESP uses an async TCP server but a sync UDP server
- // Therefore, we have to manually poll for UDP packets
- fauxmo.handle();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement