loading data from collector
This commit is contained in:
parent
568a8b88c3
commit
abe85144a1
@ -16,20 +16,24 @@
|
||||
<h1> Monthly </h1>
|
||||
<div class="ct-chart chart2"></div>
|
||||
<script >
|
||||
function load_charts(){
|
||||
|
||||
function load_chart( url , clazz ){
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
console.log(this.responseText);
|
||||
var series = [5, 2, 4, 2, 0] ; // JSON.parse(this.responseText);
|
||||
var data = { series: [ series ] };
|
||||
new Chartist.Line('.chart1', data);
|
||||
var series = JSON.parse(this.responseText);
|
||||
var data = { series: series };
|
||||
new Chartist.Line(clazz, data);
|
||||
}
|
||||
}
|
||||
xhttp.open("GET", "/data", true);
|
||||
xhttp.open("GET", url, true);
|
||||
xhttp.send();
|
||||
};
|
||||
//monthly = new Chartist.Line('.chart2', data);
|
||||
function load_charts(){
|
||||
load_chart("/weekly" , ".chart1");
|
||||
load_chart("/monthly" , ".chart2");
|
||||
}
|
||||
</script>
|
||||
<script onload="load_charts();" src="/chartist.min.js.gz"> </script>
|
||||
|
||||
|
@ -8,20 +8,29 @@ void collector_setup();
|
||||
|
||||
void collector_loop();
|
||||
|
||||
#define WEEK 1500
|
||||
#define MONTH 750
|
||||
|
||||
class Collector
|
||||
{
|
||||
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 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 bucket = 30; // sampling every 2 sec
|
||||
|
||||
String one_week( float[] );
|
||||
String one_month( float[] );
|
||||
|
||||
public:
|
||||
const int bucket = 30; // sampling every 2 sec
|
||||
|
||||
void add(float in , float out) ;
|
||||
|
||||
String week_data();
|
||||
String month_data();
|
||||
|
||||
};
|
||||
|
||||
extern Collector collector;
|
||||
|
@ -26,11 +26,13 @@ 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) );
|
||||
}
|
||||
}
|
||||
|
||||
void collector_loop(){
|
||||
delay(delayMS);
|
||||
collector.add(1 , 2);
|
||||
|
||||
// Get temperature event and print its value.
|
||||
Serial.print(F("Temperature inside: "));
|
||||
@ -41,18 +43,48 @@ void collector_loop(){
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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) + "]";
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "server.hpp"
|
||||
#include "collector.hpp"
|
||||
#include <FS.h>
|
||||
|
||||
#define DBG_OUTPUT_PORT Serial
|
||||
@ -37,38 +38,23 @@ bool handleFileRead(String path ){
|
||||
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);
|
||||
}
|
||||
|
||||
int LDRPin = A0;
|
||||
int LDRReading = 0;
|
||||
|
||||
int milisInterval = 2000;
|
||||
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 getMonthly() {
|
||||
Serial.println("Monthly start");
|
||||
String data = collector.month_data();
|
||||
Serial.println("Monthly end");
|
||||
server.send(200, "text/html", data);
|
||||
}
|
||||
|
||||
void server_setup(){
|
||||
LittleFS.begin();
|
||||
server.on("/data", getData);
|
||||
server.on("/hour", handleHours);
|
||||
server.on("/weekly", getWeekly);
|
||||
server.on("/monthly", getMonthly);
|
||||
|
||||
server.begin();
|
||||
Serial.println("HTTP server started");
|
||||
|
Loading…
Reference in New Issue
Block a user