-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainRoombaScetch.ino
397 lines (363 loc) · 9.39 KB
/
MainRoombaScetch.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <SimpleTimer.h>
#include <Roomba.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ArduinoJson.h>
//USER CONFIGURED SECTION START//
const char* ssid = "Chew-Bach-A";
const char* password = "MuisMatje123";
const char* mqtt_server = "192.168.1.84";
const int mqtt_port = 1883;
const char *mqtt_user = "mosquitto";
const char *mqtt_pass = "Abngel123";
const char *mqtt_client_name = "MainRoomba"; // Client connections can't have the same connection name
//pick an OTA password to use when updating over the air
const char *OTApassword = "Toboldlygo123=";
const int OTAport = 8266;
//USER CONFIGURED SECTION END//
const int bufferSize = 200; // Adjust the buffer size according to your needs
char logBuffer[bufferSize];
int logIndex = 0;
#define STATE_UNKNOWN 0
#define STATE_CLEANING 1
#define STATE_RETURNING 2
#define STATE_DOCKED 3
#define STATE_IDLE 4
#define RETURN_BATTERY_PERCENT 50 // Battery percent that we return to dock and ignore further 'start' commands.
#define CHARGE_NONE 0 // Not charging
#define CHARGE_RECONDITIONING 1 // Reconditioning
#define CHARGE_BULK 2 // Full Charging
#define CHARGE_TRICKLE 3 // Trickle Charging
#define CHARGE_WAITING 4 // Waiting
#define CHARGE_FAULT 5 // Charging Fault Condition
// Hold the Roomba status
struct Roomba_State {
uint8_t state;
uint8_t charge_state;
uint16_t battery_voltage;
int16_t battery_current;
uint8_t battery_temp;
uint16_t battery_charge;
uint16_t battery_capacity;
uint8_t battery_percent;
uint8_t num_restarts;
uint8_t num_timeouts;
};
struct Roomba_State MainRoomba;
WiFiClient espClient;
PubSubClient client(espClient);
SimpleTimer timer;
Roomba roomba(&Serial, Roomba::Baud115200);
// Variables
bool boot = true;
long battery_Current_mAh = 0;
long battery_Voltage = 0;
long battery_Total_mAh = 0;
long battery_percent = 0;
char battery_percent_send[50];
char battery_Current_mAh_send[50];
uint8_t tempBuf[10];
//Functions
void setup_wifi()
{
WiFi.hostname(mqtt_client_name);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
}
void reconnect()
{
// Loop until we're reconnected
int retries = 0;
while (!client.connected())
{
if(retries < 50)
{
// Attempt to connect
if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass, "roomba/status", 0, 0, "Dead Somewhere"))
{
// Once connected, publish an announcement...
if(boot == false)
{
client.publish("checkIn/roomba", "Reconnected");
}
if(boot == true)
{
client.publish("checkIn/roomba", "Rebooted");
boot = false;
}
// ... and resubscribe
ArduinoOTA.handle();
client.subscribe("roomba/commands");
}
else
{
retries++;
delay(5000);
}
}
if(retries >= 50)
{
ESP.restart();
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
String newTopic = topic;
if (newTopic == "roomba/commands")
{
payload[length] = '\0';
String newPayload = String((char *)payload);
if (newPayload == "start")
{
startCleaning();
}
if (newPayload == "stop")
{
stopCleaning();
}
if (newPayload == "restart")
{
client.publish("roomba/status", "Rebooting");
ESP.restart();
}
if (newPayload == "reset")
{
resetRoomba();
}
if (newPayload == "ota")
{
client.publish("roomba/status", "Rebooting OTA");
ArduinoOTA.end();
delay(500);
ArduinoOTA.begin();
client.publish("roomba/status", "OTA Restarted");
}
if (newPayload == "imperialmarch")
{
imperialmarch();
}
if (newPayload == "secondmarch")
{
secondmarch();
}
if (newPayload == "starwars")
{
starwars();
}
}
}
void startCleaning()
{
Serial.write(128);
delay(50);
Serial.write(131);
delay(50);
Serial.write(135);
client.publish("roomba/status", "Cleaning");
}
void stopCleaning()
{
Serial.write(128);
delay(50);
Serial.write(131);
delay(50);
Serial.write(143);
client.publish("roomba/status", "Returning");
}
void resetRoomba() {
byte command[] = { 128, 129, 11, 7 };
// Send factory reset in 19200 baud (sometimes we get stuck in this baud?!)
Serial.begin(19200);
SendCommandList( command, 4 );
delay(100);
// Send factory reset at 115200 baud (we should always be in this - but sometimes it bugs out.)
Serial.begin(115200);
SendCommandList( command, 4 );
}
void sendInfoRoomba()
{
roomba.start();
char buffer[10];
byte command[] = { 128, 149, 1, 3 };
SendCommandList( command, 4 );
delay(25);
roomba.getSensors(21, tempBuf, 1);
battery_Voltage = tempBuf[0];
delay(50);
roomba.getSensors(25, tempBuf, 2);
battery_Current_mAh = tempBuf[1]+256*tempBuf[0];
delay(50);
roomba.getSensors(26, tempBuf, 2);
battery_Total_mAh = tempBuf[1]+256*tempBuf[0];
//if(battery_Total_mAh != 0 && battery_Current_mAh != 0)
if (battery_Current_mAh != 0 && battery_Total_mAh <= 4050)
//if (battery_Current_mAh != 0 && battery_Total_mAh >= 1575 && battery_Total_mAh <= 1675)
{
int nBatPcent = 100*battery_Current_mAh/battery_Total_mAh;
// Ensure the battery percentage is within 0 to 100
if (nBatPcent >= 4 && nBatPcent <= 100)
{
String temp_str2 = String(nBatPcent);
temp_str2.toCharArray(battery_percent_send, temp_str2.length() + 1); //packaging up the data to publish to mqtt
client.publish("roomba/battery", battery_percent_send);
// Create a JSON object
DynamicJsonDocument jsonDoc(128); // Adjust the size according to your data
// Add data points to the JSON object
jsonDoc["battery_Total_mAh"] = battery_Total_mAh;
jsonDoc["battery_Current_mAh"] = battery_Current_mAh;
jsonDoc["nBatPcent"] = nBatPcent;
jsonDoc["battery_percent_send"] = battery_percent_send;
jsonDoc["battery_Voltage"] = battery_Voltage;
// Serialize the JSON object to a string
String jsonStr;
serializeJson(jsonDoc, jsonStr);
// Publish the JSON message to roomba/specials
client.publish("roomba/specials", jsonStr.c_str());
}
}
if(battery_Total_mAh == 0)
{
client.publish("roomba/battery", "NO DATA");
}
String temp_str = String(battery_Voltage);
temp_str.toCharArray(battery_Current_mAh_send, temp_str.length() + 1); //packaging up the data to publish to mqtt
client.publish("roomba/charging", battery_Current_mAh_send);
}
void setup()
{
Serial.begin(115200);
Serial.write(129);
delay(50);
Serial.write(11);
delay(50);
WiFi.mode(WIFI_STA);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
//timer.setInterval(5000, sendInfoRoomba);
timer.setInterval(20000, sendInfoRoomba);
ArduinoOTA.setPort(OTAport);
ArduinoOTA.setHostname(mqtt_client_name);
ArduinoOTA.setPassword((const char *)OTApassword);
ArduinoOTA.begin();
if (!client.connected())
{
reconnect();
}
}
void imperialmarch() {
byte Song1[35]={140,1,16,
81, 32, // C
81, 32, // G
81, 32, // F
77, 32, // E
84, 16, // D
81, 16, // C
77, 32, // G
84, 16, // F
81, 16, // E
88, 32, // E
88, 32, // E
88, 32, // E
89, 32, // F
84, 16, // C
80, 32, // G#
77, 32 // F
};
byte Song2[7]={140,2,2,
77, 16, // C
81, 32 // A
};
Serial.write(128);
delay(50);
Serial.write(132);
delay(50);
Serial.write(Song1,35);
Serial.write(Song2,7);
delay(500);
byte Part1[2]={141,1};
Serial.write(Part1,2);
delay(7300);
byte Part2[2]={141,2};
Serial.write(Part2,2);
}
void secondmarch(){
byte Song1[35]={140,1,16,39,32,39,32,39,32,53,22,60,9,39,32,53,22,60,9,39,64,64,32,64,32,64,32,65,22,60,9,44,32,53,22};
Serial.write(Song1,35);
byte Part1[2]={141,1};
Serial.write(Part1,2);
delay(6813);
byte Song2[35]={140,1,16,60,9,39,64,69,32,39,22,39,9,69,32,68,16,67,16,66,8,65,8,66,16,20,16,45,16,87,32,86,16,85,16};
Serial.write(Song2,35);
byte Part2[2]={141,1};
Serial.write(Part2,2);
delay(5125);
byte Song3[35]={140,1,16,60,8,59,8,60,16,20,16,53,8,44,16,53,12,39,8,60,12,39,12,60,8,64,64,69,32,39,22,39,9,69,32};
Serial.write(Song3,35);
byte Part3[2]={141,1};
Serial.write(Part3,2);
delay(4000);
byte Song4[35]={140,1,16,68,16,67,16,66,8,65,8,66,16,20,16,45,16,87,32,86,16,85,16,60,8,59,8,60,16,20,16,53,16,44,32};
Serial.write(Song4,35);
byte Part4[2]={141,1};
Serial.write(Part4,2);
delay(4000);
byte Song5[15]={140,1,6,53,12,60,8,39,32,53,12,36,8,39,64};
Serial.write(Song5,35);
byte Part5[2]={141,1};
Serial.write(Part5,2);
delay(2125);
}
void starwars(){
byte Song1[35]={140,3,16,
72, 128, // C
79, 128, // G
77, 64, // F
76, 64, // E
74, 64, // D
89, 128, // C
79, 128, // G
77, 64, // F
76, 64, // E
74, 64, // D
89, 128, // C
79, 128, // G
77, 64, // F
76, 64, // E
77, 64, // F
74, 128 // D
};
Serial.write(128);
delay(50);
Serial.write(131);
delay(50);
Serial.write(Song1,35);
delay(500);
byte Part1[2]={141,3};
Serial.write(Part1,2);
}
void SendCommandList( byte *ptr, byte len ) {
for ( int i = 0; i < len ; i++ ) {
Serial.write(ptr[i]);
}
}
void loop()
{
if (WiFi.status() != WL_CONNECTED) {
setup_wifi();
}
ArduinoOTA.handle();
if (!client.connected())
{
reconnect();
}
client.loop();
timer.run();
}