I’m struggeling to get this piece of code to work as expected with my particle photon …
So my setup is: one particle p1 and one particle photon, the p1 is supposed to play LED animations and i can use regular GET/PUT commands to control it using Postman (https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop)
To play the next animation, i can send a PUT command with {“play”:“next”} to http://p1_ip_address/animation
With the help of particle photon, i wanted to cycle through all animations using {“play”:“next”} every 5 seconds.
The problem is: the animation is not changed every 5 seconds, it takes most of the time more than 39 seconds for the animation to change …
Using Postman i’m able to instantly change the animation
and rickkas7, here’s a sample of the terminal output:
0000055666 [app] INFO: before connect
0000055671 [app] INFO: after connect
0000055672 [app] INFO: after stop
0000060667 [app] INFO: before connect
0000060683 [app] INFO: after connect
0000060684 [app] INFO: after stop
And before you suggest it, i already tried the httpclient lib and had the same result
The timestamps on the left are in milliseconds, so it’s making the connections every 5 seconds.
My first guess is that since you’re not reading the HTTP response code from the server, the server is timing out before acting on the request. The flush call does not not clear the received data buffer.
My second guess is that the server is not sensing the end of the request, and also is waiting to time out. Adding a Content-Length may help.
I think the “Content-Length” header is required here.
As @rickkas7 points out you should wait and read the response from the server before calling stop. You can gain more insight into what is going wrong from that sometimes as well.
I don’t really want a conversation as much as i want to PUT{“play”:“next”} every 5 seconds …
I already gave HTTPClient a shot and it wasn’t really reliable in my case: it randomly returns -1 and an empty response (the fail rate is at 80%)
Problem solved !
Avoid using client.println(); !!! use client.print() once instead with all headers and content concatenated into one String (request).
So it looks sthg like: