#include #ifdef ESP32 #include #else #include #endif #include "fauxmoESP.h" // Rename the credentials.sample.h file to credentials.h and // edit it according to your router configuration #include "credentials.h" #include fauxmoESP fauxmo; // ----------------------------------------------------------------------------- #define SERIAL_BAUDRATE 115200 #define ID_RED "red lamp" #define ID_MAGENTA "magenta lamp" #define ID_BLUE "blue lamp" #define ID_BLINK "blink lamp" #define ID_COUNT "math lamp" int ledvalue = 0; int counting = 0; int terminalcount =1000; int mood = 0; // ----------------------------------------------------------------------------- // ----------------------------------------------------------------------------- // 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 shift16(int i){ digitalWrite(D8, LOW); //Disable any internal transference in the SN74HC595 SPI.transfer16(i); //Transfer data to the SN74HC595s digitalWrite(D8, HIGH); //put on output pins } void setup() { // Init serial port and clean garbage Serial.begin(SERIAL_BAUDRATE); Serial.println(); Serial.println(); // SPI CC pinMode(D8, OUTPUT); digitalWrite(D8, LOW); SPI.begin(); shift16(0); // 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() fauxmo.createServer(true); // not needed, this is the default value fauxmo.setPort(80); // This is required for gen3 devices // 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.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 // "Alexa, set yellow lamp to fifty" (50 means 50% of brightness, note, this example does not use this functionality) // Add virtual devices fauxmo.addDevice(ID_RED); fauxmo.addDevice(ID_MAGENTA); fauxmo.addDevice(ID_BLUE); fauxmo.addDevice(ID_BLINK); fauxmo.addDevice(ID_COUNT); 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 (strcmp(device_name, ID_RED)==0) { ledvalue=state ? 0xf0f0 : 0; mood = 0; terminalcount = 1000; } else if (strcmp(device_name, ID_MAGENTA)==0) { ledvalue=state ? 0xffff : 0; mood = 0; terminalcount = 1000; } else if (strcmp(device_name, ID_BLUE)==0) { ledvalue=state ? 0x0f0f : 0; mood = 0; terminalcount = 1000; } else if (strcmp(device_name, ID_BLINK)==0) { ledvalue=state ? 0xf00f : 0; mood = 1; terminalcount = 1000; } else if (strcmp(device_name, ID_COUNT)==0) { ledvalue=state ? 0x0101 : 0; mood = 2; terminalcount = 1000; } terminalcount = 1000 - 3*value ; Serial.println(terminalcount); }); } void loop() { // fauxmoESP uses an async TCP server but a sync UDP server // Therefore, we have to manually poll for UDP packets fauxmo.handle(); /* // This is a sample code to output free heap every 5 seconds // This is a cheap way to detect memory leaks static unsigned long last = millis(); if (millis() - last > 5000) { last = millis(); Serial.printf("[MAIN] Free heap: %d bytes\n", ESP.getFreeHeap()); } */ delay(1); counting++; if (counting >= terminalcount){ counting =0; if (mood ==1){ ledvalue = ledvalue ^ 0xffff; } if (mood ==2){ ledvalue = ledvalue + 0x0101; } Serial.println(ledvalue); } // If your device state is changed by any other means (MQTT, physical button,...) // you can instruct the library to report the new state to Alexa on next request: // fauxmo.setState(ID_RED, true, 255); //Serial.println(counting); shift16(ledvalue); }
#define WIFI_SSID "myssid" #define WIFI_PASS "mypassword"
LiteBrite SMD Stick