How to prevent a hold up in Code execution when there is no network/cloud connectivitity

    #include <JC_Button.h>          // https://github.com/JChristensen/JC_Button
    SYSTEM_THREAD(ENABLED);

    IndicatorLED relayPin0(A0);
    const byte ASS_BUTTON_PIN(D5);
    Button myAssBtn(ASS_BUTTON_PIN);       
    constexpr char* ASS_MSSG = "ASSISTANCE";
    void setup()
    {
      relayPin0.begin();
      myAssBtn.begin();              
    }

    void loop()
    {
      myAssBtn.read();               
      if ( myAssBtn.isPressed() )
      {
        relayPin0.on();
      }
      if ( myAssBtn.wasPressed())   
      {
        Particle.publish(DEVICE_NAME, ASS_MSSG, 60, PRIVATE);
      }
    }

Beyond introducing a millis timer, is there any way I can rejig my code so that if there is no network connection, the relayPin0 doesn’t wait for the Particle Publish to go through before noticing it needs to turn off?

I’m guessing that’s what’s causing the relay to remain on when there is no network, anyway!

Thanks

I also don't see any command to turn it off to begin with.

If you could give us the web IDE share link, we can have a more thorough look, including the libraries.

As far as I'm aware, Publish shouldn't be blocking though. If you comment it out entirely, does it work as expected?

The obvious (partly) solution to this

would be

 if ( myAssBtn.wasPressed() && Particle.connected())   
 {
        Particle.publish(DEVICE_NAME, ASS_MSSG, 60, PRIVATE);
 }

Sometimes Particle.connected() doesn't work as expected, but this should help most cases.
Also using SYSTEM_MODE(MANUAL) may help till Particle hopefully sometime decides to undo the changes that introduced all the blocking into several functions that didn't use to block.

Also the NO_ACK switch for Particle.publish() might be worth a try (although when I last tried a long time ago it didn't quite live up to its name :wink: )

1 Like

Thanks - I hoped there would be something like that - I'll try it! :slight_smile: