and so on.
I’m adding safeguards to save locally the readings if no internet available.
The data is sent via Particle.Publish every hour. With internet everything goes great.
From the documentation I learned that once the handshake is done the time is keeped
locally. But when I cut internet, something (not my code) tries to re-connect and gets
a timeout.
I added some Serial.Print sentences in each measurement entry and this is what I get with internet:
You should remove that while(true) and use loop() as it is intended.
And I guess your problem comes from the combination of equality checks and delay(2000).
If you have a delay stretch across the one second that fits your pattern, you’ll miss that branch.
BTW, when you got Particle.connect() you need the internet since you explicitly told the system it should communicate with the cloud and it will atempt to follow that order - regardless of SYSTEM_MODE(MANUAL)
Hi @ScruffR.
The code works perfectly with internet. Otherwise the delay routine takes lot more of time.
I’m presume that the system routines are trying to re connect, but I don’t know why since
I’m running in manual mode.
First, note that I removed the while(1) which prevents loop() from ending in order to allow the system firmware housekeeping to run. Adding SYSTEM_THREAD(ENABLED); at the top of the file would allow the system and user code to run in their own threads.
This code is “non blocking” allowing loop() to complete without delay. The ledoff added code creates the 2 second (2000ms) delay before the led is turned off. Incorporate this in your code and let me know how it goes. Do you have any other blocking delays in your loop()?
By checking you connection and not telling the system to stay connected without network e.g. via if (!WiFi.ready()) WiFi.off()
You can also use SYSTEM_THREAD(ENABLED) to decouple your app from the system tasks (as mentioned by @peekay123)
Thank you @peekay123 for your detailed explanation.
Is great to have some authorized help. Honestly most of the code that I use
in Particle is the result of experimentation since I had issues that I cannot figure out how to
solve.
What I trying to archieve with my code is the following:
A sensor is read every 10 seconds
The value is accumulated in one variable
Every hour, the accumulated value is divided by the number of reads effectively done
A webhook is hit with the resultant data
…
When sending the data to the webhook, if Particle.Publish() timeout, the system enter in
offline mode. It tries to reconnect, but it keeps reading and saving the data.
If still in emergency mode when a new hours begins, it saves the data to a local array
after checking if are memory available. If not if rotates the array and delete old values.
When the system connects again it dumps the array to the webhook.
@gavpret, the while(1) code was killing the OTA and adding the 2 second delay allowed the system firmware to run a bit.
There are plenty of topics on all the things you want to do, including code. @ScruffR has great code for reconnecting wifi when a connection is lost. Do a Google search on “arduino circular buffer” which is exactly the type of buffer you need for storing offline data. There are also plenty of topics on running without a cloud connection.
That would be the way to do it.
And that’s also the scenario where System.updatesPending() would need to kick in to keep the connection open till a pending OTA update has been pulled in completely.