Web server interface added to be able to control the lights without alexa
parent
7cc9632dfb
commit
2a6070bccc
|
@ -16,3 +16,4 @@ monitor_speed = 115200
|
||||||
lib_deps =
|
lib_deps =
|
||||||
vintlabs/FauxmoESP@^3.4
|
vintlabs/FauxmoESP@^3.4
|
||||||
aircoookie/Espalexa@^2.7.0
|
aircoookie/Espalexa@^2.7.0
|
||||||
|
me-no-dev/ESP Async WebServer@^1.2.3
|
||||||
|
|
101
src/main.cpp
101
src/main.cpp
|
@ -1,7 +1,12 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <WiFi.h>
|
|
||||||
|
#define ESPALEXA_ASYNC
|
||||||
#include <Espalexa.h>
|
#include <Espalexa.h>
|
||||||
|
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <AsyncTCP.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
|
|
||||||
#include <credentials.h>
|
#include <credentials.h>
|
||||||
|
|
||||||
// prototypes
|
// prototypes
|
||||||
|
@ -14,7 +19,13 @@ void cabinetLightChanged(EspalexaDevice* dev);
|
||||||
|
|
||||||
boolean wifiConnected = false;
|
boolean wifiConnected = false;
|
||||||
|
|
||||||
|
EspalexaDevice* arbeitslicht;
|
||||||
|
EspalexaDevice* monitorlicht;
|
||||||
|
EspalexaDevice* schranklicht;
|
||||||
|
EspalexaDevice* bodenbeleuchtung;
|
||||||
|
|
||||||
Espalexa espalexa;
|
Espalexa espalexa;
|
||||||
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
const int freq = 5000;
|
const int freq = 5000;
|
||||||
const int resolution = 8;
|
const int resolution = 8;
|
||||||
|
@ -182,6 +193,47 @@ void ledStripsSetup() {
|
||||||
ledcAttachPin(bluePin4, blueChannel4);
|
ledcAttachPin(bluePin4, blueChannel4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handleDevice(AsyncWebServerRequest *request, EspalexaDevice *alexaDevice) {
|
||||||
|
if (request->hasParam("state")) {
|
||||||
|
AsyncWebParameter* state = request->getParam("state");
|
||||||
|
|
||||||
|
if (state->value().equals("on")) {
|
||||||
|
alexaDevice->setState(true);
|
||||||
|
} else {
|
||||||
|
alexaDevice->setState(false);
|
||||||
|
}
|
||||||
|
alexaDevice->doCallback();
|
||||||
|
} else if (request->hasParam("brightness")) {
|
||||||
|
AsyncWebParameter* brightness = request->getParam("brightness");
|
||||||
|
|
||||||
|
if (brightness->value().equals("100")) {
|
||||||
|
alexaDevice->setPercent(100);
|
||||||
|
} else if (brightness->value().equals("90")) {
|
||||||
|
alexaDevice->setPercent(90);
|
||||||
|
} else if (brightness->value().equals("80")) {
|
||||||
|
alexaDevice->setPercent(80);
|
||||||
|
} else if (brightness->value().equals("70")) {
|
||||||
|
alexaDevice->setPercent(70);
|
||||||
|
} else if (brightness->value().equals("60")) {
|
||||||
|
alexaDevice->setPercent(60);
|
||||||
|
} else if (brightness->value().equals("50")) {
|
||||||
|
alexaDevice->setPercent(50);
|
||||||
|
} else if (brightness->value().equals("40")) {
|
||||||
|
alexaDevice->setPercent(40);
|
||||||
|
} else if (brightness->value().equals("30")) {
|
||||||
|
alexaDevice->setPercent(30);
|
||||||
|
} else if (brightness->value().equals("20")) {
|
||||||
|
alexaDevice->setPercent(20);
|
||||||
|
} else if (brightness->value().equals("10")) {
|
||||||
|
alexaDevice->setPercent(10);
|
||||||
|
} else if (brightness->value().equals("0")) {
|
||||||
|
alexaDevice->setPercent(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
alexaDevice->doCallback();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
@ -190,12 +242,49 @@ void setup() {
|
||||||
wifiConnected = connectWifi();
|
wifiConnected = connectWifi();
|
||||||
|
|
||||||
if (wifiConnected) {
|
if (wifiConnected) {
|
||||||
espalexa.addDevice("arbeitslicht", workingLightChanged, EspalexaDeviceType::color);
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
espalexa.addDevice("monitorlicht", monitorLightChanged, EspalexaDeviceType::color);
|
request->send(200, "text/plain", "This is an example index page your server may send.");
|
||||||
espalexa.addDevice("schranklicht", cabinetLightChanged, EspalexaDeviceType::color);
|
});
|
||||||
espalexa.addDevice("bodenbeleuchtung", floorLightChanged, EspalexaDeviceType::color);
|
server.on("/lightcontrol", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
|
if (request->hasParam("device")) {
|
||||||
|
AsyncWebParameter* device = request->getParam("device");
|
||||||
|
|
||||||
espalexa.begin();
|
if (device->value().equals("arbeitslicht")) {
|
||||||
|
handleDevice(request, arbeitslicht);
|
||||||
|
} else if (device->value().equals("monitorlicht")) {
|
||||||
|
handleDevice(request, monitorlicht);
|
||||||
|
} else if (device->value().equals("schranklicht")) {
|
||||||
|
handleDevice(request, schranklicht);
|
||||||
|
} else if (device->value().equals("bodenbeleuchtung")) {
|
||||||
|
handleDevice(request, bodenbeleuchtung);
|
||||||
|
}
|
||||||
|
|
||||||
|
request->send(200, "text/plain", "Ok!");
|
||||||
|
} else {
|
||||||
|
request->send(200, "text/plain", "This is a second subpage you may have.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.onNotFound([](AsyncWebServerRequest *request){
|
||||||
|
if (!espalexa.handleAlexaApiCall(request)) //if you don't know the URI, ask espalexa whether it is an Alexa control request
|
||||||
|
{
|
||||||
|
//whatever you want to do with 404s
|
||||||
|
request->send(404, "text/plain", "Not found");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
arbeitslicht = new EspalexaDevice("arbeitslicht", workingLightChanged, EspalexaDeviceType::color);
|
||||||
|
espalexa.addDevice(arbeitslicht);
|
||||||
|
|
||||||
|
monitorlicht = new EspalexaDevice("monitorlicht", monitorLightChanged, EspalexaDeviceType::color);
|
||||||
|
espalexa.addDevice(monitorlicht);
|
||||||
|
|
||||||
|
schranklicht = new EspalexaDevice("schranklicht", cabinetLightChanged, EspalexaDeviceType::color);
|
||||||
|
espalexa.addDevice(schranklicht);
|
||||||
|
|
||||||
|
bodenbeleuchtung = new EspalexaDevice("bodenbeleuchtung", floorLightChanged, EspalexaDeviceType::color);
|
||||||
|
espalexa.addDevice(bodenbeleuchtung);
|
||||||
|
|
||||||
|
espalexa.begin(&server);
|
||||||
} else {
|
} else {
|
||||||
while (1) {
|
while (1) {
|
||||||
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
|
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
|
||||||
|
|
Loading…
Reference in New Issue