WiFi.off() not turning wifi off

Hi,

Im using SYSTEM_MODE(MANUAL);
and run this at the end of my loop:

    if(!WiFi.ready()){WiFi.off(); Serial.print("wifi off ");}

When I turn the wifi source off on my hotspot/router, the photon does not change to white but blinks orange twice and then green rapidly despite telling it to turn wifi.off.

If the wifi remains off and I reset the photon it blinks green for a while and then white (as it should).
But again, if I bring the wifi back and let the photon connect to it and then shut it off, the photon blinks cyan instead of white, forever.

I need for the device to be available as much possible since I am reading a sensor several times per second.
Scanning for wifis takes about 1 second, which is acceptable if I do it only once every minute or two, but when it blinks green or cyan, the code freezes for several seconds.

thanks

What device OS version?

1.5.2

I’m just sitting here testing similar things on 1.5.2 and I do not get the same behavior.

I’m curious - how are you detecting the loss of WiFi? Is it simply by running the !WiFi.ready() command? If so - could you test this using “system events” instead, as they are guaranteed to be executed?

void handle_system_events(system_event_t event, int param)
{
  if( event == network_status && param == network_status_off )
  {
    Serial.println( "INTERNET_DISCONNECTED");
  }
  else if( event == network_status && param == network_status_connected )
  {
    Serial.println( "CONNECTION_RESTORED");
  }
}

void setup() {
    Serial.begin(9600);
    System.on( button_click, onButtonClick );
}

J

thanks @jenschr, im happy to try this, I just want to understand how to input your code into mine. Should void handle_system_events(system_event_t event, int param) be run from the loop()? If so, what is the code I should put?

Also, the Wifi.off() should be next to
Serial.println( "INTERNET_DISCONNECTED");
correct?

I dont find anything in the particle reference page for examples.

The System.on() command would be used to hook up that function to the respective system events.

The system would then take care of calling that function when one of these events occures.

@plusmartin Here is an example of how you would use it https://go.particle.io/shared_apps/5ee8ca2a8533f6001615ce16

This code snippet will program your Particle device to go online, but not connect to the Particle Cloud service. If you double-click the Setup button on your device, it will then connect to Particle Cloud (so you can update it). I use this on many projects where I do not need Particle Cloud services or where I do not want my code to be interrupted if the Particle Cloud service goes down.

Using this, you can make your project quite resilient to Cloud issues.

2 Likes

Thanks

Where can I see the complete list of events?
I see this is useful for checking what events are happening but it does not tell me how to force the wifi to turn off.

My photon sometimes blinks cyan forever and wont even go through loop. It can only be reset with the button and this is impossible if the photon is in a remote location.

Any ideas to ensure that the wifi turns off and does NOT blink green nor cyan unit it finds the wifi again?

Try the Reference Documents? https://docs.particle.io/reference/device-os/firmware/photon/#system-events

I see this is useful for checking what events are happening but it does not tell me how to force the wifi to turn off.
Particle have been trying very hard to make the WiFi devices to stick on and connected!

WiFi.off(); should turn off the WiFi module and if you are using manual control then it is up to your app to manage the reconnection process.

My photon sometimes blinks cyan forever and wont even go through loop. It can only be reset with the button and this is impossible if the photon is in a remote location.

Using SYSTEM_THREAD(ENABLED) and SYSTEM_MODE(SEMI_AUTOMATIC) avoids most of the issues and you just let the unit find a connection again, your application (with care not to call blocking functions) will just motor on.

1 Like

Thanks,

I cannot use SYSTEM_THREAD(ENABLED) because I am using SoftAP to reset the wifi and if I use both I run low on RAM and SoftAP stops working properly.

I dont have any issues using manual mode, I am manually connecting to wifi and to particle and running Particle.process() often, but my problem is that when my router or hotspot turns off, the photon does not turn Wifi.off() and instead it blinks orange twice and then green or cyan and it blocks the code that I need to keep reading my sensors.

Only sometimes it does work as intended and it goes directly to breathe white , but not always.

Why cant I turn wifi.off whenever wifi.ready==false? or particle.connected==false ? should be very simple.
greetings,

am using SoftAP to reset the wifi
Do you mean set the credentials?

I delete all credentials before adding new ones with SoftAP. I also disconnect from Particle and WiFi (and wait for this to happen) before deleting credentials and to make sure I turn off the WiFi module and wait and then turn it back on again. Are you waiting and testing or just doing the disconnection?
I check the available memory is > 38K before going into listening mode and engaging SoftAP. All this works fine with SYSTEM_THREAD(ENABLED); If you don’t have enough free memory then you need to put the app on a diet. There is an excellent tutorial by @rickkas7 about flash and RAM usage and techniques to optimise both.

I dont have any issues using manual mode, I am manually connecting to wifi and to particle and running Particle.process() often, but my problem is that when my router or hotspot turns off, the photon does not turn Wifi.off() and instead it blinks orange twice and then green or cyan and it blocks the code that I need to keep reading my sensors.
This is exactly why you need to use SYSTEM_THREAD(ENABLED);