This library provides data update method to Fastsensing.
Dependents: SCP1000_Fastsensing
Fastsensing.cpp@7:c29803218447, 2017-05-19 (annotated)
- Committer:
- AkiraK
- Date:
- Fri May 19 01:08:43 2017 +0000
- Revision:
- 7:c29803218447
- Parent:
- 6:8338b52bd7bb
- Child:
- 9:313790e8b56d
Add LICENCE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkiraK | 1:3f52b4da889a | 1 | /* |
AkiraK | 7:c29803218447 | 2 | MIT License |
AkiraK | 7:c29803218447 | 3 | |
AkiraK | 7:c29803218447 | 4 | Copyright (c) 2017 Fast Sensing Inc. |
AkiraK | 7:c29803218447 | 5 | |
AkiraK | 7:c29803218447 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
AkiraK | 7:c29803218447 | 7 | of this software and associated documentation files (the "Software"), to deal |
AkiraK | 7:c29803218447 | 8 | in the Software without restriction, including without limitation the rights |
AkiraK | 7:c29803218447 | 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
AkiraK | 7:c29803218447 | 10 | copies of the Software, and to permit persons to whom the Software is |
AkiraK | 7:c29803218447 | 11 | furnished to do so, subject to the following conditions: |
AkiraK | 7:c29803218447 | 12 | |
AkiraK | 7:c29803218447 | 13 | The above copyright notice and this permission notice shall be included in all |
AkiraK | 7:c29803218447 | 14 | copies or substantial portions of the Software. |
AkiraK | 7:c29803218447 | 15 | |
AkiraK | 7:c29803218447 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
AkiraK | 7:c29803218447 | 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
AkiraK | 7:c29803218447 | 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
AkiraK | 7:c29803218447 | 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
AkiraK | 7:c29803218447 | 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AkiraK | 7:c29803218447 | 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
AkiraK | 7:c29803218447 | 22 | SOFTWARE. |
AkiraK | 1:3f52b4da889a | 23 | */ |
AkiraK | 7:c29803218447 | 24 | |
AkiraK | 0:f26c02be48f9 | 25 | #include "Fastsensing.h" |
AkiraK | 0:f26c02be48f9 | 26 | |
AkiraK | 3:d0d34327ea79 | 27 | void Fastsensing::ethConnect() { |
AkiraK | 3:d0d34327ea79 | 28 | eth.init(); //Use DHCP. Initialize setting of eth. |
AkiraK | 4:45527abb7f40 | 29 | eth.connect(100); //Connect Bring the interface up. |
AkiraK | 3:d0d34327ea79 | 30 | } |
AkiraK | 3:d0d34327ea79 | 31 | |
AkiraK | 3:d0d34327ea79 | 32 | void Fastsensing::ethDisconnect() { |
AkiraK | 3:d0d34327ea79 | 33 | eth.disconnect(); //EthernetInterface closed |
AkiraK | 3:d0d34327ea79 | 34 | } |
AkiraK | 3:d0d34327ea79 | 35 | |
AkiraK | 0:f26c02be48f9 | 36 | int Fastsensing::updateData(char *deviceId, char *channelId, double data) |
AkiraK | 0:f26c02be48f9 | 37 | { |
AkiraK | 3:d0d34327ea79 | 38 | //eth.init(); //Use DHCP. Initialize setting of eth. |
AkiraK | 3:d0d34327ea79 | 39 | //eth.connect(); //Connect Bring the interface up. |
AkiraK | 0:f26c02be48f9 | 40 | char reqUrl[1000]; //Char array for request |
AkiraK | 5:e34ff2b4af3f | 41 | char basedUrl[20] = "http://f-io.net/d1/"; //based http url |
AkiraK | 0:f26c02be48f9 | 42 | char query[256]; //The array is for holding query |
AkiraK | 0:f26c02be48f9 | 43 | char resResult[256]; //The array is for holding response result |
AkiraK | 0:f26c02be48f9 | 44 | strcpy(reqUrl, basedUrl); //based Url is copied to reqUrl |
AkiraK | 0:f26c02be48f9 | 45 | strcat(reqUrl, deviceId); //Concatenate reqUrl and deviceId |
AkiraK | 0:f26c02be48f9 | 46 | sprintf(query, "?%s=%f", channelId, data); //Create Query String |
AkiraK | 0:f26c02be48f9 | 47 | strcat(reqUrl, query); //Concatenate reqUrl and query |
AkiraK | 0:f26c02be48f9 | 48 | 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 | 49 | printf("%d\n", res); |
AkiraK | 0:f26c02be48f9 | 50 | if(!res) { //If session is completed |
AkiraK | 3:d0d34327ea79 | 51 | //eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 52 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 53 | return 0; //Return 0 |
AkiraK | 0:f26c02be48f9 | 54 | } else { //If session is not completed |
AkiraK | 3:d0d34327ea79 | 55 | //eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 56 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 57 | return 1; //Return 1 |
AkiraK | 0:f26c02be48f9 | 58 | } |
AkiraK | 0:f26c02be48f9 | 59 | } |
AkiraK | 0:f26c02be48f9 | 60 | |
AkiraK | 0:f26c02be48f9 | 61 | int Fastsensing::updateDataAll(char *deviceId, char *channelId[3], float data[3]) |
AkiraK | 0:f26c02be48f9 | 62 | { |
AkiraK | 3:d0d34327ea79 | 63 | //eth.init(); //Use DHCP. Initialize setting of eth. |
AkiraK | 3:d0d34327ea79 | 64 | //eth.connect(); //Connect Bring the interface up. |
AkiraK | 0:f26c02be48f9 | 65 | char reqUrl[1000]; //Char array for request |
AkiraK | 0:f26c02be48f9 | 66 | char basedUrl[20] = "http://f-io.net/D1/"; //based http url |
AkiraK | 0:f26c02be48f9 | 67 | char query[256]; //The array is for holding query |
AkiraK | 0:f26c02be48f9 | 68 | char resResult[256]; //The array is for holding response result |
AkiraK | 0:f26c02be48f9 | 69 | char delim; //delimiter |
AkiraK | 0:f26c02be48f9 | 70 | strcpy(reqUrl, basedUrl); //based Url is copied to reqUrl |
AkiraK | 0:f26c02be48f9 | 71 | strcat(reqUrl, deviceId); //Concatenate reqUrl and deviceId |
AkiraK | 0:f26c02be48f9 | 72 | for(int i = 0; i < 3; i++) { //Create request URL |
AkiraK | 0:f26c02be48f9 | 73 | if(i == 0) delim = '?'; //first query is connected with ? |
AkiraK | 0:f26c02be48f9 | 74 | else delim = '&'; //After second, query is connected with & |
AkiraK | 2:5e41a339e05c | 75 | sprintf(query, "%c%s=%.1f",delim, channelId[i], data[i]); //Create Query String |
AkiraK | 0:f26c02be48f9 | 76 | strcat(reqUrl, query); //Concatenate reqUrl and query |
AkiraK | 0:f26c02be48f9 | 77 | } |
AkiraK | 0:f26c02be48f9 | 78 | printf("%s\n", reqUrl); |
AkiraK | 3:d0d34327ea79 | 79 | 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 | 80 | //printf("%d\n", res); |
AkiraK | 1:3f52b4da889a | 81 | //printf("%d\n", http.getHTTPResponseCode()); |
AkiraK | 0:f26c02be48f9 | 82 | if(!res) { //If session is completed |
AkiraK | 3:d0d34327ea79 | 83 | //eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 84 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 85 | return 0; //Return 0 |
AkiraK | 0:f26c02be48f9 | 86 | } else { //If session is not completed |
AkiraK | 3:d0d34327ea79 | 87 | //eth.disconnect(); //EthernetInterface closed |
AkiraK | 0:f26c02be48f9 | 88 | printf("disconnected\n"); |
AkiraK | 0:f26c02be48f9 | 89 | return 1; //Return 1 |
AkiraK | 0:f26c02be48f9 | 90 | } |
AkiraK | 0:f26c02be48f9 | 91 | } |