Argon posting to ThingSpeak fails after period of inactivity

Hi everyone,

I’m building a little project for automated plants watering. It’s using simple analogue water level sensors, I’m measuring their output voltage using Argon’s analog inputs and then sending the data to various places:

  • SPI wired OLED for on-device display (Adafruit SSD1306)
  • Particle Cloud (via on-request Particle variables and occasional Publish for status events)
  • Pushover for mobile device status notifications (via Publish+WebHooks)
  • ThingSpeak for data collection (using official ThingSpeak Communication Library v1.5.1)

I also have one Particle function that acts as a simple ON/OFF switch for my ThingSpeak publishing function, so it’s not working constantly and I can switch it on remotely only when I want to monitor and visualise the measured data.

Everything works fine, apart from when I switch ON my ThingSpeak publishing function after a longer period of inactivity (roughly more than 90 minutes), the ThingSpeak.writeFields() fails with code -301 (Failed to connect to ThingSpeak) for about 7 times (roughly 3 minutes) before it eventually goes through again.

Here are the details:

  • Particle Argon, OS version 1.4.2
  • ThingSpeak library from official Particle Libraries, version 1.5.1
  • WiFi is connected, regularly tested via WiFi.ready()
  • Particle Cloud is connected, regularly tested via Particle.connected()
  • LED breathing cyan
  • Particle.publish() events are going through OK, showing as events in Console
  • Webhooks pickup these events OK, sending POST requests to Pushover OK
  • I can query Particle Variables and execute Particle Functions
  • ThingSpeak data publishing using ThingSpeak.writeFields() works also OK at regular intervals, always returning 200 correctly

So generally everything works as it should and the device is connected to the internet by all counts all the time.

When I disable my ThingSpeak publishing function for a short period (using Particle Function, which results in ThingSpeak function not running in my loop) and then enable it again, everything is still OK and running fine - ThingSpeak starts returning 200 and publishing data at first attempt.

When I disable ThingSpeak function for much longer though, roughly 90 minutes or longer, this happens:

  • ThingSpeak.writeFields() fails with code -301 (Failed to connect to ThingSpeak) at the first attempt
  • This happens for roughly 7 subsequent attempts (about 3 minutes as I send data once every 30 seconds)
  • After this period it somehow suddenly picks up again, connects, sends data, returns 200 and works OK.

During this time rest of the code still works as it should, everything is still fine and connected as in the list above, it’s purely the ThingSpeak that doesn’t work for a while. Looking at the library code, there’s nothing that would suggest something “hanging” or a need for anything else to be done after period of inactivity. Every thingSpeak.writeFields() opens up a new TCPClient connection, sends data, closes connection.

The only thing I could think of as a “cure” at this time, is to establish a “blank” connection attempt to ThingSpeak server (api.thingspeak.com) on port 80 using client.connect(), little delay and then client.stop() every time I re-enable my ThingSpeak publishing function, before the actual thingSpeak.writeFields() is eventually called in my ThingSpeak publishing function. This sometimes helps at least to decrease a number of failed attempts. It’s as if the connection somehow gets dropped and needs to be re-established. This doesn’t happen after device reset or a new firmware upload, only when it’s switched off in the code.

Any ideas?