-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathRAK1910_GPS_UBLOX7.ino
133 lines (118 loc) · 2.54 KB
/
RAK1910_GPS_UBLOX7.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
/**
@file RAK1910_GPS_UBLOX7.ino
@author rakwireless.com
@brief get and parse gps data
@version 0.1
@date 2020-12-28
@copyright Copyright (c) 2020
**/
#include <Wire.h>
#include <TinyGPS.h> //http://librarymanager/All#TinyGPS
TinyGPS gps;
String tmp_data = "";
int direction_S_N = 0; //0--S, 1--N
int direction_E_W = 0; //0--E, 1--W
void setup()
{
// Initialize Serial for debug output
time_t timeout = millis();
Serial.begin(115200);
while (!Serial)
{
if ((millis() - timeout) < 5000)
{
delay(100);
}
else
{
break;
}
}
//gps init
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, 0);
delay(1000);
digitalWrite(WB_IO2, 1);
delay(1000);
Serial1.begin(9600);
while (!Serial1)
;
Serial.println("GPS uart init ok!");
}
void direction_parse(String tmp)
{
if (tmp.indexOf(",E,") != -1)
{
direction_E_W = 0;
}
else
{
direction_E_W = 1;
}
if (tmp.indexOf(",S,") != -1)
{
direction_S_N = 0;
}
else
{
direction_S_N = 1;
}
}
void loop()
{
bool newData = false;
unsigned long chars;
unsigned short sentences, failed;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial1.available())
{
char c = Serial1.read();
tmp_data += c;
if (gps.encode(c))
newData = true;
}
}
direction_parse(tmp_data);
tmp_data = "";
if (newData)
{
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
if(direction_S_N == 0)
{
Serial.print("(S):");
}
else
{
Serial.print("(N):");
}
Serial.print("LAT=");
Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
if(direction_E_W == 0)
{
Serial.print(" (E):");
}
else
{
Serial.print(" (W):");
}
Serial.print("LON=");
Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
Serial.print(" SAT=");
Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
Serial.print(" PREC=");
Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
}
gps.stats(&chars, &sentences, &failed);
Serial.print(" CHARS=");
Serial.print(chars);
Serial.print(" SENTENCES=");
Serial.print(sentences);
Serial.print(" CSUM ERR=");
Serial.println(failed);
if (chars == 0)
Serial.println("** No characters received from GPS: check wiring **");
}