Copyright ©

Mongoose OS Forum


Wifi Configuration at Run Time

I want to flash a code in esp8266 in which end user can change their wifi credentinals at run time , but I have no idea how to achieve this. plz help.


  • After you flashed the firmware, wifi settings can be updated with a command like:

    $ mos config-set wifi.sta.enable=true wifi.ap.enable=false wifi.sta.ssid=YOUR_SSID wifi.sta.pass=YOUR_PASS

    Which will cause the device to reboot and apply settings.

  • jfcurranjfcurran United States

    I would like to configure the wireless station and AP settings within my app during execution - not via mos. Is there some example code for that ? I tried using the wifi API but ended up with execution faults...presumably because of incomplete callback routine configurations. But I can't find real clear examples or documentation for that matter.

    Thanks for any pointers,

  • I'm only a beginner, so take my advice with a grain of salt, but you can change the config settings at run-time, and the wifi SSID and password are config settings.

    Thanked by 1jfcurran
  • @Raj, You can change the config on the fly as @SeanBDurkin mentioned and then the device needs to be rebooted to reload the new config.

    Cfg.set({wifi: {sta: {ssid: 'SSID', pass: 'PASSWORD'}}});

    Just ensure that you have a condition when calling Sys.reboot(0). Otherwise, you'll enter permanent reboot and you'll have to reflash the firmware.

    Thanked by 1jfcurran
  • jfcurranjfcurran United States

    Thanks folks - what I am getting from this is
    1) changes can only be made to the configuration, not the running wifi config
    2) and you can only realize those changes by rebooting the device.

    So if you wanted a device to switch between networks based on some condition you'd have to factor in a reboot with your logic. Gosh....

    I'd like to know if a device connects to the AP. And with the network callback infrastructure it seems like there has been some deprecation. Is there a good example of monitoring devices as they connect and disconnect from the AP?

    My desired config is that this esp8266 system running MongooseOS receives TCP/IP connections from other devices and accepts data from them. Then it forwards that data on to other devices either by Bluetooth or possibly TCP/IP..

    Thanks again,

  • @jfcurran, when you change WiFi setting via the mos UI that's what it does:

    [Feb 15 19:50:47.230] Getting configuration...
    [Feb 15 19:50:48.010] Setting new configuration...
    [Feb 15 19:50:49.349] Saving and rebooting...

    So, I guess there is no other way, however, it takes only a few seconds to reboot microcontrollers, so in most cases, it shouldn't be that big of a deal.

    There is an api_events library that can listen for specified events and an api_net library has a few of them:

    So you can have something like:

    Event.addGroupHandler(Net.EVENT_GRP, function(e, callback, userdata) {
      if(e === Net.STATUS_GOT_IP) {
        print('connected to the wifi and received an IP address');
      } else if (e === Net.STATUS_DISCONNECTED) {
        print('disconnected'); //not sure if disconnection from other servers also trigger this event. Check that before relying on it.
    }, null);
    Thanked by 1jfcurran
  • jfcurranjfcurran United States

    Thanks very much Alex. I agree on the short reboot time I just object to having to save state and lose control of execution in order to make a minor change. But I'm new to Mongoose OS and there is so much functionality available.

    I saw those status codes in the mgos_net.h and was trying to use them with strange results. I'll try harder!

    Thanks again,

Sign In or Register to comment.