-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_utils.cpp
68 lines (59 loc) · 1.94 KB
/
mqtt_utils.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
/*
Vladimir Kirievskiy (C) 2019
********************************************************************************************
* @brief MQTT client software for esp8266 platform (tested on Wemos D1 mini)
* @license MIT
* SDK: Arduino IDE 1.8.5 with plugin for esp8266
*
********************************************************************************************
* @author V. Kirievskiy aka vlakir
* vladimir@kirievskiy.ru
* https://github.com/vlakir
* This software is furnished "as is", without technical support, and with no
* warranty, express or implied, as to its usefulness for any purpose.
*/
#include "mqtt_utils.h"
Timer xPostStateADCtimer, xPostStateGPIOtimer;
static WiFiClient xWifiClient;
static PubSubClient xPsClient(xWifiClient);
/*
* @brief
* MQTT procedure for placement in the main loop
*
*/
void vMqttLoop(void) {
if (!xPsClient.connected()) {
_vServerConnect();
xPostStateADCtimer.every(ADC_CHECK_PERIOD_MS, vPostADC, (void *)&xPsClient);
xPostStateGPIOtimer.every(GPIO_CHECK_PERIOD_MS, vPostGPIO, (void *)&xPsClient);
}
xPostStateADCtimer.update();
xPostStateGPIOtimer.update();
xPsClient.loop();
}
/*
* @brief
* Connect to MQTT broker
*
*/
static void _vServerConnect(void) {
static VirtualDelay singleDelay;
xPsClient.disconnect();
xPsClient.setServer(xGlobalSettings.acMQTTserver, xGlobalSettings.uiMQTTport);
xPsClient.setCallback(vRecieveCallback);
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (xPsClient.connect("arduinoClient", xGlobalSettings.acMQTTclientID, xGlobalSettings.acMQTTclientPassword)) {
Serial.println("connected");
}
else {
Serial.print("failed, rc=");
Serial.print(xPsClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
char acTopic[80];
sprintf(acTopic, "%s%s", xGlobalSettings.acDeviceID, "/management/#");
xPsClient.subscribe(acTopic);
}