Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
VoltagePage.ino
Go to the documentation of this file.
1#include <ELClientWebServer.h>
2#include <avr/io.h>
3
4#define SAMPLE_COUNT 100
5#define PERIOD_COUNT (135 * SAMPLE_COUNT)
6
7uint16_t adc_min = 0xFFFF; // min value of ADC
8uint16_t adc_max = 0; // max value of ADC
9uint32_t adc_avg = 0; // AVG average
10
11uint16_t sample_count; // collected sample
12uint32_t voltage_avg = 0; // AVG used for voltage measurement
13uint16_t measured_voltage = 0; // measured voltage
14
15#define MAX_HISTORY 3 // max history count
16
17uint8_t history_cnt = 0; // number of histories
18uint32_t history_ts[MAX_HISTORY]; // timestamp
19uint16_t history_min[MAX_HISTORY]; // min
20uint16_t history_max[MAX_HISTORY]; // max
21uint16_t history_avg[MAX_HISTORY]; // avg
22
23uint16_t calibrate = 0x128; // calibrate this manually
24
25// voltage measuring loop
27{
28 uint16_t adc = analogRead(A0); // read ADC
29
30 // set min/max/avg
31 if( adc < adc_min )
32 adc_min = adc;
33 if( adc > adc_max )
34 adc_max = adc;
35 adc_avg += adc;
36
37 voltage_avg += adc;
39
40 if( (sample_count % SAMPLE_COUNT) == 0 ) // max samples reached?
41 {
42 // calculate measured voltage
45 voltage_avg = 0;
46 }
48 {
49 // calculate min/max/avg and put into history
50
51 for(int8_t i=MAX_HISTORY-2; i >=0; i-- )
52 {
53 history_ts[i+1] = history_ts[i];
54 history_min[i+1] = history_min[i];
55 history_max[i+1] = history_max[i];
56 history_avg[i+1] = history_avg[i];
57 }
58
59 history_ts[0] = millis();
60 history_min[0] = (uint32_t)adc_min * calibrate / 256;
61 history_max[0] = (uint32_t)adc_max * calibrate / 256;
63
64 adc_min = 0xFFFF;
65 adc_max = 0;
66 adc_avg = 0;
67
70 sample_count = 0;
71 }
72}
73
74// sprintf %f is not supported on Arduino...
75String floatToString(float f)
76{
77 int16_t intg = (int16_t)(f * 100.f);
78 int16_t int_part = intg / 100;
79 int16_t fract_part = intg % 100;
80
81 char buf[20];
82 sprintf(buf, "%d.%02d", int_part, fract_part);
83
84 return String(buf);
85}
86
87void voltageRefreshCb(const char * url)
88{
89 // calculate voltage value
90 String v = floatToString((float)measured_voltage / 256.f);
91 v += " V";
92 webServer.setArgString(F("voltage"), v.begin());
93
94 char buf[20];
95 // calculate history table
96 String table = F("[[\"Time\",\"Min\",\"AVG\",\"Max\"]");
97
98 for(uint8_t i=0; i < history_cnt; i++ )
99 {
100 float min_f = (float)history_min[i] / 256.f;
101 float max_f = (float)history_max[i] / 256.f;
102 float avg_f = (float)history_avg[i] / 256.f;
103
104 table += F(",[\"");
105 table += (history_ts[i] / 1000);
106 table += F(" s\",\"");
107 table += floatToString(min_f);
108 table += " V\",\"";
109 table += floatToString(avg_f);
110 table += " V\",\"";
111 table += floatToString(max_f);
112 table += " V\"]";
113 }
114
115 table += ']';
116 webServer.setArgJson(F("table"), table.begin());
117}
118
119// page setup
121{
122 analogReference(DEFAULT);
123 sample_count = 0;
124
125 URLHandler *voltageHandler = webServer.createURLHandler(F("/Voltage.html.json"));
126
127 voltageHandler->loadCb.attach(voltageRefreshCb);
128 voltageHandler->refreshCb.attach(voltageRefreshCb);
129}
Definitions for ELClientWebServer.
void voltageLoop()
uint16_t calibrate
#define SAMPLE_COUNT
String floatToString(float f)
uint16_t history_min[MAX_HISTORY]
uint8_t history_cnt
void voltageRefreshCb(const char *url)
void voltageInit()
uint16_t history_max[MAX_HISTORY]
uint32_t voltage_avg
uint16_t history_avg[MAX_HISTORY]
uint16_t measured_voltage
#define PERIOD_COUNT
uint32_t adc_avg
uint16_t sample_count
#define MAX_HISTORY
uint16_t adc_max
uint32_t history_ts[MAX_HISTORY]
uint16_t adc_min
void attach(T *item, retT(T::*method)(argT))
Definition FP.h:147
FP< void, char * > loadCb
Callback for HTML page loading.
FP< void, char * > refreshCb
Callback for HTML page refresh.