I took half of this idea from another post on this forum, the first part is how it can scan and show the networks it can pick up.
The second part is my attempt to store multiple SSID and the keys needed to log on those, the idea was then to use server.isconnected() to determine if it is connected or not, and scan the networks and find a match if it can, then use that to log on to that SSID instead.
When calling the agent url it will also return a list of the networks it found, marking the one it is currently connected to with bold, Italic means it is a SSID we also got the code for, and are able to switch to if the one we are connected to goes down, normal means it was just found, but we have no info on it.
Agent:
`wifi <- “”;
http.onrequest(function(request,res)
{
if (request.body == “”)
{
local tempString = split(wifi, “|”);
local output = "
";
if (wifi.len() < 1)
{
output = "Waiting for the Impee to send some data to the Agent...";
}
else
{
foreach(sub in tempString)
{
local subString = split(sub, ",");
if (subString[0] == "1")
{
output += "SSID: <b>" + subString[1] + "</b>";
}
else if (subString[0] == "2")
{
output += "SSID: <i>" + subString[1] + "</i>";
}
else
{
output += "SSID: " + subString[1];
}
if (subString[1].len() > 4 && subString[1].len() <= 9)
{
output += "\\t";
}
else if (subString[1].len() <= 4)
{
output += "\\t\\t";
}
output += "\\tChannel: " + subString[2];
output += "\\tdB: " + subString[3];
output += "\\t\\tOpen: " + subString[4] + "\\r\
";
}
}
output += "
";
res.send(200, "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"2\"></head><body>" + output + "</body></html>");
}
});
device.on(“sendwifi”, function(data) { wifi = data; });
`
Device:
`connections <- [
{ ssid = “wifi1”, pw = “key1” },
{ ssid = “wifi2”, pw = “key2” },
{ ssid = “wifi3”, pw = “key3” }
];
function scanwifi()
{
local scanResults = imp.scanwifinetworks();
local wifi = “”;
foreach (idx,val in scanResults)
{
local connStatus = “0”;
if (imp.getssid() == scanResults[idx].ssid)
{
connStatus = "1";
}
else
{
foreach (idy,val in connections)
{
if (scanResults[idx].ssid == connections[idy].ssid)
{
connStatus = "2";
}
}
}
wifi += connStatus + "," + scanResults[idx].ssid +"," + scanResults[idx].channel + "," + scanResults[idx].rssi + "," + scanResults[idx].open + "|";
}
imp.wakeup(2, scanwifi); //Do this again in 1 minute
agent.send(“sendwifi”, wifi);
}
scanwifi();
server.log(“run2ning”);
server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30);
currentConnection <- 0;
// disconnects from current network (if required)
// tries connecting with the supplied ssid / pw
// executes the callback
function ChangeWifi(ssid, pw, callback) {
// if we’re connected
if (server.isconnected()) {
// flush wifi and disconnect
server.flush(30);
server.disconnect();
}
imp.setwificonfiguration(ssid, pw);
server.connect(callback, 30);
}
// This function doesn’t use the result parameter,
// but it’s required since it’s the callback from
// a server.connect()
function ConnectOrFailToNextConnection(result = null) {
// if we’re connected, do nothing
if (!server.isconnected()) {
// if we’ve already tried all the connections
if (currentConnection > connections.len()) {
// go to sleep for 5 minutes
currentConnection = 0;
imp.deepsleepfor(5*60);
} else {
// if there are still connections to try
// grab current ssid and pw, and increment currentConnection
// for the next attempt (if connection fails)
local ssid = connections[currentConnection].ssid;
local pw = connections[currentConnection].pw;
currentConnection++;
// try connecting
ChangeWifi(ssid, pw, ConnectOrFailToNextConnection)
}
}
}
// On boot, make sure we’re connected
// or try connecting
ConnectOrFailToNextConnection();
// called after imp tries to reconnect to current
// server for 1 minute and fails
server.onunexpecteddisconnect(function(reason) {
// Loop through connections until we connect
currentConnection = 0;
ConnectOrFailToNextConnection();
});`