Little Bugfixes added REST-API endpoints.

This commit is contained in:
Tobias Blum 2016-05-22 00:28:30 +02:00
parent 0364a5316f
commit fe6f191deb
2 changed files with 435 additions and 420 deletions

View file

@ -30,7 +30,7 @@ WebSocketsServer webSocket = WebSocketsServer(81);
// *************************************************************************** // ***************************************************************************
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
#ifdef __AVR__ #ifdef __AVR__
#include <avr/power.h> #include <avr/power.h>
#endif #endif
// Parameter 1 = number of pixels in strip // Parameter 1 = number of pixels in strip
@ -178,8 +178,8 @@ void setup() {
//list directory //list directory
server.on("/list", HTTP_GET, handleFileList); server.on("/list", HTTP_GET, handleFileList);
//load editor //load editor
server.on("/edit", HTTP_GET, [](){ server.on("/edit", HTTP_GET, []() {
if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound"); if (!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
}); });
//create file //create file
server.on("/edit", HTTP_PUT, handleFileCreate); server.on("/edit", HTTP_PUT, handleFileCreate);
@ -187,13 +187,15 @@ void setup() {
server.on("/edit", HTTP_DELETE, handleFileDelete); server.on("/edit", HTTP_DELETE, handleFileDelete);
//first callback is called after the request has ended with all parsed arguments //first callback is called after the request has ended with all parsed arguments
//second callback handles file uploads at that location //second callback handles file uploads at that location
server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload); server.on("/edit", HTTP_POST, []() {
server.send(200, "text/plain", "");
}, handleFileUpload);
//get heap status, analog input value and all GPIO statuses in one json call //get heap status, analog input value and all GPIO statuses in one json call
server.on("/status", HTTP_GET, [](){ server.on("/esp_status", HTTP_GET, []() {
String json = "{"; String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap()); json += "\"heap\":" + String(ESP.getFreeHeap());
json += ", \"analog\":"+String(analogRead(A0)); json += ", \"analog\":" + String(analogRead(A0));
json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16))); json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json += "}"; json += "}";
server.send(200, "text/json", json); server.send(200, "text/json", json);
json = String(); json = String();
@ -202,18 +204,24 @@ void setup() {
//called when the url is not defined here //called when the url is not defined here
//use it to load content from SPIFFS //use it to load content from SPIFFS
server.onNotFound([](){ server.onNotFound([]() {
if(!handleFileRead(server.uri())) if (!handleFileRead(server.uri()))
handleNotFound(); handleNotFound();
}); });
server.on("/upload", handleMinimalUpload); server.on("/upload", handleMinimalUpload);
server.on("/restart", []() {
DBG_OUTPUT_PORT.printf("/restart:\n");
server.send(200, "text/plain", "restarting..." );
ESP.restart();
});
// *************************************************************************** // ***************************************************************************
// Setup: SPIFFS Webserver handler // Setup: SPIFFS Webserver handler
// *************************************************************************** // ***************************************************************************
server.on("/brightness", []() { server.on("/set_brightness", []() {
if (server.arg("c").toInt() > 0) { if (server.arg("c").toInt() > 0) {
brightness = (int) server.arg("c").toInt() * 2.55; brightness = (int) server.arg("c").toInt() * 2.55;
} else { } else {
@ -235,15 +243,22 @@ void setup() {
}); });
server.on("/get_brightness", []() { server.on("/get_brightness", []() {
server.send(200, "text/plain", String((int) (brightness / 2.55)) ); String str_brightness = String((int) (brightness / 2.55));
server.send(200, "text/plain", str_brightness );
DBG_OUTPUT_PORT.print("/get_brightness: ");
DBG_OUTPUT_PORT.println(str_brightness);
}); });
server.on("/get_switch", []() { server.on("/get_switch", []() {
server.send(200, "text/plain", (mode == OFF) ? "0" : "1" ); server.send(200, "text/plain", (mode == OFF) ? "0" : "1" );
DBG_OUTPUT_PORT.printf("/get_switch: %s\n", (mode == OFF) ? "0" : "1");
}); });
server.on("/get_color", []() { server.on("/get_color", []() {
server.send(200, "text/plain", String(main_color.red, HEX) + String(main_color.green, HEX) + String(main_color.blue, HEX) ); String rgbcolor = String(main_color.red, HEX) + String(main_color.green, HEX) + String(main_color.blue, HEX);
server.send(200, "text/plain", rgbcolor );
DBG_OUTPUT_PORT.print("/get_color: ");
DBG_OUTPUT_PORT.println(rgbcolor);
}); });
server.on("/status", []() { server.on("/status", []() {
@ -321,7 +336,7 @@ void loop() {
strip.setPixelColor(i, 0, 0, 0); strip.setPixelColor(i, 0, 0, 0);
} }
strip.show(); strip.show();
mode = HOLD; //mode = HOLD;
} }
if (mode == ALL) { if (mode == ALL) {
uint16_t i; uint16_t i;

View file

@ -102,7 +102,7 @@ void getStatusJSON() {
} }
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch(type) { switch (type) {
case WStype_DISCONNECTED: case WStype_DISCONNECTED:
DBG_OUTPUT_PORT.printf("WS: [%u] Disconnected!\n", num); DBG_OUTPUT_PORT.printf("WS: [%u] Disconnected!\n", num);
break; break;
@ -120,7 +120,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
DBG_OUTPUT_PORT.printf("WS: [%u] get Text: %s\n", num, payload); DBG_OUTPUT_PORT.printf("WS: [%u] get Text: %s\n", num, payload);
// # ==> Set main color // # ==> Set main color
if(payload[0] == '#') { if (payload[0] == '#') {
// decode rgb data // decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
main_color.red = ((rgb >> 16) & 0xFF); main_color.red = ((rgb >> 16) & 0xFF);
@ -131,7 +131,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
} }
// # ==> Set delay // # ==> Set delay
if(payload[0] == '?') { if (payload[0] == '?') {
// decode delay data // decode delay data
uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
delay_ms = ((d >> 0) & 0xFF); delay_ms = ((d >> 0) & 0xFF);
@ -140,7 +140,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
} }
// # ==> Set brightness // # ==> Set brightness
if(payload[0] == '%') { if (payload[0] == '%') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10); uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
brightness = ((b >> 0) & 0xFF); brightness = ((b >> 0) & 0xFF);
DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness); DBG_OUTPUT_PORT.printf("WS: Set brightness to: [%u]\n", brightness);
@ -150,7 +150,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
// * ==> Set main color and light all LEDs (Shortcut) // * ==> Set main color and light all LEDs (Shortcut)
if(payload[0] == '*') { if (payload[0] == '*') {
// decode rgb data // decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16); uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
@ -168,7 +168,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
} }
// ! ==> Set single LED in given color // ! ==> Set single LED in given color
if(payload[0] == '!') { if (payload[0] == '!') {
// decode led index // decode led index
uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16); uint64_t rgb = (uint64_t) strtol((const char *) &payload[1], NULL, 16);
@ -190,7 +190,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
} }
// ! ==> Activate mode // ! ==> Activate mode
if(payload[0] == '=') { if (payload[0] == '=') {
// we get mode data // we get mode data
String str_mode = String((char *) &payload[0]); String str_mode = String((char *) &payload[0]);