moving sensor data collection to collector src

This commit is contained in:
Torsten Ruger 2021-06-27 22:12:14 +03:00
parent de0532bc93
commit 568a8b88c3
4 changed files with 75 additions and 57 deletions

View File

@ -1,31 +1,27 @@
#include <Arduino.h>
#include <vector>
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) ;
};
}
};
extern Collector collector;

View File

@ -0,0 +1,58 @@
#include "collector.hpp"
#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
// 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;
}
}

View File

@ -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 <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
// 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();
}

View File

@ -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";