This library provides data update method to Fastsensing.
Dependents: SCP1000_Fastsensing
Fastsensing.cpp@6:8338b52bd7bb, 2017-05-19 (annotated)
- Committer:
- AkiraK
- Date:
- Fri May 19 01:07:07 2017 +0000
- Revision:
- 6:8338b52bd7bb
- Parent:
- 4:45527abb7f40
- Parent:
- 5:e34ff2b4af3f
- Child:
- 7:c29803218447
merge
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 | 3:d0d34327ea79 | 8 | void Fastsensing::ethConnect() { |
AkiraK | 3:d0d34327ea79 | 9 | eth.init(); //Use DHCP. Initialize setting of eth. |
AkiraK | 4:45527abb7f40 | 10 | eth.connect(100); //Connect Bring the interface up. |
AkiraK | 3:d0d34327ea79 | 11 | } |
AkiraK | 3:d0d34327ea79 | 12 | |
AkiraK | 3:d0d34327ea79 | 13 | void Fastsensing::ethDisconnect() { |
AkiraK | 3:d0d34327ea79 | 14 | eth.disconnect(); //EthernetInterface closed |
AkiraK | 3:d0d34327ea79 | 15 | } |
AkiraK | 3:d0d34327ea79 | 16 | |
AkiraK | 0:f26c02be48f9 | 17 | int Fastsensing::updateData(char *deviceId, char *channelId, double data) |
AkiraK | 0:f26c02be48f9 | 18 | { |
AkiraK | 3:d0d34327ea79 | 19 | //eth.init(); //Use DHCP. Initialize setting of eth. |
AkiraK | 3:d0d34327ea79 | 20 | //eth.connect(); //Connect Bring the interface up. |
AkiraK | 0:f26c02be48f9 | 21 | char reqUrl[1000]; //Char array for request |
AkiraK | 5:e34ff2b4af3f | 22 | char basedUrl[20] = "http://f-io.net/d1/"; //based http url |
AkiraK | 0:f26c02be48f9 | 23 | char query[256]; //The array is for holding query |
AkiraK | 0:f26c02be48f9 | 24 | char resResult[256]; //The array is for holding response result |
AkiraK | 0:f26c02be48f9 | 25 | strcpy(reqUrl, basedUrl); //based Url is copied to reqUrl |
AkiraK | 0:f26c02be48f9 | 26 | strcat(reqUrl, deviceId); //Concatenate reqUrl and deviceId |
AkiraK | 0:f26c02be48f9 | 27 | sprintf(query, "?%s=%f", channelId, data); //Create Query String |
AkiraK | 0:f26c02be48f9 | 28 | strcat(reqUrl, query); //Concatenate reqUrl and query |
AkiraK | 0:f26c02be48f9 | 29 | 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 | 30 | printf("%d\n", res); |
AkiraK | 0:f26c02be48f9 | 31 | if(!res) { //If session is completed |
AkiraK | 3:d0d34327ea79 | 32 | //eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 33 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 34 | return 0; //Return 0 |
AkiraK | 0:f26c02be48f9 | 35 | } else { //If session is not completed |
AkiraK | 3:d0d34327ea79 | 36 | //eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 37 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 38 | return 1; //Return 1 |
AkiraK | 0:f26c02be48f9 | 39 | } |
AkiraK | 0:f26c02be48f9 | 40 | } |
AkiraK | 0:f26c02be48f9 | 41 | |
AkiraK | 0:f26c02be48f9 | 42 | int Fastsensing::updateDataAll(char *deviceId, char *channelId[3], float data[3]) |
AkiraK | 0:f26c02be48f9 | 43 | { |
AkiraK | 3:d0d34327ea79 | 44 | //eth.init(); //Use DHCP. Initialize setting of eth. |
AkiraK | 3:d0d34327ea79 | 45 | //eth.connect(); //Connect Bring the interface up. |
AkiraK | 0:f26c02be48f9 | 46 | char reqUrl[1000]; //Char array for request |
AkiraK | 0:f26c02be48f9 | 47 | char basedUrl[20] = "http://f-io.net/D1/"; //based http url |
AkiraK | 0:f26c02be48f9 | 48 | char query[256]; //The array is for holding query |
AkiraK | 0:f26c02be48f9 | 49 | char resResult[256]; //The array is for holding response result |
AkiraK | 0:f26c02be48f9 | 50 | char delim; //delimiter |
AkiraK | 0:f26c02be48f9 | 51 | strcpy(reqUrl, basedUrl); //based Url is copied to reqUrl |
AkiraK | 0:f26c02be48f9 | 52 | strcat(reqUrl, deviceId); //Concatenate reqUrl and deviceId |
AkiraK | 0:f26c02be48f9 | 53 | for(int i = 0; i < 3; i++) { //Create request URL |
AkiraK | 0:f26c02be48f9 | 54 | if(i == 0) delim = '?'; //first query is connected with ? |
AkiraK | 0:f26c02be48f9 | 55 | else delim = '&'; //After second, query is connected with & |
AkiraK | 2:5e41a339e05c | 56 | sprintf(query, "%c%s=%.1f",delim, channelId[i], data[i]); //Create Query String |
AkiraK | 0:f26c02be48f9 | 57 | strcat(reqUrl, query); //Concatenate reqUrl and query |
AkiraK | 0:f26c02be48f9 | 58 | } |
AkiraK | 0:f26c02be48f9 | 59 | printf("%s\n", reqUrl); |
AkiraK | 3:d0d34327ea79 | 60 | int res = http.get(reqUrl, resResult, 128); //Send request is reqUrl as get method and get Response Result. Time out is 5000 |
AkiraK | 1:3f52b4da889a | 61 | //printf("%d\n", res); |
AkiraK | 1:3f52b4da889a | 62 | //printf("%d\n", http.getHTTPResponseCode()); |
AkiraK | 0:f26c02be48f9 | 63 | if(!res) { //If session is completed |
AkiraK | 3:d0d34327ea79 | 64 | //eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 65 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 66 | return 0; //Return 0 |
AkiraK | 0:f26c02be48f9 | 67 | } else { //If session is not completed |
AkiraK | 3:d0d34327ea79 | 68 | //eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 69 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 70 | return 1; //Return 1 |
AkiraK | 0:f26c02be48f9 | 71 | } |
AkiraK | 0:f26c02be48f9 | 72 | } |