-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
110 lines (95 loc) · 3.29 KB
/
main.cpp
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
#include <Arduino.h>
#include <TimeLib.h> // Include TimeLib library for time calculations
#include "ButtonHandler.h"
#include "WifiConfigurator.h"
#include "PZEMMonitor.h"
#include <SPIFFS.h>
#include "LCDDisplay.h"
#include <Axsecure.h>
#define TRIGGER_PIN 0
#define PZEM_RX_PIN 16
#define PZEM_TX_PIN 17
#define LCD_ADDR 0x27
#define LCD_COLS 16
#define LCD_ROWS 2
ButtonHandler buttonHandler(TRIGGER_PIN);
LCDDisplay lcdDisplay(LCD_ADDR, LCD_COLS, LCD_ROWS);
WifiConfigurator wifiConfigurator(lcdDisplay);
PZEMMonitor pzemMonitor(PZEM_RX_PIN, PZEM_TX_PIN, Serial2, lcdDisplay);
void handleESP32(void *pvParameters) {
for (;;) {
if (WiFi.status() != WL_CONNECTED) {
if (wifiConfigurator.retry()) {
pzemMonitor.logValues();
delay(100);
pzemMonitor.sendDataAndClear();
}
delay(2000); // Short delay to show the connection status
}
if (buttonHandler.isButtonPressed()) {
if (wifiConfigurator.connect(false)) {
pzemMonitor.logValues();
delay(100);
pzemMonitor.sendDataAndClear();
}
}
vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay to yield control
}
}
void displayReadings(void *pvParameters) {
unsigned long lastLogTime = 0;
const unsigned long LogInterval = 1; // 1 hour in seconds
const int MaxLogsBeforeSend = 1;
int logCount = 0;
for (;;) {
lcdDisplay.displayWiFiStatus(WiFi.status() == WL_CONNECTED);
time_t currentTime = now(); // Get current time using TimeLib
pzemMonitor.readAndPrintValues();
if (currentTime - lastLogTime >= LogInterval) {
lastLogTime = currentTime;
pzemMonitor.logValues();
logCount++;
if (logCount >= MaxLogsBeforeSend) {
pzemMonitor.sendDataAndClear();
logCount = 0;
}
}
vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay to yield control
}
}
void setup() {
Serial.begin(115200);
lcdDisplay.begin();
if (!SPIFFS.begin(true)) {
Serial.println("Failed to initialize SPIFFS");
return;
} else {
Serial.println("Initialized");
}
lcdDisplay.displayText("Terant...", 0, 0);
wifiConfigurator.begin();
if (WiFi.status() == WL_CONNECTED) {
pzemMonitor.logValues();
delay(100);
pzemMonitor.sendDataAndClear();
}
xTaskCreatePinnedToCore(
handleESP32, /* Task function */
"HandleESP32", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
NULL, /* Task handle */
0); /* Core where the task should run */
xTaskCreatePinnedToCore(
displayReadings, /* Task function */
"DisplayReadings", /* Name of the task */
10000, /* Stack size in words */
NULL, /* Task input parameter */
1, /* Priority of the task */
NULL, /* Task handle */
1); /* Core where the task should run */
}
void loop() {
// Empty. Tasks are running in parallel.
}