Adafruit IO receive() causes strange debug error with Photon 2: Failed to find end of chunk size line!

I am trying to send and receive data with an Adafruit IO dashboard using a Photon 2. I am mostly referencing this documentation and example:

I connected to the dashboard and feed successfully and am able to send data. However trying to receive data using the receive() function results in the following debug event: Failed to find end of chunk size line!
This appears as an event in the particle dashboard. I cant find any other info on this error so if anyone else has a clue what it means please lmk.

I've pasted my code below.

void loop() {
    curtime = millis();

    if(curtime > longtime + 10000){  //this logs a message every 10 seconds.
        Particle.publish("Wifi test...");
        longtime = curtime;
        count += 1;
        if (testFeed.send(count)) { //this successfully sends the int to AIO dashboard
            message = "Sent count to feed: ";
        else {
            Particle.publish("failed to send to feed!");
        int i;
        testFeed.receive();     //any call of this function has resulted in strange error.
        //FeedData latest = testFeed.receive();

delay(200);				// Let’s not access AIO too quickly!	

You're only calling testReed.receive() every 10 seconds, so it will always time out because you're not processing characters from the board fast enough.

The receive needs to at the outer level of loop, where the delay is. Also you probably shouldn't have the delay.

Thanks for the reply!
I dont understand why calling receive() every 10 seconds would be a problem. The function sends a request to the AdafruitIO dashboard for the current state of the feed. In examples they write:

FeedData latest = testFeed.receive();

This saves the current state of the feed as an object. Why would calling this infrequently cause an error? What do you mean by "it will always time out"? What will time out?
You say "you're not processing characters from the board fast enough". What does this mean? Are you referring to the AdafruitIO dashboard as the "board" here? Very unclear what you're trying to explain.
I think you may have severely misunderstood the function.

I tried your solution anyways, it failed. Receive() is called rapidly without delay and I still get the same error, only more rapidly.

If anyone has any other explanation of what could be going wrong, please reply.

This has been solved! I am able to send and receive data from the Adafruit IO dashboard with the Photon 2. A detailed explanation of the problem and solution below:

The problem is within the Adafruit_IO_Particle library file Adafruit_IO_Client.cpp on Github. The library is out of date and needs updating. I recommend readers open the file linked^ and read along for the explanation. Alternatively skip to the end for the solution.

On line 169 of the file I found the error message I was receiving. I walk backwards through the function definition to determine how it reached that point. On line 166 an int "len" has value 0, causing the program to assume it received chunk data. Reading back to line 155 we can see where len is set to any value other than 0. This takes place within a while loop which iterates through every header in the _client stream received from the adafruitIO feed. Len is set to a value if the loop finds a header named "Content-Length". This means that the error is caused by one of two possibilities: the function receives chunked data but fails to find ("\r\n") in the data, or it receives json data but fails to find the header labeled "Content-Length".

To know which is taking place we need to see what is being sent from adafruitIO. I go to the AdafruitIO documentation and find the "getFeed" shell http request . It is documented as such:
curl -H "X-AIO-Key: {io_key}" /{username}/feeds/{feed_key}
This line can be typed into the terminal to see the exact response sent back from Adafruit IO dashboard. While this works, the Adafruit Particle library is making a slightly different call than this. This can be seen on line 43 of Adafruit_IO_Client.cpp. This is because the library is still using "v1" commands to interface with AIO, where AIO has since updated to "v2". Some v1 function calls such as feed.send() still work fine, although maybe not for much longer as the version is being deprecated. We can modify the curl command to more closely match what the library is calling. We get: curl -i -H "X-AIO-Key: {io_key}"{feed_key}/ I included -i to reveal the headers being sent in the output. Entering this into the terminal (including my actual io_key and feed_key) yields the result:

Notice on the 9th line down the header "content-length" is included. Why then is the library code not detecting it? Because the code checks for "Content-Length" (line 155).

 if (strcmp_P(recvbuffer, PSTR("Content-Length")) == 0) {
            len = (uint16_t)_client.parseInt();

It is case-sensitive.
The resolution is simple: edit this code to check for "content-length" instead. How do I edit the library code? Instead of importing the library, I created the files in the project and simply pasted in the code. It works!

Doing this is an easy solution to using the library. However it is not a good long term solution as Adafruit IO can completely deprecate its v1 at any day which will cause the entire library to stop working. The library should be updated to include "content-length" for a short term solution. The library should really be updated completely to use v2 commands for a long term solution.

Thanks! Please reply with any questions or comments.

1 Like