forked from jantenhove/GoodWeLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTTPublisher.cpp
175 lines (139 loc) · 5.37 KB
/
MQTTPublisher.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
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
#include "MQTTPublisher.h"
WiFiClient espClient;
PubSubClient client(espClient);
MQTTPublisher::MQTTPublisher(SettingsManager* settingsManager, GoodWeCommunicator* goodWe)
{
randomSeed(micros());
mqttSettingsManager = settingsManager;
goodweCommunicator = goodWe;
}
MQTTPublisher::~MQTTPublisher()
{
client.publish("goodwe", "offline");
client.disconnect();
}
bool MQTTPublisher::reconnect()
{
lastConnectionAttempt = millis();
debugPrint("Attempting MQTT connection to server: ");
debugPrint(mqttSettings->mqttHostName);
debugPrintln("...");
// Create a random client ID
String clientId = "GoodWeLogger-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
bool clientConnected;
if (mqttSettings->mqttUserName.length())
{
debugPrintln("Using user credientials for authentication.");
clientConnected = client.connect(clientId.c_str(), mqttSettings->mqttUserName.c_str(), mqttSettings->mqttPassword.c_str());
}
else
{
debugPrintln("Connecting without user credentials.");
clientConnected = client.connect(clientId.c_str());
}
if (clientConnected)
{
debugPrintln("connected");
// Once connected, publish an announcement...
client.publish("goodwe", "online");
return true;
}
else {
debugPrint("failed, rc=");
debugPrint(client.state());
}
return false;
}
void MQTTPublisher::start()
{
mqttSettings = mqttSettingsManager->GetSettings();
if (mqttSettings->mqttHostName.length() == 0 || mqttSettings->mqttPort == 0)
{
debugPrintln("MQTT disabled. No hostname or port set.");
return; //not configured
}
debugPrintln("MQTT enabled. Connecting.");
client.setServer(mqttSettings->mqttHostName.c_str(), mqttSettings->mqttPort);
reconnect(); //connect right away
isStarted = true;
}
void MQTTPublisher::stop()
{
isStarted = false;
}
void MQTTPublisher::handle()
{
if (!isStarted)
return;
if (!client.connected() && millis() - lastConnectionAttempt > RECONNECT_TIMEOUT) {
if (!reconnect()) return;
}
//got a valid mqtt connection. Loop through the inverts and send out the data if needed
client.loop();
bool sendRegular = millis() - lastSentRegularUpdate > mqttSettings->mqttRegularUpdateInterval;
bool sendQuick = millis() - lastSentQuickUpdate > mqttSettings->mqttQuickUpdateInterval;
if (sendRegular || sendQuick)
{
bool sendOk = true; //if a mqtt message fails, wait for retransmit at a later time
auto inverters = goodweCommunicator->getInvertersInfo();
for (char cnt = 0; cnt < inverters.size(); cnt++)
{
auto prependTopic = (String("goodwe/") + String(inverters[cnt].serialNumber));
debugPrint("Publishing prepend topic for this inverter is: ");
debugPrintln(prependTopic);
//send values when offline or online since the values can be reset when offline
if (sendQuick)
{
//send out fast changing values
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/online", inverters[cnt].isOnline && inverters[cnt].addressConfirmed ? "1" : "0");
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vpv1", String(inverters[cnt].vpv1, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vpv2", String(inverters[cnt].vpv2, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/ipv1", String(inverters[cnt].ipv1, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/ipv2", String(inverters[cnt].ipv2, 1));
//publishing sometimes cuases the wdt to reset the ESP.
//On the github page of the pubsubclient it was suggested to add extra client.loop().
client.loop();
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac1", String(inverters[cnt].vac1, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac1", String(inverters[cnt].iac1, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac1", String(inverters[cnt].fac1, 2));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/pac", String(inverters[cnt].pac));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/temp", String(inverters[cnt].temp, 1));
if (inverters[cnt].isDTSeries)
{
//On the github page of the pubsubclient it was suggested to add extra client.loop().
client.loop();
//also send tri fase info
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac2", String(inverters[cnt].vac2, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac2", String(inverters[cnt].iac2, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac2", String(inverters[cnt].fac2, 2));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/vac3", String(inverters[cnt].vac3, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/iac3", String(inverters[cnt].iac3, 1));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/fac3", String(inverters[cnt].fac3, 2));
}
}
else
{
//regular
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/workmode", String(inverters[cnt].workMode));
if (sendOk) sendOk = publishOnMQTT(prependTopic, "/eday", String(inverters[cnt].eDay));
//TODO: Rest of data
}
//On the github page of the pubsubclient it was suggested to add extra client.loop().
client.loop();
}
if (sendQuick)
lastSentQuickUpdate = millis();
if (sendRegular)
lastSentRegularUpdate = millis();
debugPrint("MQTT send status: ");
debugPrintln(sendOk);
}
}
bool MQTTPublisher::publishOnMQTT(String prepend, String topic, String value)
{
auto retVal = client.publish((prepend.c_str() + topic).c_str(), value.c_str());
yield();
return retVal;
}