Qqwy

Qqwy

TypeCheck Core Team

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:

Marked As Solved

fhunleth

fhunleth

Co-author of Nerves

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).

Also Liked

entone

entone

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, cicada/lib/network_manager at master · rosetta-home/cicada · GitHub

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.

Qqwy

Qqwy

TypeCheck Core Team

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:

ConnorRigby

ConnorRigby

Nerves Core Team

Following up on this thread check out GitHub - pcmarks/mdns_lite: A simple implementation of an mDNS (multicast Domain Name System) server. · GitHub. 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

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement