pitrix/src/http_server.cpp

85 lines
2.3 KiB
C++
Raw Normal View History

2019-06-12 18:57:31 +00:00
#include "config.h"
#ifdef HTTP_SERVER_ENABLE
#include "http_server.h"
#include "effects.h"
#include "my_wifi.h"
#include "prototypes.h"
2019-06-12 18:57:31 +00:00
#if defined( ESP8266 )
2019-06-12 18:57:31 +00:00
ESP8266WebServer http_server(HTTP_SERVER_PORT);
#elif defined( ESP32 )
ESP32WebServer http_server(HTTP_SERVER_PORT);
#endif
2019-06-12 18:57:31 +00:00
void http_server_400() {
http_server.send(400);
}
void http_server_setup() {
PGM_P text_plain = PSTR("text/plain");
http_server.on("/", HTTP_GET, [&](){
LOGln("HTTP * GET /");
http_server.send_P(200, text_plain, PSTR("Welcome to pitrix."));
});
http_server.on("/free_heap", HTTP_GET, [&](){
LOGln("HTTP * GET /free_heap");
http_server.send(200, "text/plain", String(ESP.getFreeHeap()));
});
http_server.on("/uptime", HTTP_GET, [&](){
LOGln("HTTP * GET /uptime");
http_server.send(200, "text/plain", String(millis()/1000));
});
http_server.on("/fps", HTTP_GET, [](){
LOGln("HTTP * GET /fps");
http_server.send(200, "text/plain", String(FastLED.getFPS()));
});
http_server.on("/reboot", HTTP_POST, [](){
LOGln("HTTP * POST /reboot");
ESP.restart();
});
http_server.on("/brightness", HTTP_POST, [&](){
LOG("HTTP * POST /brightness with value "); LOGln(http_server.arg("plain"));
if (!http_server.hasArg("plain")) {
http_server.send_P(400, text_plain, PSTR("No brightness given"));
return;
}
long val = http_server.arg("plain").toInt();
if (val==0 || val>255) {
http_server.send_P(400, text_plain, PSTR("New value out of bounds. (1-255)"));
return;
}
FastLED.setBrightness(val);
http_server.send(200, "text/plain", "OK");
});
http_server.on("/mode", HTTP_POST, [&](){
LOGln("HTTP * POST /mode with value "); LOGln(http_server.arg("plain"));
if (!http_server.hasArg("plain")) {
http_server.send_P(400, text_plain, PSTR("No effect given."));
return;
}
String val = http_server.arg("plain");
for (int i=0; i<effects->size(); i++) {
EffectEntry e = effects->get(i);
if (val.compareTo(e.name)==0) {
current_effect->stop();
current_effect = e.effect;
current_effect->start();
http_server.send(200, "text/plain", "OK");
return;
}
}
http_server.send_P(400, text_plain, PSTR("Unknown effect."));
});
http_server.begin();
MDNS.addService("_http", "_tcp", 80);
2019-06-12 18:57:31 +00:00
}
void http_server_loop() {
http_server.handleClient();
}
#endif