-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.cpp
More file actions
175 lines (153 loc) · 4.97 KB
/
app.cpp
File metadata and controls
175 lines (153 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// wifi
#include <ESP8266WiFi.h>
#include "app.h"
// https://www.tinkercad.com/things/dJn6LkhyR5E-interface-test
uint16_t run_time = 0;
// timing
int timezone = 8;
uint32_t epoch = 0;
uint8_t second = 0;
uint8_t minute = 0;
uint8_t hour = 0;
uint8_t day = 0;
uint8_t month = 0;
uint16_t year = 0;
float ct_energy; // watt per minute
float pv_energy; // watt per minute
float dc_energy; // watt per minute
void appSetup()
{
ntpSetup();
// srne @ D7 D8
// snat @ D5 D6
if (display_enable)
displayLoop();
// if (dht11_enable)
// dht11Loop();
// if (beep_enable)
// beepSetup();
if (analog_enable)
analogSetup();
if (display_enable)
displaySetup();
// if (ads1115_enable)
// ads1115Setup();
if (srne_enable)
srneSetup();
if (snat_enable)
snatSetup();
}
void appLoop()
{
// static float _prev_device_voltage;
static uint32_t msTick = millis();
static uint32_t total_ct_power = ct_power;
static uint32_t total_pv_power = pv_power;
static uint32_t total_dc_power = dc_power;
// static uint8_t sTick;
if (millis() - msTick >= 1000) // 1000ms refresh rate
{
msTick = millis();
ntpLoop();
if (analog_enable)
analogLoop();
delay(1);
if (srne_enable)
srneLoop();
delay(1);
if (snat_enable)
snatLoop();
delay(1);
if (second >= 59)
{
// Convert to watt-minute
ct_energy = total_ct_power / 60.0;
pv_energy = total_pv_power / 60.0;
dc_energy = total_dc_power / 60.0;
if (post_enable)
{
String csv = "&csv=";
// ct / consumption
csv += "" + String(ct_voltage, 2); // voltage
csv += ",0.00"; // current
csv += "," + String(ct_energy, 2); // energy W/m
csv += ",dc"; // type
csv += ",0.00"; // temperature
csv += ",0.00"; // charge
// srne / harvest
csv += "," + String(pv_voltage, 2); // voltage
csv += ",0.00"; // current
csv += "," + String(pv_energy, 2); // energy W/m
csv += ",dc"; // type
csv += ",0.00"; // temperature
csv += ",0.00"; // charge
// srne / battery
csv += "," + String(battery_voltage, 2); // voltage
csv += ",0.00"; // current
csv += ",0.00"; // energy W/m
csv += ",dc"; // type
csv += "," + String(battery_temperature, 2); // temperature
csv += "," + String(battery_charge, 2); // charge
// srne / controller
csv += ",0.00"; // voltage
csv += ",0.00"; // current
csv += ",0.00"; // energy W/m
csv += ",dc"; // type
csv += "," + String(mppt_temperature, 2); // temperature
csv += ",0.00"; // charge
// srne / dc consumption
csv += "," + String(dc_voltage, 2); // voltage
csv += ",0.00"; // current
csv += "," + String(dc_energy, 2); // energy W/m
csv += ",dc"; // type
csv += ",0.00"; // temperature
csv += ",0.00"; // charge
// snat / input
csv += "," + String(inv_in_voltage, 2); // input voltage
csv += ",0.00"; // input current
csv += ",0.00"; // input energy W/m
csv += ",ac"; // type
csv += ",0.00"; // input temperature
csv += "," + String(inv_in_frequency, 2); // input frequency
// snat / output
csv += "," + String(inv_out_voltage, 2); // output voltage
csv += ",0.00"; // output current
csv += "," + String(inv_out_power, 2); // output energy W/m
csv += ",ac"; // type
csv += "," + String(inv_temperature, 2); // output temperature
csv += ",0.00"; // output frequency
csv += "," + String(inv_cell_voltage, 2); // cell voltage
csv += "," + String(inv_in_fault_voltage, 2); // in fault voltage
csv += "," + String(inv_flags); // flags
postData(csv);
}
/*
if (minute >= 59)
{
minute = 0;
if (hour >= 23)
{
hour = 0;
}
else
hour++;
}
else
minute++;
*/
// resets
second = 0;
total_ct_power = ct_power;
total_pv_power = pv_power;
total_dc_power = dc_power;
}
else
{
second++;
// accumulate power every second
total_ct_power += ct_power;
total_pv_power += pv_power;
total_dc_power += dc_power;
}
}
}