Wifi reconnect : Repeating Hang in application code

Hi,

I’m building a project which have 2 pushbutton, every click need to be counted. (like counting pulses).
It’s using clickbutton lib and publishAsync to send theses data to particle.
The most important thing is that every click on the buttons is counted, if particle connection is lost for few minutes it not problematic but still need to count the button internally and send the count value afterwards.

During normal condition it’s working fine but discovered that during wifi reconnect it miss button counts.
I investigated the reason of missing events count :

  • button clicks variable are not send to particle ?. No, i setup internal counters (count button press) and they have the same value as the particle published data.
  • Blocking code in the main loop : Button.update() is triggered in the main loop, nothing fancy could block the loop, publish only if particle connected.
  • Some kind of button bounce which cause wrong count? No, hardware debounce in place with Max6817, Minimum pulse lenght is 50ms.
  • read several post on the forum and could not found a solution (system thread enabled and mode semi_auto, seems to comes from wifi.connect() blocking code)

It seems that during a Wifi reconnection the main loop hang few time for much more than 50 ms. (button press min length is 50ms, scope verified)

To make sure it is not something in my code i tried with a extremely basic code to demonstrate my issue :

A simple led blinking, changing state every 50ms using a millis.

#include "Particle.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

unsigned long lastTime = 0;

void setup() {
  pinMode(D8, OUTPUT);
  Particle.connect();
}

void loop() {
  unsigned long now = millis();
  if ((now - lastTime) >= 50) {
    lastTime = now;
    digitalWrite(D8, !digitalRead(D8));
   }
}

It works fine until I hold the antenna in a closed hand (which make loosing signal as the signal is not very strong ), Argon is loosing Wifi and try to reconnect “status led blinking Green”
During this time (status led blinking green) my test led on D8 blink normally and stop to blink for some instant then it start again in a loop.
Apparently this is due to the Wifi.connect which seems to block application code.

Checked the timings (scope) and they are always the same : code running for 3.3sec and blocking for 2 sec, then run 3.3 sec and stop 2 sec again, this is repeating until it success connecting on Wifi again.
Tried the same on a basic Thread but it didn’t change anything.

So it looks like it makes impossible to use an Particle device under bad wifi condition if you application cannot miss any external event.

I’ve read few thread about the semi auto and manual mode and the loop blocking but didn’t really found a solution, it’s not very clear in which mode what is blocking or not and it seems to depend also which firmware you are using.
The only way i think to fix that is to use another uC (like attiny) to count the button and deliver that using I2C
I’m quite amazed that it block code for 2 sec during a reconnect as wifi is handled by the ESP32 and application code by the Nordic.

If you have any idea to minimize this blocking code or if I did/say something wrong please correct me, i’m quite a noob using particle devices. (Using Argon software 1.5).

Thanks in advance
Nico

If you don’t want to miss a button press you should rather go with interrupts.

3 Likes

Hi,

Thanks,
Was thinking that wifi.connect would block also the interrupt but i gave a try and it seems to work perfectly.
I always try to prevent ISR as it could create some issues if they are not short enough but tweaked the code to get the shortest ISR code as possible and works fine.
Many thanks for the help.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.