/ fauxmo_guide / fauxmo-basic / fauxmo-basic.ino
fauxmo-basic.ino
 1  // SPDX-FileCopyrightText: 2019 Brent Rubell for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  
 5  #include <Arduino.h>
 6  #include <ESP8266WiFi.h>
 7  #include <ESPAsyncTCP.h>
 8  #include "fauxmoESP.h"
 9  
10  #define WIFI_SSID "YOUR_SSID"
11  #define WIFI_PASS "YOUR_WIFI_PASSWORD"
12  #define SERIAL_BAUDRATE 115200
13  
14  fauxmoESP fauxmo;
15  
16  // -----------------------------------------------------------------------------
17  // Wifi
18  // -----------------------------------------------------------------------------
19  
20  void wifiSetup() {
21  
22      // Set WIFI module to STA mode
23      WiFi.mode(WIFI_STA);
24  
25      // Connect
26      Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
27      WiFi.begin(WIFI_SSID, WIFI_PASS);
28  
29      // Wait
30      while (WiFi.status() != WL_CONNECTED) {
31          Serial.print(".");
32          delay(100);
33      }
34      Serial.println();
35  
36      // Connected!
37      Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
38  }
39  
40  void setup() {
41    // Init serial port and clean garbage
42    Serial.begin(SERIAL_BAUDRATE);
43    Serial.println("FauxMo demo sketch");
44    Serial.println("After connection, ask Alexa/Echo to 'turn <devicename> on' or 'off'");
45  
46    // Wifi
47    wifiSetup();
48  
49    // Fauxmo
50    fauxmo.addDevice("relay");
51    fauxmo.addDevice("pixels");
52    // Gen3 Devices or above
53    fauxmo.setPort(80);
54  
55    // Allow the FauxMo to be discovered
56    fauxmo.enable(true);
57    
58    fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
59      Serial.print("Device: ");Serial.print(device_name);
60      Serial.print(" state");
61      if(state) {
62        Serial.println("ON!");
63      }
64      else {
65        Serial.println("off");
66      }
67  
68    });
69  
70  }
71  
72  void loop() {
73    fauxmo.handle();
74  }