Processing HTTP response (ThingSpeak read last value)

I have a ThingSpeak channel, that is capturing a simple integer value in one field. The values might be 0, 1 or 2.

I only need the last known value from my channel, which I can get using the following url:

I’ve tried to use TCPClient to get this value, without success. I’ve followed the documentation but I don’t get back the single integer that I am expecting.

// Think speak channel information
String _channelID = "24587";

// TCP socket initialize
TCPClient _client;

// thingspeak IP
byte _ip[] = { 184, 106, 153, 149 };

void setup() {

void loop() {

    if (Spark.connected()){

        // fetch the last value from ThingSpeak


void ThingSpeakReadLastValue()
    Serial.println("...Connecting to Thingspeak");
    // Connecting and sending data to Thingspeak
    if(_client.connect(_ip, 80))
        Serial.println("...Connection succesful");
        // fetch the last value from ThingSpeak
        // GET /channels/24587/fields/1/last HTTP/1.1
        // Host:
        // Cache-Control: no-cache
        _client.println("GET /channels/" + _channelID + "/fields/1/last HTTP/1.1");
        _client.println("Content-Length: 0");
        // This delay is pivitol without it the TCP client will often close before the data is fully sent
        // read the values returned by the get
        Serial.println("Received value: ");
        char c =;

        Serial.println("Thingspeak value received.");
        // Failed to connect to Thingspeak
        Serial.println("Unable to connect to Thingspeak.");

Thanks in advance.

If the Server uses HTTP for communication please Note!
You are sending HEADERS to the Server (like Content-Length and HOST). You will also receive HEADERS from the Server. So the answer from the Server (on TCP Level) may be something like :

HTTP/1.1 200 OK
Connection: close
Date: Mon, 02 Feb 2015 14:14:27 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/xml; charset=iso-8859-1
Content-Length: 31

This is your first Real DATA

So to get the Answer from the Server you have to consume all the headers (until the first EMPTY HEADER) and then the answer starts!

Maybe another Problem: you are using HTTPS (at least in your Sample URL).
Is this URL available as plain HTTP?
The Spark is not HTTPS capable

I think the Server doesn’t support HTTP. You will only get this

HTTP/1.1 301 Moved Permanently
Server: nginx/1.7.5
Date: Mon, 02 Feb 2015 14:30:19 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive

as answer. :frowning:

1 Like

Thanks @softmeter.

Do you have any example on how to “consume the headers”?

I did just find this on their documentation:

HTTP Headers

If you would like to reduce the number of HTTP headers sent by our application, please add the parameter headers=false to any HTTP request.

API keys can optionally be sent via HTTP headers by setting the header name to THINGSPEAKAPIKEY and the header value to the API key.

I’ll try that tonight.


The ThingSpeak API does support HTTP (or Regular URL as they call it:
Can HTTP be forced from TCPClient?

(The HTTPS in my example is only a comment, I don’t specify to use SSL anywhere in the TCPClient specifically)

Normaly load all TCPData into a String and look for the first occurrences of 0x0d 0x0d (which marks the begin of “real data”.

The HTTPClient Library from the Community library Section of the IDE is a good starting point (worked for me).

Do you have any example on how I cab load all TCP Data into a string. All examples I’ve tried (including HTTPClient) using aren’t working for me :blush:

Actually, I just reviewed the HTTPClient code again and I haven’t tried that for a few days, I’ll give that a whirl again too. I’ll let you know how that goes.

You can read the Bytes in a Buffer (buffer char[200]).
Then terminate the Buffer with a Zero Byte (0x00).
Now you can construct the string using the buffer as an constructor like

 char buffer[200];
 String string(buffer);

You should also use the mentioned attribute from thingspeak to reduce the amount of Headers for the buffer!

I haven’t had the appetite for taking on a DFUtil installation, instead I’ve used the ThingSpeak IP number to GET info from my channel… further problems abound…

My channel field GET is:

This is currently resulting a simple “2” when requested via a browser (or POSTMAN) but on my Core the result is a 400:

> ============================
> Application>.Start of Loop.
> HttpClient>.Connecting to IP:
> HttpClient>.Start of HTTP Request.
> GET channels/24587/field/1.json HTTP/1.0
> Connection: close
> Accept: */*
> HttpClient>.End of HTTP Request.

> HttpClient>.Receiving TCP transaction of 128 bytes.
> HTTP/1.1 400 Bad Request
> Server: nginx/1.7.5
> Date: Mon, 09 Feb 2015 20:54:50 GMT
> Content-Type: text/html
> Content-Length: 172
> Connection: close

> <html>
> <head><title>400 Bad Request</title></head>
> <body bgcolor="white">
> <center><h1>400 Bad Request</h1></center>
> <hr><center>nginx/1.7.5</center>
> </body>
> </html>

> HttpClient>.End of TCP transaction.
> HttpClient>.Error: Timeout while reading response.

> HttpClient>.End of HTTP Response (5423ms).
> HttpClient>.Status Code: 400
> ----------------------------------Application>.Response status: 400
> ----------------------------------Application>.HTTP Response Body: <html>
> <head><title>400 Bad Request</title></head>
> <body bgcolor="white">
> <center><h1>400 Bad Request</h1></center>
> <hr><center>nginx/1.7.5</center>
> </body>
> </html>
> ============================

My SparkCore sketch uses the HttpClient from the Spark library:

// This #include statement was automatically added by the Spark IDE.
#include "HttpClient/HttpClient.h"

* Declaring the variables.
unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL

http_request_t request;
http_response_t response;

// thing speak api IP
IPAddress server(184,106,153,149);

void setup() {

void loop() {
    if (nextTime > millis()) {

    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.ip = server;
    //request.hostname = "";
    request.port = 80;
    //request.path = "channels/24587/fields/1/last";
    request.path = "channels/24587/field/1.json";

    // The library also supports sending a body with your request:
    //request.body = "{\"key\":\"value\"}";

    // Get request
    http.get(request, response, headers);
    Serial.print("Application>\tResponse status: ");

    Serial.print("Application>\tHTTP Response Body: ");

    nextTime = millis() + 10000;

Try adding a leading / at the start of the URL as per the line below.

request.path = “/channels/24587/field/1.json”;

1 Like

Yes! That was it!

// Argggghhhh!

Thank you!