Nerves: How to switch between Access Point and WiFi-connection mode while providing an mDNS name?

Hi there! I have asked some slightly related questions on the Nerves Slack channel already, but that is of course not a medium to treat an in-depth question in much detail.

Here is my use case: For a pilot, I am building/programming a raspberry pi, which at its core reads data from a smart electicity meter, and forwards this data to a server.

Since each of these devices ends up in the home of another person, with another WiFi network, it is important to be able to easily set up the correct WiFi connection settings (and also be able to change them later).

So the current idea was to let the Nerves device switch between AP mode and WiFi mode every minute or so, until a successful connection can be made with the configured WiFi settings. As soon as someone visits the Phoenix web-interface where you can configure these settings, the device will stop switching mode until you leave it, to not drop the connection at an inappropriate time.

From the pointers other people already gave me in the Nerves slack channel, I know that the VintageNetWizard exists, which theoretically does more or less this. However, it is currently still missing some features that the system I am building would require:

  1. mDNS support. nerves_init_gadget supports this (although until now I have only been able to make nerves_init_gadget's mDNS work over an external WiFi and not over Rpi-as-AP) but it conflicts with VintageNet. Another possibility would be a ‘Captive Portal’ (the kind of “log in to wifi” portal that you also see on public WiFi networks) but I have no idea how those are set up (and findinf information about them online proved rather difficult).
  2. Because of the confict with nerves_init_gadget, VintageNet also does not allow e.g. updating firmware over SSH and other quality-of-life improvements to the development phase that I really appreciatae.

So then it seems like I should just use NervesNetwork and build my own simple abstraction for persistence of settings and a web-UI on top of that. However, I have not been able to configure Nerves.Network and Nerves.InitGadget properly to switch at runtime between Rpi-as-AP and Rpi-connected-to-remote-WiFi modes.

It would be really appreciated if someone can help me with the way these libraries ought to be configured for this use case. Especially Nerves.InitGadget seems to mostly be built to be configured in the Mix config files only. However, looking in the code it does seem like its configuration is only applied once a connection is (re)starteed so maybe Application.put_env should be used?

And if you have information on how to solve this problem in any other way, then that would of course also be very welcome! :smiley:

1 Like

Hey there, I’m the author of the mDNS library. One thing to note is that none of the mobile browsers work with mDNS! It’s very frustrating. They really push you to build an app in their ecosystem to do any sort of local network stuff.

I’ve got a project that does work in the way you are describing, but not with a mobile browser obviously. It’s really quite old code using some of the old networking libraries, probably not worth looking at, but feel free, https://github.com/rosetta-home/cicada/tree/master/lib/network_manager

Basically, you should get an event from SystemRegistry that you have a bound IP address, whether that’s the AP or the DHCP given out from the router, you want to tell mDNS to register your name with that address, or change the registration once it’s authed to the router.

3 Likes

While VintageNet is in development and isn’t ready for general Nerves usage yet, it does work fine with mdns, ssh, and sending firmware updates over ssh.

nerves_init_gadget is really only a library aggregator to make starting out with Nerves easier. It does have a decent amount of glue code to support the USB gadget use case, but it doesn’t sound like you need that.

Entone wrote the mdns support. You can find the ssh firmware update library at nerves_firmware_ssh. Both are configurable independent from nerves_init_gadget and it may even be easier to configure them separately.

As for AP mode configuration, FarmBotOS supports this with nerves_network. I don’t know where the code is offhand, but I think you might be able to search through their repository to find it. However, they are helping out with VintageNet and are migrating over to it since it makes adding some enterprise WiFi features they need easier (and maybe other reasons).

3 Likes

Thank you very much for your in-depth reply, @fhunleth!
I’ll probably need some help with the proper configuration of mdns, vintage_net and the other applications that make up Nerves’ new networking stack, but for now this has answered my basic question. Thank you! :heart:

2 Likes

Following up on this thread check out https://github.com/pcmarks/mdns_lite. It’s a new library that works very well with VintageNet.

EDIT: For those still looking for a nerves_network implementation you can check it out here

2 Likes

This minimal example may help as well:

As we make progress on vintage_net, I’ll integrate updates here so that they’re in one place. One note on this, I added ssh support in a rush and it’s lacking proper supervision so you may see ssh access go away after a few days of uptime. It will work in the near term, and I hope to find a library to manage ssh support at transition to that.

2 Likes

Thank you so much, @ConnorRigby and @fhunleth!

I’ll try to work with the vintage_net_example and hopefully get it to do the mDNS stuff as well.

1 Like