-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt-support.cpp
174 lines (153 loc) · 5.15 KB
/
mqtt-support.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
void localMQTTCallback(char* topic, byte* payload, unsigned int length) {
// Relay actions
if (!strcmp(topic, actionSTopic.c_str()) || !strcmp(topic, groupActionSTopic.c_str()))
{
if ((char)payload[0] == '1' || ! strncasecmp_P((char *)payload, PL_ON, length))
{
desiredRelayState = 1;
}
else if ((char)payload[0] == '0' || ! strncasecmp_P((char *)payload, PL_OFF, length))
{
desiredRelayState = 0;
}
else if ((char)payload[0] == 'X' || ! strncasecmp_P((char *)payload, PL_TOGGLE, length))
{
desiredRelayState = !desiredRelayState;
}
else if ((char)payload[0] == 'S' || ! strncasecmp_P((char *)payload, PL_STATUS, length))
{
sendStatus = true;
}
else if (! strncasecmp_P((char *)payload, "wifi_config", length))
{
configWifi = true;
}
}
else if (!strcmp(topic, pingSTopic.c_str()))
// Ping action
{
sendPong = true;
} else {
mqttCallbackHandle(topic, payload, length);
}
}
void mqttSetup(void) {
Serial.println(F("mqttSetup"));
// create specific topics
mqttCallbackCreateTopics();
// after wifi and parameters are configured, create publish topics
eventTopic = String(F("event/")) + custom_unit_id.getValue() + String(F("/switch"));
groupEventTopic = String(F("event/")) + custom_group_id.getValue() + String(F("/switch"));
statusTopic = String(F("status/")) + custom_unit_id.getValue() + String(F("/relay"));
pongStatusTopic = String(F("pong/")) + custom_unit_id.getValue() + String(F("/status"));
pongMetaTopic = String(F("pong/")) + custom_unit_id.getValue() + String(F("/meta"));
// and subscribe topic
actionSTopic = String(F("action/")) + custom_unit_id.getValue() + String(F("/relay"));
groupActionSTopic = String(F("action/")) + custom_group_id.getValue() + String(F("/relay"));
pingSTopic = String(F("ping/nodes"));
client.setServer(custom_mqtt_server.getValue(), atoi(custom_mqtt_port.getValue()));
client.setCallback(localMQTTCallback);
}
void mqttLoop() {
// Handle any pending MQTT messages
client.loop();
mqttUpdateLED();
}
void mqttPublishMessage(const char* topic, const char* payload) {
// turn green LED off while transmitting
digitalWrite(LED_PIN, LEDOFF);
Serial.print(F("MQTT pub: "));
Serial.print(topic);
Serial.print(F(" = "));
Serial.println(payload);
client.publish(topic, payload);
delay(50);
mqttUpdateLED();
}
//
// Connect to MQTT broker
// Subscribe to topics, flash LED etc
//
void mqttCheckConnection(void) {
if (!client.connected())
{
if (WiFi.status() == WL_CONNECTED) {
//Wifi connected, attempt to connect to server
connections++;
Serial.print(F("new MQTT connection "));
Serial.print(connections);
Serial.print(F(" "));
if (client.connect(custom_unit_id.getValue(), custom_mqtt_user.getValue(), custom_mqtt_pass.getValue())) {
Serial.println(F("connected"));
client.subscribe(pingSTopic.c_str());
client.subscribe(actionSTopic.c_str());
client.subscribe(groupActionSTopic.c_str());
for (int i = 0; i < noSubscribedTopics; i++)
{
client.subscribe(subscribedTopics[i]->c_str());
}
mqttPublishMessage(pongStatusTopic.c_str(), (String("connected,") + String(connections)).c_str());
} else {
Serial.print(F("failed, rc="));
Serial.println(client.state());
}
}
else {
//Wifi isn't connected, so no point in trying now.
Serial.println(F(" Not connected to WiFI AP, abandoned connect."));
}
}
}
void mqttUpdateLED(void) {
//Set the status LED to ON if we are connected to the MQTT server
if (client.connected())
digitalWrite(LED_PIN, LEDON);
else
digitalWrite(LED_PIN, LEDON);
}
void mqttPublish(void) {
mqttCheckConnection();
// Relay state is updated via the interrupt *OR* the MQTT callback.
if (relayState != desiredRelayState) {
Serial.print(F("Chg state to "));
Serial.println(desiredRelayState == 0 ? PL_OFF : PL_ON);
digitalWrite(RELAY_PIN, desiredRelayState);
relayState = desiredRelayState;
sendStatus = true;
}
if (sendPong)
{
//Serial.print(F("MQTT pub: "));
String meta = getDeviceMeta(CONFIG_VERSION);
//Serial.print(meta);
//Serial.print(F(" to "));
//Serial.println(pongMetaTopic);
mqttPublishMessage(pongMetaTopic.c_str(), meta.c_str());
sendPong = false;
}
// publish event if touched
if (sendEvent) {
const char* payload = (relayState == 0) ? PL_OFF : PL_ON;
//Serial.print(F("MQTT pub: "));
//Serial.print(payload);
//Serial.print(F(" to "));
if (sendGroupEventTopic) {
Serial.println(groupEventTopic);
mqttPublishMessage(groupEventTopic.c_str(), payload);
} else {
Serial.println(eventTopic);
mqttPublishMessage(eventTopic.c_str(), payload);
}
sendEvent = false;
}
// publish state when requested to do so
if (sendStatus) {
const char* payload = (relayState == 0) ? PL_OFF : PL_ON;
//Serial.print(F("MQTT pub: "));
//Serial.print(payload);
//Serial.print(F(" to "));
//Serial.println(statusTopic);
mqttPublishMessage(statusTopic.c_str(), payload);
sendStatus = false;
}
}