This library provides data update method to Fastsensing.
Dependents: SCP1000_Fastsensing
Diff: Fastsensing.cpp
- Revision:
- 9:313790e8b56d
- Parent:
- 7:c29803218447
- Child:
- 12:fee4921dd28a
--- a/Fastsensing.cpp Fri May 19 01:09:19 2017 +0000 +++ b/Fastsensing.cpp Sun Jul 02 08:33:44 2017 +0000 @@ -24,68 +24,120 @@ #include "Fastsensing.h" -void Fastsensing::ethConnect() { - eth.init(); //Use DHCP. Initialize setting of eth. - eth.connect(100); //Connect Bring the interface up. -} +int Fastsensing::updateData(char *deviceId, char *channelId, double data, int display) +{ + char basedHead[9] = "GET /d1/"; //Query base header + char basedFoot[36] = " HTTP/1.1\r\nHost: f-io.net\r\n\r\n"; //Query base Footer + char req[256]; //req data array + char query[128]; //query array + + //Error handling about deviceID length + if(strlen(deviceId) != 16) { + printf("Error1 : Device ID length is not correctly.\n"); + return -1; + } + + //Error handling about deviceID length + if(strlen(channelId) != 8) { + printf("Error1 : Device ID length is not correctly.\n"); + return -2; + } + + //printf("Fastsensing update data\n"); + eth.connect(); //Connect bring the interface up + const char *ip = eth.get_ip_address(); //Get my IP address + //printf("My IP Address is %s\n", ip ? ip: "No IP"); -void Fastsensing::ethDisconnect() { - eth.disconnect(); //EthernetInterface closed + //Open Socket + TCPSocket socket; //Create a socket on a network interface + socket.open(ð); //Create a network socket + socket.connect("f-io.net", 80); //Connect remote host + + //Create query + strcpy(req, basedHead); //based Url is copied to req + strcat(req, deviceId); //Concatenate req and deviceId + sprintf(query, "?%s=%f", channelId, data); //Create Query String + //printf("query : %s\n", query); + + strcat(req, query); //Concatenate req and query + strcat(req, basedFoot); //Concatenate req and basedFoot + int scount = socket.send(req, sizeof req); //Count + //printf("sent %d [%.*s]\n", scount, strstr(req, "\r\n")-req, req); //Send time and Send data display + if(display == 1) printf("Sent [%.*s]\n", strstr(req, "\r\n")-req, req); //Send time and Send data display + + // Recieve response + char rbuffer[64]; + int rcount = socket.recv(rbuffer, sizeof rbuffer); //Recieve count + //printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); //Display Recieve data + if(display == 1) printf("Recieve [%.*s]\n", strstr(rbuffer, "\r\n")-rbuffer, rbuffer); //Display Recieve data + + // Close the socket + socket.close(); + + eth.disconnect(); //EthernetInterface disconnected + if(display == 1) printf("Data Update Process all completed\n"); + + return 0; } -int Fastsensing::updateData(char *deviceId, char *channelId, double data) +int Fastsensing::updateDataAll(char *deviceId, char *channelId[3], float data[3], int display) { - //eth.init(); //Use DHCP. Initialize setting of eth. - //eth.connect(); //Connect Bring the interface up. - char reqUrl[1000]; //Char array for request - char basedUrl[20] = "http://f-io.net/d1/"; //based http url - char query[256]; //The array is for holding query - char resResult[256]; //The array is for holding response result - strcpy(reqUrl, basedUrl); //based Url is copied to reqUrl - strcat(reqUrl, deviceId); //Concatenate reqUrl and deviceId - sprintf(query, "?%s=%f", channelId, data); //Create Query String - strcat(reqUrl, query); //Concatenate reqUrl and query - int res = http.get(reqUrl, resResult, 128);//Send request is reqUrl as get method and get Response Result. Time out is 5000 - printf("%d\n", res); - if(!res) { //If session is completed - //eth.disconnect(); //EthernetInterface closed - printf("disconnected\n"); - return 0; //Return 0 - } else { //If session is not completed - //eth.disconnect(); //EthernetInterface closed - printf("disconnected\n"); - return 1; //Return 1 + char basedHead[9] = "GET /d1/"; //query base header + char basedFoot[36] = " HTTP/1.1\r\nHost: f-io.net\r\n\r\n"; //query base footer + char delim; //delimiter + char req[512]; //request array + char query[128]; //query array + + //Error handling about deviceID length + if(strlen(deviceId) != 16) { + printf("Error1 : Device ID length is not correctly.\n"); + return -1; } -} + + //Error handling about channelID length + for(int a = 0; a < 3; a++) { + if(strlen(channelId[a]) != 8) { + printf("Error1 : Device ID length is not correctly.\n"); + return -2; + } + } -int Fastsensing::updateDataAll(char *deviceId, char *channelId[3], float data[3]) -{ - //eth.init(); //Use DHCP. Initialize setting of eth. - //eth.connect(); //Connect Bring the interface up. - char reqUrl[1000]; //Char array for request - char basedUrl[20] = "http://f-io.net/D1/"; //based http url - char query[256]; //The array is for holding query - char resResult[256]; //The array is for holding response result - char delim; //delimiter - strcpy(reqUrl, basedUrl); //based Url is copied to reqUrl - strcat(reqUrl, deviceId); //Concatenate reqUrl and deviceId + //printf("Fastsensing update all channel data\n"); + eth.connect(); //Connect bring the interface up + const char *ip = eth.get_ip_address(); //Get my IP address + //printf("My IP Address is %s\n", ip ? ip: "No IP"); + + //Open Socket + TCPSocket socket; //Create a socket on a network interface + socket.open(ð); //Create a network socket with ip address(stack) + socket.connect("f-io.net", 80); //Connect remote host + + //Create query + strcpy(req, basedHead); //based Url is copied to req + strcat(req, deviceId); //Concatenate req and deviceId for(int i = 0; i < 3; i++) { //Create request URL if(i == 0) delim = '?'; //first query is connected with ? else delim = '&'; //After second, query is connected with & sprintf(query, "%c%s=%.1f",delim, channelId[i], data[i]); //Create Query String - strcat(reqUrl, query); //Concatenate reqUrl and query + strcat(req, query); //Concatenate req and query } - printf("%s\n", reqUrl); - int res = http.get(reqUrl, resResult, 128); //Send request is reqUrl as get method and get Response Result. Time out is 5000 - //printf("%d\n", res); - //printf("%d\n", http.getHTTPResponseCode()); - if(!res) { //If session is completed - //eth.disconnect(); //EthernetInterface closed - printf("disconnected\n"); - return 0; //Return 0 - } else { //If session is not completed - //eth.disconnect(); //EthernetInterface closed - printf("disconnected\n"); - return 1; //Return 1 - } + strcat(req, basedFoot); //Concatenate req and basedFoot + //printf("%s\n", req); + int scount = socket.send(req, sizeof req); //Count + //printf("sent %d [%.*s]\n", scount, strstr(req, "\r\n")-req, req); //Send time and Send data display + if(display == 1) printf("Sent [%.*s]\n", strstr(req, "\r\n")-req, req); //Send time and Send data display + + // Recieve response + char rbuffer[64]; + int rcount = socket.recv(rbuffer, sizeof rbuffer); //Recieve count + //printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); //Display Recieve data + if(display == 1) printf("Recieve [%.*s]\n", strstr(rbuffer, "\r\n")-rbuffer, rbuffer); //Display Recieve data + + // Close the socket + socket.close(); + + eth.disconnect(); //EthernetInterface disconnected + if(display == 1) printf("Data Update Process all completed\n"); + + return 0; } \ No newline at end of file