fix collector logic
This commit is contained in:
parent
e37fa901ab
commit
5a8d807e18
@ -8,25 +8,37 @@ void collector_setup();
|
||||
|
||||
void collector_loop();
|
||||
|
||||
#define WEEK 1500
|
||||
#define MONTH 750
|
||||
// Set delay between sensor readings.
|
||||
#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
|
||||
{
|
||||
protected:
|
||||
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;
|
||||
float minute_in = 0 ;
|
||||
float minute_out = 0 ;
|
||||
|
||||
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[] );
|
||||
|
||||
public:
|
||||
const int bucket = 30; // sampling every 2 sec
|
||||
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();
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <DHT.h>
|
||||
#include <DHT_U.h>
|
||||
|
||||
// DHT Temperature & Humidity Sensor
|
||||
// Unified Sensor Library Example
|
||||
// Written by Tony DiCola for Adafruit Industries
|
||||
@ -13,9 +14,6 @@
|
||||
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
|
||||
// - 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/
|
||||
DHT dht_in(4, DHT11); // D2 on mini
|
||||
@ -27,13 +25,17 @@ 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) );
|
||||
float last_in = 20;
|
||||
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(){
|
||||
delay(delayMS);
|
||||
delay(DELAY);
|
||||
// Get temperature event and print its value.
|
||||
float in = dht_in.readTemperature();
|
||||
float out = dht_out.readTemperature();
|
||||
@ -41,51 +43,87 @@ 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)
|
||||
{
|
||||
minute += in;
|
||||
counter++ ;
|
||||
if(counter % bucket){
|
||||
int at_week = counter / bucket;
|
||||
week_in[at_week] = minute / bucket;
|
||||
minute = 0;
|
||||
minute_in += in;
|
||||
minute_out += out;
|
||||
minute_counter++ ;
|
||||
if(minute_counter >= MINUTE) {
|
||||
add_week(minute_in/MINUTE , minute_out / MINUTE);
|
||||
minute_in = 0;
|
||||
minute_out = 0;
|
||||
minute_counter = 0;
|
||||
}
|
||||
if(counter % (bucket*bucket)){
|
||||
int at_week = counter / bucket;
|
||||
month_in[at_week] = minute / bucket;
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
void Collector::add_week(float in , float out){
|
||||
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 data = "[";
|
||||
data += week[0];
|
||||
for( int a = 1; a < WEEK ; a = a + 1 ) {
|
||||
data += String(week[0] , 2) ;
|
||||
for( int a = 1; a < WEEK ; a += 1 ) {
|
||||
data += "," ;
|
||||
data += week[a] ;
|
||||
data += String(week[a] , 2) ;
|
||||
}
|
||||
return data + "]";
|
||||
data += "]";
|
||||
return data ;
|
||||
}
|
||||
|
||||
String Collector::week_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 data = "[";
|
||||
data += month[0];
|
||||
for( int a = 1; a < MONTH ; a = a + 1 ) {
|
||||
data += String(month[0] , 2);
|
||||
for( int a = 1; a < MONTH ; a += 1 ) {
|
||||
data += "," ;
|
||||
data += month[a] ;
|
||||
data += String(month[a], 2) ;
|
||||
}
|
||||
return data + "]";
|
||||
data += "]";
|
||||
return data ;
|
||||
}
|
||||
|
||||
String Collector::month_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 ;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ void setup() {
|
||||
DEBUG_OUT.begin(115200);
|
||||
ota_setup(ssid);
|
||||
server_setup();
|
||||
collector_setup();
|
||||
DEBUG_OUT.println(F("Setup done"));
|
||||
DEBUG_OUT.print("IP address: ");
|
||||
DEBUG_OUT.println(WiFi.softAPIP());
|
||||
|
Loading…
Reference in New Issue
Block a user