Quantcast
Channel: ThingSpeak IoT Community - All Forums
Viewing all articles
Browse latest Browse all 1833

jvasudev on Receiving Data from IOT to esp8266 using PSoC 4200 BLE

$
0
0

Hi,

 

I am not sure what you are looking for. If you are just looking for the format of the GET request t fetch a channel feed, Vinod's answer should help you. For more documentation on this you can visit: GET a Channel Feed, Get a Channel Field Feed. Else, if you are looking for a generic C program and not an Arduino sketch to send a GET request to fetch data from a channel field feed, you can use the following program as a starting point. 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>

void error(const char *msg) { perror(msg); exit(0); }

int main(int argc, char** argv)

{

/* Send data to ThingSpeak*/
int portno = 80;
char *host = "api.thingspeak.com";

struct hostent *server;
struct sockaddr_in serv_addr;
int sockfd, bytes;

/* Message to send to send to server. Note change the channel number, field number and read API key to that of your channel */
char *message = "GET /channels/<channelnumber>/fields/<fieldnumber>.json?api_key=<readAPIkey>&results=1 HTTP/1.0

";

//Buffer to receive the response
char response[4096];

// Create the socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");

// Look for the ip address
server = gethostbyname(host);
if (server == NULL) error("ERROR, no such host");

// Set the struct settings
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);

// Connect the socket to the server
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");

// Send the request
bytes = write(sockfd, message, strlen(message));

// Receive the response
bytes = read(sockfd, response, 4096);

//close the socket
close(sockfd);

// View the response
printf("Response:
%s
", response);
return 0;
}

 

The C program shows how to create a socket to connect to ThingSpeak, lookup the IP address for ThingSpeak, connect the socket to ThingSpeak, send the GET request, receive the channel field data and close the socket. You can make this more complicated by checking the number of bytes read and reading more if the buffer was full but this is just a simple and direct way. Hope this helps.


Viewing all articles
Browse latest Browse all 1833

Trending Articles