Added HTTP REST API.
This commit is contained in:
76
src/http_server.cpp
Normal file
76
src/http_server.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "config.h"
|
||||
|
||||
#ifdef HTTP_SERVER_ENABLE
|
||||
|
||||
#include "http_server.h"
|
||||
#include "effects.h"
|
||||
|
||||
ESP8266WebServer http_server(HTTP_SERVER_PORT);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
void http_server_loop() {
|
||||
http_server.handleClient();
|
||||
}
|
||||
|
||||
#endif
|
@ -11,6 +11,7 @@
|
||||
#include "my_mqtt.h"
|
||||
#include "functions.h"
|
||||
#include "effects.h"
|
||||
#include "http_server.h"
|
||||
|
||||
uint8_t starting_up = OTA_STARTUP_DELAY;
|
||||
int loop_timeouts = 0;
|
||||
@ -26,6 +27,9 @@ void setup() {
|
||||
ota_setup();
|
||||
fastled_setup();
|
||||
ntpClient.begin();
|
||||
#ifdef HTTP_SERVER_ENABLE
|
||||
http_server_setup();
|
||||
#endif
|
||||
#ifdef MQTT_ENABLE
|
||||
mqtt_setup();
|
||||
#endif
|
||||
@ -57,6 +61,9 @@ void loop() {
|
||||
#ifdef MQTT_ENABLE
|
||||
mqtt_loop();
|
||||
#endif
|
||||
#ifdef HTTP_SERVER_ENABLE
|
||||
http_server_loop();
|
||||
#endif
|
||||
|
||||
EVERY_N_MILLISECONDS(100) {
|
||||
baseHue++;
|
||||
|
Reference in New Issue
Block a user