From e28df2e41a03973e0deacccf427539330142e973 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 29 Jun 2021 11:30:47 +0300 Subject: [PATCH] move to asyn server (to get websockets) --- fan_control/data/index.html | 2 +- fan_control/include/collector.hpp | 21 +++----- fan_control/include/ota.hpp | 1 - fan_control/include/server.hpp | 5 +- fan_control/platformio.ini | 4 +- fan_control/src/collector.cpp | 54 +++++--------------- fan_control/src/server.cpp | 85 +++++++++++++++++-------------- 7 files changed, 75 insertions(+), 97 deletions(-) diff --git a/fan_control/data/index.html b/fan_control/data/index.html index 15554c6..26e84c1 100644 --- a/fan_control/data/index.html +++ b/fan_control/data/index.html @@ -24,7 +24,7 @@ console.log(this.responseText); var series = JSON.parse(this.responseText); var data = { series: series }; - new Chartist.Line(clazz, data); + new Chartist.Line(clazz, data , {high: 30 , low: -25}); } } xhttp.open("GET", url, true); diff --git a/fan_control/include/collector.hpp b/fan_control/include/collector.hpp index 564956b..0d53074 100644 --- a/fan_control/include/collector.hpp +++ b/fan_control/include/collector.hpp @@ -21,27 +21,22 @@ protected: float minute_in = 0 ; float minute_out = 0 ; + + int minute_counter = 0; + int week_counter = 0; + int month_counter = 0; + + void add_month(int from ) ; + +public: float week_in[WEEK]; float week_out[WEEK]; float month_in[MONTH]; float month_out[MONTH]; - int minute_counter = 0; - int week_counter = 0; - int month_counter = 0; - - String one_week( float[] ); - String one_month( float[] ); - - void add_month(int from ) ; - -public: - void add_week(float in , float out) ; void add(float in , float out) ; - String week_data(); - String month_data(); }; diff --git a/fan_control/include/ota.hpp b/fan_control/include/ota.hpp index 24b6827..dcba75b 100644 --- a/fan_control/include/ota.hpp +++ b/fan_control/include/ota.hpp @@ -2,7 +2,6 @@ #include #include -#include #include void ota_setup(const char* name); \ No newline at end of file diff --git a/fan_control/include/server.hpp b/fan_control/include/server.hpp index 92d1ed2..8bb09cf 100644 --- a/fan_control/include/server.hpp +++ b/fan_control/include/server.hpp @@ -1,7 +1,8 @@ -#include +#include +#include #include -static ESP8266WebServer server(80); +static AsyncWebServer server(80); void server_setup(); void server_loop(); diff --git a/fan_control/platformio.ini b/fan_control/platformio.ini index c3d9a22..0bd5524 100644 --- a/fan_control/platformio.ini +++ b/fan_control/platformio.ini @@ -16,8 +16,10 @@ framework = arduino board_build.filesystem = littlefs lib_deps = - ESP8266WiFi@1.0 + ESP8266WiFi + ottowinter/ESPAsyncWebServer-esphome @ 1.2.7 adafruit/DHT sensor library + bblanchon/ArduinoJson @ ^6.18.0 monitor_speed = 115200 upload_speed = 115200 diff --git a/fan_control/src/collector.cpp b/fan_control/src/collector.cpp index 1381349..822c4e5 100644 --- a/fan_control/src/collector.cpp +++ b/fan_control/src/collector.cpp @@ -21,6 +21,13 @@ DHT dht_out(5, DHT11); // D1 on mini Collector collector; +float next_rand( float old ){ + float f = old + random(-30 , 30) / 100.0 ; + if( f > 30.0) f = 30.0 ; + if( f < -20.0) f = -20.0 ; + return f; +} + void collector_setup(){ // Initialize device. dht_in.begin(); @@ -29,8 +36,8 @@ void collector_setup(){ float last_out = 20; for( int a = 0; a < PER_HOUR*WEEK ; a += 1 ) { collector.add_week( last_in , last_out ); - last_in += random(-30 , 30) / 100.0 ; - last_out += random(-30 , 30) / 100.0 ; + last_in = next_rand(last_in); + last_out = next_rand(last_out); } } @@ -43,11 +50,13 @@ void collector_loop(){ DEBUG_OUT.println(in); DEBUG_OUT.print(F("Temperature outside: ")); DEBUG_OUT.println(out); - //collector.add(in , out); + collector.add(in , out); } void Collector::add(float in , float out) { + if(isnan(in) ) return ; + if(isnan(out) ) return ; minute_in += in; minute_out += out; minute_counter++ ; @@ -88,42 +97,3 @@ void Collector::add_month(int from){ } } -String Collector::one_week(float week[]){ - String data = "["; - data += String(week[0] , 2) ; - for( int a = 1; a < WEEK ; a += 1 ) { - data += "," ; - data += String(week[a] , 2) ; - } - data += "]"; - return data ; -} - -String Collector::week_data(){ - String data = "["; - data += one_week(week_in) ; - data += "," ; - data += one_week(week_out) ; - data += "]"; - return data; -} - -String Collector::one_month(float month[]){ - String data = "["; - data += String(month[0] , 2); - for( int a = 1; a < MONTH ; a += 1 ) { - data += "," ; - data += String(month[a], 2) ; - } - data += "]"; - return data ; -} - -String Collector::month_data(){ - String data = "["; - data += one_month(month_in) ; - data += "," ; - data += one_month(month_out) ; - data += "]"; - return data ; -} diff --git a/fan_control/src/server.cpp b/fan_control/src/server.cpp index 7d6d4d9..aaf536e 100644 --- a/fan_control/src/server.cpp +++ b/fan_control/src/server.cpp @@ -2,8 +2,12 @@ #include "collector.hpp" #include "serial.hpp" +#include "AsyncJson.h" +#include "ArduinoJson.h" + #include +#define DEBUG_OUT Serial String getContentType(String filename){ if(filename.indexOf(".htm") > 0) return "text/html"; @@ -19,52 +23,59 @@ String getContentType(String filename){ return "text/plain"; } -bool handleFileRead(String path ){ - DEBUG_OUT.println("handleFileRead: " + path); - if(path.endsWith("/")) path += "index.html"; - String contentType = getContentType(path) + ";charset=utf-8"; - if(LittleFS.exists(path)){ - DEBUG_OUT.println("handle: " + path); - File file = LittleFS.open(path, "r"); - if(path.endsWith(".gz")){ - //server.sendHeader("Content-Encoding" , "gzip"); - } else { - contentType += ";charset=utf-8"; - } - server.streamFile(file, contentType); - file.close(); - return true; - } - return false; -} -void getWeekly() { - DEBUG_OUT.println("Weekly start"); - String data = collector.week_data(); - DEBUG_OUT.println("Weekly end"); - server.send(200, "text/html", data); -} -void getMonthly() { - DEBUG_OUT.println("Monthly start"); - String data = collector.month_data(); - DEBUG_OUT.println("Monthly end"); - server.send(200, "text/html", data); +AsyncCallbackJsonWebHandler* weekly = new AsyncCallbackJsonWebHandler("/weekly", [](AsyncWebServerRequest *request, JsonVariant &json) { + const JsonArray& root_array = json.as(); + JsonArray nested = root_array.createNestedArray(); + copyArray(collector.week_in , WEEK , nested); + nested = root_array.createNestedArray(); + copyArray(collector.week_out , WEEK , nested); +}); +AsyncCallbackJsonWebHandler* monthly = new AsyncCallbackJsonWebHandler("/monthly", [](AsyncWebServerRequest *request, JsonVariant &json) { + const JsonArray& root_array = json.as(); + JsonArray nested = root_array.createNestedArray(); + copyArray(collector.month_in , MONTH , nested); + nested = root_array.createNestedArray(); + copyArray(collector.month_out , MONTH , nested); +}); + +void notFound(AsyncWebServerRequest *request) { + request->send(404, "text/plain", "Not found"); } void server_setup(){ LittleFS.begin(); - server.on("/weekly", getWeekly); - server.on("/monthly", getMonthly); + server.addHandler(weekly); + server.addHandler(monthly); + + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ + DEBUG_OUT.println("Index.html"); + request->send(LittleFS, "/index.html", "text/html;charset=utf-8"); + }); + server.on("/chartist.min.css.gz", HTTP_GET, [](AsyncWebServerRequest *request){ + DEBUG_OUT.println("/chartist.min.css.gz"); + AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/chartist.min.css.gz", "text/css;charset=utf-8"); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + server.on("/chartist.min.js.gz", HTTP_GET, [](AsyncWebServerRequest *request){ + DEBUG_OUT.println("/chartist.min.js.gz"); + AsyncWebServerResponse *response = request->beginResponse(LittleFS, "/chartist.min.js.gz", "application/javascript;charset=utf-8"); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request){ + DEBUG_OUT.println("/favicon.ico"); + request->send(LittleFS, "/favicon.ico", "image/x-icon"); + }); - server.begin(); DEBUG_OUT.println("HTTP server started"); - server.onNotFound([](){ - if(!handleFileRead(server.uri())) - server.send(404, "text/plain", "FileNotFound"); - }); + server.onNotFound(notFound); + + server.begin(); } void server_loop() { - server.handleClient(); + }