-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmqtt.ino
75 lines (62 loc) · 1.77 KB
/
mqtt.ino
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
unsigned long lastReconnect = 0;
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("[MQTT] Message arrived: ");
int n;
for (n = 0; n < length; n++) {
Serial.print((char)payload[n]);
}
Serial.println();
char* msgString = (char*)payload;
msgString[n] = '\0';
int i = 0;
boolean startRobotCmd = false;
int robotCmd;
char* pch = strtok(msgString,":");
while (pch != NULL) {
if(i==0) {
if(strcmp(pch, "robot") == 0)
startRobotCmd = true;
else
Serial.println("[MQTT] Did not get robot command");
}
//check command
if(startRobotCmd and i==1) {
//Serial.print("[MQTT] Start checking command on validity: ");
//Serial.println(pch);
if(doAction(pch)) {
Serial.print("[MQTT] Got valid command: ");
Serial.println(pch);
}
else {
Serial.println("[MQTT] Got invalid command");
}
}
i++;
pch = strtok(NULL, ":");
}
}
//void mqttPublish(const char* format, ...) {
// char msg[100];
// snprintf(msg, 100, format, var1);
// mqtt.publish(outTopic, msg);
//}
void reconnect() {
unsigned long now = millis();
// Loop until we're reconnected
if(!mqtt.connected() and now-lastReconnect > 5000 or lastReconnect == 0) {
lastReconnect = now;
Serial.print("[MQTT] Attempting connection...");
// Attempt to connect
if (mqtt.connect("ESP8266Client")) {
// if (mqtt.connect("ESP8266Client", mqtt_user, mqtt_pass)) { // use instead previous line when MQTT broker uses credentials
Serial.println("connected");
publishStatus();
mqtt.subscribe(inTopic);
}
else {
Serial.print(" failed, rc=");
Serial.print(mqtt.state());
Serial.println(" try again in 5 seconds");
}
}
}