Skip to content

Commit f6f08f2

Browse files
committed
Initial Alexa support
1 parent 5c125d0 commit f6f08f2

4 files changed

Lines changed: 55 additions & 78 deletions

File tree

WiFiSwitch.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
ADC_MODE(ADC_VCC);
1111

12-
const char* VERSION = "0.7.12";
12+
const char* VERSION = "0.8.0";
1313

1414
// Pin to activete WiFiManager configuration routine
1515
#define RESET_PIN D8
@@ -73,6 +73,7 @@ uint32_t wifiStart();
7373
#include "discovery.h"
7474
#include "update.h"
7575
#include "ping.h"
76+
#include "alexa.h"
7677

7778
uint8_t waitWF;
7879
uint32_t wifiStart() {
@@ -201,6 +202,7 @@ void setup() {
201202
taskAddWithSemaphore(initWeb, &event.wifiConnected); // Run initWeb() on Wi-Fi connection
202203
taskAddWithSemaphore(discovery, &event.wifiConnected);
203204
taskAddWithSemaphore(initUpdate, &event.wifiConnected);
205+
taskAddWithSemaphore(initAlexa, &event.wifiConnected);
204206
//taskAdd(printTime); //For debug
205207
taskAdd(checkKey); // Key query
206208
taskAddWithSemaphore(keyPressed, &event.keyPressed); // Run keyPressed() on keyPressed event

alexa.h

Lines changed: 50 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,58 @@
11
#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+
}
3229

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+
}
4234

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
5536

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+
}
6843
});
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+
6956
taskAdd(alexaLoop);
7057
return RUN_DELETE;
7158
}
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-

settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#ifdef ARDUINO_ESP8266_NODEMCU
1212
#define WFS_DEBUG
1313
//Debug settings
14-
#define WDEBUG(format, ...) Serial.printf_P(PSTR(format), ##__VA_ARGS__);
14+
#define WDEBUG(format, ...) Serial.printf_P((format), ##__VA_ARGS__);
1515

1616
#define SDA 4
1717
#define SCL 5

web.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ uint32_t initWeb() {
922922
server.on("/list", HTTP_GET, listFile); // List/Upload/Delete page
923923
server.on("/delete", HTTP_GET, handleDelete); // Delete File
924924
server.on("/edit", HTTP_POST, handleFile, handleFileUpload); // Upload file
925-
server.onNotFound(anyFile); // call function anyFile() on any other requests
925+
//server.onNotFound(anyFile); // call function anyFile() on any other requests => moved to alexa.h
926926
//server.on("/socket", HTTP_GET, handleOverride);
927927
server.on("/net", HTTP_POST, handleNetwork);
928928
server.on("/reboot", HTTP_GET, handleReboot);

0 commit comments

Comments
 (0)