-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.cpp
116 lines (89 loc) · 2.76 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
111
112
113
114
115
116
/* ESPEM - ESP Energy monitor
* A code for ESP8266 based boards to interface with PeaceFair PZEM PowerMeters
* It can poll/collect PowerMeter data and provide it for futher processing in text/json format
*
* (c) Emil Muratov 2017-2021
*
*/
// Main headers
#include "main.h"
#include "espem.h"
#include <EmbUI.h>
#ifdef USE_FTP
#include "ftpSrv.h"
#endif
extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);
// PROGMEM strings
// sprintf template for json version data
#ifdef ESP8266
static const char PGverjson[] PROGMEM = "{\"ChipID\":\"%x\",\"FlashSize\":%u,\"Core\":\"%s\",\"SDK\":\"%s\",\"firmware\":\"%s\",\"version\":\"%s\",\"CPUMHz\":%u,\"Heap\":%u,\"Uptime\":%u,}";
#elif defined ESP32
static const char PGverjson[] PROGMEM = "{\"ChipID\":\"%s\",\"FlashSize\":%u,\"SDK\":\"%s\",\"firmware\":\"%s\",\"version\":\"%s\",\"CPUMHz\":%u,\"Heap\":%u,\"Uptime\":%u,}";
#endif
// Our instance of espem
ESPEM *espem = nullptr;
// ----
// MAIN Setup
void setup() {
#ifdef ESPEM_DEBUG
ESPEM_DEBUG.begin(BAUD_RATE); // start hw serial for debugging
#endif
LOG(println, F("Starting EspEM..."));
// Start framework, load config and connect WiFi
embui.begin();
// restore ESPEM defaults configuration
ESPEM_CFG _cfg(embui.param(FPSTR(V_EPOLLRT)).toInt(),
embui.param(FPSTR(V_EPOOLSIZE)).toInt(),
(bool)embui.param(FPSTR(V_EPFFIX)).toInt(),
(bool)embui.param(FPSTR(V_EPOLLENA)).toInt(),
(mcstate_t)embui.param(FPSTR(V_ECOLLECTORSTATE)).toInt()
);
// create and run ESPEM object
espem = new ESPEM(_cfg);
espem->begin();
embui.server.on(PSTR("/fw"), HTTP_GET, [](AsyncWebServerRequest *request){
wver(request);
});
#ifdef USE_FTP
ftp_setup(); // запуск ftp-сервера
#endif
//sync_parameters(); // sync UI params
embui.setPubInterval(20);
}
// MAIN loop
void loop() {
embui.handle();
#ifdef USE_FTP
ftp_loop(); // цикл обработки событий фтп-сервера
#endif
}
// send HTTP responce, json with controller/fw versions and status info
void wver(AsyncWebServerRequest *request) {
char buff[HTTP_VER_BUFSIZE];
timespec tp;
clock_gettime(0, &tp);
#ifdef ESP8266
snprintf_P(buff, sizeof(buff), PGverjson,
ESP.getChipId(),
ESP.getFlashChipSize(),
ESP.getCoreVersion().c_str(),
system_get_sdk_version(),
FW_NAME,
TOSTRING(FW_VER),
ESP.getCpuFreqMHz(),
ESP.getFreeHeap(),
(uint32_t)tp.tv_sec);
#else
snprintf_P(buff, sizeof(buff), PGverjson,
ESP.getChipModel(),
ESP.getFlashChipSize(),
ESP.getSdkVersion(),
FW_NAME,
TOSTRING(FW_VER),
ESP.getCpuFreqMHz(),
ESP.getFreeHeap(),
(uint32_t)tp.tv_sec);
#endif
request->send(200, FPSTR(PGmimejson), buff );
}
//