-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddmqtt.cpp
122 lines (96 loc) · 2.7 KB
/
ddmqtt.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
/* 1.2.0 VERSION */
#include "ddmqtt.h"
#include "ddcommon.h"
DDMqtt::DDMqtt(const char *deviceName, const char *mqttHost, int mqttPort, const char *mqttUser, const char *mqttPwd, const char *subscribeTopic, int subscribeQOS, int led)
{
this->_deviceName = deviceName;
this->_mqttHost = mqttHost;
this->_mqttPort = mqttPort;
this->_mqttUser = mqttUser;
this->_mqttPwd = mqttPwd;
this->_subscribeTopic = subscribeTopic;
this->_subscribeQOS = subscribeQOS;
this->_ledStatusPin = led;
this->_clientMqtt.setBufferSize(this->mqtt_max_packet_size);
}
void DDMqtt::reconnectMQTT()
{
writeToSerial("Reconnecting...", true);
while (!this->_clientMqtt.connected())
{
writeToSerial("Attempting MQTT connection to ", false);
writeToSerial(this->_mqttHost, false);
writeToSerial(":", false);
writeToSerial(this->_mqttPort, false);
writeToSerial("...", true);
this->_clientMqtt.setServer(this->_mqttHost, this->_mqttPort);
// Attempt to connect
if (this->_clientMqtt.connect(this->_deviceName, this->_mqttUser, this->_mqttPwd))
{
writeToSerial("connected", true);
if (this->callback != nullptr)
{
this->_clientMqtt.setCallback(this->callback);
if (this->_clientMqtt.subscribe(this->_subscribeTopic, this->_subscribeQOS))
{
writeToSerial("subscribed to topic ", false);
writeToSerial(this->_subscribeTopic, true);
}
else
{
writeToSerial("failed to subscribe to topic ", false);
writeToSerial(this->_subscribeTopic, true);
}
}
}
else
{
writeToSerial("failed, rc=", false);
writeToSerial(this->_clientMqtt.state(), false);
writeToSerial(" try again", true);
digitalWrite(this->_ledStatusPin, LOW);
}
delay(5000);
}
digitalWrite(this->_ledStatusPin, HIGH);
}
void DDMqtt::loop()
{
this->_clientMqtt.loop();
}
void DDMqtt::sendMessage(const char *topic, String val)
{
writeToSerial("Sending ", false);
writeToSerial(val, false);
writeToSerial(" to topic ", false);
writeToSerial(topic, true);
if (!this->_clientMqtt.connected())
{
writeToSerial("#2 MQTT not connected. Retrying... ", true);
this->reconnectMQTT();
}
this->_clientMqtt.loop();
if (!this->_clientMqtt.publish(topic, val.c_str(), true))
{
writeToSerial("MQTT publishing error", true);
digitalWrite(this->_ledStatusPin, LOW);
}
else
{
digitalWrite(this->_ledStatusPin, LOW);
delay(100);
digitalWrite(this->_ledStatusPin, HIGH);
delay(100);
digitalWrite(this->_ledStatusPin, LOW);
delay(100);
digitalWrite(this->_ledStatusPin, HIGH);
delay(100);
digitalWrite(this->_ledStatusPin, LOW);
delay(100);
digitalWrite(this->_ledStatusPin, HIGH);
}
}
void DDMqtt::setSubscribeCallback(MQTT_CALLBACK_SIGNATURE)
{
this->callback = callback;
}