-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMQTT.h
73 lines (64 loc) · 1.88 KB
/
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
/* MQTT client functions
Author: Christian Langrock
Date: 2022-August-07
*/
#include <PubSubClient.h>
PubSubClient client(espClient);
//long lastReconnectAttempt = 0;
// connect to MQTT Broker
boolean reconnect() {
// Loop until we're reconnected
if (!client.connected()) {
if(debugOutput) Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = mqtt_id + String("-");
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
if(debugOutput) Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("outTopic", "hello world");
// ... and resubscribe
//client.subscribe("inTopic");
} else {
if(debugError){
Serial.print("MQTT connection failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
}
}
}
return client.connected();
}
bool MQTT_puplish_float (char* strMQTTTopic, float fMQTTValue) {
bool Com_Ok {false};
char charBuffer[32];
String strBuffer;
strBuffer = String(fMQTTValue);
strBuffer.toCharArray(charBuffer,10);
if (!client.publish(strMQTTTopic, charBuffer, false)){
if(debugError) {
Serial.print("Can not publish to MQTT Broker: ");
Serial.print(strMQTTTopic);
Serial.print(" ");
}
}
else{
Com_Ok = true;
}
return Com_Ok;
}
bool MQTT_puplish_char (char* strMQTTTopic, char* fMQTTValue) {
bool Com_Ok {false};
if (!client.publish(strMQTTTopic, fMQTTValue, false)){
if(debugError) {
Serial.print("Can not publish to MQTT Broker: ");
Serial.print(strMQTTTopic);
Serial.print(" ");
}
}
else{
Com_Ok = true;
}
return Com_Ok;
}