From abe85144a1860d75c932e40f964a7ce96420656d Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 28 Jun 2021 11:29:15 +0300 Subject: [PATCH] loading data from collector --- fan_control/data/index.html | 16 +++++--- fan_control/include/collector.hpp | 15 ++++++-- fan_control/src/collector.cpp | 62 +++++++++++++++++++++++-------- fan_control/src/server.cpp | 40 +++++++------------- 4 files changed, 82 insertions(+), 51 deletions(-) diff --git a/fan_control/data/index.html b/fan_control/data/index.html index 7c4a26d..15554c6 100644 --- a/fan_control/data/index.html +++ b/fan_control/data/index.html @@ -16,20 +16,24 @@

Monthly

diff --git a/fan_control/include/collector.hpp b/fan_control/include/collector.hpp index 6ac1e0f..5a8b198 100644 --- a/fan_control/include/collector.hpp +++ b/fan_control/include/collector.hpp @@ -8,20 +8,29 @@ void collector_setup(); void collector_loop(); +#define WEEK 1500 +#define MONTH 750 + class Collector { protected: - float week_in[1500]; // 10 per hour, 1 week + float week_in[WEEK]; // 10 per hour, 1 week float week_out[1500]; // 10 per hour, 1 week float month_in[750]; // 1 per hour, about a month float month_out[750]; // 1 per hour, about a month float minute = 0 ; int counter = 0; - int bucket = 30; // sampling every 2 sec + + String one_week( float[] ); + String one_month( float[] ); + public: + const int bucket = 30; // sampling every 2 sec void add(float in , float out) ; - + String week_data(); + String month_data(); + }; extern Collector collector; diff --git a/fan_control/src/collector.cpp b/fan_control/src/collector.cpp index f9cd017..151c2a4 100644 --- a/fan_control/src/collector.cpp +++ b/fan_control/src/collector.cpp @@ -26,11 +26,13 @@ void collector_setup(){ // Initialize device. dht_in.begin(); dht_out.begin(); + for( int a = 0; a < 4* WEEK*collector.bucket ; a = a + 1 ) { + collector.add( random(10, 30) , random(10 , 30) ); + } } void collector_loop(){ delay(delayMS); - collector.add(1 , 2); // Get temperature event and print its value. Serial.print(F("Temperature inside: ")); @@ -41,18 +43,48 @@ void collector_loop(){ Serial.println("°C"); } - void Collector::add(float in , float out) - { - minute += in; - counter++ ; - if(counter % bucket){ - int at_week = counter / bucket; - week_in[at_week] = minute / bucket; - minute = 0; - } - if(counter % (bucket*bucket)){ - int at_week = counter / bucket; - month_in[at_week] = minute / bucket; - counter = 0; - } +void Collector::add(float in , float out) +{ + minute += in; + counter++ ; + if(counter % bucket){ + int at_week = counter / bucket; + week_in[at_week] = minute / bucket; + minute = 0; } + if(counter % (bucket*bucket)){ + int at_week = counter / bucket; + month_in[at_week] = minute / bucket; + counter = 0; + } +} + +String Collector::one_week(float week[]){ + String data = "["; + data += week[0]; + for( int a = 1; a < WEEK ; a = a + 1 ) { + data += "," ; + data += week[a] ; + } + return data + "]"; +} + +String Collector::week_data(){ + String data = "["; + return data + one_week(week_in) + "," + one_week(week_out) + "]"; +} + +String Collector::one_month(float month[]){ + String data = "["; + data += month[0]; + for( int a = 1; a < MONTH ; a = a + 1 ) { + data += "," ; + data += month[a] ; + } + return data + "]"; +} + +String Collector::month_data(){ + String data = "["; + return data + one_month(month_in) + "," + one_month(month_out) + "]"; +} diff --git a/fan_control/src/server.cpp b/fan_control/src/server.cpp index a87aa0d..dc0de89 100644 --- a/fan_control/src/server.cpp +++ b/fan_control/src/server.cpp @@ -1,4 +1,5 @@ #include "server.hpp" +#include "collector.hpp" #include #define DBG_OUTPUT_PORT Serial @@ -37,38 +38,23 @@ bool handleFileRead(String path ){ return false; } -void handleHours(){ - +void getWeekly() { + Serial.println("Weekly start"); + String data = collector.week_data(); + Serial.println("Weekly end"); + server.send(200, "text/html", data); } - -int LDRPin = A0; -int LDRReading = 0; - -int milisInterval = 2000; -int count = 0; - -void getData() { - //This is a JSON formatted string that will be served. You can change the values to whatever like. - // {"data":[{"dataValue":"1024"},{"dataValue":"23"}]} This is essentially what is will output you can add more if you like - Serial.println("Data start"); - LDRReading = analogRead(LDRPin); - String text2 = "{\"data\":["; - text2 += "{\"dataValue\":\""; - text2 += "LDRReading"; - text2 += "\"},"; - text2 += "{\"dataValue\":\""; - text2 += "count"; - text2 += "\"}"; - text2 += "]}"; - Serial.println("Data end"); - server.send(200, "text/html", text2); - count++; +void getMonthly() { + Serial.println("Monthly start"); + String data = collector.month_data(); + Serial.println("Monthly end"); + server.send(200, "text/html", data); } void server_setup(){ LittleFS.begin(); - server.on("/data", getData); - server.on("/hour", handleHours); + server.on("/weekly", getWeekly); + server.on("/monthly", getMonthly); server.begin(); Serial.println("HTTP server started");