Hello,
I know this discussion has been around, and opinions stated for various reasons. I recently have found that determining the current connected network very tedious, and prone to issues. Let me explain…
I am creating features for a device that allows it to switch to a backup hotspot when there is a connection error for critical messages, not for “normal” messages (poll, etc). I have read, and understand, the API documentation for the imp.setwificonfiguration() and the imp.getssid() calls.
imp.getssid() return the currently “programmed” credentials the imp will look for upon next connect. I get that.
The issue I have is, I can’t believe how hard it is to determine the current connected network. I made a work around, where I call the imp.getbssid() (which if I understand right does display the currently connected network bssid) and compare that to a hard-coded table where the bssid is also stored. This has to be done after connecting, and requires a build to get that data from a certain network.
The issue with referencing the bssid is that:
- You have to know this ahead of time before deploying code to devices in field
- Does not take into account a large network with multiple routers (assuming the bssid is different for each router). The credentials can be the same, but the bssid will not be.
- Not sure when the bssid is updated in the imp (5 seconds after disconnect, etc).
To explain further, I am using some simple code to swap wifi credentials between a default and backup. Try default first, and if fail, go to backup, when completed set back to default (even though it may not be able to connect).
It seems to me that it would be helpful for the device to store not only the ssid and pw from blinkup (or code), but also the bssid from the last known successful connection (once it connects, stores this so it can be referenced later over cold boot). After a while I came up with these functions (complicated, I know):
`local connections = [
{ ssid = "DEFAULT_SSID, pw = “DEFAULT_PW”, bssid = “XXXXXXXXXXXX”},
{ ssid = “BACKUP_SSID”, pw = “BACKUP_PW”, bssid = “YYYYYYYYYYYY” }];
function reconnect() {
// callback doesn’t matter, if we go to sleep while alwaysOn, this
// will continue trying to connect
server.connect(function(reason) {
if (reason != SERVER_CONNECTED) {
//try again
imp.wakeup(3, reconnect);
} else if (reason == SERVER_CONNECTED) {
if (imp.getbssid() == connections[0].bssid) {
Logger.info("Currently connected to WIFI SSID: " + connections[0].ssid);
} else {
Logger.info("Currently connected to WIFI SSID: " + connections[1].ssid);
}
}
}, TIMEOUT_WIFI);
//server.log(“line 2295”);
}
local swapTimer = null;
local altCount = 0;
local swapSpacing = 10;
local resetWifiTimer = null;
local conn = true;
function swapWIFI() {
if (imp.getbssid() == “000000000000”) {
if (conn) {
imp.setwificonfiguration(connections[1].ssid, connections[1].pw);
altCount +=1;
conn = false;
} else {
imp.setwificonfiguration(connections[0].ssid, connections[0].pw);
altCount +=1;
conn = true;
}
swapTimer = imp.wakeup(swapSpacing, swapWIFI);
} else {
Logger.info(“Alternated WIFI credentials " + altCount + " times”);
altCount = 0;
conn = false;
if (imp.getbssid() == connections[0].bssid && imp.getbssid != “000000000000”) {
Logger.info("Currently connected to WIFI SSID: " + connections[0].ssid);
imp.setwificonfiguration(connections[0].ssid, connections[0].pw); // this is to make sure the “set” credentials are really the current ones
Logger.info("SSID currently set for " + imp.getssid());
} else if (imp.getbssid() == connections[1].bssid && imp.getbssid != “000000000000”){
Logger.info("Currently connected to WIFI SSID: " + connections[1].ssid);
imp.setwificonfiguration(connections[1].ssid, connections[1].pw); // this is to make sure the “set” credentials are really the current ones
Logger.info("SSID currently set for " + imp.getssid());
}
}
}
function resetWIFI() {
if (imp.getbssid() == connections[1].bssid) {
imp.setwificonfiguration(connections[0].ssid, connections[0].pw);
resetWifiTimer = imp.wakeup(15, function() {
server.log("Resetting WIFI back to default: " + connections[0].ssid);
server.flush(30);
server.disconnect();
reconnect();
})
} else if (imp.getbssid() == connections[0].bssid) {
server.log("WIFI connected to defualt: " + connections[0].ssid);
} else if (imp.getbssid() == “000000000000”) {
reconnect();
}
}`
I used the conn variable so I could set the function to always try default the first time round swapping… The 15 sec wakup was to let any actions or firmware through before disconnecting. I would call swapWIFI when there is a connection error (onunexpecteddisconnect) and resetWIFI when the operation was done and the device in a normal state. This kinda works, but requires that the bssid be known, and that it doesn’t change.
For imp003 and above, flash is necessary, so is it possible to have the imp auto store credentials in the system allocation? I really would just like to make a call and get the connected network without all these functions…
Thanks!