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})
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:
(GOOD) server.connect(scanwifiCallback), WITH WIFI present -> imp.scanwifinetworks() returns data
(BAD) server.connect(scanwifiCallback), WITH NO WIFI present -> imp.scanwifinetworks() returns empty array
(BAD) scanwifi() without trying server.connect() first, WITH WIFI present -> imp.scanwifinetworks() returns empty array
(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!
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.
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.