-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgps_test1.ino
212 lines (190 loc) · 5.51 KB
/
gps_test1.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
#include <M5StickCPlus.h>
#include <TinyGPS++.h>
#include <QubitroMqttClient.h>
#include <WiFi.h>
TinyGPSPlus gps;
HardwareSerial ss(2);
float Lat, Lng;
String lat_str, lng_str;
int sat, battery = 0;
float b, c = 0;
// WiFi Client
WiFiClient wifiClient;
// Qubitro Client
QubitroMqttClient mqttClient(wifiClient);
// Device Parameters
char deviceID[] = "bfc2413b-58f9-418b-91b0-5899d97f71c9";
char deviceToken[] = "dbbXO80Ew4G5vUf-EiZqbF8RWhTeukKfVlafTG7j";
// WiFi Parameters
const char *ssid = "Rio_West_Students";
const char *password = "stove914wooden";
// const char *ssid = "pixel";
// const char *password = "12345678";
// const char *ssid = "utexas-iot";
// const char *password = "52756315611727283607";
void setup()
{
M5.begin();
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setSwapBytes(true);
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(7, 20, 2);
M5.Lcd.setTextColor(TFT_GREEN, TFT_BLACK);
ss.begin(9600, SERIAL_8N1, 33, 32);
wifi_init();
qubitro_init();
}
void loop()
{
while (ss.available() > 0)
if (gps.encode(ss.read()))
{
if (gps.location.isValid())
{
// Display GPS data on the LCD
M5.Lcd.setTextColor(RED);
M5.Lcd.setCursor(0, 0);
M5.Lcd.printf("</Qubitro Uplink Done/>");
Lat = gps.location.lat();
lat_str = String(Lat, 8); // Increase precision
Lng = gps.location.lng();
lng_str = String(Lng, 8); // Increase precision
sat = gps.satellites.value();
Serial.print("satellites found: ");
Serial.println(sat);
Serial.print("latitude : ");
Serial.println(lat_str);
M5.Lcd.setTextColor(TFT_GREEN, TFT_BLACK);
M5.Lcd.setCursor(0, 20);
M5.Lcd.printf("latitude : %s", lat_str);
Serial.print("longitude: ");
Serial.println(lng_str);
//*Just for example
Serial.print("Alt: ");
Serial.println(gps.altitude.feet());
Serial.print("Course: ");
Serial.println(gps.course.deg());
Serial.print("Speed: ");
Serial.println(gps.speed.mph());
Serial.print("Date: ");
printDate();
Serial.print("Time: ");
printTime();
M5.Lcd.setCursor(0, 40);
M5.Lcd.printf("longitude : %s", lng_str);
// Construct MQTT payload
String payload = "{\"latitude\": " + String(lat_str) + ",\"longitude\":" + String(lng_str) + "}";
// Check MQTT connection status
if (mqttClient.connected())
{
// Send payload if MQTT is connected
mqttClient.beginMessage(deviceID);
mqttClient.print(payload);
mqttClient.endMessage();
}
else
{
// Attempt to reconnect to MQTT
Serial.print("MQTT Connection Status: Disconnected. Reconnecting...");
delay(5000); // Wait for 5 seconds before attempting to reconnect
if (mqttClient.connect("broker.qubitro.com", 1883))
{
Serial.println("Reconnected to MQTT.");
M5.Lcd.setTextColor(RED);
M5.Lcd.setCursor(0, 25);
M5.Lcd.printf("Uplink Re-established");
mqttClient.subscribe(deviceID);
delay(2000);
M5.Lcd.fillScreen(BLACK);
}
else
{
Serial.print("MQTT Reconnection failed. Error code: ");
Serial.println(mqttClient.connectError());
}
}
// Display satellites count on the LCD
M5.Lcd.setCursor(0, 60);
M5.Lcd.printf("satellites : %d", sat);
// Introduce delay before the next loop iteration
delay(5000);
// Clear LCD color
M5.Lcd.setTextColor(TFT_GREEN, TFT_BLACK);
}
}
}
void wifi_init()
{
// Set WiFi mode
WiFi.mode(WIFI_STA);
// Disconnect WiFi
WiFi.disconnect();
delay(100);
// Initiate WiFi connection
WiFi.begin(ssid, password);
// Print connectivity status to the terminal
Serial.print("Connecting to WiFi...");
while (true)
{
delay(1000);
Serial.print(".");
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("");
Serial.println("WiFi Connected.");
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
M5.Lcd.setTextColor(RED);
M5.Lcd.setCursor(0, 8);
Serial.println("Network Connected");
break;
}
}
}
void qubitro_init()
{
Serial.println("Connecting to Qubitro...");
char host[] = "broker.qubitro.com"; // Use a public MQTT broker for testing
int port = 1883;
mqttClient.setId(deviceID);
mqttClient.setDeviceIdToken(deviceID, deviceToken);
if (!mqttClient.connect(host, port))
{
Serial.print("Connection failed. Error code: ");
Serial.println(mqttClient.connectError());
Serial.println("Visit docs.qubitro.com or create a new issue on github.com/qubitro");
}
else
{
Serial.println("Connected to Qubitro.");
M5.Lcd.setTextColor(RED);
M5.Lcd.setCursor(0, 25);
M5.Lcd.printf("Uplink Established");
mqttClient.subscribe(deviceID);
delay(2000);
M5.Lcd.fillScreen(BLACK);
}
}
void printDate()
{
Serial.print(gps.date.day());
Serial.print("/");
Serial.print(gps.date.month());
Serial.print("/");
Serial.println(gps.date.year());
}
void printTime()
{
Serial.print(gps.time.hour());
Serial.print(":");
if (gps.time.minute() < 10)
Serial.print('0');
Serial.print(gps.time.minute());
Serial.print(":");
if (gps.time.second() < 10)
Serial.print('0');
Serial.println(gps.time.second());
}