-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
107 lines (94 loc) · 2.2 KB
/
main.ino
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
#include <PubSubClient.h>
#include "LinkyHistTIC.h"
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "config.h"
LinkyHistTIC Linky(LINKY_RX, LINKY_TX);
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
char buffer[50];
bool hasNewBase = false, hasNewIInst = false, hasNewPapp = false;
uint32_t base = 0;
uint8_t iinst = 0;
uint16_t papp = 0;
int lastExec = millis();
void setup() {
// WIFI
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
if (WiFi.status() == WL_CONNECT_FAILED) {
delay(30000);
ESP.restart();
}
}
// MQTT
mqttClient.setServer(mqttServer, mqttPort);
if (!mqttClient.connect(mqttClientName, mqttUser, mqttPassword)) {
delay(30000);
ESP.restart();
}
// OTA
ArduinoOTA.setPort(otaPort);
ArduinoOTA.setHostname(otaHostname);
ArduinoOTA.setPassword(otaPassword);
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
});
ArduinoOTA.begin();
Linky.Init();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
delay(30000);
ESP.restart();
}
if (!mqttClient.connected()) {
delay(30000);
ESP.restart();
}
ArduinoOTA.handle();
Linky.Update();
mqttClient.loop();
if (Linky.baseIsNew()) {
base = Linky.base();
hasNewBase = true;
}
if (Linky.iinstIsNew()) {
iinst = Linky.iinst();
hasNewIInst = true;
}
if (Linky.pappIsNew()) {
papp = Linky.papp();
hasNewPapp = true;
}
if (millis() - lastExec >= mqttRefreshRateMillis) {
// Executed every `mqttRefreshRateMillis`ms
lastExec = millis();
if (hasNewBase) {
sprintf(buffer, "%d", base);
mqttClient.publish(mqttTopicIndexConso, buffer);
hasNewBase = false;
}
if (hasNewIInst) {
sprintf(buffer, "%d", iinst);
mqttClient.publish(mqttTopicIntensiteInstantanee, buffer);
hasNewIInst = false;
}
if (hasNewPapp) {
sprintf(buffer, "%d", papp);
mqttClient.publish(mqttTopicPuissanceApparente, buffer);
hasNewPapp = false;
}
} else {
delay(10);
}
}