fix collector logic

This commit is contained in:
Torsten Ruger 2021-06-28 15:15:26 +03:00
parent e37fa901ab
commit 5a8d807e18
3 changed files with 88 additions and 37 deletions

View File

@ -8,25 +8,37 @@ void collector_setup();
void collector_loop(); void collector_loop();
#define WEEK 1500 // Set delay between sensor readings.
#define MONTH 750 #define DELAY 3000 //3s , longer and the web requests time-out
#define MINUTE 120 // 6 min average
#define WEEK 1000 // 4 days worth of 6min sample
#define MONTH 750 // 1 month of hourly sample
#define PER_HOUR 10
class Collector class Collector
{ {
protected: protected:
float week_in[WEEK]; // 10 per hour, 1 week float minute_in = 0 ;
float week_out[1500]; // 10 per hour, 1 week float minute_out = 0 ;
float month_in[750]; // 1 per hour, about a month
float month_out[750]; // 1 per hour, about a month float week_in[WEEK];
float minute = 0 ; float week_out[WEEK];
int counter = 0;
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_week( float[] );
String one_month( float[] ); String one_month( float[] );
public: void add_month(int from ) ;
const int bucket = 30; // sampling every 2 sec
public:
void add_week(float in , float out) ;
void add(float in , float out) ; void add(float in , float out) ;
String week_data(); String week_data();
String month_data(); String month_data();

View File

@ -4,6 +4,7 @@
#include <Adafruit_Sensor.h> #include <Adafruit_Sensor.h>
#include <DHT.h> #include <DHT.h>
#include <DHT_U.h> #include <DHT_U.h>
// DHT Temperature & Humidity Sensor // DHT Temperature & Humidity Sensor
// Unified Sensor Library Example // Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries // Written by Tony DiCola for Adafruit Industries
@ -13,9 +14,6 @@
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library // - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor // - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
// Set delay between sensor readings based on sensor details.
uint32_t delayMS = 3000 ;
// mini https://chewett.co.uk/blog/1066/pin-numbering-for-wemos-d1-mini-esp8266/ // mini https://chewett.co.uk/blog/1066/pin-numbering-for-wemos-d1-mini-esp8266/
DHT dht_in(4, DHT11); // D2 on mini DHT dht_in(4, DHT11); // D2 on mini
@ -27,13 +25,17 @@ void collector_setup(){
// Initialize device. // Initialize device.
dht_in.begin(); dht_in.begin();
dht_out.begin(); dht_out.begin();
for( int a = 0; a < 4* WEEK*collector.bucket ; a = a + 1 ) { float last_in = 20;
collector.add( random(10, 30) , random(10 , 30) ); 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 ;
} }
} }
void collector_loop(){ void collector_loop(){
delay(delayMS); delay(DELAY);
// Get temperature event and print its value. // Get temperature event and print its value.
float in = dht_in.readTemperature(); float in = dht_in.readTemperature();
float out = dht_out.readTemperature(); float out = dht_out.readTemperature();
@ -41,51 +43,87 @@ 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)
{ {
minute += in; minute_in += in;
counter++ ; minute_out += out;
if(counter % bucket){ minute_counter++ ;
int at_week = counter / bucket; if(minute_counter >= MINUTE) {
week_in[at_week] = minute / bucket; add_week(minute_in/MINUTE , minute_out / MINUTE);
minute = 0; minute_in = 0;
minute_out = 0;
minute_counter = 0;
} }
if(counter % (bucket*bucket)){ }
int at_week = counter / bucket;
month_in[at_week] = minute / bucket; void Collector::add_week(float in , float out){
counter = 0; week_in[week_counter] = in;
week_out[week_counter] = out;
if((week_counter % PER_HOUR) == 0){
add_month( week_counter - PER_HOUR);
}
week_counter++ ;
if(week_counter >= WEEK) {
week_counter = 0;
}
}
void Collector::add_month(int from){
if( from < 0) return;
if( from + PER_HOUR > WEEK) return ;
float month_i = 0;
float month_o = 0;
for(int i = 0 ; i < PER_HOUR ; i++){
month_i += week_in[ from + i];
month_o += week_out[ from + i];
}
month_in[month_counter] = month_i / PER_HOUR;
month_out[month_counter] = month_o / PER_HOUR ;
month_counter++ ;
if(month_counter >= MONTH) {
month_counter = 0;
} }
} }
String Collector::one_week(float week[]){ String Collector::one_week(float week[]){
String data = "["; String data = "[";
data += week[0]; data += String(week[0] , 2) ;
for( int a = 1; a < WEEK ; a = a + 1 ) { for( int a = 1; a < WEEK ; a += 1 ) {
data += "," ; data += "," ;
data += week[a] ; data += String(week[a] , 2) ;
} }
return data + "]"; data += "]";
return data ;
} }
String Collector::week_data(){ String Collector::week_data(){
String data = "["; String data = "[";
return data + one_week(week_in) + "," + one_week(week_out) + "]"; data += one_week(week_in) ;
data += "," ;
data += one_week(week_out) ;
data += "]";
return data;
} }
String Collector::one_month(float month[]){ String Collector::one_month(float month[]){
String data = "["; String data = "[";
data += month[0]; data += String(month[0] , 2);
for( int a = 1; a < MONTH ; a = a + 1 ) { for( int a = 1; a < MONTH ; a += 1 ) {
data += "," ; data += "," ;
data += month[a] ; data += String(month[a], 2) ;
} }
return data + "]"; data += "]";
return data ;
} }
String Collector::month_data(){ String Collector::month_data(){
String data = "["; String data = "[";
return data + one_month(month_in) + "," + one_month(month_out) + "]"; data += one_month(month_in) ;
data += "," ;
data += one_month(month_out) ;
data += "]";
return data ;
} }

View File

@ -12,6 +12,7 @@ void setup() {
DEBUG_OUT.begin(115200); DEBUG_OUT.begin(115200);
ota_setup(ssid); ota_setup(ssid);
server_setup(); server_setup();
collector_setup();
DEBUG_OUT.println(F("Setup done")); DEBUG_OUT.println(F("Setup done"));
DEBUG_OUT.print("IP address: "); DEBUG_OUT.print("IP address: ");
DEBUG_OUT.println(WiFi.softAPIP()); DEBUG_OUT.println(WiFi.softAPIP());