move to asyn server (to get websockets)
This commit is contained in:
parent
5a8d807e18
commit
e28df2e41a
@ -24,7 +24,7 @@
|
|||||||
console.log(this.responseText);
|
console.log(this.responseText);
|
||||||
var series = JSON.parse(this.responseText);
|
var series = JSON.parse(this.responseText);
|
||||||
var data = { series: series };
|
var data = { series: series };
|
||||||
new Chartist.Line(clazz, data);
|
new Chartist.Line(clazz, data , {high: 30 , low: -25});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xhttp.open("GET", url, true);
|
xhttp.open("GET", url, true);
|
||||||
|
@ -21,27 +21,22 @@ protected:
|
|||||||
float minute_in = 0 ;
|
float minute_in = 0 ;
|
||||||
float minute_out = 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_in[WEEK];
|
||||||
float week_out[WEEK];
|
float week_out[WEEK];
|
||||||
|
|
||||||
float month_in[MONTH];
|
float month_in[MONTH];
|
||||||
float month_out[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_week(float in , float out) ;
|
||||||
void add(float in , float out) ;
|
void add(float in , float out) ;
|
||||||
String week_data();
|
|
||||||
String month_data();
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <ESP8266mDNS.h>
|
#include <ESP8266mDNS.h>
|
||||||
#include <WiFiUdp.h>
|
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
|
|
||||||
void ota_setup(const char* name);
|
void ota_setup(const char* name);
|
@ -1,7 +1,8 @@
|
|||||||
#include <ESP8266WebServer.h>
|
#include <ESPAsyncTCP.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
|
|
||||||
static ESP8266WebServer server(80);
|
static AsyncWebServer server(80);
|
||||||
|
|
||||||
void server_setup();
|
void server_setup();
|
||||||
void server_loop();
|
void server_loop();
|
||||||
|
@ -16,8 +16,10 @@ framework = arduino
|
|||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
ESP8266WiFi@1.0
|
ESP8266WiFi
|
||||||
|
ottowinter/ESPAsyncWebServer-esphome @ 1.2.7
|
||||||
adafruit/DHT sensor library
|
adafruit/DHT sensor library
|
||||||
|
bblanchon/ArduinoJson @ ^6.18.0
|
||||||
|
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
upload_speed = 115200
|
upload_speed = 115200
|
||||||
|
@ -21,6 +21,13 @@ DHT dht_out(5, DHT11); // D1 on mini
|
|||||||
|
|
||||||
Collector collector;
|
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(){
|
void collector_setup(){
|
||||||
// Initialize device.
|
// Initialize device.
|
||||||
dht_in.begin();
|
dht_in.begin();
|
||||||
@ -29,8 +36,8 @@ void collector_setup(){
|
|||||||
float last_out = 20;
|
float last_out = 20;
|
||||||
for( int a = 0; a < PER_HOUR*WEEK ; a += 1 ) {
|
for( int a = 0; a < PER_HOUR*WEEK ; a += 1 ) {
|
||||||
collector.add_week( last_in , last_out );
|
collector.add_week( last_in , last_out );
|
||||||
last_in += random(-30 , 30) / 100.0 ;
|
last_in = next_rand(last_in);
|
||||||
last_out += random(-30 , 30) / 100.0 ;
|
last_out = next_rand(last_out);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,11 +50,13 @@ void collector_loop(){
|
|||||||
DEBUG_OUT.println(in);
|
DEBUG_OUT.println(in);
|
||||||
DEBUG_OUT.print(F("Temperature outside: "));
|
DEBUG_OUT.print(F("Temperature outside: "));
|
||||||
DEBUG_OUT.println(out);
|
DEBUG_OUT.println(out);
|
||||||
//collector.add(in , out);
|
collector.add(in , out);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Collector::add(float in , float out)
|
void Collector::add(float in , float out)
|
||||||
{
|
{
|
||||||
|
if(isnan(in) ) return ;
|
||||||
|
if(isnan(out) ) return ;
|
||||||
minute_in += in;
|
minute_in += in;
|
||||||
minute_out += out;
|
minute_out += out;
|
||||||
minute_counter++ ;
|
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 ;
|
|
||||||
}
|
|
||||||
|
@ -2,8 +2,12 @@
|
|||||||
#include "collector.hpp"
|
#include "collector.hpp"
|
||||||
#include "serial.hpp"
|
#include "serial.hpp"
|
||||||
|
|
||||||
|
#include "AsyncJson.h"
|
||||||
|
#include "ArduinoJson.h"
|
||||||
|
|
||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
|
|
||||||
|
#define DEBUG_OUT Serial
|
||||||
|
|
||||||
String getContentType(String filename){
|
String getContentType(String filename){
|
||||||
if(filename.indexOf(".htm") > 0) return "text/html";
|
if(filename.indexOf(".htm") > 0) return "text/html";
|
||||||
@ -19,52 +23,59 @@ String getContentType(String filename){
|
|||||||
return "text/plain";
|
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() {
|
AsyncCallbackJsonWebHandler* weekly = new AsyncCallbackJsonWebHandler("/weekly", [](AsyncWebServerRequest *request, JsonVariant &json) {
|
||||||
DEBUG_OUT.println("Weekly start");
|
const JsonArray& root_array = json.as<JsonArray>();
|
||||||
String data = collector.week_data();
|
JsonArray nested = root_array.createNestedArray();
|
||||||
DEBUG_OUT.println("Weekly end");
|
copyArray(collector.week_in , WEEK , nested);
|
||||||
server.send(200, "text/html", data);
|
nested = root_array.createNestedArray();
|
||||||
}
|
copyArray(collector.week_out , WEEK , nested);
|
||||||
void getMonthly() {
|
});
|
||||||
DEBUG_OUT.println("Monthly start");
|
AsyncCallbackJsonWebHandler* monthly = new AsyncCallbackJsonWebHandler("/monthly", [](AsyncWebServerRequest *request, JsonVariant &json) {
|
||||||
String data = collector.month_data();
|
const JsonArray& root_array = json.as<JsonArray>();
|
||||||
DEBUG_OUT.println("Monthly end");
|
JsonArray nested = root_array.createNestedArray();
|
||||||
server.send(200, "text/html", data);
|
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(){
|
void server_setup(){
|
||||||
LittleFS.begin();
|
LittleFS.begin();
|
||||||
server.on("/weekly", getWeekly);
|
server.addHandler(weekly);
|
||||||
server.on("/monthly", getMonthly);
|
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");
|
DEBUG_OUT.println("HTTP server started");
|
||||||
|
|
||||||
server.onNotFound([](){
|
server.onNotFound(notFound);
|
||||||
if(!handleFileRead(server.uri()))
|
|
||||||
server.send(404, "text/plain", "FileNotFound");
|
server.begin();
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void server_loop() {
|
void server_loop() {
|
||||||
server.handleClient();
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user