move to asyn server (to get websockets)

This commit is contained in:
Torsten Ruger 2021-06-29 11:30:47 +03:00
parent 5a8d807e18
commit e28df2e41a
7 changed files with 75 additions and 97 deletions

View File

@ -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);

View File

@ -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();
};

View File

@ -2,7 +2,6 @@
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
void ota_setup(const char* name);

View File

@ -1,7 +1,8 @@
#include <ESP8266WebServer.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <LittleFS.h>
static ESP8266WebServer server(80);
static AsyncWebServer server(80);
void server_setup();
void server_loop();

View File

@ -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

View File

@ -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 ;
}

View File

@ -2,8 +2,12 @@
#include "collector.hpp"
#include "serial.hpp"
#include "AsyncJson.h"
#include "ArduinoJson.h"
#include <FS.h>
#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>();
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>();
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();
}