This library provides data update method to Fastsensing.

Dependents:   SCP1000_Fastsensing

Committer:
AkiraK
Date:
Sun Jul 02 08:33:44 2017 +0000
Revision:
9:313790e8b56d
Parent:
7:c29803218447
Child:
12:fee4921dd28a
Resolve update time bugs. However this library is stable on mbedos under 5.3.

Who changed what in which revision?

UserRevisionLine numberNew 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 9:313790e8b56d 27 int Fastsensing::updateData(char *deviceId, char *channelId, double data, int display)
AkiraK 9:313790e8b56d 28 {
AkiraK 9:313790e8b56d 29 char basedHead[9] = "GET /d1/"; //Query base header
AkiraK 9:313790e8b56d 30 char basedFoot[36] = " HTTP/1.1\r\nHost: f-io.net\r\n\r\n"; //Query base Footer
AkiraK 9:313790e8b56d 31 char req[256]; //req data array
AkiraK 9:313790e8b56d 32 char query[128]; //query array
AkiraK 9:313790e8b56d 33
AkiraK 9:313790e8b56d 34 //Error handling about deviceID length
AkiraK 9:313790e8b56d 35 if(strlen(deviceId) != 16) {
AkiraK 9:313790e8b56d 36 printf("Error1 : Device ID length is not correctly.\n");
AkiraK 9:313790e8b56d 37 return -1;
AkiraK 9:313790e8b56d 38 }
AkiraK 9:313790e8b56d 39
AkiraK 9:313790e8b56d 40 //Error handling about deviceID length
AkiraK 9:313790e8b56d 41 if(strlen(channelId) != 8) {
AkiraK 9:313790e8b56d 42 printf("Error1 : Device ID length is not correctly.\n");
AkiraK 9:313790e8b56d 43 return -2;
AkiraK 9:313790e8b56d 44 }
AkiraK 9:313790e8b56d 45
AkiraK 9:313790e8b56d 46 //printf("Fastsensing update data\n");
AkiraK 9:313790e8b56d 47 eth.connect(); //Connect bring the interface up
AkiraK 9:313790e8b56d 48 const char *ip = eth.get_ip_address(); //Get my IP address
AkiraK 9:313790e8b56d 49 //printf("My IP Address is %s\n", ip ? ip: "No IP");
AkiraK 3:d0d34327ea79 50
AkiraK 9:313790e8b56d 51 //Open Socket
AkiraK 9:313790e8b56d 52 TCPSocket socket; //Create a socket on a network interface
AkiraK 9:313790e8b56d 53 socket.open(&eth); //Create a network socket
AkiraK 9:313790e8b56d 54 socket.connect("f-io.net", 80); //Connect remote host
AkiraK 9:313790e8b56d 55
AkiraK 9:313790e8b56d 56 //Create query
AkiraK 9:313790e8b56d 57 strcpy(req, basedHead); //based Url is copied to req
AkiraK 9:313790e8b56d 58 strcat(req, deviceId); //Concatenate req and deviceId
AkiraK 9:313790e8b56d 59 sprintf(query, "?%s=%f", channelId, data); //Create Query String
AkiraK 9:313790e8b56d 60 //printf("query : %s\n", query);
AkiraK 9:313790e8b56d 61
AkiraK 9:313790e8b56d 62 strcat(req, query); //Concatenate req and query
AkiraK 9:313790e8b56d 63 strcat(req, basedFoot); //Concatenate req and basedFoot
AkiraK 9:313790e8b56d 64 int scount = socket.send(req, sizeof req); //Count
AkiraK 9:313790e8b56d 65 //printf("sent %d [%.*s]\n", scount, strstr(req, "\r\n")-req, req); //Send time and Send data display
AkiraK 9:313790e8b56d 66 if(display == 1) printf("Sent [%.*s]\n", strstr(req, "\r\n")-req, req); //Send time and Send data display
AkiraK 9:313790e8b56d 67
AkiraK 9:313790e8b56d 68 // Recieve response
AkiraK 9:313790e8b56d 69 char rbuffer[64];
AkiraK 9:313790e8b56d 70 int rcount = socket.recv(rbuffer, sizeof rbuffer); //Recieve count
AkiraK 9:313790e8b56d 71 //printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); //Display Recieve data
AkiraK 9:313790e8b56d 72 if(display == 1) printf("Recieve [%.*s]\n", strstr(rbuffer, "\r\n")-rbuffer, rbuffer); //Display Recieve data
AkiraK 9:313790e8b56d 73
AkiraK 9:313790e8b56d 74 // Close the socket
AkiraK 9:313790e8b56d 75 socket.close();
AkiraK 9:313790e8b56d 76
AkiraK 9:313790e8b56d 77 eth.disconnect(); //EthernetInterface disconnected
AkiraK 9:313790e8b56d 78 if(display == 1) printf("Data Update Process all completed\n");
AkiraK 9:313790e8b56d 79
AkiraK 9:313790e8b56d 80 return 0;
AkiraK 3:d0d34327ea79 81 }
AkiraK 3:d0d34327ea79 82
AkiraK 9:313790e8b56d 83 int Fastsensing::updateDataAll(char *deviceId, char *channelId[3], float data[3], int display)
AkiraK 0:f26c02be48f9 84 {
AkiraK 9:313790e8b56d 85 char basedHead[9] = "GET /d1/"; //query base header
AkiraK 9:313790e8b56d 86 char basedFoot[36] = " HTTP/1.1\r\nHost: f-io.net\r\n\r\n"; //query base footer
AkiraK 9:313790e8b56d 87 char delim; //delimiter
AkiraK 9:313790e8b56d 88 char req[512]; //request array
AkiraK 9:313790e8b56d 89 char query[128]; //query array
AkiraK 9:313790e8b56d 90
AkiraK 9:313790e8b56d 91 //Error handling about deviceID length
AkiraK 9:313790e8b56d 92 if(strlen(deviceId) != 16) {
AkiraK 9:313790e8b56d 93 printf("Error1 : Device ID length is not correctly.\n");
AkiraK 9:313790e8b56d 94 return -1;
AkiraK 0:f26c02be48f9 95 }
AkiraK 9:313790e8b56d 96
AkiraK 9:313790e8b56d 97 //Error handling about channelID length
AkiraK 9:313790e8b56d 98 for(int a = 0; a < 3; a++) {
AkiraK 9:313790e8b56d 99 if(strlen(channelId[a]) != 8) {
AkiraK 9:313790e8b56d 100 printf("Error1 : Device ID length is not correctly.\n");
AkiraK 9:313790e8b56d 101 return -2;
AkiraK 9:313790e8b56d 102 }
AkiraK 9:313790e8b56d 103 }
AkiraK 0:f26c02be48f9 104
AkiraK 9:313790e8b56d 105 //printf("Fastsensing update all channel data\n");
AkiraK 9:313790e8b56d 106 eth.connect(); //Connect bring the interface up
AkiraK 9:313790e8b56d 107 const char *ip = eth.get_ip_address(); //Get my IP address
AkiraK 9:313790e8b56d 108 //printf("My IP Address is %s\n", ip ? ip: "No IP");
AkiraK 9:313790e8b56d 109
AkiraK 9:313790e8b56d 110 //Open Socket
AkiraK 9:313790e8b56d 111 TCPSocket socket; //Create a socket on a network interface
AkiraK 9:313790e8b56d 112 socket.open(&eth); //Create a network socket with ip address(stack)
AkiraK 9:313790e8b56d 113 socket.connect("f-io.net", 80); //Connect remote host
AkiraK 9:313790e8b56d 114
AkiraK 9:313790e8b56d 115 //Create query
AkiraK 9:313790e8b56d 116 strcpy(req, basedHead); //based Url is copied to req
AkiraK 9:313790e8b56d 117 strcat(req, deviceId); //Concatenate req and deviceId
AkiraK 0:f26c02be48f9 118 for(int i = 0; i < 3; i++) { //Create request URL
AkiraK 0:f26c02be48f9 119 if(i == 0) delim = '?'; //first query is connected with ?
AkiraK 0:f26c02be48f9 120 else delim = '&'; //After second, query is connected with &
AkiraK 2:5e41a339e05c 121 sprintf(query, "%c%s=%.1f",delim, channelId[i], data[i]); //Create Query String
AkiraK 9:313790e8b56d 122 strcat(req, query); //Concatenate req and query
AkiraK 0:f26c02be48f9 123 }
AkiraK 9:313790e8b56d 124 strcat(req, basedFoot); //Concatenate req and basedFoot
AkiraK 9:313790e8b56d 125 //printf("%s\n", req);
AkiraK 9:313790e8b56d 126 int scount = socket.send(req, sizeof req); //Count
AkiraK 9:313790e8b56d 127 //printf("sent %d [%.*s]\n", scount, strstr(req, "\r\n")-req, req); //Send time and Send data display
AkiraK 9:313790e8b56d 128 if(display == 1) printf("Sent [%.*s]\n", strstr(req, "\r\n")-req, req); //Send time and Send data display
AkiraK 9:313790e8b56d 129
AkiraK 9:313790e8b56d 130 // Recieve response
AkiraK 9:313790e8b56d 131 char rbuffer[64];
AkiraK 9:313790e8b56d 132 int rcount = socket.recv(rbuffer, sizeof rbuffer); //Recieve count
AkiraK 9:313790e8b56d 133 //printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); //Display Recieve data
AkiraK 9:313790e8b56d 134 if(display == 1) printf("Recieve [%.*s]\n", strstr(rbuffer, "\r\n")-rbuffer, rbuffer); //Display Recieve data
AkiraK 9:313790e8b56d 135
AkiraK 9:313790e8b56d 136 // Close the socket
AkiraK 9:313790e8b56d 137 socket.close();
AkiraK 9:313790e8b56d 138
AkiraK 9:313790e8b56d 139 eth.disconnect(); //EthernetInterface disconnected
AkiraK 9:313790e8b56d 140 if(display == 1) printf("Data Update Process all completed\n");
AkiraK 9:313790e8b56d 141
AkiraK 9:313790e8b56d 142 return 0;
AkiraK 0:f26c02be48f9 143 }