forked from Schnup89/OpenV_NodeMCU
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVitoWifi_NodeMCU.ino
More file actions
299 lines (264 loc) · 11.5 KB
/
VitoWifi_NodeMCU.ino
File metadata and controls
299 lines (264 loc) · 11.5 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <VitoWiFi.h>
#include <AsyncMqttClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
//HTTP-Server fuer Info-Webseite
ESP8266WebServer httpServer(80);
//HTTP-Server fuer Sketch/Firmware Update
ESP8266HTTPUpdateServer httpUpdater;
//###### Variablen
volatile bool updateVitoWiFi = false;
bool bStopVito = false;
unsigned long lastMillis;
bool bLastMqttCheck = true;
Ticker timer;
int systemUpTimeMn = 0;
int systemUpTimeHr = 0;
int systemUpTimeDy = 0;
//###### Konfiguration
static const char SSID[] = "MySSID";
static const char PASS[] = "MySSIDPass";
static const IPAddress BROKER(192, 168, 10, 60);
static const uint16_t PORT = 1883;
static const char CLIENTID[] = "VitoWifi";
static const char MQTTUSER[] = "myMQTTUser";
static const char MQTTPASS[] = "myMQTTPass";
static const int READINTERVAL = 60; //Abfrageintervall OptoLink, in Sekunden
uint32_t period = 1 * 60000L; // 1 Minuten
VitoWiFi_setProtocol(P300);
//###### Allgemein
DPTemp getTempA("gettempa", "allgemein", 0x0800); //Aussentemperatur
DPStat getAlarmStatus("getalarmstatus", "allgemein", 0x0A82); //Sammelstoerung Ja/Nein
//###### Warmwasser
DPTemp getTempWWist("gettempwwist", "wasser", 0x0804); //Warmwasser-Ist
DPTempS getTempWWsoll("gettempwwsoll", "wasser", 0x6300); //Warmwasser-Soll
//DPTempS setTempWWsoll("settempwwsoll", "wasser", 0x6300); //Warmwasser-Soll schreiben
//###### Kessel
DPTemp getTempKist("gettempkist", "kessel", 0x0802); //Kesseltemperatur-Ist
//###### Brenner
DPStat getBrennerStatus("getbrennerstatus", "brenner", 0x55D3); //Brennerstatus
DPHours getBrennerStunden1("getbrennerstunden1", "brenner", 0x08A7); //Brennerstunden Stufe1
//###### Heizkreise
DPTemp getTempVListM1("gettempvlistm1", "heizkreise", 0x2900); //HK1 Vorlauftemp
DPTemp getTempVListM2("gettempvlistm2", "heizkreise", 0x3900); //HK2 Vorlauftemp
DPTempS getTempRaumNorSollM1("gettempraumnorsollm1", "heizkreise", 0x2306); //HK1 Raumtemp-Soll
//DPTempS setTempRaumNorSollM1("settempraumnorsollm1", "heizkreise", 0x2306); //HK1 Raumtemp-Soll schreiben
DPTempS getTempRaumNorSollM2("gettempraumnorsollm2", "heizkreise", 0x3306); //HK2 Raumtemp-Soll
//DPTempS setTempRaumNorSollM2("settempraumnorsollm2", "heizkreise", 0x3306); //HK2 Raumtemp-Soll schreiben
//###### Betriebsarten
DPMode getBetriebArtM1("getbetriebartm1","betriebsarten", 0x2301); //HK1 0=Abschaltb,1=nur WW,2=heiz+WW, 3=DauernRed,3=Dauer Norma.
DPMode getBetriebArtM2("getbetriebartm2","betriebsarten", 0x3301); //HK2 0=Abschaltb,1=nur WW,2=heiz+WW, 3=DauernRed,3=Dauer Norma.
DPStat getBetriebPartyM1("getbetriebpartym1","betriebsarten", 0x2303); //HK1 Party
DPStat getBetriebPartyM2("getbetriebpartym2","betriebsarten", 0x3303); //HK2 Party
DPStat setBetriebPartyM1("setbetriebpartym1","betriebsarten", 0x2330); //HK1 Party schreiben
DPStat setBetriebPartyM2("setbetriebpartym2","betriebsarten", 0x3330); //HK2 Party schreiben
//###### Objekte und Event-Handler
AsyncMqttClient mqttClient;
WiFiEventHandler wifiConnectHandler;
WiFiEventHandler wifiDisconnectHandler;
void connectToWiFi() {
//Mit WLAN verbinden
WiFi.begin(SSID, PASS);
}
void connectToMqtt() {
//Mit MQTT-Server verbinden
mqttClient.connect();
}
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
//Wenn WLAN-Verbindung steht und IP gesetzt, zu MQTT verbinden
timer.once(2, connectToMqtt);
}
void onMqttConnect(bool sessionPresent) {
//Wenn MQTT verbunden, Topics abonnieren
mqttClient.subscribe("VITOWIFI/setBetriebPartyM1", 0);
mqttClient.subscribe("VITOWIFI/setBetriebPartyM2", 0);
//mqttClient.subscribe("VITOWIFI/setTempWWsoll", 0);
//mqttClient.subscribe("VITOWIFI/setTempRaumNorSollM1", 0);
//mqttClient.subscribe("VITOWIFI/setTempRaumNorSollM2", 0);
mqttClient.publish("VITOWIFI/$status/online", 1, true, "1"); //Topic setzen, dieser haelt den Verfuegbarkeitsstatus
//Timer aktivieren, alle X Sekunden die Optolink-Schnittstelle abfragen
timer.attach(READINTERVAL, [](){
updateVitoWiFi = true;
});
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
//Wenn Mqtt Verbindung verloren und wifi noch verbunden
if (WiFi.isConnected()) {
//Mqtt erneut verbinden
timer.once(2, connectToMqtt);
}
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
//Wenn WLAN Verbindung verloren, neu verbinden
timer.once(2, connectToWiFi);
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
//Wenn abonnierte MQTT Nachricht erhalten
if(strcmp(topic,"VITOWIFI/setBetriebPartyM1") == 0) {
bool setParty = 0;
//Wert(Payload) auswerten und Variable setzen
if(strstr(payload,"1")) setParty = 1;
//In DPValue Konvertieren (siehe github vitowifi fuer Datentypen)
DPValue value(setParty);
//Wert an Optolink schicken
VitoWiFi.writeDatapoint(setBetriebPartyM1, value);
//VitoWiFi.writeDatapoint(setBetriebPartyM1, value);
//VitoWiFi.writeDatapoint(setBetriebPartyM1, value);
//Wert auslesen um aktuellen Status an MQTT-Broker zu senden
VitoWiFi.readDatapoint(getBetriebPartyM1);
}
if(strcmp(topic,"VITOWIFI/setBetriebPartyM2") == 0) {
bool setParty = 0;
//if(strcmp(payload,"1") == 0) setParty = 1;
if(strstr(payload,"1")) setParty = 1;
DPValue value(setParty);
VitoWiFi.writeDatapoint(setBetriebPartyM2, value);
//VitoWiFi.writeDatapoint(setBetriebPartyM2, value);
//VitoWiFi.writeDatapoint(setBetriebPartyM2, value);
VitoWiFi.readDatapoint(getBetriebPartyM2);
}
/*if(strcmp(topic,"VITOWIFI/setTempWWsoll") == 0) {
uint8_t setTemp = atoi(payload);
if(setTemp>=45 && setTemp<=60){
DPValue value(setTemp);
VitoWiFi.writeDatapoint(setTempWWsoll, value);
VitoWiFi.readDatapoint(getTempWWsoll);
}
}
if(strcmp(topic,"VITOWIFI/setTempRaumNorSollM1") == 0) {
uint8_t setTemp = atoi(payload);
if(setTemp>=3 && setTemp<=37){
DPValue value(setTemp);
VitoWiFi.writeDatapoint(setTempRaumNorSollM1, value);
VitoWiFi.readDatapoint(getTempRaumNorSollM1);
}
}
if(strcmp(topic,"VITOWIFI/setTempRaumNorSollM2") == 0) {
uint8_t setTemp = atoi(payload);
if(setTemp>=3 && setTemp<=37){
DPValue value(setTemp);
VitoWiFi.writeDatapoint(setTempRaumNorSollM2, value);
VitoWiFi.readDatapoint(getTempRaumNorSollM2);
}
}*/
}
//DPTemp & DPHours - float | MQTT-Topic bsp: VITOWIFI/gettempa
void tempCallbackHandler(const IDatapoint& dp, DPValue value) {
//Umwandeln, und zum schluss per mqtt publish an mqtt-broker senden
char outVal[9];
dtostrf(value.getFloat(), 6, 2, outVal);
char outName[30] = "VITOWIFI/";
strcat(outName,dp.getName());
mqttClient.publish(outName, 1, true, outVal);
}
//DPTemps - uint8_t | MQTT-Topic bsp: VITOWIFI/getBetriebArtM1
void tempSCallbackHandler(const IDatapoint& dp, DPValue value) {
//Umwandeln, und zum schluss per mqtt publish an mqtt-broker senden
int nValue = value.getU8();
char outName[30] = "VITOWIFI/";
strcat(outName,dp.getName());
mqttClient.publish(outName, 1, true, String(nValue).c_str());
}
//DPStat - bool | MQTT-Topic bsp: VITOWIFI/getbrennerstatus
void statCallbackHandler(const IDatapoint& dp, DPValue value) {
//Umwandeln, und zum schluss per mqtt publish an mqtt-broker senden
char outName[30] = "VITOWIFI/";
strcat(outName,dp.getName());
mqttClient.publish(outName, 1, true, (value.getBool()) ? "1" : "0");
}
void setup() {
//DEBUG WiFi.mode(WIFI_AP_STA);
//Setze WLAN-Optionen
WiFi.mode(WIFI_STA);
WiFi.hostname(CLIENTID);
//Setze Datenpunkte als beschreibbar
setBetriebPartyM1.setWriteable(true);
setBetriebPartyM2.setWriteable(true);
//setTempWWsoll.setWriteable(true);
//setTempRaumNorSollM1.setWriteable(true);
//setTempRaumNorSollM2.setWriteable(true);
//Zuweisung der Datenpunkte anhand des Rueckgabewerts an entsprechende Handler
//(siehe github vitowifi fuer Datentypen)
getTempA.setCallback(tempCallbackHandler);
getTempWWist.setCallback(tempCallbackHandler);
getTempKist.setCallback(tempCallbackHandler);
getTempVListM1.setCallback(tempCallbackHandler);
getTempVListM2.setCallback(tempCallbackHandler);
getBrennerStunden1.setCallback(tempCallbackHandler);
getTempWWsoll.setCallback(tempSCallbackHandler);
getTempRaumNorSollM1.setCallback(tempSCallbackHandler);
getTempRaumNorSollM2.setCallback(tempSCallbackHandler);
getAlarmStatus.setCallback(statCallbackHandler);
getBrennerStatus.setCallback(statCallbackHandler);
getBetriebPartyM1.setCallback(statCallbackHandler);
getBetriebPartyM2.setCallback(statCallbackHandler);
//Wichtig, da sonst ueber die Serielle-Konsole (Optolink) Text geschrieben wird
VitoWiFi.disableLogger();
//Setze Serielle PINS an VitoWifi
VitoWiFi.setup(&Serial);
//Verbindungsaufbau und setzen der Optionen
wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onMessage(onMqttMessage);
wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
mqttClient.setServer(BROKER, PORT);
mqttClient.setClientId(CLIENTID);
mqttClient.setCredentials(MQTTUSER, MQTTPASS);
mqttClient.setKeepAlive(5);
mqttClient.setCleanSession(true);
mqttClient.setWill("VITOWIFI/$status/online", 1, true, "0");
connectToWiFi();
//Info-Webseite anzeigen auf HTTP-Port 80
httpServer.on("/", [](){
httpServer.send(200, "text/html", "<h1>Vito-Status: " + String((bStopVito ? "Stopped" : "Running")) + "</h1> <br><br> <b>MQTT-Connected: " + String(mqttClient.connected()) + "</b><br><a href='/start'>Start</a> <a href='/stop'>Stop</a> <br><br> <b>Compiled: " __DATE__ " " __TIME__ "<br>Uptime: " + String(systemUpTimeDy) + ":" + String(systemUpTimeHr) + ":" + String(systemUpTimeMn) + "</b><br><br><a href='/reboot'>reboot</a><br><a href='/update'>update</a><br>");
});
//Stop-Funktion sollte etwas schieflaufen :)
httpServer.on("/stop", [](){
bStopVito = true;
httpServer.send(200, "text/plain", "OK - Stopped");
});
//Startfunktion
httpServer.on("/start", [](){
bStopVito = false;
httpServer.send(200, "text/plain", "OK - Started");
});
//Reboot ueber Webinterface
httpServer.on("/reboot", [](){
httpServer.send(200, "text/plain", "OK - rebooting...");
ESP.restart();
});
//Starte Webserver fuer Sketch/Firmware-Update
httpUpdater.setup(&httpServer);
httpServer.begin();
}
void loop() {
if (!bStopVito){
VitoWiFi.loop();
if (updateVitoWiFi && mqttClient.connected()) {
updateVitoWiFi = false;
VitoWiFi.readAll();
}
}
httpServer.handleClient();
//Jede Minute
if (millis() - lastMillis >= period) {
lastMillis = millis(); //get ready for the next iteration
if (!mqttClient.connected() && !bLastMqttCheck){
ESP.restart();
}
if (!mqttClient.connected()){
bLastMqttCheck = false;
}else{
bLastMqttCheck = true;
}
long millisecs = millis();
systemUpTimeMn = int((millisecs / (1000 * 60)) % 60);
systemUpTimeHr = int((millisecs / (1000 * 60 * 60)) % 24);
systemUpTimeDy = int((millisecs / (1000 * 60 * 60 * 24)) % 365);
}
}