-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP32-dual-GPS-BTserial.ino
341 lines (302 loc) · 11.6 KB
/
ESP32-dual-GPS-BTserial.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
/*
ESP32 dual-GPS-BT-server with watch dog timer
By hooking up two multiGNSS GPS modules to ESP32 ,
this program averages two GPS output and
outputs NMEA to Bluetooth Serial Port Profile.
NMEA can be monitored by ESP32's USB serial port and BT serial terminal on Android.
Only $GNGGA and $GNRMC are regenerated by this program.
When Serial.BT stops , watch dog timer reboot this program.
This program requires Android phone has the Bluetooth Serial Port Profile
to connect to ESP32-GPS.
Application : run Lefebure to replace from Android phone's embeded GPS to your GPS
https://play.google.com/store/apps/details?id=com.lefebure.ntripclient&hl=ja&gl=US
GPS1 TX should be connected to GPIO_NUM_16 of ESP32
GPS2 TX should be connected to GPIO_NUM_33 of ESP32
GPS modules used to test : M5stack GPS module (using AT6558) , connected by 9600bps serial
Ref. http://arduiniana.org/libraries/tinygpsplus/
https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/
just tested in North , East region.
Copyright 2021(C) coniferconifer
MIT License
*/
//#include <Ticker.h> // timer
#include <TinyGPS++.h>
#define VERSION "20210901"
TinyGPSPlus gps1;
TinyGPSPlus gps2;
#define GPS_BAUD 9600
// https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
TaskHandle_t Task1;
#define PI 3.141592653589793
#define EARTH 6378137.0
boolean printRMS = false;
const int iIntervalTime = 3; // watch dog interval in sec
long lastGPSread = 0;
int Alt = 0; //geoid height at your place
#define NMEA_BUFSIZE 200
void printChecksum( char buf[]) {
int i = 0; unsigned int sum = 0;
for (i = 1; i < NMEA_BUFSIZE; i++) {
if ( buf[i] == 0) {
//Serial.printf(" %d ",i);
break;
}
sum = sum ^ (unsigned int)buf[i];
}
sum = sum % 256;
Serial.printf("*%02x\r\n", sum);
SerialBT.printf("*%02x\r\n", sum);
}
void printNMEA(int yy, byte month, byte dd, byte hh, byte mm, byte ss, byte centiss, double lat , double lon , \
int height, float hdop, int numSat , double course, double speedKnot ) {
char buf[NMEA_BUFSIZE];
char NS = 'N';
char EW = 'E';
if ( lat < 0.0 ) {
lat = -1.0 * lat;
NS = 'S';
}
if ( lon < 0.0 ) {
lon = -1.0 * lon;
EW = 'W';
}
int lathigh = (int)lat;
//Serial.printf("lathigh=%d ", lathigh);
double d_minutes = (lat - (double)lathigh) * 60.0; //double minutes
// Serial.printf("d_minutes=%f", d_minutes);
int min = (int)d_minutes;
d_minutes = d_minutes - (double)min;
d_minutes = d_minutes * 1000000.0;
int lonhigh = (int)lon;
double d_minutes_lon = (lon - (double)lonhigh) * 60.0;
// Serial.printf("lonhigh=%d ", lonhigh);
// Serial.printf("d_minutes_lon=%f\r\n", d_minutes_lon);
int min_lon = (int)d_minutes_lon;
d_minutes_lon = d_minutes_lon - (double)min_lon;
d_minutes_lon = d_minutes_lon * 1000000.0;
sprintf(buf, "$GNGGA,%02d%02d%02d.%03d,%d%02d.%06d,%c,%3d%02d.%06d,%c,1,%02d,%1.1f,%d.0,M,0.0,M,,0000\0", \
(int)hh, (int)mm, (int)ss, (int)centiss * 10, lathigh, min, (int)d_minutes, NS, lonhigh, min_lon, (int)d_minutes_lon, \
EW, numSat, hdop, height);
// char buf2[]="$GPGGA,085120.307,3541.1493,N,13945.3994,E,1,08,1.0,6.9,M,35.9,M,,0000\0";
Serial.print(buf);
SerialBT.print(buf);
printChecksum(buf);
//Serial.printf("year=%02d\r\n",yy);
//Serial.printf("course=%3.1f speed=%3.1f \r\n", course, speedKnot);
sprintf(buf, "$GNRMC,%02d%02d%02d.%03d,A,%d%02d.%06d,%c,%3d%02d.%06d,%c,%3.1f,%3.1f,%02d%02d%02d,,,A\0", \
(int)hh, (int)mm, (int)ss, (int)centiss * 10, lathigh, min, (int)d_minutes, NS, lonhigh, min_lon, (int)d_minutes_lon, \
EW, speedKnot, course, (int)dd, (int)month, (int)yy - 2000);
Serial.print(buf);
SerialBT.print(buf);
printChecksum(buf);
}
int num_sat1, num_sat2;
int height1, height2;
float hdop1 = 1.0;
float hdop2 = 1.0;
double Lat1 = 360.0;
double Lat2 = 0.0;
double Lon1 = 360.0;
double Lon2 = 0.0;
double speedKnot1 = 0.0;
double speedKnot2 = 0.0;
double course1 = 0.0;
double course2 = 0.0;
double averageLat = 0.0;
double averageLong = 0.0;
double sumLat = 0.0;
double sumLong = 0.0;
long sumCounter = 0;
double sumLatSquare = 0.0;
double sumLongSquare = 0.0;
double squareLatVariant = 0.0;
double squareLongVariant = 0.0;
double xVariant, yVariant;
long ts1, ts2;
void getGPSInfo2()
{
static long loopCount = 1;
if (gps2.location.isValid() )
//check if GPS is fixed
{
if (millis() - 1000 > ts2) { //update every second
int Year = gps2.date.year();
byte Month = gps2.date.month();
byte Day = gps2.date.day();
byte Hour = gps2.time.hour();
byte Minute = gps2.time.minute();
byte Second = gps2.time.second();
byte centiss = gps2.time.centisecond();
speedKnot2 = gps2.speed.knots();
course2 = gps2.course.deg();
num_sat2 = gps2.satellites.value();
height2 = gps2.altitude.meters() - Alt;
hdop2 = (float)gps2.hdop.value() / 100.0;
Lat2 = gps2.location.lat();
Lon2 = gps2.location.lng();
if (printRMS == true) {
sumLat = sumLat + Lat2;
sumLong = sumLong + Lon2;
sumCounter = sumCounter + 1;
averageLat = sumLat / (double)sumCounter;
averageLong = sumLong / (double)sumCounter;
sumLatSquare = sumLatSquare + Lat2 * Lat2;
sumLongSquare = sumLongSquare + Lon2 * Lon2;
squareLatVariant = sumLatSquare / (double)sumCounter - averageLat * averageLat;
squareLongVariant = sumLongSquare / (double)sumCounter - averageLong * averageLong;
if ( squareLatVariant < 0.0 ) squareLatVariant = 0.0;
if ( squareLongVariant < 0.0 ) squareLongVariant = 0.0;
squareLatVariant = sqrt(squareLatVariant);
squareLongVariant = sqrt(squareLongVariant);
// Serial.printf("%d, %d , %1.16f , %1.16f , %f, %f, %3.6f, %3.6f, %3.6f, %3.6f \r\n", \
millis(), sumCounter, squareLatVariant, squareLongVariant, sumLatSquare, sumLongSquare, sumLat, sumLong, averageLat, averageLong);
yVariant = EARTH * squareLatVariant * (PI / 180.0);
xVariant = EARTH * cos(Lat2 * PI / 180) * squareLongVariant * (PI / 180.0);
double dRMS = sqrt(xVariant * xVariant + yVariant * yVariant);//dRMS
Serial.printf("GPS RMS: %.1f m\r\n", dRMS);
// Serial.printf("%d, %d , %1.16f , %1.16f , %3.6f, %3.6f, %3.6f, %3.6f \r\n", millis(), sumCounter, squareLatVariant, squareLongVariant, sumLat, sumLong, averageLat, averageLong);
}
loopCount++;
digitalWrite(GPIO_NUM_2, loopCount % 2);
ts2 = millis();
if (gps1.location.isValid()) {
double k = (1.0 / hdop2) / (1.0 / hdop1 + 1.0 / hdop2); // weight factor
// Serial.printf("hdop1=%f hdop2=%f k=%f\r\n",hdop1,hdop2,k);
if (Lat1 != 360.0 ) {
#define WEIGHTED_AVERAGE
#ifndef WEIGHTED_AVERAGE
printNMEA(Year, Month, Day, Hour, Minute, Second, centiss, \
(Lat2 + Lat1) / 2.0 , (Lon2 + Lon1) / 2.0 , \
(height1 + height2) / 2.0 , (hdop1 + hdop2) / 2.0, \
(num_sat1 + num_sat2) / 2 , (course1 + course2) / 2.0 , \
(speedKnot1 + speedKnot2) / 2.0 ) ;
#else
printNMEA(Year, Month, Day, Hour, Minute, Second, centiss, \
k * Lat2 + (1.0 - k)*Lat1 , k * Lon2 + (1.0 - k)*Lon1 , \
k * height1 + (1.0 - k)*height2 , (hdop1 + hdop2) / 2.0, \
(num_sat1 + num_sat2) / 2 , k * course1 + (1.0 - k)*course2 , \
k * speedKnot1 + (1.0 - k)*speedKnot2 ) ;
#endif
}
}
}
}
}
void getGPSInfo1()
{
static long loopCount = 1;
if (gps1.location.isValid() )
//check if GPS is fixed
{
// if (millis() - 1000 > ts1) { //update every second
/* int Year = gps1.date.year();
byte Month = gps1.date.month();
byte Day = gps1.date.day();
byte Hour = gps1.time.hour();
byte Minute = gps1.time.minute();
byte Second = gps1.time.second();
byte centiSecond = gps1.time.centisecond();
double speedKnot1 = gps1.speed.knots();
double course1 = gps1.course.deg();
*///not used
num_sat1 = gps1.satellites.value();
height1 = gps1.altitude.meters() - Alt;
hdop1 = (float)gps1.hdop.value() / 100.0;
Lat1 = gps1.location.lat();
Lon1 = gps1.location.lng();
loopCount++;
ts1 = millis();
// }
}
}
// Watch dog for BT serial
void codeForBTserial(void * parameter)
{
portTickType xLastWakeTime;
const portTickType xFrequency = 1000;//run here every 1000msec
while (1) {
xLastWakeTime = xTaskGetTickCount();// Initialise the xLastWakeTime variable with the current time.
if ( (millis() - lastGPSread) > iIntervalTime * 1000 ) {
Serial.println();
Serial.println();
Serial.printf("Serial BT hang? rebooting now %d \r\n", lastGPSread);
ESP.restart();
}
vTaskDelayUntil( &xLastWakeTime, xFrequency );
}
}
void setup() {
Serial.begin(115200);
pinMode(GPIO_NUM_2, OUTPUT); //LED
digitalWrite(GPIO_NUM_2, LOW);
Serial.print("ESP32-dual-GPS-BTserial.ino "); Serial.println(VERSION);
Serial.println("Setup Android terminal to pair with GPS-BT-server");
SerialBT.begin("GPS-BT-server");
Serial.println("The device started, now you can pair it with bluetooth!");
//GPS port definition
Serial1.begin(GPS_BAUD, SERIAL_8N1 , GPIO_NUM_33, GPIO_NUM_34);
Serial2.begin(GPS_BAUD);// GPIO_NUM_16 for RX (GPS TX) ,GPIO_NUM_17 for TX (GPS RX)
// start timer interrupt
// ticker.attach(iIntervalTime, kickRoutine);
lastGPSread = millis();
ts1 = millis();
ts2 = millis();
xTaskCreatePinnedToCore( codeForBTserial, "BTserialWatch", 4000, NULL, 1, &Task1, 1); //core 1
}
void loop() {
char c;
char buf[NMEA_BUFSIZE];
//#define PRINTNMEA
#ifdef PRINTNMEA
if (Serial2.available() > 0) {
c = Serial2.read();
Serial.print(c);
lastGPSread = millis();
}
#else
static int i = 0;
if (Serial2.available() > 0) {
c = Serial2.read();
if ( i > NMEA_BUFSIZE - 2) {
// Serial.println("buffer overflow");
i = 0;
}
buf[i++] = c;
//Serial.print(c);
if (c == '\n') {
buf[i] = 0x00; //terminate by 0x00
char work[NMEA_BUFSIZE]; //seems necessary to avoid buf error while Serial.printf
memcpy( work, buf, i + 1 );
i = 0; //pass through $??GSV $??GSA $??TXT
if ( work[0] == '$' && work[4] == 'S' && work[5] == 'V') {
Serial.printf("%s", work); //Serial.printf(":%s",work); //check pass through data
SerialBT.printf("%s", work);
}
if ( work[0] == '$' && work[4] == 'S' && work[5] == 'A') {
Serial.printf("%s", work);
SerialBT.printf("%s", work);
}
if ( work[0] == '$' && work[4] == 'X' && work[5] == 'T') {
Serial.printf("%s", work);
SerialBT.printf("%s", work);
}
}
// Serial.write(c); // monitor for GPS application
lastGPSread = millis();
//SerialBT.write(c); // NMEA can be monitored by BT-Serial application for Android
if (gps2.encode(c)) {
getGPSInfo2();
}
}
if (Serial1.available()) {
c = Serial1.read();
lastGPSread = millis();
//Serial.write(c); // monitor for GPS application
if (gps1.encode(c)) {
getGPSInfo1();
}
}
#endif
}