Send GPS data to Thing Speak channel & SD card

Dependencies:   GPS SDFileSystem mbed

Fork of Send_GPS_data by Lucas Prido

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "GPS.h"
00003 #include "SDFileSystem.h"
00004 
00005 Serial pc(USBTX,USBRX, 9600);
00006 GPS my_gps(PA_9, PA_10);//Arduino pins D1, D0
00007 DigitalOut myled(LED1);
00008 
00009 Serial sim800(PA_2,PA_3, 9600);//Arduino pins A7, A2
00010 SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd");//Arduino pins D11, D12, D13, D10
00011 //                PB_5      PB_4     PB_3    PA_11
00012 
00013 char* wapi_key = "4PI3GKVCH4OW96LX";
00014 
00015 void init_sim(){
00016     sim800.printf("AT+CPIN?\r\n");
00017     wait(0.2);
00018     sim800.printf("AT+CGATT?\r\n");          //To check the GPRS is attached or not.
00019     wait(0.2);
00020     sim800.printf("AT+CIPSHUT\r\n");         //To reset the IP session if any.
00021     wait(0.2);
00022     sim800.printf("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n");     //To Set the connection type to GPRS.
00023     wait(0.2);
00024     sim800.printf("AT+SAPBR=3,1,\"APN\",\"3IoT.com\"\r\n");
00025     wait(0.2);
00026     sim800.printf("AT+SAPBR=1,1\r\n");       //To Enable the GPRS.
00027     wait(0.2);
00028     sim800.printf("AT+SAPBR=2,1\r\n");       //To get the IP address.
00029     wait(0.2);
00030 }
00031 
00032 int new_file(char* adr_file)//fonction to write on new file
00033 {
00034     FILE *fp = fopen(adr_file, "w");
00035     if(fp == NULL) {
00036         pc.printf("Unable to write the file\r\n");
00037         return -1;
00038     } else {
00039         fprintf(fp, "----New File----\r\n");
00040         fclose(fp);
00041         pc.printf("File successfully written!\r\n");
00042         return 0; //success
00043     }
00044 }
00045 
00046 int read_file(char* adr_file)//fct to read a file
00047 {
00048     char c;
00049     FILE *file;
00050     pc.printf("\r\nRead: %s\r\n", adr_file);
00051     file = fopen(adr_file, "r");
00052     if (file) {
00053         while (!feof(file)) {
00054             c = getc(file);
00055             pc.putc(c);
00056         }
00057         fclose(file);
00058         return 0; //success
00059     }
00060     return -1;
00061 }
00062 
00063 int add_data(char* adr_flie, char* msg)//fct to update a file
00064 {
00065     FILE *fp = fopen(adr_flie, "a");
00066     if(fp == NULL) {
00067         pc.printf("Unable to update the file\r\n");
00068         return 0; //success
00069     } else {
00070         fprintf(fp, msg);
00071         fclose(fp);
00072         pc.printf("File successfully update/written!\r\n");
00073         return -1;
00074     }
00075 }
00076 
00077 void send_ts(char* url_para)//Get url using sim800
00078 {
00079     sim800.printf("AT+HTTPINIT\r\n");   //enable the HTTP mode.
00080     wait(0.2);
00081     sim800.printf("AT+HTTPPARA=\"CID\",1\r\n");  //To sets up HTTP parameters for the HTTP call.
00082     wait(0.2);
00083     sim800.printf(url_para);                 //Set URL
00084     wait(0.2);
00085     sim800.printf("AT+HTTPACTION=0\r\n");    //Start the HTTP GET session, by giving this command.
00086     wait(1);
00087     sim800.printf("AT+HTTPREAD\r\n");        //read the received data.
00088     wait(1);
00089     sim800.printf("AT+HTTPTERM\r\n");     //disable HTTP mode
00090     wait(0.2);
00091 }
00092 
00093 char msg_gps[300];
00094 char url_sim[300];
00095 
00096 int main() {
00097     pc.printf("\r\n*******TEST Send GPS Data*******\r\n");
00098 
00099     sim800.printf("ATZ\r\n");//reset SIM800H conf
00100     wait(1);
00101     //init sim conf & create new file
00102     init_sim();
00103     new_file("/sd/coordinate.txt");//create new "empty" juste wrote "New file"
00104     read_file("/sd/coordinate.txt");
00105     
00106     while(1) {
00107 
00108         if(my_gps.sample() == 1) {
00109             myled=1;
00110             
00111             //put data in string to send
00112             sprintf(url_sim,"AT+HTTPPARA=\"URL\",\"api.thingspeak.com/update?api_key=%s&field1=%f&field2=%f&field3=%f\"\r\n", wapi_key, my_gps.latitude, my_gps.longitude, my_gps.utc);
00113             sprintf(msg_gps,"GPS Coordinate: Latitude: %0.4f, Longitude: %f0.4, UTC: %f0.4\r\n", my_gps.latitude, my_gps.longitude, my_gps.utc);
00114             
00115             //send data to ThingSpeak channel & SD card
00116             send_ts(url_sim);
00117             add_data("/sd/coordinate.txt",msg_gps);
00118             
00119             //print data on terminal
00120             pc.printf("latitude: %0.2f, longitude: %0.2f, utc: %f\r\n", my_gps.latitude, my_gps.longitude, my_gps.utc);
00121             pc.printf("Msg: %s\r\n", my_gps.msg);
00122             
00123             myled=0;
00124             wait(60);//wait 60 sec for the test
00125         }
00126     }
00127 }