Is it possible to stop launching softAP if the P1 is unable to connect to a WiFi network?


#1

Hello,
I am passing WiFi credentials to P1 over UART. When the credentials are sent P1 tries to connect to WiFi using those credentials and if the credentials are wrong, P1 should wait till the credentials are sent from UART again. But this is not happening at present.

Instead, the P1 RGB LED blinks blue if the credentials are wrong. When I open Serial console and type something, it says waiting for binary file to be sent … press a to abort. I think P1 entered listening mode where it is expecting credentials over USB. I want to define my custom listening mode. How do I do this?

pseudo code:

SYSTEM_MODE(SEMI_AUTOMATIC)

bool notConnected = true;
void setup(){
}

void loop(){
    if(notConnected){
         while(!Serial.available());  //get WiFi SSID and password
         //decode message format
        if(ConnectToWiFi(ssid, pass)){
             notConnected = true;
        }
    }
}

bool ConnectToWiFi(char * ssid, char * pass)
{
    bool ConnectToWiFi(char * ssid, char * password)
  {
  WiFi.on();
	if(WiFi.hasCredentials()){
		WiFi.clearCredentials();
	}
	WiFi.setCredentials(ssid, password);
	WiFi.connect();
	while(WiFi.connecting());
	return WiFi.ready();
  }
}

Thanks
Dheeraj


#2

From here https://docs.particle.io/reference/firmware/photon/#connect-

Attempts to connect to the Wi-Fi network. If there are no credentials stored, this will enter listening mode (see below for how to avoid this.)

Where is this below pointing to?


#3

WiFi.listen(false) isn’t working


#4

WiFi.listen(false) is working, but you need to use SYSTEM_THREAD(ENABLED) to have your loop() run parallel to Listening Mode.

https://docs.particle.io/reference/firmware/photon/#listen-

And to enter Listening Mode it would be WiFi.listen()

Additionally if you ever issue another WiFi.connect() or Particle.connect() this will force the device back into LM immediately too.


#5

Hi @ScruffR,
Thanks for replying I tried this app

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

void setup()
{	
	WiFi.on();
        WiFi.listen(false);
}

void loop()
{
}

The access point won’t shut down.


WiFi.listen(false) doesn't work? Please nooooo
#6

Actually with that code Listening Mode should not even be entered.

But even if it was, having WiFi.listen(false) there would probably not work anyway as it’ll be too early as LM might not even be fully up then.

Rather put it into loop()

void loop() {
  static uint32_t ms = millis();

  if (millis() - ms > 10000 && WiFi.listening()) {
    WiFi.listen(false);
    ms = millis();
  }
}

BTW

That’s “pointing” to this paragraph immediately after the above


#7

After moving WiFi.listen() into loop() I can see that the code in loop() is working alongside with softAP. The access point is still on. Here is my code

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
void ConnectToWiFi(char * ssid, char * password);

void setup()
{	
	WiFi.on();
        ConnectToWiFi(SSID, PASSWORD);
}

void loop()
{
     while(!Serial.available());
    ConnectToWiFi(SSID, PASSWORD);
     WIFI_CONNECTION_STATUS = WiFi.ready();
    if(!WIFI_CONNECTION_STATUS){
        Serial.println("wifi hotspot doesnt exist");
    }
}

void ConnectToWiFi(char * ssid, char * password)
{
	if(WiFi.hasCredentials()){
		WiFi.clearCredentials();
	}
	WiFi.setCredentials(ssid, password);
	WiFi.connect();
	while(WiFi.connecting());
}

I am sending wifi credentials over Serial. If I enter the right credentials, it should connect and be connected. If that wifi hotspot is turned off, it should wait for credentials over serial. With the mentioned code, my loop() works in parallel with soft AP. Also after some time, the particle closes it’s Serial. (I think my P1 is restarting).


#8

You should have Particle.process() in your while() loops.

And as I said

I think this is happening.

BTW, instead of circling in a while() you should consider using waitUntil() or waitFor()

And your network is a WPA2 network, available and not hidden?


#9

I am testing the reliability of WiFi. I am using my phone hotspot as an access point. It is available and not hidden. When the hostpot is on P1 connects to it. When it is off I want P1 to switch to a new WiFi hotspot if the credentials sent over UART are success or if the old WiFi hotspot is back, it should reconnect to that hotspot.

Is it possible to wait for Serial data as well as keep the loop() running?


#10

Serial uses a 64 byte buffer and is interrupt driven, so it’s safe to have loop() running while you are waiting for data as long you keep checking the buffer often enough to prevent a buffer overrun.


#11

You were right. Another WiFi.connect() or Particle.connect() forces the device into Listening Mode. I have to take this into account.