Skip to content

Commit c7727d9

Browse files
committed
refactor sync codebae with ArduinoCarSecurity
1 parent 92c8a3b commit c7727d9

34 files changed

+2002
-1291
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode/c_cpp_properties.json
2+
build

Diff for: .vscode/arduino.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"board": "esp8266:esp8266:d1_mini_clone",
3+
"port": "COM5",
4+
"sketch": "ArduinoEnergyLogger.ino",
5+
"output": "./build"
6+
}

Diff for: .vscode/settings.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"arduino.useArduinoCli": true,
3+
"arduino.logLevel": "info",
4+
"arduino.allowPDEFiletype": false,
5+
"arduino.enableUSBDetection": true,
6+
"arduino.disableTestingOpen": false,
7+
"arduino.skipHeaderProvider": false,
8+
"arduino.additionalUrls": [
9+
"http://arduino.esp8266.com/stable/package_esp8266com_index.json"
10+
],
11+
}

Diff for: ArduinoEnergyLogger.ino

+60-233
Original file line numberDiff line numberDiff line change
@@ -4,265 +4,92 @@
44
55
Hardware:
66
- Wemos D1 mini (Compatible board)
7-
7+
88
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
1116
*/
1217

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+
{
21029
static unsigned long msTick = millis();
30+
static uint8_t sTick;
21131

212-
if (millis() - msTick >= 1000) { // run every 1000 ms
32+
if (millis() - msTick >= ms)
33+
{ // run every N ms
21334
msTick = millis();
21435

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;
22742
run_time++;
43+
if (post_enable)
44+
postLoop();
22845
}
229-
else {
230-
second++;
46+
else
47+
{
48+
sTick++;
23149
}
23250
}
23351
}
23452

235-
void setup() {
53+
void setup()
54+
{
23655
delay(10);
23756

23857
Serial.begin(115200);
23958

24059
Serial.println();
241-
Serial.println(APssid);
60+
Serial.println(APPNAME);
24261

24362
loadConfig();
24463

245-
wifiSetup();
246-
247-
srneSetup();
248-
snatSetup();
249-
250-
displaySetup();
251-
252-
//ads.begin();
64+
networkSetup();
65+
ntpSetup();
66+
webserverSetup();
25367

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();
25576

256-
webserverSetup();
77+
appSetup();
25778

25879
delay(100);
25980
}
26081

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();
26393

264-
// dns server
265-
dnsServer.processNextRequest();
266-
// web server
267-
webServer.handleClient();
94+
appLoop();
26895
}

Diff for: README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ Supports
2424
Files
2525
-----
2626
* ArduinoEnergyLogger.ino - main file
27-
* ct.ino - support file for ct sensor reading (based on emon).
28-
* display.ino - support file for displaying data via LCD.
29-
* post.ino - support file for posting data to a HTTP server.
30-
* srne.ino - support file for extracting data from a srne mppt controller.
31-
* snat.ino - support file for extracting data from a snat psw inverter.
32-
* webserver.ino - support file for the device webserver, used for wifi and device configurations/informations.
33-
* wifi.ino - support file for wifi.
34-
* utils.ino - helpers.
27+
* analog.h - support file for ct sensor reading (based on emon) [Edit cpp/h].
28+
* display.h - support file for displaying data via OLED [Edit cpp/h].
29+
* post.h - support file for posting data to a HTTP server IoT.
30+
* srne.h - support file for extracting data from a srne mppt controller.
31+
* snat.h - support file for extracting data from a snat psw inverter.
32+
* webserver.h - support file for the device webserver, used for wifi and device configurations/informations [Edit cpp/h].
33+
* utils.h - helpers.
34+
* hardware - directory of hardware design eagle files
35+
* screeshot - directory of images used for README
3536

3637

3738
Wiring for CT sensor, SRNE, and SNAT

0 commit comments

Comments
 (0)