Server.connect(callback,timeout) sends a state=0 when my wifi access point is turned off

Hi All,

I’m trying to log all the WifiNetworks that the imp is able to see, caches those in the NV table, then upload that cache once in awhile.

The code works ONLY IF i am able to connect to my blink up wifi. If i am out of range, my scanNetworksCallback() is called with a state = 0.

I don’t see a code of zero anywhere in the docs (https://electricimp.com/docs/api/server/connect/) or when i print out the values of the states…

2014-02-20 13:09:09 UTC-7: [Device] SERVER_CONNECTED=5
2014-02-20 13:09:09 UTC-7: [Device] NO_WIFI=1
2014-02-20 13:09:09 UTC-7: [Device] NO_IP_ADDRESS=2
2014-02-20 13:09:09 UTC-7: [Device] NO_SERVER=4
2014-02-20 13:09:09 UTC-7: [Device] NOT_RESOLVED=3

so what can i do to be able to call imp.scanwifinetworks() IF I AM out of range of my wifi network

I have put the basic code below…ommiiting stuff that i think is extraneous

`
hannah.on_btn1_changed = function(state) {
scanAndStoreNetworks()
}
function scanNetworksCallback(state){
local scanResults = imp.scanwifinetworks();
local time = time()
local networks = []
foreach (idx,val in scanResults) {
local scannedNetwork = scanResults[idx]
networks.push({“bssid”:scannedNetwork.bssid,“rssi”:scannedNetwork.rssi})
}
nv.wifiLog.push( { “time”:time,“networks”:networks, “state”:state})

if(server.isconnected()){
    //send the wifi networks to the agent
}

}

function scanAndStoreNetworks(){
if(!server.isconnected()){
server.connect(scanNetworksCallback,30)
}else{
scanNetworksCallback(SERVER_CONNECTED)
}
}

`

If you’re only wanting to scan for networks, you don’t have to wait until server.connect() succeeds (or fails). In fact you shouldn’t, because once it’s failed, it turns the Wifi chip off again. What you probably want is something like this:
`
function scanNetworksCallback(state) {
if (server.isconnected()) {
sendWifiNetworksToAgent();
}
}

function scanAndStoreNetworks() {
if (!server.isconnected()) {
server.connect(scanNetworksCallback, 30);
}
local scanResults = imp.scanwifinetworks();
local time = time()
local networks = []
foreach (idx,val in scanResults) {
local scannedNetwork = scanResults[idx]
networks.push({“bssid”:scannedNetwork.bssid,“rssi”:scannedNetwork.rssi})
}
nv.wifiLog.push( { “time”:time,“networks”:networks, “state”:state})

if(server.isconnected()){
    sendWifiNetworksToAgent();
}

}`

Peter

so that was my original approach, but no dice. I’ve attached my stripped down code for the hannah board.

when I do it the way you suggest, i always get an empty array from imp.scanwifinetworks()

Since my device will be battery powered, i need to conserve power and stay offline until i need to send data.

Here are the scenarios:
Assuming i always begin in a server.isconnected()==false state:

  1. (GOOD) server.connect(scanwifiCallback), WITH WIFI present -> imp.scanwifinetworks() returns data
  2. (BAD) server.connect(scanwifiCallback), WITH NO WIFI present -> imp.scanwifinetworks() returns empty array
  3. (BAD) scanwifi() without trying server.connect() first, WITH WIFI present -> imp.scanwifinetworks() returns empty array
  4. (BAD) scanwifi(),without trying server.connect() first, WITH NO WIFI present -> imp.scanwifinetworks() returns empty array

it seems like imp.scanwifinetworks() always returns an empty array, because the wifi state is offline when i finally call it…either directly when i know i’m disconnected, or if i’m out of range of my WAP, and do the scan in the server.connect(callback)…

DOH! nevermind… i realize i am being bitten because the callback is being executed after the state of the wifi chip has gone from disconnected-> attemptingtoconnect->disconnected again… i’ll post the final version of the code that works later today.

your snippet made it pretty obvious what was going wrong thanks!

ace, did you have any success with this?

Peter, I’ve tried scanning for wifi networks while the imp is not connected yet is trying to find a connection. imp.scanwifinetoworks() always returns an empty array except when I have a valid connection.

Actually, when I say it “always” returns an empty array, that’s not true. If I scan every 5 seconds, while the device is trying to connect, I do get a populated array about once on every 20 attempts, which is even more confusing.

There’s a known issue that scanwifinetworks doesn’t work if the wifi chip is powered down; ie, if you’re not connected or attempting to connect. The workaround would be to issue a background connect with a suitable timeout then scan.

This is fixed in the next release.

Thanks for the confirmation, Hugo. It would be helpful for Electric Imp to consider a page for listing known issues for the current release. I know that many tech companies have a policy of not acknowledging bugs until the fix is deployed. This is the second one in a week for me, and it does consume some time convincing myself that it’s not my code before I approach the forum for help.