How do you assign imp to a 2nd (or 3rd) WiFi hub

I could get my imp assigned to the first WiFi hub using blinkup but as I move from place to place (hub to hub) I still want to be able to develop, test and use my imp. How do I use blinkup to assign imp to 2nd WiFi hub without losing credentials for 1st hub. Is this possible and if so how do I go about it.

here is some code you can try.

Some of it is from the developer pages and I think it was further developed by member @MikeyDK

Please post back if it works for you and especially if you find a way to improve it.

One thing I think you should be careful of is not to use server.log unless you know there is a connection. I think this can cause this code to fail. This is why I wrapped server.log into function mylog.

You can use or discard the _debug functions. I put that in so that I could print out to serial and I did it because i have had some problems with not being able to connect after wifi or ISP outages. I have not figured that problem out yet but I have done some testing with this code under normal circumstances and it seemed to work.

I had a Verizon jetpack and also my Android smartphone that can create a wifi hotspot. I would alternate between these networks and my home network. As I recall it takes some patience waiting for it to connect and maybe? a power cycle but for sure I have witnessed this method work.

`

_wifi <- {
current = 0,
connections = [
{ ssid = “onessid”, pw = “thwpassword” },
{ ssid = “anotherssid”, pw = “another_pwd” },
{ ssid = “ssidwithemptypassword”, pw = “” }
]
};

//// BELOW HERE, THERE SHOULD NOT BE ANY PERSONAL INFORMATION ////////////////

// ***********WIFI CODE **************** {

//MikeyDK
function mylog(note){
if (server.isconnected()){
server.log(note);
}
}

function _debug(note){

    debuguart.write(note);

}

function ChangeWifi(ssid, pw, callback) {

// if we're connected
// disconnects from current network (if required)
// tries connecting with the supplied ssid / pw
// executes the callback 


if (server.isconnected()) {
    // flush wifi and disconnect
    _debug("disconnect, flush 30\

“);
server.flush(30);
server.disconnect();
}
_debug(“setwifi
”);
_debug(ssid);
_debug(pw);
debug("
”);
imp.setwificonfiguration(ssid, pw);
server.connect(callback, 30);
}

function ConnectOrFailToNextConnection(result = null) {

// if we're connected, do nothing
 
// This function doesn't use the result parameter,
// but it's required since it's the callback from 
// a server.connect()

if (!server.isconnected()) {
    // if we've already tried all the connections
    if (_wifi.current > _wifi.connections.len()) {
        // go to sleep for 5 minutes
        _wifi.current = 0;
        _debug("end of list, sleep 5 min\

");
imp.deepsleepfor(5*60);
} else {
// if there are still connections to try
// grab current ssid and pw, and increment _wifi.current
// for the next attempt (if connection fails)
local ssid = _wifi.connections[_wifi.current].ssid;
local pw = _wifi.connections[_wifi.current].pw;
_wifi.current++;
// try connecting
_debug(“going to change wifi
”);
ChangeWifi(ssid, pw, ConnectOrFailToNextConnection)
}
}
else
{
_debug(“connected, about to server_log
”);
server.log("connected: " + imp.getssid());
}
}

server.onunexpecteddisconnect(function(reason) {

// called after imp tries to reconnect to current
// server for 1 minute and fails
// Loop through connections until we connect

_wifi.current = 0;
ConnectOrFailToNextConnection();

});

server.setsendtimeoutpolicy(RETURN_ON_ERROR, WAIT_TIL_SENT, 30);

// On boot, make sure we’re connected
// or try connecting
ConnectOrFailToNextConnection();

//The following line is ALWAYS commented except when troubleshooting
//ChangeWifi( “someotherSSID”, “passwordforwifi”,ConnectOrFailToNextConnection );

// END WIFI CONFIGURATION
//}

`

Thanks this looks interesting

@gerriko, have you tried this yet? success?

I have been using this code for a while but recently found a problem. It seems like the first wifi ssid in the table does not work so I have done a workaround to make a fake ssid and fake password for the first (0th) item in the table. I have been testing by taking my devices between work and home. My home ssid was first in the list.

change this:
connections = [ { ssid = "onessid", pw = "thwpassword" }, { ssid = "anotherssid", pw = "another_pwd" }, { ssid = "ssidwithemptypassword", pw = "" } ]

to this

connections = [ { ssid = "a_totallyfake_ssid", pw = "nonsense" }, { ssid = "myhomessid", pw = "another_pwd" }, { ssid = "myworkssid_withemptypassword", pw = "" } ]

where the first one is not anything I need. Of course this does not make sense at all but I did an experiment and it works. Until I learn the root cause I need to leave my code this way.

I once had to do a blink-up redo because a device I had would not come online with the former code. Recently I was almost in the same boat before I decided to try broadcasting the wifi from my Android device and it came back online!

I seemed to have confirmed this ill behavior

Today I made a totally new model using code from the electric imp documentation. The first ssid on the list will not connect. I can gain access by turning on a second wifi network. I can also correct the problem by adding a nonsense wifi ssid and password.

The workaround is fine but I wish I could understand why the first item on the list does not connect. Tomorrow I am going to reverse the test and put my work wifi in the first spot before I unplug the imp and bring it to work.

_wifi.current is used as a zero-based index, so shouldn’t the test be:

// if we've already tried all the connections if (_wifi.current >= _wifi.connections.len()) {

good eye! yes , I think so. or it can just be “=” comparison.

That does not directly explain the failure to connect, however because the item that does not connect is the first one on the list. Still, seems like a run-time error would occur with that code always causing a reboot instead of the intended behavior.

I will try this correction today and report back

I got the test code here:

electricimp.com/docs/api/server/setsendtimeoutpolicy/

still happens. I have to make a fake ssid and have it be the first, 0th element. I don’t think I can do much more. I am doing my tests by bringing my devices to work and then back home again. When they get stuck I turn on my smartphone hotspot to recover it. Anyone have ideas what is going wrong?