TA1 / Mbed 2 deprecated Sim800_tiap20s_http

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //It is the code of the GPRS communication by the use of the Sim800 module.
00002 //This code has work is "to upload data on data.sparkfun.com in every 5 seconds.
00003 //To check the uploaded data the URL is "data.sparkfun.com/streams/roYVdJgWrXUpxVpDYJpA".
00004 
00005 #include "mbed.h"
00006 #include <string> 
00007 
00008 Serial sim800(D8,D2);                                   //Serial communication of the sim800 and pc.
00009 Serial pc(USBTX,USBRX);
00010 
00011 Ticker t_SendData;
00012 
00013 char x[300],y[30],z[30],l[30],m[30];;
00014 float nilai = 44 ;
00015 
00016 void SendData();
00017 int main()
00018 {
00019 
00020     pc.baud(9600);
00021     sim800.baud(9600);
00022 
00023     pc.printf("*******GPRS TEST*******\r\n");
00024 
00025     sim800.printf("ATE0\r\n");                              //Command for the TURN OFF the repated terms(ECHO).
00026         sim800.scanf("%s",x);                               //Take response in string x given by the "sim800".
00027         pc.printf("%s\r\n",x);
00028        
00029         sim800.printf("AT+CGATT?\r\n");                     //To check the GPRS is attached or not.
00030             sim800.scanf("%s%s",x,y);
00031             pc.printf("%s   %s\r\n",x,y);
00032         
00033         sim800.printf("AT+CIPSHUT\r\n");                    //To reset the IP session if any.
00034             sim800.scanf("%s%s",x,y);
00035             pc.printf("%s   %s\r\n",x,y);
00036     
00037         sim800.printf("AT+SAPBR=3,1,Contype,GPRS\r\n");     //To Set the connection type to GPRS.
00038             sim800.scanf("%s",x);
00039             pc.printf("%s\r\n",x);
00040         
00041         sim800.printf("AT+SAPBR=3,1,APN,internet\r\n");          //To Set the APN to to "www" , It might be different for you, and depends on the network.
00042             sim800.scanf("%s",x);
00043             pc.printf("%s\r\n",x);
00044         
00045         sim800.printf("AT+SAPBR =1,1\r\n");                 //To Enable the GPRS.
00046             sim800.scanf("%s",x);
00047             pc.printf("%s\r\n",x);
00048         
00049         sim800.printf("AT+SAPBR =2,1\r\n");                 //To get the IP address.
00050             sim800.scanf("%s%s%s",x,y,z);
00051             pc.printf("%s   %s  %s\r\n",x,y,z); 
00052         
00053         
00054         sim800.printf("AT+HTTPINIT\r\n");                   //To enable the HTTP mode.
00055             sim800.scanf("%s",x);
00056             pc.printf("%s\r\n",x);
00057         
00058         sim800.printf("AT+HTTPPARA=CID,1\r\n");             //To sets up HTTP parameters for the HTTP call.
00059             sim800.scanf("%s",x);
00060             pc.printf("%s\r\n",x);
00061             
00062         t_SendData.attach(&SendData, 20);
00063         while(true)
00064         {
00065             //nilai = nilai + 1;
00066 //            pc.printf("loop nilai : %f\r\n",nilai);
00067 //        
00068 //            sim800.printf("AT+HTTPPARA=URL,api.thingspeak.com/update?api_key=9R927S4HY3F10GRQ&field2=%0.2f\r\n",nilai);
00069 //                                                            //The above command is for "To Set the url to the address of the webpage you want to access".
00070 //                sim800.scanf("%s",x);
00071 //                pc.printf("%s\r\n",x);
00072 //            
00073 //            sim800.printf("AT+HTTPACTION=0\r\n");           //To Start the HTTP GET session, by giving this command.
00074 //                sim800.scanf("%s%s%s",x,y,z);
00075 //                pc.printf("%s   %s  %s\r\n",x,y,z);
00076 //        
00077 //            //sim800.printf("AT+HTTPREAD\r\n");               //To read the received data.
00078 ////                sim800.scanf("%s%s%s%s%s",x,y,z,l,m);
00079 ////                pc.printf("%s   %s  %s  %s  %s\r\n",x,y,z,l,m);
00080 ////            
00081 //            wait(10000);
00082         }
00083 }
00084 
00085 void SendData() {
00086     
00087     nilai = nilai + 1;
00088     pc.printf("loop nilai : %f\r\n",nilai);
00089         
00090     sim800.printf("AT+HTTPPARA=URL,api.thingspeak.com/update?api_key=9R927S4HY3F10GRQ&field3=%0.2f\r\n",nilai);
00091                                                             //The above command is for "To Set the url to the address of the webpage you want to access".
00092     sim800.scanf("%s",x);
00093     pc.printf("%s\r\n",x);
00094             
00095     sim800.printf("AT+HTTPACTION=0\r\n");           //To Start the HTTP GET session, by giving this command.
00096     sim800.scanf("%s%s%s",x,y,z);
00097     pc.printf("%s   %s  %s\r\n",x,y,z);
00098         
00099             //sim800.printf("AT+HTTPREAD\r\n");               //To read the received data.
00100 //                sim800.scanf("%s%s%s%s%s",x,y,z,l,m);
00101 //                pc.printf("%s   %s  %s  %s  %s\r\n",x,y,z,l,m);
00102     
00103 }