├── makefile ├── ThingSpeakLinux.h ├── main.c ├── README.md └── ThingSpeakLinux.c /makefile: -------------------------------------------------------------------------------- 1 | ThingSpeakC: ThingSpeakLinux.c main.c 2 | gcc ThingSpeakLinux.c main.c -o ThingSpeakC -------------------------------------------------------------------------------- /ThingSpeakLinux.h: -------------------------------------------------------------------------------- 1 | /* 2 | Module: Header - Communication with ThingSpeak (on Linux) via TCP Socket 3 | Author: Pedro Bertoleti 4 | Date: 05/2016 5 | */ 6 | 7 | //defines 8 | #define THINGSPEAK_OFFLINE_ERROR 0 9 | #define THINGSPEAK_CONNECTION_ERROR 1 10 | #define OPEN_SOCKET_ERROR 2 11 | #define PARAMS_ERROR 3 12 | #define SEND_OK 4 13 | 14 | //global prototypes 15 | char SendDataToThingSpeak(int FieldNo, float * FieldArray, char * Key, int SizeOfKey); 16 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* 2 | Example of using ThingSpeak on Linux 3 | Author: Pedro Bertoleti 4 | Date: 05/2016 5 | */ 6 | 7 | #include 8 | #include 9 | #include "ThingSpeakLinux.h" 10 | 11 | //Test channel: https://thingspeak.com/channels/118265 12 | //Key used for test channel: E4AKR731LNBQ67EW 13 | 14 | int main (int argc, char *argv[]) 15 | { 16 | float FieldsArray[argc-1]; 17 | int i; 18 | 19 | //arguments: 20 | //argv's index - description 21 | //0 not used 22 | //1 key of a ThingSpeak's channel (which you desire to send data) 23 | //2 ... data to be sent to ThingSpeak's channel 24 | // 25 | //IMPORTANT: here, all data sent must be integer type numbers (this api does not support other types) 26 | 27 | 28 | //copying data arguments 29 | for(i=2;i 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "ThingSpeakLinux.h" 17 | 18 | //defines 19 | #define URL_THINGSPEAK "api.thingspeak.com" 20 | #define PORT_THINGSPEAK 80 21 | #define BEGIN_OF_HTTP_REQ "GET /update?key=" 22 | #define END_OF_HTTP_REQ "\r\n\r\n" 23 | #define MAX_SIZE 9999 24 | 25 | //local variables 26 | 27 | //local prototypes 28 | 29 | //implementation 30 | 31 | //Function: Send some data to a ThingSpeak's channel 32 | //Parameters: - Number of fields (data to be sent) 33 | // - Array of integer values (all data that must be sent) 34 | // - ThingSpeaks's channel Key 35 | // - Size of ThingSpeak's channel key 36 | //Return: - 37 | char SendDataToThingSpeak(int FieldNo, float * FieldArray, char * Key, int SizeOfKey) 38 | { 39 | int sockfd, portno, n; 40 | struct sockaddr_in serv_addr; 41 | struct hostent *ServerTCP; 42 | int ReqStringSize; 43 | int i; 44 | char ReqString[MAX_SIZE]; 45 | char BeginOfHTTPReq[]=BEGIN_OF_HTTP_REQ; 46 | char EndOfHTTPReq[]=END_OF_HTTP_REQ; 47 | char *ptReqString; 48 | 49 | if (FieldNo <=0) 50 | return PARAMS_ERROR; 51 | 52 | //Setting up HTTP Req. string: 53 | bzero(&ReqString,sizeof(ReqString)); 54 | sprintf(ReqString,"%s%s",BeginOfHTTPReq,Key); 55 | 56 | ptReqString = &ReqString[0]+(int)strlen(ReqString); 57 | for(i=1; i<= FieldNo; i++) 58 | { 59 | sprintf(ptReqString,"&field%d=%.2f",i,FieldArray[i-1]); 60 | ptReqString = &ReqString[0]+(int)strlen(ReqString); 61 | } 62 | 63 | sprintf(ptReqString,"%s",EndOfHTTPReq); 64 | printf("%s",EndOfHTTPReq); 65 | //Connecting to ThingSpeak and sending data: 66 | portno = PORT_THINGSPEAK; 67 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 68 | 69 | //Step 1: opening a socket 70 | if (sockfd < 0) 71 | return OPEN_SOCKET_ERROR; 72 | 73 | //Step 2: check if ThingSpeak is online 74 | ServerTCP = gethostbyname(URL_THINGSPEAK); 75 | if (ServerTCP == NULL) 76 | return THINGSPEAK_OFFLINE_ERROR; 77 | 78 | //Step 3: setting up TCP/IP socket structure 79 | bzero((char *) &serv_addr, sizeof(serv_addr)); 80 | serv_addr.sin_family = AF_INET; 81 | bcopy((char *)ServerTCP->h_addr, 82 | (char *)&serv_addr.sin_addr.s_addr, 83 | ServerTCP->h_length); 84 | serv_addr.sin_port = htons(portno); 85 | 86 | //Step 4: connecting to ThingSpeak server (via HTTP port / port no. 80) 87 | if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 88 | return THINGSPEAK_CONNECTION_ERROR; 89 | 90 | //Step 5: sending data to ThingSpeak's channel 91 | write(sockfd,ReqString,strlen(ReqString)); 92 | 93 | //Step 6: close TCP connection 94 | close(sockfd); 95 | 96 | //All done! 97 | return SEND_OK; 98 | } 99 | 100 | --------------------------------------------------------------------------------