mqtt.ino
1.71 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
55
56
57
58
59
60
61
62
63
64
65
#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"); */
Serial.println("mqttSubscribe");
mqttClient.subscribe(PREAMBLE T_FILL TS_DURATION,1);
mqttClient.subscribe(PREAMBLE T_ROTATE TS_DURATION ,1);
mqttClient.subscribe(PREAMBLE T_FERTILIZE TS_DURATION ,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) {
Serial.println("mqttData");
String dataStr(data);
Serial.println(topic);
Serial.println(PREAMBLE T_FILL TS_DURATION);
if (topic == PREAMBLE T_FILL TS_DURATION) {
Serial.println("rotate duration");
enqueue(C_FILL_DURATION, time(NULL), stringToLong(dataStr));
}
if (topic == PREAMBLE T_ROTATE TS_DURATION) {
enqueue(C_ROTATE_DURATION, time(NULL), stringToLong(dataStr));
}
if (topic == PREAMBLE T_FERTILIZE TS_DURATION) {
enqueue(C_FERTILIZE_DURATION, time(NULL), stringToLong(dataStr));
}
/* if (topic == PREAMBLE T_PUMPLEVEL) { */
/* enqueue("pumplevel", time(NULL), stringToLong(dataStr)); */
/* } */
/* if (topic == PREAMBLE T_PUMPABORT) { */
/* enqueue("pumpabort", 0, 0); */
/* } */
}