|
4 | 4 |
|
5 | 5 | Hardware:
|
6 | 6 | - Wemos D1 mini (Compatible board)
|
7 |
| - |
| 7 | +
|
8 | 8 | Software:
|
9 |
| - - Arduino 1.8.12 (Stable) |
10 |
| - - Board 2.7.1, payload bug empty |
| 9 | + - Arduino 2.2.1 (Stable) |
| 10 | + - vscode-arduino (https://github.com/vscode-arduino/vscode-arduino) |
| 11 | + - Board 3.1.2 |
| 12 | + - Adafruit SSD1306 2.5.7 |
| 13 | + - Adafruit ADS1115 2.4.0 |
| 14 | + - NTPClient 3.2.1 |
| 15 | + - ArduinoJson 6.21.3 |
11 | 16 | */
|
12 | 17 |
|
13 |
| -#include <ESP8266WiFi.h> |
14 |
| -#include <ESP8266HTTPClient.h> |
15 |
| -#include <DNSServer.h> |
16 |
| -#include <ESP8266WebServer.h> |
17 |
| - |
18 |
| -#include <WiFiClientSecureBearSSL.h> |
19 |
| -// openssl s_client -connect api.thingspeak.com:443 | openssl x509 -fingerprint -noout |
20 |
| -//const uint8_t fingerprint[20] = {0x27, 0x18, 0x92, 0xDD, 0xA4, 0x26, 0xC3, 0x07, 0x09, 0xB9, 0x7A, 0xE6, 0xC5, 0x21, 0xB9, 0x5B, 0x48, 0xF7, 0x16, 0xE1}; |
21 |
| - |
22 |
| -#include <SoftwareSerial.h> |
23 |
| - |
24 |
| -//#include <Adafruit_ADS1015.h> |
25 |
| -//Adafruit_ADS1115 ads(0x48); |
26 |
| - |
27 |
| -#include <EEPROM.h> |
28 |
| - |
29 |
| -#define APssid "ESSMonitor-AP" |
30 |
| -#define APpassword "12345678" |
31 |
| - |
32 |
| -char ssid[32] = ""; |
33 |
| -char password[32] = ""; |
34 |
| - |
35 |
| -// Bmon part |
36 |
| -//float device_voltage; |
37 |
| - |
38 |
| -// Post API |
39 |
| -uint8_t post_enable; |
40 |
| -char api_key[32] = ""; // NIJCG7UI28O9CAYD |
41 |
| -char api_url[256] = ""; // https://api.thingspeak.com/update // https://192.168.2.1:8001/cgi-bin/custom-full.cgi?a=solard |
42 |
| -uint16_t http_timeout = 0; |
43 |
| - |
44 |
| -// CT sensor part |
45 |
| -uint8_t ct_enable; |
46 |
| -float ct_calibration; |
47 |
| -float ct_pf; |
48 |
| -float ct_current; |
49 |
| -float ct_voltage; |
50 |
| -float ct_power; // w |
51 |
| - |
52 |
| -// SRNE Modbus part |
53 |
| -uint8_t srne_enable; |
54 |
| -float battery_voltage; |
55 |
| -float battery_charge; |
56 |
| -float pv_voltage; |
57 |
| -float pv_power; |
58 |
| -float mppt_voltage; |
59 |
| -float mppt_power; |
60 |
| -float battery_temperature; |
61 |
| -float mppt_temperature; |
62 |
| -float dc_voltage; |
63 |
| -float dc_current; |
64 |
| -float dc_power; |
65 |
| - |
66 |
| -float min_battery_voltage; |
67 |
| -float max_battery_voltage; |
68 |
| -float max_charging_current; |
69 |
| -float max_discharging_current; |
70 |
| -float max_charging_power; |
71 |
| -float max_discharging_power; |
72 |
| -float battery_charging_amphr; |
73 |
| -float battery_discharging_amphr; |
74 |
| -float power_generation; |
75 |
| -float power_consumption; |
76 |
| - |
77 |
| -uint16_t operating_days; |
78 |
| -uint16_t battery_overdischarges; |
79 |
| -uint16_t battery_fullcharges; |
80 |
| -uint32_t sum_battery_charging_amphr; |
81 |
| -uint32_t sum_battery_discharging_amphr; |
82 |
| -uint32_t sum_power_generation; |
83 |
| -uint32_t sum_power_consumption; |
84 |
| -uint8_t load_status; |
85 |
| -uint8_t load_brightness; |
86 |
| -uint8_t charging_status; |
87 |
| - |
88 |
| -// SNAT Megatec part |
89 |
| -uint8_t snat_enable; |
90 |
| -float inv_in_voltage; |
91 |
| -float inv_in_fault_voltage; |
92 |
| -float inv_out_voltage; |
93 |
| -float inv_out_power; // w |
94 |
| -float inv_in_frequency; |
95 |
| -float inv_temperature; |
96 |
| -float inv_cell_voltage; |
97 |
| -char inv_flags[9] = ""; |
98 |
| - |
99 |
| -// |
100 |
| -uint8_t beep_enable; |
101 |
| -uint8_t display_enable; |
102 |
| - |
103 |
| -uint16_t modbus_error = 0; |
104 |
| -uint16_t megatec_error = 0; |
105 |
| -uint16_t wifi_error = 0; |
106 |
| -uint16_t post_error = 0; |
107 |
| -uint16_t http_error = 0; |
108 |
| -int http_code = 0; |
109 |
| - |
110 |
| -uint16_t run_time = 0; |
111 |
| - |
112 |
| -// timing |
113 |
| -uint8_t second = 0; |
114 |
| -//uint8_t minute; |
115 |
| -//uint8_t hour; |
116 |
| - |
117 |
| -// Web server |
118 |
| -ESP8266WebServer webServer(80); |
119 |
| -// DNS server |
120 |
| -DNSServer dnsServer; |
121 |
| - |
122 |
| -/* Soft AP network parameters */ |
123 |
| -IPAddress apIP(192, 168, 4, 1); |
124 |
| -IPAddress netMsk(255, 255, 255, 0); |
125 |
| - |
126 |
| -void loadConfig() { |
127 |
| - EEPROM.begin(512); |
128 |
| - |
129 |
| - // Wifi part |
130 |
| - EEPROM.get(0, ssid); // 32 |
131 |
| - EEPROM.get(32, password); // 32 |
132 |
| - |
133 |
| - // Post part |
134 |
| - EEPROM.get(64, post_enable); // 1 |
135 |
| - EEPROM.get(65, api_key); // 32 |
136 |
| - EEPROM.get(97, api_url); // 256 |
137 |
| - EEPROM.get(353, http_timeout); // 2 |
138 |
| - |
139 |
| - // Emon part |
140 |
| - EEPROM.get(355, ct_enable); // 1 |
141 |
| - EEPROM.get(356, ct_calibration); // 8 bytes |
142 |
| - EEPROM.get(364, ct_voltage); // 8 bytes |
143 |
| - EEPROM.get(372, ct_pf); // 8 bytes |
144 |
| - |
145 |
| - // SRNE part |
146 |
| - EEPROM.get(380, srne_enable); // 1 |
147 |
| - // SNAT part |
148 |
| - EEPROM.get(381, snat_enable); // 1 |
149 |
| - // Display part |
150 |
| - EEPROM.get(382, display_enable); // 1 |
151 |
| - // BEEP part |
152 |
| - EEPROM.get(383, beep_enable); // 1 |
153 |
| - |
154 |
| - char ok[3]; |
155 |
| - EEPROM.get(384, ok); |
156 |
| - EEPROM.end(); |
157 |
| - |
158 |
| - if (String(ok) != String("OK")) { |
159 |
| - ssid[0] = 0; |
160 |
| - password[0] = 0; |
161 |
| - post_enable = 0; |
162 |
| - api_key[0] = 0; |
163 |
| - api_url[0] = 0; |
164 |
| - http_timeout = 0; |
165 |
| - ct_calibration = 30.0; |
166 |
| - ct_voltage = 0.0; |
167 |
| - ct_pf = 1.0; |
168 |
| - srne_enable = 0; |
169 |
| - snat_enable = 0; |
170 |
| - display_enable = 0; |
171 |
| - beep_enable = 0; |
172 |
| - } |
173 |
| -} |
174 |
| - |
175 |
| -void saveConfig() { |
176 |
| - EEPROM.begin(512); |
177 |
| - |
178 |
| - // Wifi part |
179 |
| - EEPROM.put(0, ssid); // 32 |
180 |
| - EEPROM.put(32, password); // 32 |
181 |
| - |
182 |
| - // Post part |
183 |
| - EEPROM.put(64, post_enable); // 1 |
184 |
| - EEPROM.put(65, api_key); // 32 |
185 |
| - EEPROM.put(97, api_url); // 256 |
186 |
| - EEPROM.put(353, http_timeout); // 2 |
187 |
| - |
188 |
| - // Emon part |
189 |
| - EEPROM.put(355, ct_enable); // 1 |
190 |
| - EEPROM.put(356, ct_calibration); // 8 bytes |
191 |
| - EEPROM.put(364, ct_voltage); // 8 bytes |
192 |
| - EEPROM.put(372, ct_pf); // 8 bytes |
193 |
| - |
194 |
| - // SRNE part |
195 |
| - EEPROM.put(380, srne_enable); // 1 |
196 |
| - // SNAT part |
197 |
| - EEPROM.put(381, snat_enable); // 1 |
198 |
| - // Dsiplay part |
199 |
| - EEPROM.put(382, display_enable); // 1 |
200 |
| - // BEEP part |
201 |
| - EEPROM.put(383, beep_enable); // 1 |
202 |
| - |
203 |
| - char ok[3] = "OK"; |
204 |
| - EEPROM.put(384, ok); |
205 |
| - EEPROM.commit(); |
206 |
| - EEPROM.end(); |
207 |
| -} |
208 |
| - |
209 |
| -void execEverySecond() { |
| 18 | +#include "eeprom.h" |
| 19 | +#include "network.h" |
| 20 | +#include "ntp.h" |
| 21 | +#include "display.h" |
| 22 | +#include "analog.h" |
| 23 | +#include "webserver.h" |
| 24 | +#include "post.h" |
| 25 | +#include "app.h" |
| 26 | + |
| 27 | +void execEvery(int ms) |
| 28 | +{ |
210 | 29 | static unsigned long msTick = millis();
|
| 30 | + static uint8_t sTick; |
211 | 31 |
|
212 |
| - if (millis() - msTick >= 1000) { // run every 1000 ms |
| 32 | + if (millis() - msTick >= ms) |
| 33 | + { // run every N ms |
213 | 34 | msTick = millis();
|
214 | 35 |
|
215 |
| - if (ct_enable) extractCT(); |
216 |
| - delay(1); |
217 |
| - if (srne_enable) extractSRNE(); |
218 |
| - delay(1); |
219 |
| - if (snat_enable) extractSNAT(); |
220 |
| - //delay(1); |
221 |
| - if (display_enable) displayData(); |
222 |
| - |
223 |
| - if (second >= 59) { |
224 |
| - if (post_enable == 1) postCsv(); |
225 |
| - else if (post_enable == 2) postInflux(); |
226 |
| - second = 0; |
| 36 | + ntpLoop(); |
| 37 | + // Serial.println(epoch); |
| 38 | + |
| 39 | + if (sTick >= 59) |
| 40 | + { |
| 41 | + sTick = 0; |
227 | 42 | run_time++;
|
| 43 | + if (post_enable) |
| 44 | + postLoop(); |
228 | 45 | }
|
229 |
| - else { |
230 |
| - second++; |
| 46 | + else |
| 47 | + { |
| 48 | + sTick++; |
231 | 49 | }
|
232 | 50 | }
|
233 | 51 | }
|
234 | 52 |
|
235 |
| -void setup() { |
| 53 | +void setup() |
| 54 | +{ |
236 | 55 | delay(10);
|
237 | 56 |
|
238 | 57 | Serial.begin(115200);
|
239 | 58 |
|
240 | 59 | Serial.println();
|
241 |
| - Serial.println(APssid); |
| 60 | + Serial.println(APPNAME); |
242 | 61 |
|
243 | 62 | loadConfig();
|
244 | 63 |
|
245 |
| - wifiSetup(); |
246 |
| - |
247 |
| - srneSetup(); |
248 |
| - snatSetup(); |
249 |
| - |
250 |
| - displaySetup(); |
251 |
| - |
252 |
| - //ads.begin(); |
| 64 | + networkSetup(); |
| 65 | + ntpSetup(); |
| 66 | + webserverSetup(); |
253 | 67 |
|
254 |
| - dnsServer.start(53, "*", apIP); |
| 68 | + // if (beep_enable) |
| 69 | + // beepSetup(); |
| 70 | + if (analog_enable) |
| 71 | + analogSetup(); |
| 72 | + if (display_enable) |
| 73 | + displaySetup(); |
| 74 | + // if (ads1115_enable) |
| 75 | + // ads1115Setup(); |
255 | 76 |
|
256 |
| - webserverSetup(); |
| 77 | + appSetup(); |
257 | 78 |
|
258 | 79 | delay(100);
|
259 | 80 | }
|
260 | 81 |
|
261 |
| -void loop() { |
262 |
| - execEverySecond(); |
| 82 | +void loop() |
| 83 | +{ |
| 84 | + execEvery(1000); |
| 85 | + |
| 86 | + if (display_enable) |
| 87 | + displayLoop(); |
| 88 | + // if (dht11_enable) |
| 89 | + // dht11Loop(); |
| 90 | + |
| 91 | + networkLoop(); |
| 92 | + webserverLoop(); |
263 | 93 |
|
264 |
| - // dns server |
265 |
| - dnsServer.processNextRequest(); |
266 |
| - // web server |
267 |
| - webServer.handleClient(); |
| 94 | + appLoop(); |
268 | 95 | }
|
0 commit comments