-
Notifications
You must be signed in to change notification settings - Fork 0
/
projectCode.c
397 lines (348 loc) · 10.1 KB
/
projectCode.c
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
#include <WiFi.h>
#include <PubSubClient.h>
#include <TinyGPS++.h>
#include "SSD1306Wire.h"
// Heltec OLED Internal Wiring
#define OLED_SDA 4
#define OLED_SCL 15
#define OLED_RESET 16
SSD1306Wire display(0x3c, OLED_SDA, OLED_SCL);
// WiFi Setup Variables
WiFiClient espClient;
const char* ssid = "Jupiter40";//Jupiter40Ashley's iPhone
const char* password = "planet40";//planet40passw0rd!
const int wifiTimeout = 10;
boolean wifiEnabled = true;
// Address of the MQTT Server/Node-RED/Raspberry Pi/FRED
char* mqtt_server = "10.158.42.203";//192.168.137.93
PubSubClient mqttClient(espClient);
long lastPublished = 0;
int publishInterval = 5000;
char msg[50];
String latString;
String lngString;
String courseString;
String mpsString;
String mphString;
String cardinalString;
String distanceString;
String courseToString;
// GPS
//Wiring: RX to TX, TX to RX
// Using Serial2 for GPS
HardwareSerial SerialGPS(2);
#define GPS_RX 13
#define GPS_TX -1
// The TinyGPS++ object
TinyGPSPlus gps;
// Default Destination
// Oracle Conference Center: 37.532340, -122.264024
double destinationLat = 37.532340;
double destinationLng = -122.264024;
// LED Pin
const int ledPin = LED_BUILTIN;
//Vibrating Motor Pins
const int leftPin = 27;
const int rightPin = 26;
#define BUZZ_OFF 0
#define BUZZ_START 1
#define BUZZING 2
#define BUZZ_PAUSE 3
#define BUZZ_DELAY_START 4
int leftState = BUZZ_OFF;
int rightState = BUZZ_OFF;
int leftCount = 0;
int rightCount = 0;
unsigned long leftTimestamp = 0;
unsigned long rightTimestamp = 0;
void setup() {
// Setup Serial Monitor
Serial.begin(9600);
delay(1000);
Serial.println();
Serial.println("Serial Monitor enabled...");
// Setup GPS on SerialGPS
SerialGPS.begin(9600, SERIAL_8N1, GPS_RX, GPS_TX);
delay(1000);
// Initialising the UI will init the display too.
pinMode(16, OUTPUT);
digitalWrite(16, LOW); // set GPIO16 low to reset OLED
delay(50);
digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 to high
display.init();
display.clear();
display.flipScreenVertically();
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
dualPrint(0, 0, "SSD1306 Working");
display.display();
delay(1000);
//Setup WiFi and MQTT
if (wifiEnabled) {
setup_wifi();
delay(2000);
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setCallback(receiveMQTT);
}
// Setup GPIO pins
pinMode(ledPin, OUTPUT);
pinMode(leftPin, OUTPUT);
pinMode(rightPin, OUTPUT);
Serial.println("Setup Complete");
}
// WARNING: Do NOT use delays in loop.
// If you use delays in loop, MQTT messages will be dropped.
void loop() {
if (wifiEnabled) {
if (!mqttClient.connected()) {
connectMQTT();
}
mqttClient.loop();
}
buzzState(&leftState, &leftCount, &leftTimestamp, leftPin);
buzzState(&rightState, &rightCount, &rightTimestamp, rightPin);
while (SerialGPS.available() > 0) {
byte gpsData = SerialGPS.read();
//Serial.write(gpsData); //Print debug
gps.encode(gpsData);
if (gps.location.isUpdated()) {
// Get GPS coords as strings rounded to 6 decimals
latString = String(gps.location.lat(), 6);
lngString = String(gps.location.lng(), 6);
courseString = String(gps.course.deg(), 2);
mpsString = String(gps.speed.mps(), 2);
mphString = String(gps.speed.mph(), 1);
cardinalString = TinyGPSPlus::cardinal(gps.course.deg());
calculateDestination();
// Refresh OLED Display
display.clear();
if (!wifiEnabled) {
display.drawString(115, 0, "X");
}
display.drawString(0, 0, "Current Speed");
display.drawString(0, 16, mphString + "mph");
display.drawString(75, 16, cardinalString);
display.drawString(0, 32, "Destination");
display.drawString(0, 48, distanceString);
display.drawString(75, 48, courseToString);
display.display();
break;
}
}
// Check last publish time
long now = millis();
if (now - lastPublished > publishInterval) {
//Print to coordinates to serial monitor
Serial.print("Lat= " + latString);
Serial.println(" Lng= " + lngString);
Serial.print("Course= " + courseString + " deg " + cardinalString);
Serial.println(" Speed= " + mpsString + "m/s " + mphString + "mph");
if (wifiEnabled) {
//Send MQTT GPS Message to NodeRED
publishGPS();
}
lastPublished = now;
}
}
void buzzState(int *state, int *count, unsigned long *timestamp, int pin) {
switch (*state) {
case BUZZ_START:
*timestamp = millis();
*state = BUZZING;
digitalWrite(pin, HIGH);
break;
case BUZZING:
if (millis() - *timestamp > 500) {
*count = *count - 1;
if (*count <= 0) {
*state = BUZZ_OFF;
}
else {
*timestamp = millis();
*state = BUZZ_PAUSE;
}
digitalWrite(pin, LOW);
}
break;
case BUZZ_PAUSE:
if (millis() - *timestamp > 500) {
*timestamp = millis();
*state = BUZZING;
digitalWrite(pin, HIGH);
}
break;
case BUZZ_DELAY_START:
*timestamp = millis();
*state = BUZZ_PAUSE;
digitalWrite(pin, LOW);
break;
default:
return;
}
}
void calculateDestination() {
double metersToDestination =
TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
destinationLat,
destinationLng);
double milesToDestination = metersToDestination / 1609.344;
if (milesToDestination < .1) {
distanceString = String(milesToDestination * 5280.0, 0) + " ft";
}
else {
distanceString = String(milesToDestination, 1) + " mi";
}
double courseToDestination =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
destinationLat,
destinationLng);
courseToString = TinyGPSPlus::cardinal(courseToDestination);
}
//Publish MQTT GPS messages to Node-RED/RaspPi/FRED
void publishGPS() {
if (mqttClient.connected()) {
//String json = "{\"lat\":" + latString + ", \"lng\":" + lngString + "}";
String s = latString + "," + lngString;
//Convert String type to char array
char tempString[s.length() + 1];
s.toCharArray(tempString, s.length() + 1);
mqttClient.publish("esp32/gps", tempString);
}
}
//Callback function that interrupts loop when a message is received
//Do NOT use delays in this function
void receiveMQTT(char* topic, byte * message, unsigned int length) {
// Debug print message topic
Serial.print("Message topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageString;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageString += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
if (String(topic) == "esp32/led") {
Serial.print("Changing output to ");
if (messageString == "on") {
Serial.println("on");
digitalWrite(ledPin, HIGH);
}
else if (messageString == "off") {
Serial.println("off");
digitalWrite(ledPin, LOW);
}
}
else if (String(topic) == "esp32/destination") {
// Parse message for longitude and latitude
int index = messageString.indexOf(',');
String s1 = messageString.substring(0, index);
String s2 = messageString.substring(index + 1, messageString.length());
// Convert type string to double
destinationLat = s1.toDouble();
destinationLng = s2.toDouble();
Serial.print("Destination received: ");
Serial.print(destinationLat, 6);
Serial.print(", ");
Serial.println(destinationLng, 6);
}
else if (String(topic) == "esp32/signal") {
Serial.println("signal received " + messageString);
if (messageString == "left") {
leftState = BUZZ_START;
leftCount = 1;
}
else if (messageString == "left2") {
leftState = BUZZ_START;
leftCount = 2;
}
else if (messageString == "right") {
rightState = BUZZ_START;
rightCount = 1;
}
else if (messageString == "right2") {
rightState = BUZZ_START;
rightCount = 2;
}
else if (messageString == "both") {
leftState = BUZZ_START;
leftCount = 1;
rightState = BUZZ_START;
rightCount = 1;
}
else if (messageString == "alternating") {
leftState = BUZZ_START;
leftCount = 2;
rightState = BUZZ_DELAY_START;
rightCount = 2;
}
}
}
// Setup WiFi to SSID with password
// WiFi will timeout after set number of retries
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
display.clear();
dualPrint(0, 0, "Connecting to Wifi");
dualPrint(0, 16, "SSID: " + String(ssid));
display.display();
WiFi.begin(ssid, password);
int retryCount = 0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
retryCount++;
if (retryCount > wifiTimeout) {
wifiEnabled = false;
dualPrint("WiFi Failed.");
return;
}
}
// Display WiFi Stats
display.clear();
dualPrint(0, 0, "WiFi connected");
dualPrint(0, 16, "IP address: ");
dualPrint(0, 32, WiFi.localIP().toString());
display.display();
}
// Connects to MQTT server (RasPi/Node-RED/FRED)
void connectMQTT() {
// Loop until we're reconnected
int retryCount = 0; //TODO finish retry count later
while (!mqttClient.connected()) {
dualPrint("MQTT Connect...");
dualPrint(0, 16, String(mqtt_server));
// Attempt to connect
if (mqttClient.connect("ESP8266Client")) {
dualPrint(0, 32, "Success");
// Subscribe
mqttClient.subscribe("esp32/led");
mqttClient.subscribe("esp32/destination");
mqttClient.subscribe("esp32/signal");
} else {
dualPrint(0, 32, "Failed, rc=" + String(mqttClient.state()));
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
display.display();
delay(1000);
}
// dualPrint is a helper function that will
// print text in Serial Monitor and the OLED Screen
void dualPrint(String text) {
display.clear();
dualPrint(0, 0, text);
display.display();
}
void dualPrint(int x, int y, String text) {
Serial.println(text);
display.drawString(x, y, text);
}