-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetGPS_push.ino
186 lines (164 loc) · 4.16 KB
/
getGPS_push.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
#include <ESP8266WiFi.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define BaudRrate 9600
#define GPSBaud 9600
#define SSID "Asus_J"
#define PASS "12347890"
#define HOST "140.125.32.146"
#define PORT 8088
String dog_name = "testDog";
static const int RXPin = 0, TXPin = 2;
static const int ledPin = 16;
String GET = "GET /getGPS/?";
double gps_lat, gps_lng, gps_date, gps_time;
bool isGet;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
pinMode(4, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
Serial.begin(BaudRrate);
ss.begin(GPSBaud);
Serial.println( "ESP8266 Ready!" );
Serial.print("Connect to ");
Serial.println( SSID );
WiFi.begin( SSID, PASS );
while( WiFi.status() != WL_CONNECTED )
{
delay(500);
Serial.print( "." );
}
Serial.println( "" );
Serial.println( "WiFi connected" );
Serial.println( "IP address: " );
Serial.println( WiFi.localIP() );
Serial.println( "" );
Serial.print(F("Testing TinyGPS++ library v. "));
Serial.println(TinyGPSPlus::libraryVersion());
Serial.println();
delay(1000);
}
void loop()
{
while (ss.available() > 0)
{
if (gps.encode(ss.read()))
{
isGet = false;
displayInfo(); //顯示GPS資訊
if (isGet)
{
updateGPS(); //上傳GPS資料
digitalWrite(ledPin, LOW);
delay(300);
digitalWrite(ledPin, HIGH);
}
delay(15000);
}
}
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while(true);
}
}
void updateGPS()
{
// 設定 ESP8266 作為 Client 端
WiFiClient client;
if( !client.connect( HOST, PORT ) )
{
Serial.println( "connection failed" );
return;
}
else
{
char gps_lat_char[12];
char gps_lng_char[12];
dtostrf(gps_lat,3,6,gps_lat_char);
dtostrf(gps_lng,3,6,gps_lng_char);
String getStr = GET +
"dog_name=" + String(dog_name) +
"&latitude=" + String(gps_lat_char) +
"&longitude=" + String(gps_lng_char) +
"&ok=ok" +
" HTTP/1.1\r\n" +
"Host: " + HOST +
"\r\nConnection: close\r\n\r\n";
client.print( String(getStr) );
//Serial.println( String(getStr) );
delay(10);
//client.stop();
}
}
void displayInfo()
{
Serial.print(F("Location: "));
if (gps.location.isValid())
{
isGet = true;
gps_lat = gps.location.lat();
gps_lng = gps.location.lng();
Serial.print(gps_lat,6);
Serial.print(F(","));
Serial.print(gps_lng,6);
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" Date/Time: "));
if (gps.date.isValid())
{
//gps_date =
Serial.print(gps.date.month());
Serial.print(F("/"));
Serial.print(gps.date.day());
Serial.print(F("/"));
Serial.print(gps.date.year());
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" "));
if (gps.time.isValid())
{
//gps_time =
if (gps.time.hour() < 10)
Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(F(":"));
if (gps.time.minute() < 10)
Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(F(":"));
if (gps.time.second() < 10)
Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(F("."));
if (gps.time.centisecond() < 10)
Serial.print(F("0"));
Serial.print(gps.time.centisecond());
}
else
{
Serial.print(F("INVALID"));
}
/*
Serial.println("Satellite Count:");
Serial.println(gps.satellites.value());
Serial.println("Latitude:");
Serial.println(gps.location.lat(), 6);
Serial.println("Longitude:");
Serial.println(gps.location.lng(), 6);
Serial.println("Speed MPH:");
Serial.println(gps.speed.mph());
Serial.println("Altitude Feet:");
Serial.println(gps.altitude.feet());
*/
Serial.println();
}