mqtt.ino
1.3 KB
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
#include "mqtt.h"
long stringToLong(String s) {
char arr[12];
s.toCharArray(arr, sizeof(arr));
return atol(arr);
}
void setup_mqtt() {
// setup callbacks
mqttClient.onConnected(mqttConnected);
mqttClient.onDisconnected(mqttDisconnected);
mqttClient.onPublished(mqttPublished);
mqttClient.onData(mqttData);
// connect
mqttClient.connect();
}
void mqttSubscribe() {
/* mqttClient.subscribe("home/compost/pump"); */
mqttClient.subscribe(PREAMBLE T_PUMPDURATION,1);
mqttClient.subscribe(PREAMBLE T_PUMPLEVEL,1);
mqttClient.subscribe(PREAMBLE T_PUMPABORT,1);
/* Serial.println("subscribed to" PREAMBLE T_PUMPDURATION); */
}
void mqttPublished() {
/* Serial.println("published cb"); */
}
void mqttConnected() {
mqttSubscribe();
Serial.println("connected");
}
void mqttDisconnected() {
Serial.println("disconnected");
delay(500);
mqttClient.connect();
}
void mqttData(String& topic, String& data) {
String dataStr(data);
/* mqttClient.publish(PREAMBLE T_PUMPSTATE, dataStr ); */
if (topic == PREAMBLE T_PUMPDURATION) {
insert("pumpduration", time(NULL), stringToLong(dataStr), 0);
}
if (topic == PREAMBLE T_PUMPLEVEL) {
insert("pumplevel", time(NULL), 1, stringToLong(dataStr));
}
if (topic == PREAMBLE T_PUMPABORT) {
insert("pumpabort", 0, 0, 0);
}
}