Can I detect WiFi is out of range?...and act upon it

I am trying to detect if the WiFi is out of range and once it looses connection I want to act upon it. Same as if it comes within range (connected) I also want to act upon it. Is there some code available for this function?

Yes use RSSI see Hugo’s Bar chart in this post http://forums.electricimp.com/discussion/398/rssi-to-user-understandable-value/p1

I use this, a bit crude but does the job I use it on the agent as the device passes RSSI up but you can do it on the deviec (imp)

`
// on device
wifi(imp.rssi());

function wifi(rssi){
// below -87 zero (still connected, but barely)
// -87 … -82 1
// -82 … -77 2
// -77 … -72 3
// -72 … -67 4
// above -67 5

local bars = 0;
if(-82 >= rssi  && rssi >= -87) { bars = 1;}
if(-77 >= rssi  && rssi >= -82) { bars = 2;}
if(-72 >= rssi  && rssi >= -77) { bars = 3;}
if(-67 >= rssi  && rssi >= -72) { bars = 4;}
if(rssi >= -67) {bars = 5;}  
if(rssi == 0) {bars = 0;} 
return bars;

}`

Thank you for your input.
Can the IMP002 detect it is connected to a certain router (like your home ONLY)
…and signal this to the agent?

I have been working on code for switching wifi access points. I started with code from the documentation and put it into my application - naturally it morphed along the way.

I’ve done a lot of testing and it works 99%

http://forums.electricimp.com/discussion/432/multiple-wifi-settings#Item_31

This code from the Nov 26 post could be combined with measuring rssi to do what you want. The code that i am using assumes that you either have one access point or the other (like home or work) and doesn’t bother to look at rssi.

The only trouble I had was today it reported the wrong ssid from imp.getssid(), but just once. I was turning APs on and off, and also cycling power on the imp and it somehow got to this state. The Access point that it said it was connected to is not in the vicinity at all so… . I don’t know what happened there.

This is an advanced topic and you need to be careful with the order of your code. Be careful not to call server.log or agent.send until after the code verifies connection.

Well, definitely ensure that you set your connection policy to RETURN_ON_ERROR at the very START of your code to ensure that you will never call server.log or agent.send when configured for blocking I/O, anyway :slight_smile: