loading data from collector

This commit is contained in:
Torsten Ruger 2021-06-28 11:29:15 +03:00
parent 568a8b88c3
commit abe85144a1
4 changed files with 82 additions and 51 deletions

View File

@ -16,20 +16,24 @@
<h1> Monthly </h1> <h1> Monthly </h1>
<div class="ct-chart chart2"></div> <div class="ct-chart chart2"></div>
<script > <script >
function load_charts(){
function load_chart( url , clazz ){
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText); console.log(this.responseText);
var series = [5, 2, 4, 2, 0] ; // JSON.parse(this.responseText); var series = JSON.parse(this.responseText);
var data = { series: [ series ] }; var data = { series: series };
new Chartist.Line('.chart1', data); new Chartist.Line(clazz, data);
} }
} }
xhttp.open("GET", "/data", true); xhttp.open("GET", url, true);
xhttp.send(); xhttp.send();
}; };
//monthly = new Chartist.Line('.chart2', data); function load_charts(){
load_chart("/weekly" , ".chart1");
load_chart("/monthly" , ".chart2");
}
</script> </script>
<script onload="load_charts();" src="/chartist.min.js.gz"> </script> <script onload="load_charts();" src="/chartist.min.js.gz"> </script>

View File

@ -8,20 +8,29 @@ void collector_setup();
void collector_loop(); void collector_loop();
#define WEEK 1500
#define MONTH 750
class Collector class Collector
{ {
protected: protected:
float week_in[1500]; // 10 per hour, 1 week float week_in[WEEK]; // 10 per hour, 1 week
float week_out[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_in[750]; // 1 per hour, about a month
float month_out[750]; // 1 per hour, about a month float month_out[750]; // 1 per hour, about a month
float minute = 0 ; float minute = 0 ;
int counter = 0; int counter = 0;
int bucket = 30; // sampling every 2 sec
String one_week( float[] );
String one_month( float[] );
public: public:
const int bucket = 30; // sampling every 2 sec
void add(float in , float out) ; void add(float in , float out) ;
String week_data();
String month_data();
}; };
extern Collector collector; extern Collector collector;

View File

@ -26,11 +26,13 @@ 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 ) {
collector.add( random(10, 30) , random(10 , 30) );
}
} }
void collector_loop(){ void collector_loop(){
delay(delayMS); delay(delayMS);
collector.add(1 , 2);
// Get temperature event and print its value. // Get temperature event and print its value.
Serial.print(F("Temperature inside: ")); Serial.print(F("Temperature inside: "));
@ -41,18 +43,48 @@ void collector_loop(){
Serial.println("°C"); Serial.println("°C");
} }
void Collector::add(float in , float out) void Collector::add(float in , float out)
{ {
minute += in; minute += in;
counter++ ; counter++ ;
if(counter % bucket){ if(counter % bucket){
int at_week = counter / bucket; int at_week = counter / bucket;
week_in[at_week] = minute / bucket; week_in[at_week] = minute / bucket;
minute = 0; minute = 0;
}
if(counter % (bucket*bucket)){
int at_week = counter / bucket;
month_in[at_week] = minute / bucket;
counter = 0;
}
} }
if(counter % (bucket*bucket)){
int at_week = counter / bucket;
month_in[at_week] = minute / bucket;
counter = 0;
}
}
String Collector::one_week(float week[]){
String data = "[";
data += week[0];
for( int a = 1; a < WEEK ; a = a + 1 ) {
data += "," ;
data += week[a] ;
}
return data + "]";
}
String Collector::week_data(){
String data = "[";
return data + one_week(week_in) + "," + one_week(week_out) + "]";
}
String Collector::one_month(float month[]){
String data = "[";
data += month[0];
for( int a = 1; a < MONTH ; a = a + 1 ) {
data += "," ;
data += month[a] ;
}
return data + "]";
}
String Collector::month_data(){
String data = "[";
return data + one_month(month_in) + "," + one_month(month_out) + "]";
}

View File

@ -1,4 +1,5 @@
#include "server.hpp" #include "server.hpp"
#include "collector.hpp"
#include <FS.h> #include <FS.h>
#define DBG_OUTPUT_PORT Serial #define DBG_OUTPUT_PORT Serial
@ -37,38 +38,23 @@ bool handleFileRead(String path ){
return false; return false;
} }
void handleHours(){ void getWeekly() {
Serial.println("Weekly start");
String data = collector.week_data();
Serial.println("Weekly end");
server.send(200, "text/html", data);
} }
void getMonthly() {
int LDRPin = A0; Serial.println("Monthly start");
int LDRReading = 0; String data = collector.month_data();
Serial.println("Monthly end");
int milisInterval = 2000; server.send(200, "text/html", data);
int count = 0;
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(){ void server_setup(){
LittleFS.begin(); LittleFS.begin();
server.on("/data", getData); server.on("/weekly", getWeekly);
server.on("/hour", handleHours); server.on("/monthly", getMonthly);
server.begin(); server.begin();
Serial.println("HTTP server started"); Serial.println("HTTP server started");