minor cleanup, start data collector (dummy) and rest

This commit is contained in:
Torsten Ruger 2021-06-27 21:46:11 +03:00
parent f6690fb43f
commit de0532bc93
5 changed files with 99 additions and 35 deletions

View File

@ -0,0 +1,31 @@
#include <vector>
using namespace std;
class Collector
{
protected:
float week[100];
float month[100];
float minute = 0 ;
int counter = 0;
int max = 100 ;
int bucket = 10;
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;
}
}
};

View File

@ -4,3 +4,4 @@
static ESP8266WebServer server(80); static ESP8266WebServer server(80);
void server_setup(); void server_setup();
void server_loop();

View File

@ -2,6 +2,7 @@
#include "ota.hpp" #include "ota.hpp"
#include "server.hpp" #include "server.hpp"
#include "collector.hpp"
// DHT Temperature & Humidity Sensor // DHT Temperature & Humidity Sensor
// Unified Sensor Library Example // Unified Sensor Library Example
@ -31,20 +32,20 @@ void setup() {
Serial.begin(115200); Serial.begin(115200);
ota_setup(ssid); ota_setup(ssid);
server_setup(); server_setup();
Serial.println(WiFi.softAPIP());
// Initialize device. // Initialize device.
dht_in.begin(); dht_in.begin();
dht_out.begin(); dht_out.begin();
Serial.println(F("DHT set up")); Serial.println(F("Setup done"));
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
} }
void loop() { void loop() {
ArduinoOTA.handle(); ArduinoOTA.handle();
server_loop();
// Delay between measurements. // Delay between measurements.
delay(delayMS); delay(1000);
// Get temperature event and print its value. // Get temperature event and print its value.
Serial.print(F("Temperature inside: ")); Serial.print(F("Temperature inside: "));

View File

@ -1,7 +1,7 @@
#include "ota.hpp" #include "ota.hpp"
void ota_setup(const char* ssid) { void ota_setup(const char* ssid) {
Serial.begin(115200);
Serial.println("Booting"); Serial.println("Booting");
boolean result = WiFi.softAP(ssid); boolean result = WiFi.softAP(ssid);
while (result == false) { while (result == false) {
@ -49,8 +49,4 @@ void ota_setup(const char* ssid) {
else if (error == OTA_END_ERROR) Serial.println("End Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed");
}); });
ArduinoOTA.begin(); ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} }

View File

@ -7,31 +7,33 @@ ESP8266WebServer* serv = NULL;
String getContentType(String filename){ String getContentType(String filename){
if(filename.endsWith(".htm")) return "text/html"; if(filename.indexOf(".htm") > 0) return "text/html";
else if(filename.endsWith(".html")) return "text/html"; else if(filename.indexOf(".html")> 0) return "text/html";
else if(filename.endsWith(".css")) return "text/css"; else if(filename.indexOf(".css")> 0) return "text/css";
else if(filename.endsWith(".js")) return "application/javascript"; else if(filename.indexOf(".js")> 0) return "application/javascript";
else if(filename.endsWith(".png")) return "image/png"; else if(filename.indexOf(".png")> 0) return "image/png";
else if(filename.endsWith(".gif")) return "image/gif"; else if(filename.indexOf(".gif")> 0) return "image/gif";
else if(filename.endsWith(".jpg")) return "image/jpeg"; else if(filename.indexOf(".jpg")> 0) return "image/jpeg";
else if(filename.endsWith(".ico")) return "image/x-icon"; else if(filename.indexOf(".ico")> 0) return "image/x-icon";
else if(filename.endsWith(".xml")) return "text/xml"; else if(filename.indexOf(".xml")> 0) return "text/xml";
else if(filename.endsWith(".pdf")) return "application/x-pdf"; else if(filename.indexOf(".pdf")> 0) return "application/x-pdf";
else if(filename.endsWith(".zip")) return "application/x-zip";
else if(filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain"; return "text/plain";
} }
bool handleFileRead(String path ){ bool handleFileRead(String path ){
DBG_OUTPUT_PORT.println("handleFileRead: " + path); DBG_OUTPUT_PORT.println("handleFileRead: " + path);
if(path.endsWith("/")) path += "index.html"; if(path.endsWith("/")) path += "index.html";
String contentType = getContentType(path); String contentType = getContentType(path) + ";charset=utf-8";
String pathWithGz = path + ".gz"; if(LittleFS.exists(path)){
if(LittleFS.exists(pathWithGz) || LittleFS.exists(path)){ DBG_OUTPUT_PORT.println("handle: " + path);
if(LittleFS.exists(pathWithGz))
path += ".gz";
File file = LittleFS.open(path, "r"); 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";
}
server.streamFile(file, contentType);
file.close(); file.close();
return true; return true;
} }
@ -42,11 +44,44 @@ void handleHours(){
} }
void server_setup(){ int LDRPin = A0;
LittleFS.begin(); int LDRReading = 0;
server.on("/hour", HTTP_GET, handleHours);
server.onNotFound([](){ int milisInterval = 2000;
if(!handleFileRead(server.uri())) int count = 0;
server.send(404, "text/plain", "FileNotFound");
}); 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 server_setup(){
LittleFS.begin();
server.on("/data", getData);
server.on("/hour", handleHours);
server.begin();
Serial.println("HTTP server started");
server.onNotFound([](){
if(!handleFileRead(server.uri()))
server.send(404, "text/plain", "FileNotFound");
});
}
void server_loop() {
server.handleClient();
}