-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMQTTLink.cpp
187 lines (144 loc) · 4.99 KB
/
MQTTLink.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
184
185
186
187
/*
MQTTLink.cpp Espress Connection MQTT for esp8266
Copyright (c) 2016 David Paiva ([email protected]). All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "MQTTLink.h"
#include "globals.h"
#include <myWebServer.h>
#include <ArduinoJson.h>
#include <FS.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
WiFiClient wclient;
PubSubClient mqttclient(wclient);
MQTTLinkClass MQTTLink;
//mqTT stuffs
void callbackMQTT(const MQTT::Publish& pub) { //callback when we get a msg from subtopics....
// handle message arrived
DebugPrint(pub.topic());
DebugPrint(" => ");
/*if (pub.has_stream()) {
uint8_t buf[100];
int read;
while (read = pub.payload_stream()->read(buf, 100)) {
Serial.write(buf, read);
}
pub.payload_stream()->stop();
DebugPrintln("");
}
else*/
DebugPrintln(pub.payload_string());
if (pub.topic() == MQTTLink.mqSubTopic + "/SetTemp") //setTemp
{
int tbSetTemp;
tbSetTemp = pub.payload_string().toInt(); //set RemoteTemp;
if (tbSetTemp >= 1) HMGlobal.SetTemp(tbSetTemp);
}
if (pub.topic() == MQTTLink.mqSubTopic + "/SetAlarm") //setTemp
{
HMGlobal.ConfigAlarms(pub.payload_string());
}
}
void MQTTLinkClass::StartMqtt()
{
if (mqEnabled == false) exit;
mqttclient.set_server(mqServer, mqPort);
mqttclient.set_callback(callbackMQTT);
DebugPrintln("MQTT Started");
}
void MQTTLinkClass::SubscribeMqtt() ///called in loop and enabled example to test : MyEspSub/SetTemp and message is just the int value
{
if (mqEnabled == false) exit;
if (mqttclient.connected()) mqttclient.loop();
if (WiFi.status() == WL_CONNECTED) {
if (!mqttclient.connected()) {
if (mqttclient.connect(MQTT::Connect("espHeater").set_auth(mqUser, mqPassword)
//.set_keepalive(15)
//Send will message with QoS=0 and retain=true
.set_will(mqPubTopic + "/status", "offline", 0, true)))
{
mqttclient.publish(MQTT::Publish(mqPubTopic + "/status", "online").set_retain());
mqttclient.subscribe(mqSubTopic + "/#");
DebugPrintln("connected to MQTT");
}
}
}
}
void MQTTLinkClass::PublishMQTT()
{
if (mqEnabled == false) exit;
if (WiFi.status() == WL_CONNECTED) {
if (mqttclient.connected()) {
mqttclient.publish(mqPubTopic + "/SetTemp", HMGlobal.hmSetPoint);
mqttclient.publish(mqPubTopic + "/PitTemp", HMGlobal.hmProbeTemp[0]);
mqttclient.publish(mqPubTopic + "/Food1", HMGlobal.hmProbeTemp[1]);
mqttclient.publish(mqPubTopic + "/Food2", HMGlobal.hmProbeTemp[2]);
mqttclient.publish(mqPubTopic + "/Food3", HMGlobal.hmProbeTemp[3]);
mqttclient.publish(mqPubTopic + "/Fan", HMGlobal.hmFan);
mqttclient.publish(mqPubTopic + "/FanAvg", HMGlobal.hmFanMovAvg);
mqttclient.publish(mqPubTopic + "/LidOpenCount", HMGlobal.hmLidOpenCountdown);
} //if mqtt connected
} //if wifi connected
}
void MQTTLinkClass::SendAlarm(String AlarmMsg)
{
if (mqEnabled == false) exit;
if (WiFi.status() == WL_CONNECTED) {
if (mqttclient.connected()) {
mqttclient.publish(mqPubTopic + "/Alarm", AlarmMsg);
}
}
}
void MQTTLinkClass::begin() //loads settings from json file....
{
String values = "";
File f = SPIFFS.open("/cloudgen.json", "r");
if (!f) {
DebugPrintln("mqtt config not found");
}
else { //file exists;
values = f.readStringUntil('\n'); //read json
f.close();
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(values); //parse weburl
if (!root.success())
{
DebugPrintln("parseObject() mqtt failed");
return;
}
if (root["mqserver"].asString() != "") { //verify good json info
if (String(root["mqstatus"].asString()).toInt() == 1) mqEnabled = true; else mqEnabled = false;
mqServer = root["mqserver"].asString();
mqUser = root["mquser"].asString();
mqPassword = root["mqpass"].asString();
mqPubTopic = root["mqpub"].asString();
mqSubTopic = root["mqsub"].asString();
mqInterval = String(root["mqint"].asString()).toInt();
mqPort = String(root["mqport"].asString()).toInt();
}
} //file exists;
if (mqEnabled) StartMqtt();
}
void MQTTLinkClass::handle()
{
if (mqEnabled == false) return;
SubscribeMqtt();
unsigned long curTime = millis();
//manage timers manually....more stable this way!
if (curTime - lastMqttChk >= mqInterval * 1000) { //mqtt stuffs
PublishMQTT();
lastMqttChk = millis();
}
}