This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTTPublisher.cpp
142 lines (117 loc) · 2.67 KB
/
MQTTPublisher.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
#include "MQTTPublisher.h"
#include "Settings.h"
WiFiClient espClient;
PubSubClient client(espClient);
MQTTPublisher::MQTTPublisher(bool inDebugMode)
{
randomSeed(micros());
debugMode = inDebugMode;
}
MQTTPublisher::~MQTTPublisher()
{
client.publish("CO2Meter", "offline");
client.disconnect();
}
bool MQTTPublisher::reconnect()
{
lastConnectionAttempt = millis();
if (debugMode)
{
Serial.print("MQTT) Attempt connection to server: ");
Serial.print(MQTT_HOST_NAME);
}
// Create a random client ID
String clientId = "CO2Meter-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
bool clientConnected;
if (String(MQTT_USER_NAME).length())
{
Serial.println("MQTT) Connecting with credientials");
clientConnected = client.connect(clientId.c_str(), MQTT_USER_NAME, MQTT_PASSWORD);
}
else
{
Serial.println("MQTT) Connecting without credentials");
clientConnected = client.connect(clientId.c_str());
}
if (clientConnected)
{
if (debugMode)
{
Serial.println("MQTT) connected");
}
hasMQTT = true;
// Once connected, publish an announcement...
client.publish("CO2Meter", "online");
return true;
}
else
{
if (debugMode)
{
Serial.print("MQTT) failed, rc=");
Serial.print(client.state());
}
}
return false;
}
void MQTTPublisher::start()
{
if (String(MQTT_HOST_NAME).length() == 0 || MQTT_PORT == 0)
{
Serial.println("MQTT) disabled. No hostname or port set.");
return; //not configured
}
if (debugMode)
{
Serial.println("MQTT) enabled. Connecting.");
}
client.setServer(MQTT_HOST_NAME, MQTT_PORT);
reconnect();
isStarted = true;
}
void MQTTPublisher::stop()
{
isStarted = false;
}
void MQTTPublisher::handle()
{
if (!isStarted)
return;
if (!client.connected() && millis() - lastConnectionAttempt > RECONNECT_TIMEOUT)
{
hasMQTT = false;
if (!reconnect())
return;
}
if (hasWIFI && millis() - lastUpdateMqtt > MQTT_UPDATE_INTERVAL)
{
lastUpdateMqtt = millis();
// Check if last send value differs from new value
if (lastEco2 != sendEco2)
{
if (debugMode)
{
Serial.println("MQTT) SEND ECO2");
}
// SEND MQTT
publishOnMQTT(MQTT_TOPIC, "/eco2", String(lastEco2));
}
if (lastTVoc != sendTVoc)
{
if (debugMode)
{
Serial.println("MQTT) SEND TVOC");
}
// SEND MQTT
publishOnMQTT(MQTT_TOPIC, "/tvoc", String(lastTVoc));
}
}
}
bool MQTTPublisher::publishOnMQTT(String prepend, String topic, String value)
{
auto retVal = client.publish((prepend.c_str() + topic).c_str(), value.c_str());
yield();
return retVal;
}