forked from melkati/CO2-Gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCO2_Gadget_MQTT.h
191 lines (175 loc) · 6.17 KB
/
CO2_Gadget_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
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
188
189
190
191
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* SETUP MQTT FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#ifdef SUPPORT_MQTT
#include <PubSubClient.h>
char charPublish[20];
PubSubClient mqttClient(espClient);
#endif
void mqttReconnect() {
#ifdef SUPPORT_MQTT
static uint64_t timeStamp = 0;
uint16_t secondsBetweenRetries = 15; // Keep trying to connect to MQTT broker for 3 minutes (12 times every 15 secs)
uint16_t maxConnectionRetries = 12;
static uint16_t connectionRetries = 0;
if (millis() - timeStamp > (secondsBetweenRetries*1000)) { // Max one try each secondsBetweenRetries*1000 seconds
timeStamp = millis();
String subscriptionTopic;
if (!mqttClient.connected()) {
Serial.printf("-->[MQTT] Attempting MQTT connection... ");
// Attempt to connect
if (mqttClient.connect((mqttClientId).c_str(), (mqttUser).c_str(), (mqttPass).c_str())) {
Serial.printf("connected\n");
Serial.print("-->[MQTT] rootTopic: ");
Serial.println(rootTopic);
// Subscribe
subscriptionTopic = rootTopic + "/calibration";
mqttClient.subscribe((subscriptionTopic).c_str());
printf("-->[MQTT] Suscribing to: %s\n", subscriptionTopic.c_str());
subscriptionTopic = rootTopic + "/ambientpressure";
mqttClient.subscribe((subscriptionTopic).c_str());
printf("-->[MQTT] Suscribing to: %s\n", subscriptionTopic.c_str());
} else {
++connectionRetries;
mqttClient.setSocketTimeout(2); //Drop timeout to 2 secs for subsecuents tries
Serial.printf(" not possible to connect to %s ", mqttBroker.c_str());
Serial.printf("Connection status: %d. (%d of %d retries)\n", mqttClient.state(), connectionRetries, maxConnectionRetries); // Possible States: https://pubsubclient.knolleary.net/api#state
if (connectionRetries >= maxConnectionRetries) {
activeMQTT = false;
Serial.printf("-->[MQTT] Max retries to connect to MQTT broker reached, disabling MQTT...\n");
}
}
}
}
#endif
}
// Function called when data is received via MQTT
void callbackMQTT(char *topic, byte *message, unsigned int length) {
Serial.print("-->[MQTT] Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Payload: ");
String messageTemp;
String topicToLookAt;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
topicToLookAt = rootTopic + "/calibration";
if (strcmp(topic, topicToLookAt.c_str()) == 0) {
calibrationValue = messageTemp.toInt();
printf("-->[MQTT] Received calibration command with value %d\n", calibrationValue);
pendingCalibration = true;
}
topicToLookAt = rootTopic + "/ambientpressure";
if (strcmp(topic, topicToLookAt.c_str()) == 0) {
ambientPressureValue = messageTemp.toInt();
printf("-->[MQTT] Received ambient pressure with value %d\n", ambientPressureValue);
pendingAmbientPressure = true;
}
}
void publishIntMQTT(String topic, int16_t payload) {
#ifdef SUPPORT_MQTT
dtostrf(payload, 0, 0, charPublish);
topic = rootTopic + topic;
if (!inMenu) {
Serial.printf("-->[MQTT] Publishing %d to ", payload);
Serial.println("topic: " + topic);
mqttClient.publish((topic).c_str(), charPublish);
}
#endif
}
void publishFloatMQTT(String topic, float payload) {
#ifdef SUPPORT_MQTT
dtostrf(payload, 0, 2, charPublish);
topic = rootTopic + topic;
if (!inMenu) {
Serial.printf("-->[MQTT] Publishing %.0f to ", payload);
Serial.println("topic: " + topic);
mqttClient.publish((topic).c_str(), charPublish);
}
#endif
}
void publishStrMQTT(String topic, String payload) {
#ifdef SUPPORT_MQTT
payload.toCharArray(charPublish, payload.length());
topic = rootTopic + topic;
if (!inMenu) {
Serial.printf("-->[MQTT] Publishing %s to ", payload);
Serial.println("topic: " + topic);
mqttClient.publish((topic).c_str(), charPublish);
}
#endif
}
void initMQTT() {
#ifdef SUPPORT_MQTT
if (activeMQTT) {
if (!activeWIFI) {
activeMQTT = false;
return;
}
if (mqttClient.connected()) {
mqttClient.disconnect();
}
Serial.printf("-->[MQTT] Initializing MQTT to broker IP: %s\n", mqttBroker.c_str());
mqttClient.setServer(mqttBroker.c_str(), 1883);
mqttClient.setCallback(callbackMQTT);
mqttReconnect();
}
#endif
}
void publishMQTTAlarms() {
static bool MQTTGreenAlarm, MQTTOrangeAlarm, MQTTRedAlarm = false;
if ((co2>=co2OrangeRange) && (MQTTGreenAlarm)) {
MQTTGreenAlarm = false;
publishStrMQTT("/green", "OFF");
}
if ((co2<co2OrangeRange) && (!MQTTGreenAlarm)) {
MQTTGreenAlarm = true;
publishStrMQTT("/green", "ON");
}
if ((co2>=co2OrangeRange) && (!MQTTOrangeAlarm)) {
MQTTOrangeAlarm = true;
publishStrMQTT("/orange", "ON");
}
if ((co2<co2OrangeRange-PIN_HYSTERESIS) && (MQTTOrangeAlarm)) {
MQTTOrangeAlarm = false;
publishStrMQTT("/orange", "OFF");
}
if ((co2>co2RedRange) && (!MQTTRedAlarm)) {
MQTTRedAlarm = true;
publishStrMQTT("/red", "ON");
}
if ((co2<=co2RedRange-PIN_HYSTERESIS) && (MQTTRedAlarm)) {
MQTTRedAlarm = false;
publishStrMQTT("/red", "OFF");
}
}
void publishMQTT() {
#ifdef SUPPORT_MQTT
if (activeMQTT) {
if ((WiFi.status() == WL_CONNECTED) && (mqttClient.connected())) {
publishIntMQTT("/co2", co2);
publishFloatMQTT("/temp", temp);
publishFloatMQTT("/humi", hum);
publishMQTTAlarms();
}
// Serial.print("Free heap: ");
// Serial.println(ESP.getFreeHeap());
}
#endif
}
void mqttClientLoop() {
if (activeMQTT) {
#ifdef SUPPORT_MQTT
mqttReconnect(); // Make sure MQTT client is connected
if (mqttClient.connected()) {
mqttClient.loop();
}
#endif
}
}