-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotvac-mqtt.ino
executable file
·259 lines (202 loc) · 6.62 KB
/
botvac-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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
// Update with suitable values
//#define MAX_BUFFER 8192
#define SSID "yourwifinetwork"
#define SSID_PASSWORD "password"
#define MQTT_SERVER "openhab"
#define MQTT_PORT 1883
#define MQTT_CLIENT_ID "botvac"
#define WIFI_HOSTNAME MQTT_CLIENT_ID
#define MQTT_USER "openhab"
#define MQTT_PASSWORD "password"
#define MQTT_TOPIC_SUBSCRIBE "/home/vacuum/command"
#define MQTT_WILL_TOPIC "/home/vacuum/connected"
#define MQTT_PUB_TOPIC "/home/vacuum/status/"
#define MQTT_RETAIN false
#define NUM_POLL_CMDS 3
String pollCommands[NUM_POLL_CMDS] = {"GetErr", "GetCharger", "GetAnalogSensors"};
#define NUM_ONESHOT_CMDS 3
String oneshotCommands[NUM_ONESHOT_CMDS] = {"GetTime", "GetVersion", "GetWarranty"};
bool queueOneshots = true;
uint8_t curr_pollcmd = 0;
uint8_t curr_oscmd = 0;
// Timer
unsigned long interval = 30000;
unsigned long previousMillis = millis();
bool prioCommandRunning=false;
WiFiClient espClient;
PubSubClient mqttClient(espClient);
//Connect with telnet for debug info
WiFiServer TelnetServer(23);
WiFiClient Telnet;
// Executes a command and waits for a response
void executeSerialCommand(const String& payload, bool isPrioCommand = false) {
Telnet.println((String)__FUNCTION__ + "(" + payload + ")");
//consume bytes left on the wire
while (Serial.available() > 0) {
Telnet.println((String)"Consuming: " + Serial.read());
}
Serial.println(payload);
while (!Serial.available()) { //wait for response
delay(1);
}
bool nextIsHeader = false;
bool parseUnits = false;
while ((Serial.available()) ) {
if ((!isPrioCommand) && (prioCommandRunning))
return;
yield();
ArduinoOTA.handle(); //Safety measure in case this loop locks up
String msg = Serial.readStringUntil('\n');
//Telnet.println(msg);
msg.trim();
//remove trailing commas and ^Z
while ((msg.length() > 0) && ( (msg.lastIndexOf(',') == msg.length() - 1) || (msg.lastIndexOf('\x1A') == msg.length() - 1))) {
msg = msg.substring(0, msg.length() - 1);
}
Telnet.println(msg);
if (msg.indexOf(payload) != -1){
nextIsHeader = true;
} else if (nextIsHeader) {
nextIsHeader = false;
parseUnits = (msg.indexOf("Unit") != -1);
if (payload == "GetErr") {
mqttClient.publish(((String)MQTT_PUB_TOPIC + "error").c_str(), msg.c_str(), MQTT_RETAIN);
}
else if (payload == "GetTime") {
mqttClient.publish(((String)MQTT_PUB_TOPIC + "time").c_str(), msg.c_str(), MQTT_RETAIN);
}
//Telnet.println((String) msg + ": " + (bool)parseUnits);
} else if ((msg.length() > 0) && (msg.charAt(0) != '\x1A')) {
//Telnet.println(msg);
{
String subtopic, value;
if (parseUnits) {
uint8_t firstComma = msg.indexOf(',');
uint8_t secondComma = msg.indexOf(',', firstComma + 1);
uint8_t lastComma = msg.lastIndexOf(',');
subtopic = msg.substring(0, firstComma);
subtopic.trim();
String unit = msg.substring(firstComma + 1, secondComma);
unit.trim();
if (secondComma == lastComma){
value = msg.substring(secondComma + 1);
} else {
value = msg.substring(secondComma + 1, lastComma);
}
value.trim();
subtopic += "_" + unit;
} else {
subtopic = msg.substring(0, msg.indexOf(','));
subtopic.trim();
value = msg.substring(msg.indexOf(',')+1);
value.trim();
}
//Telnet.println(subtopic + ": " + value);
mqttClient.publish(((String)MQTT_PUB_TOPIC + subtopic).c_str(), value.c_str(), MQTT_RETAIN);
}
}
}
previousMillis = millis();
}
void pollVacuum() {
if (queueOneshots) {
executeSerialCommand(oneshotCommands[curr_oscmd]);
if (curr_oscmd + 1 < NUM_ONESHOT_CMDS) {
curr_oscmd++;
} else {
curr_oscmd=0;
queueOneshots = false;
}
} else {
executeSerialCommand(pollCommands[curr_pollcmd]);
if (curr_pollcmd + 1 < NUM_POLL_CMDS)
curr_pollcmd++;
else
curr_pollcmd=0;
}
}
void setup() {
// botvac serial console is 115200 baud, 8 data bits, no parity, one stop bit (8N1)
Serial.begin(115200);
Serial.setTimeout(5000);
setup_wifi();
ArduinoOTA.begin();
TelnetServer.begin();
TelnetServer.setNoDelay(true);
Telnet.println("setup");
mqttClient.setServer(MQTT_SERVER, MQTT_PORT);
mqttClient.setCallback(mqttCallback);
while (Serial.available() > 0) {
Serial.read();
}
previousMillis = millis();
}
void setup_wifi() {
WiFi.mode(WIFI_STA);
WiFi.hostname(WIFI_HOSTNAME);
WiFi.begin(SSID, SSID_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void handleTelnet() {
if (TelnetServer.hasClient()) {
if (!Telnet || !Telnet.connected()) {
if (Telnet) Telnet.stop();
Telnet = TelnetServer.available();
} else {
TelnetServer.available().stop();
}
}
}
void mqttCallback(char* cptr_topic, byte* bptr_payload, unsigned int payloadLength) {
String topic(cptr_topic);
//Some add-a-null-termination-trickery
char cptr_payload[payloadLength + 1];
strncpy(cptr_payload, (char*)bptr_payload, payloadLength);
cptr_payload[payloadLength] = '\0';
String payload(cptr_payload);
Telnet.println(((String)__FUNCTION__)+"(" + topic +", " + payload + ", " + payloadLength + ")");
if (topic.length() > 0) {
prioCommandRunning = true;
delay(100);
executeSerialCommand(payload, true);
prioCommandRunning = false;
}
}
void mqttReconnect() {
unsigned int retries = 0;
while (!mqttClient.connected() && retries < 5) {
yield();
if (mqttClient.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD, MQTT_WILL_TOPIC, 0, true, "0")) {
mqttClient.subscribe(MQTT_TOPIC_SUBSCRIBE);
mqttClient.publish(MQTT_WILL_TOPIC, "2", true);
queueOneshots = true;
} else {
ArduinoOTA.handle();
retries++;
delay(5000);
}
}
}
void loop() {
ArduinoOTA.handle();
handleTelnet();
if (WiFi.status() != WL_CONNECTED){
setup_wifi();
}
if (!mqttClient.connected()) {
mqttReconnect();
Telnet.println("Reconnect MQTT");
}
yield();
if (mqttClient.connected()) {
mqttClient.loop();
if (millis() - previousMillis > interval)
pollVacuum();
}
}