Advice/best practice for unreliable internet connections

Hi amor, robc,

Thanks for your offers of help, I really appreciate it. The PublishQueueAsyncRK library looks nice and I’ll use that in future for publish uploads.

For this application however I’m not actually reading a sensor, instead I’m actuating a bunch of relays connected via I2C to open valves/pumps and take air samples for pollution monitoring. To take a sample I run a 60-180 second long sequence, timed using mills(), that sends the relevant I2C commands to open to the correct relays in turn.

This sampling setup has two main functions:

  1. Take a single sample on demand.
  2. Run a sampling sequence - e.g. take a sample every hour / every day at 14:33 for a month / one sample a month for a year / etc…

A single sample is taken by entering the correct String in a Particle.function() on console.particle.io

For the sequence I set the time for the next sample and run countdowns using millis() and Time.now(). I can start, stop, and change the sequence interval using String commands in a Particle.function().

@amor To answer your three questions:

  1. The Argon is always awake, I use the RTC (via the SdCardLogHandlerRK library) so I can have long, yet precise sampling intervals without being concerned with millis() rollovers or having a power glitch losing the millis() count.
  2. The I2C controls a MCP23017 I/O expander that opens/closes relays.
  3. I want to be connected to the cloud at all times so the user can send commands via the Particle.functions() to control the device.

From this thread (Particle.connect() blocking main loop permanently, even with SYSTEM_THREAD(ENABLED)) I’m assuming that the Particle.connect() call will block the code in my main() loop. This means that if I leave the Argon in AUTOMATIC mode, I lose connection for a moment, and the Argon tries to re-connect my main code will not be run until connection is re-established. The Argon can be flashing green for ~45seconds while connecting even on a good day.

It seems like this causes me three problems:

  1. If the Argon tries to re-connect just before a sample is due to be taken I risk missing the sampling time.
  2. If the Argon tries to re-connect during a sample, the relay timing sequence will be ruined and I won’t take a valid sample.
  3. Finally if the wifi/cloud connection goes down and stays down won’t I be stuck in a Particle.connect() block forever?

I’ve found a few other threads where people are asking similar questions (e.g. Particle.connect(timeout, retryDelay), Photon set Particle.connect() Timeout?), and there are hints that there might be a solution via a timeout or something to do with running SYSTEM_THREAD(ENABLED), but I haven’t been able to figure out what that solution is!