-
Notifications
You must be signed in to change notification settings - Fork 9
/
GPS-GPRS
170 lines (140 loc) · 4.92 KB
/
GPS-GPRS
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
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial gprsSerial(7, 8);
SoftwareSerial gpsSerial(3, 2);
Adafruit_GPS GPS(&gpsSerial);
#define GPSECHO false
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
String lat = "0.0";
String lon = "0.0";
String spd = "0.0";
void setup(){
gprsSerial.begin(19200); // the GPRS baud rate
Serial.begin(115200); // the GPRS baud rate
GPS.begin(9600);
pinMode(9, OUTPUT);
digitalWrite(9,LOW);
delay(100);
digitalWrite(9,HIGH);
delay(500);
digitalWrite(9,LOW);
delay(100);
pinMode(13, OUTPUT);
digitalWrite(13,LOW);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
useInterrupt(true);
delay(1000);
gpsSerial.println(PMTK_Q_RELEASE);
}
SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();
#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;
#endif
}
void useInterrupt(boolean v) {
if (v) {
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {
TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}
uint32_t timer = millis();
void loop(){
SubmitHttpRequest();
if (gprsSerial.available())
Serial.write(gprsSerial.read());
if (! usingInterrupt) {
char c = GPS.read();
if (GPSECHO)
if (c) Serial.print(c);
}
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
if (timer > millis()) timer = millis();
if (millis() - timer > 500) {
timer = millis(); // reset the timer
Serial.print("\nTime: ");
Serial.print(GPS.hour, DEC); Serial.print(':');
Serial.print(GPS.minute, DEC); Serial.print(':');
Serial.print(GPS.seconds, DEC); Serial.print('.');
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
Serial.print("Location: ");
Serial.print(GPS.latitude, 4); Serial.print("-"); Serial.print(GPS.lat);
Serial.print(", ");
Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
Serial.print("Location (in degrees, works with Google Maps): ");
Serial.print(GPS.latitudeDegrees, 4);
lat = String(GPS.latitudeDegrees, 4);
Serial.print(", ");
Serial.println(GPS.longitudeDegrees, 4);
lon = String(GPS.longitudeDegrees, 4);
Serial.print("Speed (knots): "); Serial.println(GPS.speed)*1.151;
spd = String((GPS.speed)*1.151);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
}
}
}
///SubmitHttpRequest()
///this function is submit a http request
///attention:the time of delay is very important, it must be set enough
void SubmitHttpRequest()
{
// gprsSerial.println("AT+CSQ");
// delay(100);
// ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.
// gprsSerial.println("AT+CGATT?");
// delay(100);
// ShowSerialData();
gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
delay(1000);
// ShowSerialData();
gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"CMNET\"");//setting the APN, the second need you fill in your local apn server
delay(2000);
// delay(4000);
// ShowSerialData();
gprsSerial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
delay(1000);
// delay(2000);
// ShowSerialData();
gprsSerial.println("AT+HTTPINIT"); //init the HTTP request
delay(1000);
// delay(2000);
// ShowSerialData();
gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://198.74.52.26/map.php?lat="+lat+"&lon="+lon+"&speed="+spd+"\"");// setting the httppara, the second parameter is the website you want to access
delay(500);
// delay(1000);
// ShowSerialData();
gprsSerial.println("AT+HTTPACTION=0");//submit the request
// delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
//while(!mySerial.available());
// ShowSerialData();
// gprsSerial.println("AT+HTTPREAD");// read the data from the website you access
// delay(300);
// ShowSerialData();
// gprsSerial.println("");
// delay(100);
}
//void ShowSerialData()
//{
// while(gprsSerial.available()!=0)
// Serial.write(gprsSerial.read());
//}