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
7
uint16_t
adc_min
= 0xFFFF;
// min value of ADC
8
uint16_t
adc_max
= 0;
// max value of ADC
9
uint32_t
adc_avg
= 0;
// AVG average
10
11
uint16_t
sample_count
;
// collected sample
12
uint32_t
voltage_avg
= 0;
// AVG used for voltage measurement
13
uint16_t
measured_voltage
= 0;
// measured voltage
14
15
#define MAX_HISTORY 3
// max history count
16
17
uint8_t
history_cnt
= 0;
// number of histories
18
uint32_t
history_ts
[
MAX_HISTORY
];
// timestamp
19
uint16_t
history_min
[
MAX_HISTORY
];
// min
20
uint16_t
history_max
[
MAX_HISTORY
];
// max
21
uint16_t
history_avg
[
MAX_HISTORY
];
// avg
22
23
uint16_t
calibrate
= 0x128;
// calibrate this manually
24
25
// voltage measuring loop
26
void
voltageLoop
()
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;
38
sample_count
++;
39
40
if
( (
sample_count
%
SAMPLE_COUNT
) == 0 )
// max samples reached?
41
{
42
// calculate measured voltage
43
voltage_avg
/=
SAMPLE_COUNT
;
44
measured_voltage
=
voltage_avg
*
calibrate
/ 256;
45
voltage_avg
= 0;
46
}
47
if
(
sample_count
==
PERIOD_COUNT
)
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;
62
history_avg
[0] = (
adc_avg
/
PERIOD_COUNT
) *
calibrate
/ 256;
63
64
adc_min
= 0xFFFF;
65
adc_max
= 0;
66
adc_avg
= 0;
67
68
if
(
history_cnt
<
MAX_HISTORY
)
69
history_cnt
++;
70
sample_count
= 0;
71
}
72
}
73
74
// sprintf %f is not supported on Arduino...
75
String
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
87
void
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
120
void
voltageInit
()
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
}
ELClientWebServer.h
Definitions for ELClientWebServer.
voltageLoop
void voltageLoop()
Definition
VoltagePage.ino:26
calibrate
uint16_t calibrate
Definition
VoltagePage.ino:23
SAMPLE_COUNT
#define SAMPLE_COUNT
Definition
VoltagePage.ino:4
floatToString
String floatToString(float f)
Definition
VoltagePage.ino:75
history_min
uint16_t history_min[MAX_HISTORY]
Definition
VoltagePage.ino:19
history_cnt
uint8_t history_cnt
Definition
VoltagePage.ino:17
voltageRefreshCb
void voltageRefreshCb(const char *url)
Definition
VoltagePage.ino:87
voltageInit
void voltageInit()
Definition
VoltagePage.ino:120
history_max
uint16_t history_max[MAX_HISTORY]
Definition
VoltagePage.ino:20
voltage_avg
uint32_t voltage_avg
Definition
VoltagePage.ino:12
history_avg
uint16_t history_avg[MAX_HISTORY]
Definition
VoltagePage.ino:21
measured_voltage
uint16_t measured_voltage
Definition
VoltagePage.ino:13
PERIOD_COUNT
#define PERIOD_COUNT
Definition
VoltagePage.ino:5
adc_avg
uint32_t adc_avg
Definition
VoltagePage.ino:9
sample_count
uint16_t sample_count
Definition
VoltagePage.ino:11
MAX_HISTORY
#define MAX_HISTORY
Definition
VoltagePage.ino:15
adc_max
uint16_t adc_max
Definition
VoltagePage.ino:8
history_ts
uint32_t history_ts[MAX_HISTORY]
Definition
VoltagePage.ino:18
adc_min
uint16_t adc_min
Definition
VoltagePage.ino:7
FP::attach
void attach(T *item, retT(T::*method)(argT))
Definition
FP.h:147
URL_HANDLER
Definition
ELClientWebServer.h:18
URL_HANDLER::loadCb
FP< void, char * > loadCb
Callback for HTML page loading.
Definition
ELClientWebServer.h:38
URL_HANDLER::refreshCb
FP< void, char * > refreshCb
Callback for HTML page refresh.
Definition
ELClientWebServer.h:56
Gbox420_Mega_Main
ELClient
examples
webserver_controls
VoltagePage.ino
Generated by
1.9.8