Listen network won't shut down in semi_automatic

0.4.7

I have my device start in listen mode upon power cycle and exit after a set period of time. This is in case the user wants to change the wifi password. They can cycle power. My issues is calling WiFi.listen(false) doesn’t shut down the softAP network. After the device exits listen mode it connects to the cloud but the softAP network Photon-XXXX is still present. The problem is that the setup app fails because the network doesn’t disappear. so the app never re-connects to the internet.

#include "application.h"
#include "SparkIntervalTimer.h"

SYSTEM_MODE(SEMI_AUTOMATIC);

#define PIN_LED D7

IntervalTimer conn_timer;
bool end_listen_once = false;
#define TIMER_INTERVAL 30000

void end_listen()
{
  static int loop_cnt = 0;
  if(loop_cnt > 2 && !end_listen_once)
  {
    WiFi.listen(false);
    Particle.connect();
    end_listen_once = true;
  }
  loop_cnt++;
}

void setup()
{
  conn_timer.begin(end_listen, TIMER_INTERVAL, hmSec, TIMER5);
  Particle.disconnect();
  WiFi.listen();
  pinMode(PIN_LED, OUTPUT);
  digitalWrite(PIN_LED, HIGH);


}

void loop()
{
  Particle.connect();
  delay(1000);
  if(digitalRead(PIN_LED) == HIGH) digitalWrite(PIN_LED, LOW);
  else digitalWrite(PIN_LED, HIGH);
}

I don’t know if I made a mistake here, but it is driving me crazy.

Hmm, I’ve not seen this problem before, but you could always shoot a WiFi.off() before Particle.connect().
This should ensure that SoftAP will be killed :wink:

@ScruffR

I don’t think this is true… To state my desired intention. I want the unit to always start in “listen” mode whether it has credentials or not. If it has credentials I want it exit listen mode and try to connect after a finite amount if time. If it doesn’t have credentials it should stay in listen forever.

The issue is the softAP network Photon-XXXX never goes away. I used some debug code to turn off WiFi manually and sure enough I get a breathing white light and the network Photon-XXXX is still there. I can’t seem to kill it and it wrecks my configuration app if the softAP network doesn’t go away after it connects to the cloud.

#include "application.h"
#include "SparkIntervalTimer.h"

SYSTEM_MODE(SEMI_AUTOMATIC);

#define PIN_LED D7

IntervalTimer conn_timer;
volatile int loop_cnt = 0;
#define TIMER_INTERVAL 30000

void end_listen()
{

  loop_cnt++;
  if(Particle.connected())
  {
    loop_cnt = 0;
  }
  if(WiFi.listening())
  {
    if(loop_cnt > 2)
    {
      if(WiFi.hasCredentials())
      {
        Serial.println("about to turn off listen");
        WiFi.listen(false);
        WiFi.off();
        Particle.connect();
        loop_cnt = 0;
      }
    }
  }
  else if(!Particle.connected())
  {
    if(WiFi.ready())
    {
      WiFi.disconnect();
      WiFi.off();
      Particle.connect();
    }
    else if(loop_cnt > 7)
    {
      Serial.println("about to reset the system");
      System.reset();
    }
  }
  Serial.println(loop_cnt);
  Serial.println(WiFi.hasCredentials());
}

void setup()
{
  Serial.begin(9600);
  while(!Serial.available());
  conn_timer.begin(end_listen, TIMER_INTERVAL, hmSec, TIMER5);
  loop_cnt = 0;
  WiFi.listen();
  pinMode(PIN_LED, OUTPUT);
  digitalWrite(PIN_LED, HIGH);


}

void loop()
{
  if( Serial.available())
  {
    int c = Serial.read();
    switch(c){
      case 'o': WiFi.off(); break;
      case 'O': WiFi.on(); break;
      case 'c': Particle.disconnect(); break;
      case 'C': Particle.connect(); break;
      case 'w': WiFi.disconnect(); break;
      case 'W': WiFi.connect(); break;
      case 'l': WiFi.listen(false); break;
      case 'L': WiFi.listen(); break;
    }
  }
  delay(1000);
  if(digitalRead(PIN_LED) == HIGH) digitalWrite(PIN_LED, LOW);
  else digitalWrite(PIN_LED, HIGH);
}

Was this fixed?

1 Like