This library provides data update method to Fastsensing.
Dependents: SCP1000_Fastsensing
Fastsensing.cpp@1:3f52b4da889a, 2017-04-04 (annotated)
- Committer:
- AkiraK
- Date:
- Tue Apr 04 06:17:45 2017 +0000
- Revision:
- 1:3f52b4da889a
- Parent:
- 0:f26c02be48f9
- Child:
- 2:5e41a339e05c
- Child:
- 5:e34ff2b4af3f
Add basic information of this library.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkiraK | 1:3f52b4da889a | 1 | /* |
AkiraK | 1:3f52b4da889a | 2 | * Created by Akira Kashihara <akira@fastsensing.com> |
AkiraK | 1:3f52b4da889a | 3 | * Description : You can upload sensing data Fastsensing and display them. |
AkiraK | 1:3f52b4da889a | 4 | * This Library needs your device ID, channel ID and each data value. |
AkiraK | 1:3f52b4da889a | 5 | */ |
AkiraK | 0:f26c02be48f9 | 6 | #include "Fastsensing.h" |
AkiraK | 0:f26c02be48f9 | 7 | |
AkiraK | 0:f26c02be48f9 | 8 | int Fastsensing::updateData(char *deviceId, char *channelId, double data) |
AkiraK | 0:f26c02be48f9 | 9 | { |
AkiraK | 0:f26c02be48f9 | 10 | eth.init(); //Use DHCP. Initialize setting of eth. |
AkiraK | 0:f26c02be48f9 | 11 | eth.connect(); //Connect Bring the interface up. |
AkiraK | 0:f26c02be48f9 | 12 | char reqUrl[1000]; //Char array for request |
AkiraK | 0:f26c02be48f9 | 13 | char basedUrl[20] = "http://f-io.net/D1/"; //based http url |
AkiraK | 0:f26c02be48f9 | 14 | char query[256]; //The array is for holding query |
AkiraK | 0:f26c02be48f9 | 15 | char resResult[256]; //The array is for holding response result |
AkiraK | 0:f26c02be48f9 | 16 | strcpy(reqUrl, basedUrl); //based Url is copied to reqUrl |
AkiraK | 0:f26c02be48f9 | 17 | strcat(reqUrl, deviceId); //Concatenate reqUrl and deviceId |
AkiraK | 0:f26c02be48f9 | 18 | sprintf(query, "?%s=%f", channelId, data); //Create Query String |
AkiraK | 0:f26c02be48f9 | 19 | strcat(reqUrl, query); //Concatenate reqUrl and query |
AkiraK | 0:f26c02be48f9 | 20 | int res = http.get(reqUrl, resResult, 128);//Send request is reqUrl as get method and get Response Result. Time out is 5000 |
AkiraK | 0:f26c02be48f9 | 21 | printf("%d\n", res); |
AkiraK | 0:f26c02be48f9 | 22 | if(!res) { //If session is completed |
AkiraK | 0:f26c02be48f9 | 23 | eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 24 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 25 | return 0; //Return 0 |
AkiraK | 0:f26c02be48f9 | 26 | } else { //If session is not completed |
AkiraK | 0:f26c02be48f9 | 27 | eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 28 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 29 | return 1; //Return 1 |
AkiraK | 0:f26c02be48f9 | 30 | } |
AkiraK | 0:f26c02be48f9 | 31 | } |
AkiraK | 0:f26c02be48f9 | 32 | |
AkiraK | 0:f26c02be48f9 | 33 | int Fastsensing::updateDataAll(char *deviceId, char *channelId[3], float data[3]) |
AkiraK | 0:f26c02be48f9 | 34 | { |
AkiraK | 0:f26c02be48f9 | 35 | eth.init(); //Use DHCP. Initialize setting of eth. |
AkiraK | 0:f26c02be48f9 | 36 | eth.connect(); //Connect Bring the interface up. |
AkiraK | 0:f26c02be48f9 | 37 | char reqUrl[1000]; //Char array for request |
AkiraK | 0:f26c02be48f9 | 38 | char basedUrl[20] = "http://f-io.net/D1/"; //based http url |
AkiraK | 0:f26c02be48f9 | 39 | char query[256]; //The array is for holding query |
AkiraK | 0:f26c02be48f9 | 40 | char resResult[256]; //The array is for holding response result |
AkiraK | 0:f26c02be48f9 | 41 | char delim; //delimiter |
AkiraK | 0:f26c02be48f9 | 42 | strcpy(reqUrl, basedUrl); //based Url is copied to reqUrl |
AkiraK | 0:f26c02be48f9 | 43 | strcat(reqUrl, deviceId); //Concatenate reqUrl and deviceId |
AkiraK | 0:f26c02be48f9 | 44 | for(int i = 0; i < 3; i++) { //Create request URL |
AkiraK | 0:f26c02be48f9 | 45 | if(i == 0) delim = '?'; //first query is connected with ? |
AkiraK | 0:f26c02be48f9 | 46 | else delim = '&'; //After second, query is connected with & |
AkiraK | 1:3f52b4da889a | 47 | sprintf(query, "%c%s=%.4f",delim, channelId[i], data[i]); //Create Query String |
AkiraK | 0:f26c02be48f9 | 48 | strcat(reqUrl, query); //Concatenate reqUrl and query |
AkiraK | 0:f26c02be48f9 | 49 | } |
AkiraK | 0:f26c02be48f9 | 50 | printf("%s\n", reqUrl); |
AkiraK | 0:f26c02be48f9 | 51 | int res = http.get(reqUrl, resResult, 300); //Send request is reqUrl as get method and get Response Result. Time out is 5000 |
AkiraK | 1:3f52b4da889a | 52 | //printf("%d\n", res); |
AkiraK | 1:3f52b4da889a | 53 | //printf("%d\n", http.getHTTPResponseCode()); |
AkiraK | 0:f26c02be48f9 | 54 | if(!res) { //If session is completed |
AkiraK | 0:f26c02be48f9 | 55 | eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 56 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 57 | return 0; //Return 0 |
AkiraK | 0:f26c02be48f9 | 58 | } else { //If session is not completed |
AkiraK | 0:f26c02be48f9 | 59 | eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 60 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 61 | return 1; //Return 1 |
AkiraK | 0:f26c02be48f9 | 62 | } |
AkiraK | 0:f26c02be48f9 | 63 | } |