It is the code of the Sim800 GPRS code.

Dependencies:   mbed

Committer:
champion0311
Date:
Fri Aug 05 08:49:59 2016 +0000
Revision:
0:50d54df4a4c2
SIM800_GPRS_CODE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
champion0311 0:50d54df4a4c2 1 //It is the code of the GPRS communication by the use of the Sim800 module.
champion0311 0:50d54df4a4c2 2 //This code has work is "to upload data on data.sparkfun.com in every 5 seconds.
champion0311 0:50d54df4a4c2 3 //To check the uploaded data the URL is "data.sparkfun.com/streams/roYVdJgWrXUpxVpDYJpA".
champion0311 0:50d54df4a4c2 4
champion0311 0:50d54df4a4c2 5 #include "mbed.h"
champion0311 0:50d54df4a4c2 6 #include <string>
champion0311 0:50d54df4a4c2 7
champion0311 0:50d54df4a4c2 8 Serial sim800(PTE0,PTE1); //Serial communication of the sim800 and pc.
champion0311 0:50d54df4a4c2 9 Serial pc(USBTX,USBRX);
champion0311 0:50d54df4a4c2 10
champion0311 0:50d54df4a4c2 11 AnalogIn pot(PTB0);
champion0311 0:50d54df4a4c2 12 AnalogIn ldr(PTB1);
champion0311 0:50d54df4a4c2 13
champion0311 0:50d54df4a4c2 14 char x[300],y[30],z[30],l[30],m[30];
champion0311 0:50d54df4a4c2 15
champion0311 0:50d54df4a4c2 16 int main()
champion0311 0:50d54df4a4c2 17 {
champion0311 0:50d54df4a4c2 18 float potval = 0;
champion0311 0:50d54df4a4c2 19 float ldrval = 0;
champion0311 0:50d54df4a4c2 20
champion0311 0:50d54df4a4c2 21 pc.baud(9600);
champion0311 0:50d54df4a4c2 22 sim800.baud(9600);
champion0311 0:50d54df4a4c2 23
champion0311 0:50d54df4a4c2 24 pc.printf("*******GPRS TEST*******\r\n");
champion0311 0:50d54df4a4c2 25
champion0311 0:50d54df4a4c2 26 sim800.printf("ATE0\r\n"); //Command for the TURN OFF the repated terms(ECHO).
champion0311 0:50d54df4a4c2 27 sim800.scanf("%s",x); //Take response in string x given by the "sim800".
champion0311 0:50d54df4a4c2 28 pc.printf("%s\r\n",x);
champion0311 0:50d54df4a4c2 29
champion0311 0:50d54df4a4c2 30 sim800.printf("AT+CGATT?\r\n"); //To check the GPRS is attached or not.
champion0311 0:50d54df4a4c2 31 sim800.scanf("%s%s",x,y);
champion0311 0:50d54df4a4c2 32 pc.printf("%s %s\r\n",x,y);
champion0311 0:50d54df4a4c2 33
champion0311 0:50d54df4a4c2 34 sim800.printf("AT+CIPSHUT\r\n"); //To reset the IP session if any.
champion0311 0:50d54df4a4c2 35 sim800.scanf("%s%s",x,y);
champion0311 0:50d54df4a4c2 36 pc.printf("%s %s\r\n",x,y);
champion0311 0:50d54df4a4c2 37
champion0311 0:50d54df4a4c2 38 sim800.printf("AT+SAPBR=3,1,Contype,GPRS\r\n"); //To Set the connection type to GPRS.
champion0311 0:50d54df4a4c2 39 sim800.scanf("%s",x);
champion0311 0:50d54df4a4c2 40 pc.printf("%s\r\n",x);
champion0311 0:50d54df4a4c2 41
champion0311 0:50d54df4a4c2 42 sim800.printf("AT+SAPBR=3,1,APN,www\r\n"); //To Set the APN to to "www" , It might be different for you, and depends on the network.
champion0311 0:50d54df4a4c2 43 sim800.scanf("%s",x);
champion0311 0:50d54df4a4c2 44 pc.printf("%s\r\n",x);
champion0311 0:50d54df4a4c2 45
champion0311 0:50d54df4a4c2 46 sim800.printf("AT+SAPBR =1,1\r\n"); //To Enable the GPRS.
champion0311 0:50d54df4a4c2 47 sim800.scanf("%s",x);
champion0311 0:50d54df4a4c2 48 pc.printf("%s\r\n",x);
champion0311 0:50d54df4a4c2 49
champion0311 0:50d54df4a4c2 50 sim800.printf("AT+SAPBR =2,1\r\n"); //To get the IP address.
champion0311 0:50d54df4a4c2 51 sim800.scanf("%s%s%s",x,y,z);
champion0311 0:50d54df4a4c2 52 pc.printf("%s %s %s\r\n",x,y,z);
champion0311 0:50d54df4a4c2 53
champion0311 0:50d54df4a4c2 54
champion0311 0:50d54df4a4c2 55 sim800.printf("AT+HTTPINIT\r\n"); //To enable the HTTP mode.
champion0311 0:50d54df4a4c2 56 sim800.scanf("%s",x);
champion0311 0:50d54df4a4c2 57 pc.printf("%s\r\n",x);
champion0311 0:50d54df4a4c2 58
champion0311 0:50d54df4a4c2 59 sim800.printf("AT+HTTPPARA=CID,1\r\n"); //To sets up HTTP parameters for the HTTP call.
champion0311 0:50d54df4a4c2 60 sim800.scanf("%s",x);
champion0311 0:50d54df4a4c2 61 pc.printf("%s\r\n",x);
champion0311 0:50d54df4a4c2 62
champion0311 0:50d54df4a4c2 63
champion0311 0:50d54df4a4c2 64 float lng,lat = 0;
champion0311 0:50d54df4a4c2 65 while(lat == 0)
champion0311 0:50d54df4a4c2 66 {
champion0311 0:50d54df4a4c2 67 sim800.printf("AT+CIPGSMLOC=1,1\r\n"); //To check the Location of GSM in Co-Ordinates(Longitude,Latitude).
champion0311 0:50d54df4a4c2 68 sim800.scanf("%s%s%s",x,y,z);
champion0311 0:50d54df4a4c2 69 sscanf(y,"0,%f,%f",&lng,&lat); //To sperate out the value of longitude and latitude from the string 'y'.
champion0311 0:50d54df4a4c2 70 }
champion0311 0:50d54df4a4c2 71 pc.printf("%0.6f %0.6f",lng,lat);
champion0311 0:50d54df4a4c2 72
champion0311 0:50d54df4a4c2 73
champion0311 0:50d54df4a4c2 74 while(1)
champion0311 0:50d54df4a4c2 75 {
champion0311 0:50d54df4a4c2 76 potval = pot.read();
champion0311 0:50d54df4a4c2 77 ldrval = ldr.read();
champion0311 0:50d54df4a4c2 78
champion0311 0:50d54df4a4c2 79 sim800.printf("AT+HTTPPARA=URL,data.sparkfun.com/input/roYVdJgWrXUpxVpDYJpA?private_key=jkgpNMYr81cgMGgbypgr&co_lat=%0.6f&co_long=%0.6f&ldr=%0.2f&pot=%0.2f\r\n",lat,lng,potval,ldrval);
champion0311 0:50d54df4a4c2 80 //The above command is for "To Set the url to the address of the webpage you want to access".
champion0311 0:50d54df4a4c2 81 sim800.scanf("%s",x);
champion0311 0:50d54df4a4c2 82 pc.printf("%s\r\n",x);
champion0311 0:50d54df4a4c2 83
champion0311 0:50d54df4a4c2 84 sim800.printf("AT+HTTPACTION=0\r\n"); //To Start the HTTP GET session, by giving this command.
champion0311 0:50d54df4a4c2 85 sim800.scanf("%s%s%s",x,y,z);
champion0311 0:50d54df4a4c2 86 pc.printf("%s %s %s\r\n",x,y,z);
champion0311 0:50d54df4a4c2 87
champion0311 0:50d54df4a4c2 88 sim800.printf("AT+HTTPREAD\r\n"); //To read the received data.
champion0311 0:50d54df4a4c2 89 sim800.scanf("%s%s%s%s%s",x,y,z,l,m);
champion0311 0:50d54df4a4c2 90 pc.printf("%s %s %s %s %s\r\n",x,y,z,l,m);
champion0311 0:50d54df4a4c2 91
champion0311 0:50d54df4a4c2 92 wait(5);
champion0311 0:50d54df4a4c2 93 }
champion0311 0:50d54df4a4c2 94 }