This library provides data update method to Fastsensing.

Dependents:   SCP1000_Fastsensing

Committer:
AkiraK
Date:
Wed Jul 12 10:11:36 2017 +0000
Revision:
12:fee4921dd28a
Parent:
9:313790e8b56d
Channel ID length error code is fixed.; Add display TCPSocket.open error code.

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