-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmqtt-support.cpp
173 lines (153 loc) · 5.25 KB
/
mqtt-support.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
// MQTT support routines
#include "esp-sensors.h"
#include "mqtt-support.h"
// Prefix for MQTT topics
char *mqttPrefixName;
// Check connection to MQTT broker
boolean check_mqtt() {
if (myClient.connected())
return true;
return false;
} // boolean check_mqtt()
// Internal mqtt stuff
void mqtt_loop() {
myClient.loop();
} // void mqtt_loop()
char tmpMqttTopic[256];
// Use unique MQTT prefix to generate topic name (be sure outBuffer has enough space - +32 bytes!!!)
void generateMqttTopic(char *outBuffer, char *topic, char *subtopic, boolean verification) {
if (!verification)
sprintf(outBuffer, "%s%s/%s", mqttPrefixName, topic, subtopic);
else
sprintf(outBuffer, "%s%s_CURRENT/%s", mqttPrefixName, topic, subtopic);
} // void generateMqttTopic(char *outbuffer, char *topic, char *subtopic)
// Connect to MQTT broker, returns true if connected
boolean connect_mqtt(char *mqttServer, char *mqttClientName, char *mqttUser, char *mqttPass, std::function<void(char*, uint8_t*, unsigned int)> mqttCallback) {
if (myClient.connected())
return true;
myClient.setServer(mqttServer, MQTT_PORT);
myClient.setCallback(mqttCallback);
if (myClient.connect(mqttClientName, mqttUser, mqttPass)) {
#ifdef DEBUG
Serial.println("Connected to MQTT broker");
#endif
return true;
} else {
#ifdef DEBUG
Serial.println("Failed to connect to MQTT broker!");
#endif
return false;
}
} // boolean connect_mqtt(char *mqttServer, char *mqttClientName, char *mqttUser, char *mqttPass, std::function<void(char*, uint8_t*, unsigned int)> mqttCallback)
char tmpBuf[256];
char tmpBufO[256];
char tmpBufT[256];
// Publish Plug&Play info to MQTT
void pnp_mqtt(char *topic, char *name, char *groups, char *type, char *min, char *max) {
if (!myClient.connected())
return;
snprintf(tmpBuf, sizeof(tmpBuf) - 1, "%s%s%s/%s", MQTT_PNP_PREFIX, mqttPrefixName, topic, "name");
myClient.publish(tmpBuf, name, true);
#ifdef DEBUG
Serial.println("PnP: ");
Serial.print(tmpBuf);
Serial.print(':');
Serial.print(name);
#endif
snprintf(tmpBuf, sizeof(tmpBuf) - 1, "%s%s%s/%s", MQTT_PNP_PREFIX, mqttPrefixName, topic, "groups");
myClient.publish(tmpBuf, groups, true);
#ifdef DEBUG
Serial.println("PnP: ");
Serial.print(tmpBuf);
Serial.print(':');
Serial.print(groups);
#endif
snprintf(tmpBuf, sizeof(tmpBuf) - 1, "%s%s%s/%s", MQTT_PNP_PREFIX, mqttPrefixName, topic, "type");
myClient.publish(tmpBuf, type, true);
#ifdef DEBUG
Serial.println("PnP: ");
Serial.print(tmpBuf);
Serial.print(':');
Serial.print(type);
#endif
if (min != NULL) {
snprintf(tmpBuf, sizeof(tmpBuf) - 1, "%s%s%s/%s", MQTT_PNP_PREFIX, mqttPrefixName, topic, "min");
myClient.publish(tmpBuf, min, true);
}
if (max != NULL) {
snprintf(tmpBuf, sizeof(tmpBuf) - 1, "%s%s%s/%s", MQTT_PNP_PREFIX, mqttPrefixName, topic, "max");
myClient.publish(tmpBuf, max, true);
}
delay(10);
// Add output _CURRENT topic for input topics
if (type[0] == 'I') {
snprintf(tmpBufO, sizeof(tmpBufO) - 1, "%s_CURRENT", topic);
snprintf(tmpBufT, sizeof(tmpBufT) - 1, "%s", type);
tmpBufT[0] = 'O';
pnp_mqtt(tmpBufO, "", "Verification", tmpBufT, min, max);
}
} // void pnp_mqtt(char *topic, char *name, char *groups, char *type, char *min, char *max)
// Subscribe to MQTT topics
void subscribe_mqtt(char *topic) {
if (!myClient.connected())
return;
generateMqttTopic(tmpMqttTopic, topic, "status", false);
myClient.subscribe(tmpMqttTopic);
myClient.loop();
#ifdef DEBUG
Serial.print("Subscribe: ");
Serial.println(tmpMqttTopic);
#endif
delay(10);
} // void subscribe_mqtt(char *topic)
char tmpValue[64];
// Publish to topic (int)
void publish_mqttI(char *topic, int value, boolean verification) {
sprintf(tmpValue, "%d", value);
publish_mqttS(topic, tmpValue, verification);
} // void publish_mqttI(char *topic, int value)
// Publish to topic (string)
void publish_mqttS(char *topic, char *value, boolean verification) {
if (!myClient.connected())
return;
generateMqttTopic(tmpMqttTopic, topic, "status", verification);
myClient.publish(tmpMqttTopic, value, true);
#ifdef DEBUG
Serial.print(tmpMqttTopic);
Serial.print('=');
Serial.println(value);
#endif
} // void publish_mqttS(char *topic, char *value)
// Publish to topic (float)
void publish_mqttF(char *topic, float value, boolean verification) {
char *p;
char floatBuf[16];
p = dtostrf(value, 5, 2, floatBuf);
// Cut leading space
while (*p == ' ')
p++;
publish_mqttS(topic, p, verification);
} // void publish_mqttF(char *topic, float value)
// Compare message for "ON"
boolean cmpPayloadON(char *msg) {
if ((msg[1] == 'N') || (msg[1] == 'n'))
return true;
return false;
} // boolean cmpPayloadON(char *msg)
// Compare message for "OFF"
boolean cmpPayloadOFF(char *msg) {
if ((msg[1] == 'F') || (msg[1] == 'f'))
return true;
return false;
} // boolean cmpPayloadOFF(char *msg)
// Compare string to topic name
boolean cmpTopic(char *incoming, char *topic) {
int topicLength = strlen(topic);
if (memcmp(incoming, topic, topicLength))
return false;
if ((incoming[topicLength] != '/') || (incoming[topicLength + 1] == '\0'))
return false;
if (!strcmp(incoming + topicLength + 1, "status"))
return true;
return false;
} // boolean cmpTopic(char *incoming, char *topic)