Added HTTP REST API.

This commit is contained in:
Fabian Schlenz 2019-06-12 20:57:31 +02:00
parent 308196d185
commit bfe46220ca
5 changed files with 108 additions and 6 deletions

View File

@ -15,23 +15,26 @@ nice-looking LED matrix everytime you want to update the software.
Checkout the code, rename `include/config.sample.h` to `include/config.h` Checkout the code, rename `include/config.sample.h` to `include/config.h`
and edit it to match your preferences / environment. and edit it to match your preferences / environment.
Then compile and flash it, preferrably using PlatformIO. Then compile and flash it, preferably using PlatformIO.
## Control it ## Control it
Currently, control is only possible via MQTT. `MQTT_TOPIC` is set in Currently, control is possible via MQTT and / or HTTP REST API.
`include/config.h` and defaults to `pitrix`. To use MQTT, you have to define `MQTT_ENABLE` and configure your MQTT sever's credentials. MQTT_TOPIC` is set in `include/config.h` and defaults to `pitrix`.
To use the HTTP REST API, you have to define HTTP_SERVER_ENABLE and set a port number
to listen on (80 is set by default).
Possible commands / topics are: Possible commands / topics are:
* `MQTT_TOPIC/mode` lets you select an effect to show. See `src/effects.cpp` * `MQTT_TOPIC/mode` / `POST /mode` lets you select an effect to show. See `src/effects.cpp`
for a list. Default effect is `cycle`, which will cycle through some of for a list. Default effect is `cycle`, which will cycle through some of
the available effects. Another effect is `off`, which will just display the available effects. Another effect is `off`, which will just display
black, effectively turning the display off. (pitrix stays running, so you black, effectively turning the display off. (pitrix stays running, so you
can turn it on again by simply selecting another mode.) can turn it on again by simply selecting another mode.)
* `MQTT_TOPIC/brightness` sets the brightness of the display. Valid values * `MQTT_TOPIC/brightness` / `POST /brightness` sets the brightness of the display. Valid values
are between 1 (darkest possible setting) and 255 (maximum brightness). are between 1 (darkest possible setting) and 255 (maximum brightness).
* `MQTT_TOPIC/reboot` reboots pitrix. Send any value. * `MQTT_TOPIC/reboot` / `POST /reboot` reboots pitrix. Send any value.
You can set retained values to have pitrix read them at startup, effectively You can set retained values to have pitrix read them at startup, effectively
setting a default effect or brightness. (Do NOT set a retained value for setting a default effect or brightness. (Do NOT set a retained value for
@ -49,4 +52,6 @@ seconds:
* `MQTT_TOPIC/uptime` contains the uptime of pitrix in seconds. * `MQTT_TOPIC/uptime` contains the uptime of pitrix in seconds.
* `MQTT_TOPIC/fps` contains the currently reached frames per second. * `MQTT_TOPIC/fps` contains the currently reached frames per second.
If you enabled HTTP server, you can always make GET requests to `/free_heap`, `/uptime` or `/fps` to get those values.
If you enabled `DEBUG`, log messages will be sent to `MQTT_TOPIC/log`. If you enabled `DEBUG`, log messages will be sent to `MQTT_TOPIC/log`.

View File

@ -24,6 +24,9 @@
#define NTP_INTERVAL 300000 // Interval in ms to update the time from the NTP server. 300000 ms = 5 minutes #define NTP_INTERVAL 300000 // Interval in ms to update the time from the NTP server. 300000 ms = 5 minutes
#define NTP_OFFSET 7200 // Offset of your local time from UTC in seconds. Germany, daylight savings time = 2 hours = 7200 seconds #define NTP_OFFSET 7200 // Offset of your local time from UTC in seconds. Germany, daylight savings time = 2 hours = 7200 seconds
#define HTTP_SERVER_ENABLE
#define HTTP_SERVER_PORT 80
#define MQTT_ENABLE // Use MQTT. Add slashes to the start of the line to disable MQTT completely. #define MQTT_ENABLE // Use MQTT. Add slashes to the start of the line to disable MQTT completely.
#define MQTT_SERVER "..." // Data for connecting to the MQTT server #define MQTT_SERVER "..." // Data for connecting to the MQTT server
#define MQTT_PORT 1883 #define MQTT_PORT 1883

11
include/http_server.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "config.h"
#ifdef HTTP_SERVER_ENABLE
#include <ESP8266WebServer.h>
extern ESP8266WebServer http_server;
void http_server_setup();
void http_server_loop();
#endif

76
src/http_server.cpp Normal file
View 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

View File

@ -11,6 +11,7 @@
#include "my_mqtt.h" #include "my_mqtt.h"
#include "functions.h" #include "functions.h"
#include "effects.h" #include "effects.h"
#include "http_server.h"
uint8_t starting_up = OTA_STARTUP_DELAY; uint8_t starting_up = OTA_STARTUP_DELAY;
int loop_timeouts = 0; int loop_timeouts = 0;
@ -26,6 +27,9 @@ void setup() {
ota_setup(); ota_setup();
fastled_setup(); fastled_setup();
ntpClient.begin(); ntpClient.begin();
#ifdef HTTP_SERVER_ENABLE
http_server_setup();
#endif
#ifdef MQTT_ENABLE #ifdef MQTT_ENABLE
mqtt_setup(); mqtt_setup();
#endif #endif
@ -57,6 +61,9 @@ void loop() {
#ifdef MQTT_ENABLE #ifdef MQTT_ENABLE
mqtt_loop(); mqtt_loop();
#endif #endif
#ifdef HTTP_SERVER_ENABLE
http_server_loop();
#endif
EVERY_N_MILLISECONDS(100) { EVERY_N_MILLISECONDS(100) {
baseHue++; baseHue++;