forked from MTJoker/HomeStatusDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HSDMqtt.cpp
184 lines (156 loc) · 3.62 KB
/
HSDMqtt.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
175
176
177
178
179
180
181
182
183
#include "HSDMqtt.hpp"
HSDMqtt::HSDMqtt(const HSDConfig& config, MQTT_CALLBACK_SIGNATURE)
:
m_config(config),
m_pubSubClient(m_wifiClient),
m_maxConnectRetries(3),
m_numConnectRetriesDone(0),
m_retryDelay(5000),
m_millisLastConnectTry(0),
m_numberOfInTopics(0)
{
m_pubSubClient.setCallback(callback);
}
void HSDMqtt::begin()
{
initTopics();
addTopic(m_config.getMqttStatusTopic());
addTopic(m_config.getMqttTestTopic());
IPAddress mqttIpAddr;
if(mqttIpAddr.fromString(m_config.getMqttServer()))
{
// valid ip address entered
m_pubSubClient.setServer(mqttIpAddr, 1883);
}
else
{
// invalid ip address, try as hostname
m_pubSubClient.setServer(m_config.getMqttServer(), 1883);
}
}
void HSDMqtt::initTopics()
{
for(uint32_t index = 0; index < MAX_IN_TOPICS; index++)
{
m_inTopics[index] = NULL;
}
m_numberOfInTopics = 0;
}
void HSDMqtt::handle()
{
if(connected())
{
m_pubSubClient.loop();
}
else if(!m_connectFailure)
{
unsigned long currentMillis = millis();
if( (currentMillis - m_millisLastConnectTry) >= m_retryDelay)
{
m_millisLastConnectTry = currentMillis;
if(m_numConnectRetriesDone < m_maxConnectRetries)
{
if(reconnect())
{
Serial.println("DEBUG: Reconnect successful");
m_numConnectRetriesDone = 0;
}
else
{
m_numConnectRetriesDone++;
Serial.println("DEBUG: Reconnect unsuccessful, m_numConnectRetriesDone = " + String(m_numConnectRetriesDone));
}
}
else
{
Serial.println(F("Failed to connect Mqtt."));
m_connectFailure = true;
}
}
}
}
bool HSDMqtt::connected() const
{
return m_pubSubClient.connected();
}
bool HSDMqtt::reconnect()
{
bool retval = false;
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
Serial.print(F("Connecting to MQTT broker "));
Serial.print(String(m_config.getMqttServer()));
Serial.print(" with client id " + clientId + "... ");
const char* willTopic = m_config.getMqttWillTopic();
bool connected = false;
if(isTopicValid(willTopic))
{
connected = m_pubSubClient.connect(clientId.c_str(), willTopic, 0, true, "off");
}
else
{
connected = m_pubSubClient.connect(clientId.c_str());
}
if(connected)
{
Serial.println(F("connected"));
if(isTopicValid(willTopic))
{
publish(willTopic, "on");
}
for(uint32_t index = 0; index < m_numberOfInTopics; index++)
{
subscribe(m_inTopics[index]);
}
retval = true;
}
else
{
Serial.print(F("failed, rc = "));
Serial.println(m_pubSubClient.state());
}
return retval;
}
void HSDMqtt::subscribe(const char* topic)
{
if(isTopicValid(topic))
{
Serial.print(F("Subscribing to topic "));
Serial.println(topic);
if(!m_pubSubClient.subscribe(topic))
{
Serial.print("Failed to subscribe to topic ");
Serial.println(topic);
}
}
}
void HSDMqtt::publish(String topic, String msg)
{
if(m_pubSubClient.publish(topic.c_str(), msg.c_str()))
{
Serial.println("Published msg " + msg + " for topic " + topic);
}
else
{
Serial.println("Error publishing msg " + msg + " for topic " + topic);
}
}
bool HSDMqtt::addTopic(const char* topic)
{
bool success = false;
if(isTopicValid(topic))
{
if(m_numberOfInTopics < (MAX_IN_TOPICS - 1))
{
m_inTopics[m_numberOfInTopics] = topic;
m_numberOfInTopics++;
success = true;
}
}
return success;
}
bool HSDMqtt::isTopicValid(const char* topic)
{
return (strlen(topic) > 0);
}