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