|
1 | 1 | #pragma once |
2 | | -#include <fauxmoESP.h> |
3 | | - |
4 | | -fauxmoESP fauxmo; |
5 | | - |
6 | | -#define ID_CHAN_1 "channel one lamp" |
7 | | -#define ID_CHAN_2 "channel two lamp" |
8 | | -#define ID_CHAN_3 "channel three lamp" |
9 | | -#define ID_CHAN_4 "channel four lamp" |
10 | | -#define ID_CHAN_5 "channel five lamp" |
11 | | -#define ID_CHAN_6 "channel six lamp" |
12 | | -#define ID_CHAN_7 "channel seven lamp" |
13 | | -#define ID_CHAN_8 "channel eight lamp" |
14 | | - |
15 | | - |
16 | | -uint32_t alexaInit() { |
17 | | - // By default, fauxmoESP creates it's own webserver on the defined port |
18 | | - // The TCP port must be 80 for gen3 devices (default is 1901) |
19 | | - // This has to be done before the call to enable() |
20 | | - fauxmo.createServer(true); // not needed, this is the default value |
21 | | - fauxmo.setPort(80); // This is required for gen3 devices |
22 | | - |
23 | | - // You have to call enable(true) once you have a WiFi connection |
24 | | - // You can enable or disable the library at any moment |
25 | | - // Disabling it will prevent the devices from being discovered and switched |
26 | | - fauxmo.enable(true); |
27 | | - |
28 | | - // You can use different ways to invoke alexa to modify the devices state: |
29 | | - // "Alexa, turn yellow lamp on" |
30 | | - // "Alexa, turn on yellow lamp |
31 | | - // "Alexa, set yellow lamp to fifty" (50 means 50% of brightness, note, this example does not use this functionality) |
| 2 | +#include <Espalexa.h> |
| 3 | + |
| 4 | +#define ID_CHAN_1 "channel one" |
| 5 | +#define ID_CHAN_2 "channel two" |
| 6 | +#define ID_CHAN_3 "channel three" |
| 7 | +#define ID_CHAN_4 "channel four" |
| 8 | +#define ID_CHAN_5 "channel five" |
| 9 | +#define ID_CHAN_6 "channel six" |
| 10 | +#define ID_CHAN_7 "channel seven" |
| 11 | +#define ID_CHAN_8 "channel eight" |
| 12 | + |
| 13 | +Espalexa espalexa; |
| 14 | + |
| 15 | +template <int I> |
| 16 | +void chChanged(uint8_t brightness) { |
| 17 | + if (brightness == 255) { // ON |
| 18 | + WDEBUG("Alexa: ON CH%d\n", I); |
| 19 | + socket[I]->manual = SON; |
| 20 | + } |
| 21 | + else if (brightness == 0) { // OFF |
| 22 | + WDEBUG("Alexa: OFF CH%d\n", I); |
| 23 | + socket[I]->manual = SOFF; |
| 24 | + } |
| 25 | + else { // Value |
| 26 | + WDEBUG("Alexa: DIM %d CH%d\n", brightness, I); |
| 27 | + } |
| 28 | +} |
32 | 29 |
|
33 | | - // Add virtual devices |
34 | | - fauxmo.addDevice(ID_CHAN_1); |
35 | | - fauxmo.addDevice(ID_CHAN_2); |
36 | | - fauxmo.addDevice(ID_CHAN_3); |
37 | | - fauxmo.addDevice(ID_CHAN_4); |
38 | | - fauxmo.addDevice(ID_CHAN_5); |
39 | | - fauxmo.addDevice(ID_CHAN_6); |
40 | | - fauxmo.addDevice(ID_CHAN_7); |
41 | | - fauxmo.addDevice(ID_CHAN_8); |
| 30 | +uint32_t alexaLoop() { |
| 31 | + espalexa.loop(); |
| 32 | + return 100; |
| 33 | +} |
42 | 34 |
|
43 | | - fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) { |
44 | | - |
45 | | - // Callback when a command from Alexa is received. |
46 | | - // You can use device_id or device_name to choose the element to perform an action onto (relay, LED,...) |
47 | | - // 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). |
48 | | - // Just remember not to delay too much here, this is a callback, exit as soon as possible. |
49 | | - // If you have to do something more involved here set a flag and process it in your main loop. |
50 | | - |
51 | | - Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value); |
52 | | -/* |
53 | | - // Checking for device_id is simpler if you are certain about the order they are loaded and it does not change. |
54 | | - // Otherwise comparing the device_name is safer. |
| 35 | +void anyFile(); //From web.h |
55 | 36 |
|
56 | | - if (strcmp(device_name, ID_YELLOW)==0) { |
57 | | - digitalWrite(LED_YELLOW, state ? HIGH : LOW); |
58 | | - } else if (strcmp(device_name, ID_GREEN)==0) { |
59 | | - digitalWrite(LED_GREEN, state ? HIGH : LOW); |
60 | | - } else if (strcmp(device_name, ID_BLUE)==0) { |
61 | | - digitalWrite(LED_BLUE, state ? HIGH : LOW); |
62 | | - } else if (strcmp(device_name, ID_PINK)==0) { |
63 | | - digitalWrite(LED_PINK, state ? HIGH : LOW); |
64 | | - } else if (strcmp(device_name, ID_WHITE)==0) { |
65 | | - digitalWrite(LED_WHITE, state ? HIGH : LOW); |
66 | | - } |
67 | | -*/ |
| 37 | +uint32_t initAlexa() { |
| 38 | + server.onNotFound([](){ |
| 39 | + if (!espalexa.handleAlexaApiCall(server.uri(),server.arg(0))) { |
| 40 | + //server.send(404, "text/plain", "Not found"); |
| 41 | + anyFile(); |
| 42 | + } |
68 | 43 | }); |
| 44 | + // Add devices |
| 45 | + espalexa.addDevice(ID_CHAN_1, chChanged<0>); |
| 46 | + espalexa.addDevice(ID_CHAN_2, chChanged<1>); |
| 47 | + espalexa.addDevice(ID_CHAN_3, chChanged<2>); |
| 48 | + espalexa.addDevice(ID_CHAN_4, chChanged<3>); |
| 49 | + espalexa.addDevice(ID_CHAN_5, chChanged<4>); |
| 50 | + espalexa.addDevice(ID_CHAN_6, chChanged<5>); |
| 51 | + espalexa.addDevice(ID_CHAN_7, chChanged<6>); |
| 52 | + espalexa.addDevice(ID_CHAN_8, chChanged<7>); |
| 53 | + |
| 54 | + espalexa.begin(&server); |
| 55 | + |
69 | 56 | taskAdd(alexaLoop); |
70 | 57 | return RUN_DELETE; |
71 | 58 | } |
72 | | - |
73 | | -uint32_t alexaLoop()() { |
74 | | - // fauxmoESP uses an async TCP server but a sync UDP server |
75 | | - // Therefore, we have to manually poll for UDP packets |
76 | | - fauxmo.handle(); |
77 | | - |
78 | | - // If your device state is changed by any other means (MQTT, physical button,...) |
79 | | - // you can instruct the library to report the new state to Alexa on next request: |
80 | | - // fauxmo.setState(ID_YELLOW, true, 255); |
81 | | - return 100; |
82 | | -} |
83 | | - |
0 commit comments