-
Notifications
You must be signed in to change notification settings - Fork 9
/
GPRSprogram
70 lines (58 loc) · 2.18 KB
/
GPRSprogram
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
#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial gprsSerial(7, 8);
void setup(){
gprsSerial.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the GPRS baud rate
delay(500);
}
void loop(){
if (Serial.available())
switch(Serial.read())
{
case 'h':
SubmitHttpRequest();
break;
}
if (gprsSerial.available())
Serial.write(gprsSerial.read());
}
///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(4000);
ShowSerialData();
gprsSerial.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
delay(2000);
ShowSerialData();
gprsSerial.println("AT+HTTPINIT"); //init the HTTP request
delay(2000);
ShowSerialData();
gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://198.74.52.26/map.php?lat="+lat+"&lon="+lon+"\"");// setting the httppara, the second parameter is the website you want to access
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());
}