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
+30 -15
View File
@@ -30,7 +30,7 @@ WebSocketsServer webSocket = WebSocketsServer(81);
// ***************************************************************************
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#include <avr/power.h>
#endif
// Parameter 1 = number of pixels in strip
@@ -178,8 +178,8 @@ void setup() {
//list directory
server.on("/list", HTTP_GET, handleFileList);
//load editor
server.on("/edit", HTTP_GET, [](){
if(!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
server.on("/edit", HTTP_GET, []() {
if (!handleFileRead("/edit.htm")) server.send(404, "text/plain", "FileNotFound");
});
//create file
server.on("/edit", HTTP_PUT, handleFileCreate);
@@ -187,13 +187,15 @@ void setup() {
server.on("/edit", HTTP_DELETE, handleFileDelete);
//first callback is called after the request has ended with all parsed arguments
//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
server.on("/status", HTTP_GET, [](){
server.on("/esp_status", HTTP_GET, []() {
String json = "{";
json += "\"heap\":"+String(ESP.getFreeHeap());
json += ", \"analog\":"+String(analogRead(A0));
json += ", \"gpio\":"+String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json += "\"heap\":" + String(ESP.getFreeHeap());
json += ", \"analog\":" + String(analogRead(A0));
json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json += "}";
server.send(200, "text/json", json);
json = String();
@@ -202,18 +204,24 @@ void setup() {
//called when the url is not defined here
//use it to load content from SPIFFS
server.onNotFound([](){
if(!handleFileRead(server.uri()))
server.onNotFound([]() {
if (!handleFileRead(server.uri()))
handleNotFound();
});
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
// ***************************************************************************
server.on("/brightness", []() {
server.on("/set_brightness", []() {
if (server.arg("c").toInt() > 0) {
brightness = (int) server.arg("c").toInt() * 2.55;
} else {
@@ -235,15 +243,22 @@ void setup() {
});
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.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.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", []() {
@@ -321,7 +336,7 @@ void loop() {
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
mode = HOLD;
//mode = HOLD;
}
if (mode == ALL) {
uint16_t i;
+7 -7
View File
@@ -102,7 +102,7 @@ void getStatusJSON() {
}
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
switch(type) {
switch (type) {
case WStype_DISCONNECTED:
DBG_OUTPUT_PORT.printf("WS: [%u] Disconnected!\n", num);
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);
// # ==> Set main color
if(payload[0] == '#') {
if (payload[0] == '#') {
// decode rgb data
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
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
if(payload[0] == '?') {
if (payload[0] == '?') {
// decode delay data
uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
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
if(payload[0] == '%') {
if (payload[0] == '%') {
uint8_t b = (uint8_t) strtol((const char *) &payload[1], NULL, 10);
brightness = ((b >> 0) & 0xFF);
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)
if(payload[0] == '*') {
if (payload[0] == '*') {
// decode rgb data
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
if(payload[0] == '!') {
if (payload[0] == '!') {
// decode led index
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
if(payload[0] == '=') {
if (payload[0] == '=') {
// we get mode data
String str_mode = String((char *) &payload[0]);