diff --git a/fan_control/include/collector.hpp b/fan_control/include/collector.hpp index 26373aa..6ac1e0f 100644 --- a/fan_control/include/collector.hpp +++ b/fan_control/include/collector.hpp @@ -1,31 +1,27 @@ +#include + #include + using namespace std; +void collector_setup(); + +void collector_loop(); + class Collector { protected: - float week[100]; - float month[100]; + float week_in[1500]; // 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 max = 100 ; - int bucket = 10; + int bucket = 30; // sampling every 2 sec public: - void add(float val) - { - minute += val; - counter++ ; - if(counter % bucket){ - int at_week = counter / bucket; - week[at_week] = minute / bucket; - minute = 0; - } - if(counter % (bucket*bucket)){ - int at_week = counter / bucket; - month[at_week] = minute / bucket; - counter = 0; - } + void add(float in , float out) ; + +}; - } -}; \ No newline at end of file +extern Collector collector; diff --git a/fan_control/src/collector.cpp b/fan_control/src/collector.cpp new file mode 100644 index 0000000..f9cd017 --- /dev/null +++ b/fan_control/src/collector.cpp @@ -0,0 +1,58 @@ +#include "collector.hpp" + +#include +#include +#include +// DHT Temperature & Humidity Sensor +// Unified Sensor Library Example +// Written by Tony DiCola for Adafruit Industries +// Released under an MIT license. + +// REQUIRES the following Arduino libraries: +// - 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 +DHT dht_out(5, DHT11); // D1 on mini + +Collector collector; + +void collector_setup(){ + // Initialize device. + dht_in.begin(); + dht_out.begin(); +} + +void collector_loop(){ + delay(delayMS); + collector.add(1 , 2); + + // Get temperature event and print its value. + Serial.print(F("Temperature inside: ")); + Serial.print(dht_in.readTemperature()); + Serial.println(F("°C")); + Serial.print(F("Temperature outside: ")); + Serial.print(dht_out.readTemperature()); + 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; + } + } diff --git a/fan_control/src/main.cpp b/fan_control/src/main.cpp index fc24f83..f9346e0 100644 --- a/fan_control/src/main.cpp +++ b/fan_control/src/main.cpp @@ -4,26 +4,6 @@ #include "server.hpp" #include "collector.hpp" -// DHT Temperature & Humidity Sensor -// Unified Sensor Library Example -// Written by Tony DiCola for Adafruit Industries -// Released under an MIT license. - -// REQUIRES the following Arduino libraries: -// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library -// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor - -#include -#include -#include - - -// mini https://chewett.co.uk/blog/1066/pin-numbering-for-wemos-d1-mini-esp8266/ -DHT dht_in(4, DHT11); // D2 on mini -DHT dht_out(5, DHT11); // D1 on mini - -// Set delay between sensor readings based on sensor details. -uint32_t delayMS = 3000 ; const char* ssid = "fan_XXX"; @@ -32,9 +12,6 @@ void setup() { Serial.begin(115200); ota_setup(ssid); server_setup(); - // Initialize device. - dht_in.begin(); - dht_out.begin(); Serial.println(F("Setup done")); Serial.print("IP address: "); Serial.println(WiFi.softAPIP()); @@ -43,15 +20,5 @@ void setup() { void loop() { ArduinoOTA.handle(); server_loop(); - - // Delay between measurements. - delay(1000); - - // Get temperature event and print its value. - Serial.print(F("Temperature inside: ")); - Serial.print(dht_in.readTemperature()); - Serial.println(F("°C")); - Serial.print(F("Temperature outside: ")); - Serial.print(dht_out.readTemperature()); - Serial.println("°C"); + collector_loop(); } diff --git a/fan_control/src/server.cpp b/fan_control/src/server.cpp index 2881c64..a87aa0d 100644 --- a/fan_control/src/server.cpp +++ b/fan_control/src/server.cpp @@ -3,8 +3,6 @@ #define DBG_OUTPUT_PORT Serial -ESP8266WebServer* serv = NULL; - String getContentType(String filename){ if(filename.indexOf(".htm") > 0) return "text/html"; @@ -28,7 +26,6 @@ bool handleFileRead(String path ){ DBG_OUTPUT_PORT.println("handle: " + path); File file = LittleFS.open(path, "r"); if(path.endsWith(".gz")){ - DBG_OUTPUT_PORT.println("sETTING GZIP" ); //server.sendHeader("Content-Encoding" , "gzip"); } else { contentType += ";charset=utf-8";