-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_mqtt.h
106 lines (95 loc) · 3.19 KB
/
my_mqtt.h
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
/*
* refrigerator-lights: https://github.com/munichmakerlab/refrigerator-lights
* Copyright (C) 2016 Juergen Skrotzky alias Jorgen ([email protected])
*
* This sketch lights up a 15x10 WS2812b RGB LEDs.
* The lights are fully controllable over WiFi by MQTT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef my_mqtt_h
#define my_mqtt_h
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <QueueArray.h> // http://playground.arduino.cc/Code/QueueArray
#include "config.h"
bool mqttNewMessage = false;
struct sMqttMessage {
String topic;
String payload;
sMqttMessage(String strTopic="", String strPayload="") {
topic = strTopic;
payload = strPayload;
};
~sMqttMessage(){
topic = "";
payload = "";
};
};
QueueArray <sMqttMessage> mqttMessages;
// initial mqtt client with mqtt broker server ip address
WiFiClient client;
PubSubClient mqttClient(client, mqtt_host);
void publishMQTTMessage(String strTopic, String strMessage, bool bRetain=false) {
if (bRetain)
mqttClient.publish(MQTT::Publish(strTopic, strMessage).set_retain().set_qos(1));
else
mqttClient.publish(strTopic, strMessage);
DEBUG_PRINTLN("send MQTT: topic='" + strTopic + "', message='" + strMessage + "'");
}
void subscribeMQTTTopic(String strTopic) {
mqttClient.subscribe(strTopic);
DEBUG_PRINTLN("subscribe MQTT: topic='" + strTopic + "'");
}
bool connectMQTT() {
DEBUG_PRINTLN("Connect to MQTT...");
DEBUG_PRINT("MQTT host: ");
DEBUG_PRINTLN(mqtt_host);
DEBUG_PRINT("MQTT user: ");
DEBUG_PRINTLN(mqtt_user);
DEBUG_PRINT("MQTT pass: ");
DEBUG_PRINTLN(mqtt_pass);
String strClientID = String(mqtt_client_id) + String("-") + String(strChipID);
mqttClient.loop();
mqttClient.disconnect();
mqttClient.loop();
mqttClient.connect(MQTT::Connect(strClientID).set_clean_session().set_will(strTopicPrefixChipID+"state","0",1,true).set_auth(mqtt_user,mqtt_pass).set_keepalive(30));
mqttClient.loop();
delay(100);
if ( mqttClient.connected() ) {
DEBUG_PRINTLN("\nMQTT reconnected");
publishMQTTMessage(strTopicPrefixChipID + "state", "1", true);
publishMQTTMessage(strTopicPrefix + "connected", strChipID + "," + strIPAddr);
subscribeMQTTTopic(strTopicPrefixChipID + "#");
return true;
}
return false;
}
void checkConnect() {
uint32_t beginWait = millis();
while (millis() - beginWait < 1000) {
delay(100);
if ( WiFi.status() == WL_CONNECTED ) {
if (connectMQTT())
return;
}
}
}
void processMQTTLoop() {
if ( WiFi.status() != WL_CONNECTED || mqttClient.connected() != 1 ) {
checkConnect();
}
mqttClient.loop();
}
#endif //mqtt_h