first versione of ThingSpeak IOT library (not tested)
Revision 0:92f5363c7790, committed 2014-04-04
- Comitter:
- mbedakhela
- Date:
- Fri Apr 04 13:44:43 2014 +0000
- Commit message:
- first version of thingspeak IOT library; not tested
Changed in this revision
diff -r 000000000000 -r 92f5363c7790 parse.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parse.cpp Fri Apr 04 13:44:43 2014 +0000 @@ -0,0 +1,367 @@ +/* ************************************************************************** + * + * + * + * + * ************************************************************************** + * FileName: parse.c + * Dependencies: + * Module: + * Compiler: + * + * Author Rev. Date Comment + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Stefano Lai 1.0 03/27/2014 First release + * + * + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * Software License Agreement + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License (version 2) as published by + * the Free Software Foundation AND MODIFIED BY OpenPicus team. + * + * ***NOTE*** The exception to the GPL is included to allow you to distribute + * a combined work that includes OpenPicus code without being obliged to + * provide the source code for proprietary components outside of the OpenPicus + * code. + * OpenPicus software is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * + * Warranty + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT + * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT + * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF + * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS + * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE + * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER + * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT + * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. + * + **************************************************************************/ + +#include "mbed.h" +#include "parse.h" +#include "TCPSocketConnection.h" +#include <string> +//#include "string.h" +//#include "HWlib.h" + + /*------------------------------------------------------------------------------------- + | Function: dynamicPARSE(char *tab[][5], int rows) | + | Description: Function to parse strings. | + | Returns: - | + | Parameters: char *tab[][5] - pointer to bidimensional array | + | | + | tab[0][0] => type of parse (use denfine) | + | - BETWEEN | + | - BEFORE | + | - AFTER | + | tab[0][1] => string/token where to start | + | tab[0][2] => string/token where to stop | + | tab[0][3] => destination string | + | tab[0][4] => source string | + | | + | int rows - number of the array rows | + -------------------------------------------------------------------------------------*/ + +void PARSE::dynamicPARSE(char *tab[][5], int rows) +{ + int i = 0; + + for(i=0; i<rows; i++) + { + if ( !(strcmp(tab[i][0],"BETWEEN")) ) + { + PARSEbetween( tab[i][4], tab[i][1], tab[i][2], tab[i][3]); + } + + else if ( !(strcmp(tab[i][0],"BEFORE")) ) + { + PARSEbefore( tab[i][4], tab[i][2], tab[i][3]); + } + + else //after + { + PARSEafter( tab[i][4], tab[i][1], tab[i][3]); + } + } +} + + /*------------------------------------------------------------------------------------------------------- + | Function: HTTPdynamicPARSE(char host[], char command[], char *tab[][5], int rows, int snifftime)| + | Description: Function to parse strings with built-in TCPClient. | + | Returns: - | + | Parameters: char host[] - IP or Website address to which want to connect | + | | + | char command[] - command to be sent through the TCPClient | + | | + | char *tab[][5] - pointer to bidimensional array | + | | + | tab[0][0] => type of parse (use denfine) | + | - BETWEEN | + | - BEFORE | + | - AFTER | + | tab[0][1] => string/token where to start | + | tab[0][2] => string/token where to stop | + | tab[0][3] => destination string | + | tab[0][4] => option destination string (use denfine) | + | - YES -> destination string = buffer | + | - NO -> destination string = tab[0][3] | + | | + | int rows - number of the array rows | + | | + | int snifftime - time in 10 microseconds to accumulate more characters | + -------------------------------------------------------------------------------------------------------*/ + +void PARSE::HTTPdynamicPARSE(char host[], char command[], char *tab[][5], int rows, int snifftime) +{ + //TCP_SOCKET Socket; +// int flag = 0; +TCPSocketConnection ThingSock; + int size; + int i = 0; + + //Socket=TCPClientOpen ( host, "80"); + //while((Socket==INVALID_SOCKET)); + //while(!TCPisConn(Socket)); + + //TCPWrite ( Socket, command, strlen(command) ); + //vTaskDelay(snifftime); + /////////////////////////////////// + ThingSock.connect(host, 80); + + while(!ThingSock.is_connected()); + ThingSock.send_all(command, (int)strlen(command)); + + + + /* + while( flag==0 ) + { + + size = TCPRxLen ( Socket ); + + if((int)size<=0) + { + i++; + if(i==10) + break; + } + else + flag = 1; + } + + if(flag==1) + { +*/ + char buffer[(int)size]; + + //TCPRead ( Socket, buffer, (int)size ); + //vTaskDelay(200); + //TCPClientClose ( Socket ); + + ThingSock.receive_all(buffer, (int)size); + ThingSock.close(); + + + + + for(i=0; i<rows; i++) + { + if ( !(strcmp(tab[i][0],"BETWEEN")) ) + { + + if ( !(strcmp(tab[i][4],"YES")) ) + PARSEbetween( buffer, tab[i][1], tab[i][2], buffer); + else + PARSEbetween( buffer, tab[i][1], tab[i][2], tab[i][3]); + + } + + else if ( !(strcmp(tab[i][0],"BEFORE")) ) + { + + if ( !(strcmp(tab[i][4],"YES")) ) + PARSEbefore( buffer, tab[i][2], buffer); + else + PARSEbefore( buffer, tab[i][2], tab[i][3]); + } + + else //after + { + + if ( !(strcmp(tab[i][4],"YES")) ) + PARSEafter( buffer, tab[i][1], buffer); + else + PARSEafter( buffer, tab[i][1], tab[i][3]); + + } + } +// } +} + + /*------------------------------------------------------------------------------------------------------- + | Function: multiPARSEbetween(char str[], char start[], char stop[], char *substr[], int multi) | + | Description: Function to use more PARSEbetween in a string. | + | Returns: - | + | Parameters: char str[] - source string | + | | + | char start[] - string/token where to start | + | | + | char stop[] - string/token where to stop | + | | + | char *substr[] - pointer to destination string | + | | + | int multi - number of executions of the PARSEbetween function | + -------------------------------------------------------------------------------------------------------*/ + +void PARSE::multiPARSEbetween(char str[], char start[], char stop[], char *substr[], int multi) +{ + int i; + char *pf; + char *pi; + int pd; + int c; + int j; + + int sizestart = strlen(start); + + pi=strstr(str,start); + + + for( j=0; j<multi; j++) + { + + pf=strstr(pi+sizestart,stop); + pd=pf-pi; + c=pd-sizestart; + + for(i=0;i<c;i++) + { + *(substr[j]+i)=*(pi+sizestart+i); + } + *(substr[j]+i)='\0'; + pi=strstr(pi+sizestart,start); + + } +} + + /*------------------------------------------------------------------------------------------------------- + | Function: PARSEafter(char str[], char start[], char substr[]) | + | Description: Function to parse a string and rewrite it from start token to the end. | + | Returns: - | + | Parameters: char str[] - source string | + | | + | char start[] - string/token where to start | + | | + | char substr[] - destination string | + -------------------------------------------------------------------------------------------------------*/ + +void PARSE::PARSEafter(char str[], char start[], char substr[]) +{ + int i; + int sizestart; + int sizestr; + char *ptrsub; + char *ptrstr; + char *pi; + int c; + + ptrsub=substr; + sizestart=strlen(start); + sizestr=strlen(str); + ptrstr=str; + + pi=strstr(str,start); + c=sizestr-(int)pi+(int)ptrstr-sizestart+1; + + for(i=0;i<c;i++) + { + *(ptrsub+i)=*(pi+sizestart+i); + } + *(ptrsub+i)='\0'; +} + + /*------------------------------------------------------------------------------------------------------- + | Function: PARSEbefore(char str[], char stop[], char substr[]) | + | Description: Function to parse a string and rewrite it from start to the stop token. | + | Returns: - | + | Parameters: char str[] - source string | + | | + | char stop[] - string/token where to stop | + | | + | char substr[] - destination string | + -------------------------------------------------------------------------------------------------------*/ + +void PARSE::PARSEbefore(char str[], char stop[], char substr[]) +{ + int i; + char *pf; + char *ptrsub; + char *ptrstr; + int c; + + ptrsub=substr; + ptrstr=str; + + pf=strstr(str,stop); + c=(int)pf-(int)ptrstr; + + + for(i=0;i<c;i++) + { + *(ptrsub+i)=*(ptrstr+i); + } + *(ptrsub+i)='\0'; + +} + + /*------------------------------------------------------------------------------------------------------- + | Function: PARSEbetween(char str[], char start[], char stop[], char substr[]) | + | Description: Function to parse a string and rewrite it from start token to the stop token. | + | Returns: - | + | Parameters: char str[] - source string | + | | + | char start[] - string/token where to start | + | | + | char stop[] - string/token where to stop | + | | + | char substr[] - destination string | + -------------------------------------------------------------------------------------------------------*/ + +void PARSE::PARSEbetween(char str[], char start[], char stop[], char substr[]) +{ + int i; + int sizestart; + char *pf; + char *ptrsub; + char *pi; + int pd; + int c; + + ptrsub=substr; + sizestart=strlen(start); + + pi=strstr(str,start); + pf=strstr(pi+sizestart,stop); + + pd=pf-pi; + c=pd-sizestart; + + for(i=0;i<c;i++) + { + *(ptrsub+i)=*(pi+sizestart+i); + } + *(ptrsub+i)='\0'; + +}
diff -r 000000000000 -r 92f5363c7790 parse.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/parse.h Fri Apr 04 13:44:43 2014 +0000 @@ -0,0 +1,75 @@ +/* ************************************************************************** + * + * + * + * + * ************************************************************************** + * FileName: parse.h + * Dependencies: + * Module: + * Compiler: + * + * Author Rev. Date Comment + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Stefano Lai 1.0 03/27/2014 First release + * + * + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * Software License Agreement + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License (version 2) as published by + * the Free Software Foundation AND MODIFIED BY OpenPicus team. + * + * ***NOTE*** The exception to the GPL is included to allow you to distribute + * a combined work that includes OpenPicus code without being obliged to + * provide the source code for proprietary components outside of the OpenPicus + * code. + * OpenPicus software is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * + * Warranty + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT + * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT + * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF + * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS + * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE + * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER + * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT + * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. + * + **************************************************************************/ +#ifndef MBED_PARSE_H +#define MBED_PARSE_H + +#define BETWEEN "BETWEEN" +#define BEFORE "BEFORE" +#define AFTER "AFTER" + +#define YES "YES" +#define NO "NO" + + +class PARSE{ +public: + void dynamicPARSE(char *tab[][5], int rows); + void HTTPdynamicPARSE(char host[], char command[], char *tab[][5], int rows, int snifftime); + void multiPARSEbetween(char str[], char start[], char stop[], char *substr[], int multi); + void PARSEafter(char str[], char start[], char substr[]); + void PARSEbefore(char str[], char stop[], char substr[]); + void PARSEbetween(char str[], char start[], char stop[], char substr[]); + +private: + +}; + +#endif /*#define MBED_PARSE_H*/
diff -r 000000000000 -r 92f5363c7790 thingspeak.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thingspeak.cpp Fri Apr 04 13:44:43 2014 +0000 @@ -0,0 +1,361 @@ +/* ************************************************************************** + * + * + * + * + * ************************************************************************** + * FileName: thingspeak.c + * Dependencies: + * Module: + * Compiler: + * + * Author Rev. Date Comment + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Stefano Lai 1.0 03/27/2014 First release + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * Software License Agreement + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License (version 2) as published by + * the Free Software Foundation AND MODIFIED BY OpenPicus team. + * + * ***NOTE*** The exception to the GPL is included to allow you to distribute + * a combined work that includes OpenPicus code without being obliged to + * provide the source code for proprietary components outside of the OpenPicus + * code. + * OpenPicus software is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * + * Warranty + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT + * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT + * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF + * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS + * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE + * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER + * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT + * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. + * + **************************************************************************/ +#include "mbed.h" +#include "thingspeak.h" +#include "TCPSocketConnection.h" + +/**************************************************************************************** + * --- Global Parameters --- * + ***************************************************************************************/ + +//static TCP_SOCKET Socket; +//TCPSocketConnection ThingSock; + + + + +static int i; +static BOOL flagTCP=FALSE; +static BOOL flagTCPisCON=FALSE; +static int z = 0; +static int error = 0; + + /*------------------------------------------------------------------------------------- + | Function: WdataF(char* apikey, float value[], int nvalue) | + | Description: Function to write float data in a specific ThingSpeak database. | + | Returns: BYTE 0 - No error | + | 1 - TCPClientOpen error | + | Parameters: char* apikey - ThingSpeak write api key | + | | + | int* value - array with float data to send to ThingSpeak | + | | + | int nvalue - number of value elements | + -------------------------------------------------------------------------------------*/ + +int THINGSPEAK::wfTHINGSPEAK(char* apikey, float* value, int nvalue) +{ + error = 0; + char messageTS[nvalue*20]; + char bufferTS[20]; + TCPSocketConnection ThingSock; + flagTCP=0; + + for(i=0; i<nvalue;i++) + { + if(i==0) + { + sprintf(messageTS,"field%d=%2.1f", i+1, (double)value[i]); + } + else + { + sprintf(bufferTS,"&field%d=%2.1f", i+1, (double)value[i]); + strcat(messageTS,bufferTS); + } + + } + + char strPOST[195+(int)strlen(messageTS)]; + sprintf( strPOST, "POST /update HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\nX-THINGSPEAKAPIKEY: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s", apikey, (int)strlen(messageTS), messageTS); + + while(flagTCP==FALSE) + { + if(z==5) + { + z=0; + flagTCP=TRUE; + error=1; + } + else + { + //Socket=TCPClientOpen ( IPThingSpeak, "80"); + //vTaskDelay(25); + //flagTCP=TCPisConn(Socket); + //flagTCPisCON=flagTCP; + //vTaskDelay(25); + //z++; + ThingSock.connect(IPThingSpeak, 80); + //ThingSock.connect(IPThingSpeak, 80); + flagTCP=ThingSock.is_connected(); + flagTCPisCON=flagTCP; + z++; + } + } + + if(flagTCPisCON==TRUE) + { + //TCPWrite ( Socket, strPOST, (int)strlen(strPOST) ); + //vTaskDelay(200); + //TCPClientClose ( Socket ); + //flagTCPisCON=FALSE; + + ThingSock.send_all(strPOST, (int)strlen(strPOST)); + ThingSock.close(); + flagTCPisCON=FALSE; + } + + return error; +} + + /*------------------------------------------------------------------------------------- + | Function: WdataI(char* apikey, int* value, int nvalue) | + | Description: Function to write integer data in a specific ThingSpeak database. | + | Returns: BYTE 0 - No error | + | 1 - TCPClientOpen error | + | Parameters: char* apikey - ThingSpeak write api key | + | | + | int* value - array with integer data to send to ThingSpeak | + | | + | int nvalue - number of value elements | + -------------------------------------------------------------------------------------*/ + +int THINGSPEAK::wiTHINGSPEAK(char* apikey, int* value, int nvalue) +{ + error = 0; + char messageTS[nvalue*20]; + char bufferTS[20]; + TCPSocketConnection ThingSock; + flagTCP=0; + + for(i=0; i<nvalue;i++) + { + if(i==0) + { + sprintf(messageTS,"field%d=%d", i+1, value[i]); + } + else + { + sprintf(bufferTS,"&field%d=%d", i+1, value[i]); + strcat(messageTS,bufferTS); + } + + } + + char strPOST[195+(int)strlen(messageTS)]; + sprintf( strPOST, "POST /update HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\nX-THINGSPEAKAPIKEY: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s", apikey, (int)strlen(messageTS), messageTS); + + while(flagTCP==FALSE) + { + if(z==5) + { + z=0; + flagTCP=TRUE; + error=1; + } + else + { + //Socket=TCPClientOpen ( IPThingSpeak, "80"); + //vTaskDelay(25); + //flagTCP=TCPisConn(Socket); + //flagTCPisCON=flagTCP; + //vTaskDelay(25); + //z++; + ThingSock.connect(IPThingSpeak, 80); + flagTCP=ThingSock.is_connected(); + flagTCPisCON=flagTCP; + z++; + } + } + + if(flagTCPisCON==TRUE) + { + //TCPWrite ( Socket, strPOST, (int)strlen(strPOST) ); + //vTaskDelay(200); + //TCPClientClose ( Socket ); + //flagTCPisCON=FALSE; + ThingSock.send_all(strPOST, (int)strlen(strPOST)); + ThingSock.close(); + flagTCPisCON=FALSE; + } + + return error; +} + + /*------------------------------------------------------------------------------------- + | Function: thingHTTP(char* apikey, char* data) | + | Description: Function to use ThingHTTP APP. | + | Returns: BYTE 0 - No error | + | 1 - TCPClientOpen error | + | 2 - ThingSpeak response timeout | + | Parameters: char* apikey - ThingSpeak write api key | + | | + | char* data - pointer to the return data/string value from ThingSpeak| + -------------------------------------------------------------------------------------*/ + +int THINGSPEAK::thingHTTP(char* apikey, char* data) +{ + error = 0; + char strPOST[300]; + int size; + int flag = 0; + TCPSocketConnection ThingSock; + i=0; + flagTCP=0; + + sprintf( strPOST, "POST /apps/thinghttp/send_request HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\napi_key=%s",8+(int)strlen(apikey), apikey); + + while(flagTCP==FALSE) + { + if(z==5) + { + z=0; + flagTCP=TRUE; + error=1; + } + else + { + //Socket=TCPClientOpen ( IPThingSpeak, "80"); + //vTaskDelay(25); + //flagTCP=TCPisConn(Socket); + //flagTCPisCON=flagTCP; + //vTaskDelay(25); + //z++; + ThingSock.connect(IPThingSpeak, 80); + flagTCP=ThingSock.is_connected(); + flagTCPisCON=flagTCP; + z++; + } + } + + if(flagTCPisCON==TRUE) + { + //TCPWrite ( Socket, strPOST, (int)strlen(strPOST) ); + ThingSock.send_all(strPOST, (int)strlen(strPOST)); + while( flag==0 ) + { + // size = TCPRxLen ( Socket ); + + if((int)size<=0) + { + i++; + // vTaskDelay(5); + if(i==100) + { + error=2; + break; + } + } + else + flag = 1; + } + if(flag==1) + { + char bufferTS[(int)size]; + + //TCPRead ( Socket, bufferTS, (int)size ); + + //vTaskDelay(100); + ThingSock.receive_all(bufferTS, (int)size); + + strcpy(data,bufferTS); + } + //TCPClientClose ( Socket ); + ThingSock.close(); + flagTCPisCON=FALSE; + } + return error; +} + /*----------------------------------------------------------------------------------------- + | Function: thingTWEET(char* apikey, char* message) | + | Description: Function to use ThingTWEET APP. | + | Returns: BYTE 0 - No error | + | 1 - TCPClientOpen error | + | Parameters: char* apikey - ThingSpeak TWEET api key | + | | + | char* message - message to send on Twitter to change status, you can | + | add some tag in this way: | + | message&lat=(your latitude)&long=(your longitude) | + | for available tags see this | + | https://dev.twitter.com/docs/api/1/post/statuses/update | + -----------------------------------------------------------------------------------------*/ + +int THINGSPEAK::thingTWEET(char* apikey, char* message) +{ + error = 0; + char strPOST[400]; + TCPSocketConnection ThingSock; + flagTCP=0; + + sprintf( strPOST, "POST /apps/thingtweet/1/statuses/update HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\napi_key=%s&status=%s",8+(int)strlen(apikey)+(int)strlen(message), apikey, message); + + while(flagTCP==FALSE) + { + if(z==5) + { + z=0; + flagTCP=TRUE; + error=1; + } + else + { + //Socket=TCPClientOpen ( IPThingSpeak, "80"); + //vTaskDelay(25); + //flagTCP=TCPisConn(Socket); + //flagTCPisCON=flagTCP; + //vTaskDelay(25); + //z++; + ThingSock.connect(IPThingSpeak, 80); + flagTCP=ThingSock.is_connected(); + flagTCPisCON=flagTCP; + z++; + } + } + + if(flagTCPisCON==TRUE) + { + //TCPWrite ( Socket, strPOST, (int)strlen(strPOST) ); + //vTaskDelay(200); + //TCPClientClose ( Socket ); + //flagTCPisCON=FALSE; + ThingSock.send_all(strPOST, (int)strlen(strPOST)); + ThingSock.close(); + flagTCPisCON=FALSE; + } + + return error; +}
diff -r 000000000000 -r 92f5363c7790 thingspeak.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thingspeak.h Fri Apr 04 13:44:43 2014 +0000 @@ -0,0 +1,65 @@ +/* ************************************************************************** + * + * + * + * + * ************************************************************************** + * FileName: thingspeak.h + * Dependencies: + * Module: + * Compiler: + * + * Author Rev. Date Comment + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * Stefano Lai 1.0 03/27/2014 First release + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * + * Software License Agreement + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License (version 2) as published by + * the Free Software Foundation AND MODIFIED BY OpenPicus team. + * + * ***NOTE*** The exception to the GPL is included to allow you to distribute + * a combined work that includes OpenPicus code without being obliged to + * provide the source code for proprietary components outside of the OpenPicus + * code. + * OpenPicus software is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * + * Warranty + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT + * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT + * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * WE ARE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF + * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS + * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE + * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER + * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT + * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE. + * + **************************************************************************/ +#ifndef MBED_THINGSPEAK_H +#define MBED_THINGSPEAK_H + +#define TRUE 1 +#define FALSE 0 +#define IPThingSpeak "184.106.153.149" + +class THINGSPEAK{ +public: + int wfTHINGSPEAK(char* writeapikey, float* value, int nvalue); + int wiTHINGSPEAK(char* writeapikey, int* value, int nvalue); + int thingHTTP(char* apikey, char* data); + int thingTWEET(char* apikey, char* message); + +private: +}; +#endif /*#define MBED_THINGSPEAK_H*/ \ No newline at end of file