Send GPS data to Thing Speak channel & SD card
Dependencies: GPS SDFileSystem mbed
Fork of Send_GPS_data by
NUCLEO F303K8 pins:
Picture:
main.cpp@2:c189e0c04d99, 2018-02-08 (annotated)
- Committer:
- LucasPrido
- Date:
- Thu Feb 08 09:32:28 2018 +0000
- Revision:
- 2:c189e0c04d99
- Parent:
- 0:0fee0bdbe239
small update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LucasPrido | 0:0fee0bdbe239 | 1 | #include "mbed.h" |
LucasPrido | 0:0fee0bdbe239 | 2 | #include "GPS.h" |
LucasPrido | 0:0fee0bdbe239 | 3 | #include "SDFileSystem.h" |
LucasPrido | 0:0fee0bdbe239 | 4 | |
LucasPrido | 0:0fee0bdbe239 | 5 | Serial pc(USBTX,USBRX, 9600); |
LucasPrido | 0:0fee0bdbe239 | 6 | GPS my_gps(PA_9, PA_10);//Arduino pins D1, D0 |
LucasPrido | 0:0fee0bdbe239 | 7 | DigitalOut myled(LED1); |
LucasPrido | 0:0fee0bdbe239 | 8 | |
LucasPrido | 0:0fee0bdbe239 | 9 | Serial sim800(PA_2,PA_3, 9600);//Arduino pins A7, A2 |
LucasPrido | 0:0fee0bdbe239 | 10 | SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd");//Arduino pins D11, D12, D13, D10 |
LucasPrido | 0:0fee0bdbe239 | 11 | // PB_5 PB_4 PB_3 PA_11 |
LucasPrido | 0:0fee0bdbe239 | 12 | |
LucasPrido | 2:c189e0c04d99 | 13 | char* wapi_key = "4PI3GKVCH4OW96LX"; |
LucasPrido | 2:c189e0c04d99 | 14 | |
LucasPrido | 0:0fee0bdbe239 | 15 | void init_sim(){ |
LucasPrido | 0:0fee0bdbe239 | 16 | sim800.printf("AT+CPIN?\r\n"); |
LucasPrido | 0:0fee0bdbe239 | 17 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 18 | sim800.printf("AT+CGATT?\r\n"); //To check the GPRS is attached or not. |
LucasPrido | 0:0fee0bdbe239 | 19 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 20 | sim800.printf("AT+CIPSHUT\r\n"); //To reset the IP session if any. |
LucasPrido | 0:0fee0bdbe239 | 21 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 22 | sim800.printf("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n"); //To Set the connection type to GPRS. |
LucasPrido | 0:0fee0bdbe239 | 23 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 24 | sim800.printf("AT+SAPBR=3,1,\"APN\",\"3IoT.com\"\r\n"); |
LucasPrido | 0:0fee0bdbe239 | 25 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 26 | sim800.printf("AT+SAPBR=1,1\r\n"); //To Enable the GPRS. |
LucasPrido | 0:0fee0bdbe239 | 27 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 28 | sim800.printf("AT+SAPBR=2,1\r\n"); //To get the IP address. |
LucasPrido | 0:0fee0bdbe239 | 29 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 30 | } |
LucasPrido | 0:0fee0bdbe239 | 31 | |
LucasPrido | 2:c189e0c04d99 | 32 | int new_file(char* adr_file)//fonction to write on new file |
LucasPrido | 0:0fee0bdbe239 | 33 | { |
LucasPrido | 0:0fee0bdbe239 | 34 | FILE *fp = fopen(adr_file, "w"); |
LucasPrido | 0:0fee0bdbe239 | 35 | if(fp == NULL) { |
LucasPrido | 0:0fee0bdbe239 | 36 | pc.printf("Unable to write the file\r\n"); |
LucasPrido | 0:0fee0bdbe239 | 37 | return -1; |
LucasPrido | 0:0fee0bdbe239 | 38 | } else { |
LucasPrido | 0:0fee0bdbe239 | 39 | fprintf(fp, "----New File----\r\n"); |
LucasPrido | 0:0fee0bdbe239 | 40 | fclose(fp); |
LucasPrido | 0:0fee0bdbe239 | 41 | pc.printf("File successfully written!\r\n"); |
LucasPrido | 0:0fee0bdbe239 | 42 | return 0; //success |
LucasPrido | 0:0fee0bdbe239 | 43 | } |
LucasPrido | 0:0fee0bdbe239 | 44 | } |
LucasPrido | 0:0fee0bdbe239 | 45 | |
LucasPrido | 2:c189e0c04d99 | 46 | int read_file(char* adr_file)//fct to read a file |
LucasPrido | 0:0fee0bdbe239 | 47 | { |
LucasPrido | 0:0fee0bdbe239 | 48 | char c; |
LucasPrido | 0:0fee0bdbe239 | 49 | FILE *file; |
LucasPrido | 0:0fee0bdbe239 | 50 | pc.printf("\r\nRead: %s\r\n", adr_file); |
LucasPrido | 0:0fee0bdbe239 | 51 | file = fopen(adr_file, "r"); |
LucasPrido | 0:0fee0bdbe239 | 52 | if (file) { |
LucasPrido | 0:0fee0bdbe239 | 53 | while (!feof(file)) { |
LucasPrido | 0:0fee0bdbe239 | 54 | c = getc(file); |
LucasPrido | 0:0fee0bdbe239 | 55 | pc.putc(c); |
LucasPrido | 0:0fee0bdbe239 | 56 | } |
LucasPrido | 0:0fee0bdbe239 | 57 | fclose(file); |
LucasPrido | 0:0fee0bdbe239 | 58 | return 0; //success |
LucasPrido | 0:0fee0bdbe239 | 59 | } |
LucasPrido | 0:0fee0bdbe239 | 60 | return -1; |
LucasPrido | 0:0fee0bdbe239 | 61 | } |
LucasPrido | 0:0fee0bdbe239 | 62 | |
LucasPrido | 2:c189e0c04d99 | 63 | int add_data(char* adr_flie, char* msg)//fct to update a file |
LucasPrido | 0:0fee0bdbe239 | 64 | { |
LucasPrido | 0:0fee0bdbe239 | 65 | FILE *fp = fopen(adr_flie, "a"); |
LucasPrido | 0:0fee0bdbe239 | 66 | if(fp == NULL) { |
LucasPrido | 0:0fee0bdbe239 | 67 | pc.printf("Unable to update the file\r\n"); |
LucasPrido | 0:0fee0bdbe239 | 68 | return 0; //success |
LucasPrido | 0:0fee0bdbe239 | 69 | } else { |
LucasPrido | 0:0fee0bdbe239 | 70 | fprintf(fp, msg); |
LucasPrido | 0:0fee0bdbe239 | 71 | fclose(fp); |
LucasPrido | 0:0fee0bdbe239 | 72 | pc.printf("File successfully update/written!\r\n"); |
LucasPrido | 0:0fee0bdbe239 | 73 | return -1; |
LucasPrido | 0:0fee0bdbe239 | 74 | } |
LucasPrido | 0:0fee0bdbe239 | 75 | } |
LucasPrido | 0:0fee0bdbe239 | 76 | |
LucasPrido | 2:c189e0c04d99 | 77 | void send_ts(char* url_para)//Get url using sim800 |
LucasPrido | 0:0fee0bdbe239 | 78 | { |
LucasPrido | 0:0fee0bdbe239 | 79 | sim800.printf("AT+HTTPINIT\r\n"); //enable the HTTP mode. |
LucasPrido | 0:0fee0bdbe239 | 80 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 81 | sim800.printf("AT+HTTPPARA=\"CID\",1\r\n"); //To sets up HTTP parameters for the HTTP call. |
LucasPrido | 0:0fee0bdbe239 | 82 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 83 | sim800.printf(url_para); //Set URL |
LucasPrido | 0:0fee0bdbe239 | 84 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 85 | sim800.printf("AT+HTTPACTION=0\r\n"); //Start the HTTP GET session, by giving this command. |
LucasPrido | 0:0fee0bdbe239 | 86 | wait(1); |
LucasPrido | 0:0fee0bdbe239 | 87 | sim800.printf("AT+HTTPREAD\r\n"); //read the received data. |
LucasPrido | 0:0fee0bdbe239 | 88 | wait(1); |
LucasPrido | 0:0fee0bdbe239 | 89 | sim800.printf("AT+HTTPTERM\r\n"); //disable HTTP mode |
LucasPrido | 0:0fee0bdbe239 | 90 | wait(0.2); |
LucasPrido | 0:0fee0bdbe239 | 91 | } |
LucasPrido | 0:0fee0bdbe239 | 92 | |
LucasPrido | 0:0fee0bdbe239 | 93 | char msg_gps[300]; |
LucasPrido | 0:0fee0bdbe239 | 94 | char url_sim[300]; |
LucasPrido | 0:0fee0bdbe239 | 95 | |
LucasPrido | 0:0fee0bdbe239 | 96 | int main() { |
LucasPrido | 0:0fee0bdbe239 | 97 | pc.printf("\r\n*******TEST Send GPS Data*******\r\n"); |
LucasPrido | 0:0fee0bdbe239 | 98 | |
LucasPrido | 0:0fee0bdbe239 | 99 | sim800.printf("ATZ\r\n");//reset SIM800H conf |
LucasPrido | 0:0fee0bdbe239 | 100 | wait(1); |
LucasPrido | 0:0fee0bdbe239 | 101 | //init sim conf & create new file |
LucasPrido | 0:0fee0bdbe239 | 102 | init_sim(); |
LucasPrido | 0:0fee0bdbe239 | 103 | new_file("/sd/coordinate.txt");//create new "empty" juste wrote "New file" |
LucasPrido | 0:0fee0bdbe239 | 104 | read_file("/sd/coordinate.txt"); |
LucasPrido | 0:0fee0bdbe239 | 105 | |
LucasPrido | 0:0fee0bdbe239 | 106 | while(1) { |
LucasPrido | 0:0fee0bdbe239 | 107 | |
LucasPrido | 0:0fee0bdbe239 | 108 | if(my_gps.sample() == 1) { |
LucasPrido | 0:0fee0bdbe239 | 109 | myled=1; |
LucasPrido | 0:0fee0bdbe239 | 110 | |
LucasPrido | 0:0fee0bdbe239 | 111 | //put data in string to send |
LucasPrido | 2:c189e0c04d99 | 112 | 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); |
LucasPrido | 0:0fee0bdbe239 | 113 | 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); |
LucasPrido | 0:0fee0bdbe239 | 114 | |
LucasPrido | 0:0fee0bdbe239 | 115 | //send data to ThingSpeak channel & SD card |
LucasPrido | 0:0fee0bdbe239 | 116 | send_ts(url_sim); |
LucasPrido | 0:0fee0bdbe239 | 117 | add_data("/sd/coordinate.txt",msg_gps); |
LucasPrido | 0:0fee0bdbe239 | 118 | |
LucasPrido | 0:0fee0bdbe239 | 119 | //print data on terminal |
LucasPrido | 0:0fee0bdbe239 | 120 | pc.printf("latitude: %0.2f, longitude: %0.2f, utc: %f\r\n", my_gps.latitude, my_gps.longitude, my_gps.utc); |
LucasPrido | 0:0fee0bdbe239 | 121 | pc.printf("Msg: %s\r\n", my_gps.msg); |
LucasPrido | 0:0fee0bdbe239 | 122 | |
LucasPrido | 0:0fee0bdbe239 | 123 | myled=0; |
LucasPrido | 2:c189e0c04d99 | 124 | wait(60);//wait 60 sec for the test |
LucasPrido | 0:0fee0bdbe239 | 125 | } |
LucasPrido | 0:0fee0bdbe239 | 126 | } |
LucasPrido | 0:0fee0bdbe239 | 127 | } |