forked from thehookup/MQTT-Roomba-ESP01
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRoomba_500_WITH_POWER_SAVING.ino
269 lines (249 loc) · 7.1 KB
/
Roomba_500_WITH_POWER_SAVING.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
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
//only if you need to connect to multiple AP WIFI
//#include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h>
#include <SimpleTimer.h>
#include <Roomba.h>
//USER CONFIGURED SECTION START//
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxxx";
const char* mqtt_server = "xxxxxxxx";
const int mqtt_port = 1883;
const char *mqtt_user = "xxxxxxxx";
const char *mqtt_pass = "xxxxxx";
const char *mqtt_client_name = "Roomba"; // Client connections can't have the same connection name
//USER CONFIGURED SECTION END//
WiFiClient espClient;
PubSubClient client(espClient);
SimpleTimer timer;
Roomba roomba(&Serial, Roomba::Baud115200);
//define GPIO 0 on ESP01, please change it you need use other ESP modules
#define noSleepPin 0
// 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];
String status_log;
boolean connectioWasAlive = true;
//Function to connect to WIFI first time
void setup_wifi()
{
// WiFi.hostname(mqtt_client_name);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
}
//verbose function to monitor WIFI connection in loop function, it not connected to WIFI; you can also use simpler setup_wifi() function
// DON'T USE IT IF YOU HAVE'T HW WIFI PROBLEM ON ESP01
void monitorWiFi()
{
if (WiFi.status() != WL_CONNECTED)
{
if (connectioWasAlive == true)
{
connectioWasAlive = false;
Serial.print("Looking for WiFi ");
}
Serial.print(".");
delay(500);
}
else if (connectioWasAlive == false)
{
connectioWasAlive = true;
Serial.printf(" connected to %s\n", WiFi.SSID().c_str());
}
}
//Function to reconnect to MQTT SERVER
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
client.subscribe("roomba/commands");
}
else
{
retries++;
// Wait 5 seconds before retrying
delay(5000);
}
}
if(retries >= 50)
{
ESP.restart();
}
}
}
//Function to richiamare????
void callback(char* topic, byte* payload, unsigned int length)
{
String newTopic = topic;
payload[length] = '\0';
String newPayload = String((char *)payload);
if (newTopic == "roomba/commands")
{
if (newPayload == "start")
{
startCleaning();
}
if (newPayload == "stop")
{
stopCleaning();
}
}
}
//Function to start cleaning of Roomba
void startCleaning()
{
//128 start command
Serial.write(128);
delay(50);
//131 safe command
Serial.write(131);
delay(50);
//135 cleaning mode
Serial.write(135);
client.publish("roomba/status", "Cleaning");
}
//Function to send to dock station your Roomba
void stopCleaning()
{
//128 start command
Serial.write(128);
delay(50);
//131 safe command
Serial.write(131);
delay(50);
//143 dock station direction
Serial.write(143);
client.publish("roomba/status", "Returning");
}
//Function to send Roomba info to MQTT Server
void sendInfoRoomba()
{
roomba.start();
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)
{
int nBatPcent = 100*battery_Current_mAh/battery_Total_mAh;
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);
}
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);
}
//Mainly function to start before all other functions, it run only one time/first time you boot ESP01
void setup()
{
//configura ESP per comunicare al baud rate di 115200
Serial.begin(115200);
//129 baud (codice 11 indica il baud rate 115200, che è comunque quello di default del roomba)
Serial.write(129);
delay(50);
Serial.write(11);
delay(50);
// configura la parte WIFI del ESP01 per collegarsi al AP WIFI con i dati defini all'inizio
WiFi.mode(WIFI_STA);
setup_wifi();
// Reset the Roomba.
Serial.begin(115200);
pinMode(noSleepPin, OUTPUT);
digitalWrite(noSleepPin, HIGH);
// it wake up Roomba the first time
resetRoomba();
// it connect to MQTT server
client.setServer(mqtt_server, mqtt_port);
//richiama la funzione dediucata di start o stop in base al comando ricevuto via MQTT
client.setCallback(callback);
timer.setInterval(5000, sendInfoRoomba);
timer.setInterval(59000, StayAwake);
}
//Function to wake up Roomba 500 from power saving mode (you have to use it only if you have this feature in last firmware Roomba(
void StayAwake() {
status_log += "Pulsing the BRC pin...\n";
digitalWrite(noSleepPin, LOW);
delay(100);
digitalWrite(noSleepPin, HIGH);
}
//Function to wake up Roomba 500 from power saving mode (you have to use it only if you have this feature in last firmware Roomba(
void resetRoomba() {
//128 start
//129 baud (codice 11 indica il baud rate 115200, il codice 7 indica il baud rate 19200)
byte command[] = { 128, 129, 11, 7 };
//inviando 3 segnali consecutivi di baud rate change si abbasserebbe il baud rate utilizzato per comunicare col Roomba, invece si vuole solo tenere acceso il Roomba
digitalWrite(noSleepPin, LOW);
delay(100);
digitalWrite(noSleepPin, HIGH);
Serial.begin(19200);
SendCommandList(command, 4);
delay(500);
Serial.begin(115200);
SendCommandList(command, 4);
digitalWrite(noSleepPin, LOW);
delay(100);
digitalWrite(noSleepPin, HIGH);
timer.setTimeout(5000, StayAwake);
}
//Function to wake up Roomba 500 from power saving mode (you have to use it only if you have this feature in last firmware Roomba(
void SendCommandList( byte *ptr, byte len ) {
status_log += "TX:";
for ( int i = 0; i < len ; i++ ) {
status_log += " ";
status_log += ptr[i];
Serial.write(ptr[i]);
}
delay(25);
status_log += "\n";
}
//Function to loop continuosily MQTT server re-connection + riconnessione WIFI
void loop()
{
//monitor MQTT connection
if (!client.connected())
{
reconnect();
}
client.loop();
timer.run();
//monitor WIFI connection DON'T USE IT IF YOU HAVE'T HW WIFI PROBLEM ON ESP01
// monitorWiFi();
// setup_wifi();
}