Web server interface added to be able to control the lights without alexa

master
alex 2022-09-04 21:25:33 +02:00
parent 7cc9632dfb
commit 2a6070bccc
2 changed files with 96 additions and 6 deletions

View File

@ -16,3 +16,4 @@ monitor_speed = 115200
lib_deps =
vintlabs/FauxmoESP@^3.4
aircoookie/Espalexa@^2.7.0
me-no-dev/ESP Async WebServer@^1.2.3

View File

@ -1,7 +1,12 @@
#include <Arduino.h>
#include <WiFi.h>
#define ESPALEXA_ASYNC
#include <Espalexa.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <credentials.h>
// prototypes
@ -14,7 +19,13 @@ void cabinetLightChanged(EspalexaDevice* dev);
boolean wifiConnected = false;
EspalexaDevice* arbeitslicht;
EspalexaDevice* monitorlicht;
EspalexaDevice* schranklicht;
EspalexaDevice* bodenbeleuchtung;
Espalexa espalexa;
AsyncWebServer server(80);
const int freq = 5000;
const int resolution = 8;
@ -182,6 +193,47 @@ void ledStripsSetup() {
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() {
Serial.begin(115200);
@ -190,12 +242,49 @@ void setup() {
wifiConnected = connectWifi();
if (wifiConnected) {
espalexa.addDevice("arbeitslicht", workingLightChanged, EspalexaDeviceType::color);
espalexa.addDevice("monitorlicht", monitorLightChanged, EspalexaDeviceType::color);
espalexa.addDevice("schranklicht", cabinetLightChanged, EspalexaDeviceType::color);
espalexa.addDevice("bodenbeleuchtung", floorLightChanged, EspalexaDeviceType::color);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", "This is an example index page your server may send.");
});
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 {
while (1) {
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");