-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_client_esp32.ino
484 lines (442 loc) · 14.4 KB
/
mqtt_client_esp32.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <Wire.h> // Not 100% sure if this included is needed, test without
#include <Math.h> // Needed for distance calculation stuff
#include <SPI.h> // Needed for comms with vision
// **************DEFINITIONS*************
#define VSPI_MISO MISO
#define VSPI_MOSI MOSI
#define VSPI_SCLK SCK
#define VSPI_SS SS
#define RXD2 16
#define TXD2 17
//***************COMMUNICATION SETUP*************
// Parameters for SPI
SPISettings settings(100000, MSBFIRST, SPI_MODE0);
uint8_t spi_counter[6]; // [0] = c20, [1] = c21, [2] = c22, [3] = c23, [4] = c24, [5] = c25
uint16_t spi_val;
uint8_t spi_reg;
uint16_t spi_returnval;
uint16_t change_char = 0;
void calcDistance(int col);
void resetCounter();
// Parameters for the wifi connection (will need to change depending on location)
const char* ssid = "Khayle"; //"The Circus";
const char* password = "pass1234"; //"Hail_Pietr0";
//const char* ssid = "AndroidAP8029"; //"The Circus";
//const char* password = "hirk8481"; //"Hail_Pietr0";
//const char* ssid = "iPhonedeYuna";
//const char* password = "yuna1612";
//const char* ssid = "VM6446132";
//const char* password = "ty5VxqxnW3yr";
// Parameters for the mqtt connection
const char* mqtt_server = "3.8.182.14";
const int mqtt_port = 8883; // Currently using encrypted version
const char* mqtt_user = "esp32";
const char* mqtt_pwd = "#8HAGxb3*V%+CD8^";
const char* ca_cert = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIFtTCCA52gAwIBAgIUKdBx7TTn8jGu+U1mGwEtDoeXaLIwDQYJKoZIhvcNAQEN\n" \
"BQAwajEXMBUGA1UEAwwOQW4gTVFUVCBicm9rZXIxFjAUBgNVBAoMDU93blRyYWNr\n" \
"cy5vcmcxFDASBgNVBAsMC2dlbmVyYXRlLUNBMSEwHwYJKoZIhvcNAQkBFhJub2Jv\n" \
"ZHlAZXhhbXBsZS5uZXQwHhcNMjEwNTI3MTgxNTA1WhcNMzIwNTI0MTgxNTA1WjBq\n" \
"MRcwFQYD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ADAQH/MA0GCSqGSIb3DQEBDQUAA4ICAQCI7D/e+4uH\n" \
"F4KUqgJfXb0SPkZHCBalyqQmFHaD+2ohGUhso/X+sSFkwL92XZ6HRfqgBSJrMBS2\n" \
"dZAlWBh6U/75ecQPUgLXQ5B5opgZtI7gXxO34Gn4D7F6hbqYNNtTvt8D6BkOCvKS\n" \
"fxFOorf0gh8EH2zteeNuIqcPMl9BxU/7SSpbUxNhywwsvXXzqwQ9xzHXuSX429QC\n" \
"8n3qO+2+il+n6erOSPh9/XLiaaXw9kUO39eOdyeeawLHzMGwje9hj6ajd7wpNWa+\n" \
"f7GZGZHWZVmirc5MZggXMkL4fMIqfgxEsyZKTz7vfiegVS+xZEl3PMtVBkCeKmHR\n" \
"rrpTgHxN7Ox1ifQ35giZDBAh2px4yzSDvVvqxoD6I3mh20oF6+pfCZhh0TWiFYBI\n" \
"BhHSgbTj9OW3N1DLcZINx5uKwGtVyvA1eg0gi6a546cWfdeXC4vq2TinMBcjaWf/\n" \
"7U0sIo0YN0BJRBu8IgenbcVI9UnDSk0ryVeKzDieoor70LlY3OwkfSSIcU4Z8Qkh\n" \
"8vPGucuGX2Gcoayp9yMfUWQ5aek5ZXj80pw0MVQFsI24z+TlLu9RJO/nP3bE3ADo\n" \
"Lhz27tN2lpVZIqrrwtl5yaKZXpmnJgsam5lWho5B8BRj6Au7RGp6BcipjOzhfI1e\n" \
"KKaH0SCVC/XiMCCdR/dFBKwUKOoEfibqXQ==\n" \
"-----END CERTIFICATE-----\n";
WiFiClientSecure espClient;
PubSubClient client(espClient);
long lastMsg = 0; // Timer for sending coords
char buffer[30]; // buffer for messages sent by ESP32
// LED Pin
const int ledPin = 4;
// Rover Parameters
struct Rover{
int angle = 0;
std::pair<int,int> coords = {0,0}; // coords.first = x, coords.second = y
char mode = 'M';
char dir = 'S';
};
Rover rover;
std::pair<int,int> dest = {0,0};
// Obstacle Parameters
struct Obstacle{
int colour; // pink = 1, green = 2, blue = 3, orange = 4
std::pair<int,int> coords = {0,0};
};
Obstacle obstacle;
bool newObstacle = 0;
//****************Function declarations******************
void setup_wifi();
void callback(char* topic, byte* message, unsigned int length); // Called when receiving message from MQTT broker
void reconnect();
void genCoordMsg(char *buf); // Generates message containing rover information as JSON string
void genObsMsg(char *buf); // Generates message containing obstacle information as JSON string
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200); // Debugging
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); // Communicate with drive
// Set up wireless comms
setup_wifi();
espClient.setCACert(ca_cert); // Set SSL/TLS certificate
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback); // Sets callback function
pinMode(ledPin, OUTPUT);
// Setup SPI stuff
pinMode(VSPI_SS, OUTPUT);
SPI.begin();
resetCounter();
spi_returnval = 0;
}
// This function is called whenever we receive a message to a topic we are subscribed to
void callback(char* topic, byte* message, unsigned int length) {
// For debugging, comment this out in production
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Add more if statements to control more GPIOs with MQTT (figure out communication with pins)
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "toESP32/dir" && rover.mode == 'M') { // Single char
rover.dir = (char)message[0];
Serial.print("Sending direction ");
Serial.println(rover.dir);
Serial2.print(rover.dir);
}
else if (String(topic) == "toESP32/mode") { // Single char
rover.mode = (char)message[0];
Serial.print("Sending mode ");
Serial.println(rover.mode);
if (rover.mode == 'E') {
Serial2.print('M');
}
else {
Serial2.print(rover.mode);
}
}
// Receives messages of the form "<x_coord,y_coord>"
// Also not sure if storing the destination coords is necessary, doing it for now
else if (String(topic) == "toESP32/coord" && rover.mode == 'C') {
String toDrive;
toDrive += (char)message[0]; // Adds '<'
char bufX[6];
int i = 1;
while((char)message[i] != ',') {
bufX[i-1] = message[i];
toDrive += (char)message[i++];
}
toDrive += (char)message[i]; // Adds ','
bufX[(i++)-1] = '\0';
String x(bufX);
dest.first = x.toInt();
char bufY[6];
int j = 0;
while((char)message[i] != '>') {
bufY[j++] = message[i];
toDrive += (char)message[i++];
}
toDrive += (char)message[i]; // Adds '>'
Serial2.print(toDrive); // Sends "<x_coord,y_coord>" to drive
bufY[j++] = '\0';
String y(bufY);
dest.second = y.toInt();
// Debugging
Serial.print("Sending: ");
Serial.println(toDrive);
Serial.print("x coord: ");
Serial.print(dest.first);
Serial.print(", y coord: ");
Serial.println(dest.second);
}
else if (String(topic) == "toESP32/output") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
//else if (String(topic) == "toESP32/x_coord")
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTTS connection...");
// Attempt to connect
if (client.connect("ESP32Client", mqtt_user, mqtt_pwd)) {
Serial.println("connected securely");
// Subscribe
client.subscribe("toESP32/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
// ************** UART STUFF *****************
// Receiving stuff from drive
if (Serial2.available()) {
char received_char = Serial2.read();
if (received_char == '<') {
String x = Serial2.readStringUntil(',');
rover.coords.first = x.toInt();
String y = Serial2.readStringUntil(',');
rover.coords.second = y.toInt();
String a = Serial2.readStringUntil('>');
rover.angle = a.toInt();
// Serial.println("Received: x = "+x+", y = "+y+", a = "+a);
}
}
// ************** SPI STUFF ******************
// Only care about vision if we are in exploration mode
if (rover.mode == 'E') {
// Transfer stuff
SPI.beginTransaction(settings);
digitalWrite(VSPI_SS, LOW);
spi_val = SPI.transfer16(spi_returnval);
spi_returnval = 0;
digitalWrite(VSPI_SS, HIGH);
SPI.endTransaction();
bool charhaschanged = false;
if (change_char != spi_val){
change_char = spi_val;
charhaschanged = true;
}
// Processing data received
if (spi_val == 2048 && charhaschanged == true){
Serial2.print('R'); // Tells drive to turn right
//Serial.print("We're rotating right \n");
resetCounter();
}
if (spi_val == 4096 && charhaschanged == true){
Serial2.print('L'); // Tells drive to turn left
resetCounter();
}
if (spi_val == 8192 && charhaschanged == true){
Serial2.print('B'); // Tells drive to go backwards
resetCounter();
}
if (spi_val == 16384 && charhaschanged == true){
Serial2.print('F'); // Tells drive to go forward
resetCounter();
}
if (spi_val > 32768 && charhaschanged == true){
Serial2.print('S'); // Tells drive to stop
}
if (spi_val > 32768){
spi_val -= 32768;
spi_val >>= 7;
spi_reg = spi_val & 7;
spi_val >>= 3;
switch(spi_reg)
{
case 0:
//Serial.print("We have a pink ball \n");
calcDistance(1);
break;
case 1:
//Serial.print("We have a yellow ball \n");
calcDistance(4);
break;
case 2:
Serial.print("We have a green ball \n");
calcDistance(2);
Serial.print("We sent the green ball \n");
break;
case 3:
//Serial.print("We have a blue ball \n");
calcDistance(3);
break;
default:
Serial.print("Invalid ball detected \n");
}
}
}
// ************** MQTT STUFF *******************
if (!client.connected()) {
reconnect();
}
// Allows client to process incoming messages and maintain connection to MQTT broker
client.loop();
// Handles publishing data about new obstacle to server
if (newObstacle) {
genObsMsg(buffer);
Serial.print("Sending message: ");
Serial.println(buffer);
client.publish("fromESP32/obstacle", buffer, false); // Ensure messages arent retained
newObstacle = 0;
}
// Updates server with rover coords
long now = millis();
if (now - lastMsg > 200) {
lastMsg = now;
genCoordMsg(buffer);
client.publish("fromESP32/rover_coords", buffer, false);
Serial.println("Sending " + String(buffer));
}
// Sends test message every 2 seconds
/*
long now = millis();
if (now - lastMsg > 10000) {
lastMsg = now;
/*
genCoordMsg(buffer);
Serial.print("Sending message: ");
Serial.println(buffer);
client.publish("fromESP32/rover_coords", buffer, false);
rover.coords.first = (rover.coords.first + 1)%1000;
rover.coords.second = (rover.coords.second + 1)%1000;
rover.angle = (rover.angle + 1)%360;
obstacle.colour = (obstacle.colour)%4 + 1;
obstacle.coords.first = (obstacle.coords.first +100)%1000;
obstacle.coords.second = (obstacle.coords.second + 100)%1000;
newObstacle = 1;
}
*/
}
void genCoordMsg(char *buf)
{
int cur = 0;
char x[6] = "{\"x\":";
for(int i = 0; i < 5; i++){
buf[cur++] = x[i];
}
char num[6];
sprintf(num, "%d", rover.coords.first);
int cnt = 0;
while(num[cnt]){
buf[cur++] = num[cnt++];
}
char y[6] = ",\"y\":";
for(int i = 0; i < 5; i++){
buf[cur++] = y[i];
}
sprintf(num, "%d", rover.coords.second);
cnt = 0;
while(num[cnt]){
buf[cur++] = num[cnt++];
}
char a[6] = ",\"a\":";
for(int i = 0; i < 5; i++){
buf[cur++] = a[i];
}
sprintf(num, "%d", rover.angle);
cnt = 0;
while(num[cnt]){
buf[cur++] = num[cnt++];
}
buf[cur++] = '}';
while(cur < 30){
buf[cur++] = '\0';
}
}
void genObsMsg(char *buf)
{
int cur = 0;
char c[6] = "{\"c\":";
for(int i = 0; i < 5; i++){
buf[cur++] = c[i];
}
buf[cur++] = (char)(obstacle.colour+48);
char x[6] = ",\"x\":";
for(int i = 0; i < 5; i++){
buf[cur++] = x[i];
}
char num[6];
sprintf(num, "%d", obstacle.coords.first);
int cnt = 0;
while(num[cnt]){
buf[cur++] = num[cnt++];
}
char y[6] = ",\"y\":";
for(int i = 0; i < 5; i++){
buf[cur++] = y[i];
}
sprintf(num, "%d", obstacle.coords.second);
cnt = 0;
while(num[cnt]){
buf[cur++] = num[cnt++];
}
buf[cur++] = '}';
while(cur < 30){
buf[cur++] = '\0';
}
}
void resetCounter(){
for(int i = 0; i < 4; i++){
spi_counter[i] = 0;
}
}
void calcDistance(int col)
{
for(int i = 0; i < 5; i++){
Serial.println("calculating distance...");
Serial.println(spi_val);
if(spi_val == i+26){
spi_counter[i]++;
if(spi_counter[i] == 100){
obstacle.colour = col;
int x_diff = (i+26.0)*10.0*sin(rover.angle);
int y_diff = (i+26.0)*10.0*cos(rover.angle);
obstacle.coords.first = rover.coords.first + x_diff;
obstacle.coords.second = rover.coords.second + y_diff;
newObstacle = 1;
spi_returnval = 32768+col;
resetCounter();
}
}
}
}