Photon locking up with HttpClient library

I'm using the HttpClient library to send and receive with Home Assistant. My photon is locking up and I can cause it to happen by fetching particle variables with the particle app. I have 10 particle variables. Sometimes the variables are returned instantly and often there is a delay of 5-10 seconds. Sometimes they come in one by one slowly filling in. I stopped my http calls for a test and refreshing particle variables was became instant every time. I think that that when the http calls are running, which are called every 5 secs, they must take some time and if I refresh particle variables at the same time it causes the delay or lock up. When I say lock up the program stops executing, I get an error refreshing variables, the particle app shows the device is offline, and the led on the photo that normally breaths cyan freezes at and stays the same brightness. I have to power cycle it to get it going again. How can I troubleshoot this? Is there something I can do in my code to stop delay fetching particle variables until the http call is done and vice versa?

  • Fetching 10 variables is not only inefficient, but will use a very large number of data operations. Each fetch is 1 data operation, and each account on the free plan has a limit of 100,000 data operations per month. When you exceed this limit, your account will be blocked until the beginning of the next monthly billing cycle. If the data is not filling up the event 1024 byte variable, you should combine the data into a single variable.
  • If you are fetching multiple variables you should do so sequentially, not in parallel. The device can't handle a large number of requests at once.
  • However polling for variables is still inefficient, and pushing the data from the device using Particle.publish and receiving it using webhooks or SSE may be more efficient.
  • The HTTP client library is not very efficient and will block during each request. It's not recommended for production use. You may be able to improve performance by calling it from a worker thread so it won't block the loop thread, which will also block variable requests.
  • If the device is locking up, you almost certainly have a bug, probably related to memory management, such as using a freed pointer, writing to a null pointer, or overwriting the end of a memory block.

I only fetch the variables manually using the particle app. Since that means it is on demand I'm not using up my data operations very fast. I only use it to check on things.

The app has no way to fetch the variables other than refreshing the screen. I'd hope that under the hood it does this sequentially since I have no control over how it does it. Do you know for sure how it does it?

Is there something better than the HTTP client library that I should be using to make these calls?

I'll try the worker thread. That's the kind of solution I was thinking might help.

I'll have to look into your other suggestions to understand how I might troubleshoot for them.

Thanks for your comments!

Particle publish might work for my application except that would go through the cloud right? I like that the http client works locally without a cloud service.

I was looking into how to implement worker threads and before figuring that out I read a post about a problem that was solved by using SYSTEM_THREAD(ENABLED). Could this be a viable solution?

I'm testing it now and it seems to be working better. Not sure I'm able to reliably reproduce the problem though so I will have to give it some time to see if it makes a difference.

Yes, you should always use SYSTEM_THREAD(ENABLED).

1 Like