Setting up a Wifi hotspot on a Raspberry Pi 3, turn off AP mode when connected to home network

How do I manage a Raspberry Pi 3 as a stand-alone AP that switches to DHCP mode when successfully connected to a home network? Do I need a custom snap that executes commands using the REST interfaces provided by wifi-ap/network-manager snaps or is there a better tool out there?

Another problem I have is just getting the AP to broadcast properly. I’ve follow this guide, but my RPI just sets its own IP @ 10.0.60.1 and doesn’t show any broadcasted public default ssid “Ubuntu”.

I also installed network-manager snap before wifi-ap.

https://docs.ubuntu.com/core/en/stacks/network/wifi-ap/docs/installation

More info (warning…verbose mode):

I’m working on a wifi on-boarding flow for an IoT device similar to Amazon echo using a Raspberry Pi 3. Ubuntu Core seems to be designed to solve most of the problems with IoT device management so I’m trying it out.

I achieved wifi onboarding without using Core by following this code as a guide:

The user onboard flow looks like this:

  1. Boot RPI, try to connect to any existing network.
  2. After X attempts, launch RPI into AP mode (using hostapd for broadcasting and udhcpd for leasing IP addresses to any client that connects)
  3. Start web server on the RPI and accept connections on port 80.
  4. Clients can connect to RPI’s SSID and select home SSID and enter password for home SSID.
  5. RPI attempts to connect to home wifi, and if successful, closes AP mode and launches IoT controller app.

I’m struggling with how to achieve the above flow with snaps. I can’t find how to setup an open wifi using an ssid that I provide.

I think the flow should look like this:

  1. I (developer) install Ubuntu Core on RPI3, https://developer.ubuntu.com/core/get-started/raspberry-pi-2-3

  2. I install necessary snaps, prepare RPI for customer use.

  • network-manager
  • wifi-ap
  • my custom snap for webserver/IoT controller app
  • other snaps I don’t know I need
  1. On RPI boot, launch custom snap Webserver that manages the wifi. If no wifi exists, send REST command to wifi-ap snap to start AP mode, set static IP. (ex. 10.0.0.2)
  2. Using webserver snap, collects home ssid/password that user inputs.
  3. Webserver collects ssid/password, uses wifi-ap REST interface to a) shutdown AP mode and b) connect to home wifi.
  4. Webserver launches the IoT controller app snap.

Any thoughts on how to achieve this obboarding process? It seems like a common IoT problem so hoping for some guidance.

does this help ?

Thanks for the tip. I had already followed this guide but with no success.

Here’s my reproduction steps:

Fresh Ubuntu Core 16 install
Image name: ubuntu-core-16-armhf+raspi3.img.xz

On first boot, I setup the RPI to use the home wifi connection and login to the store with my Ubuntu One email.
(I know your guide started with an ethernet connection first, then started a wifi. Could this initial wifi connect cause conflicts with the wifi ssid?)

# Set hostname/timezone 
sudo hostnamectl set-hostname pi3
sudo timedatectl set-timezone America/New_York
# Install wifi-ap snap
snap install wifi-ap
# Start wizard
sudo wifi-ap.setup-wizard
# Wizard output
2018/07/25 15:31:41 wifi.ssid=Test123
2018/07/25 15:31:41 wifi.address=192.168.1.1
2018/07/25 15:31:41 dhcp.range-start=192.168.1.2
2018/07/25 15:31:41 disabled=false
2018/07/25 15:31:41 wifi.security=open
2018/07/25 15:31:41 wifi.netmask=ffffff00
2018/07/25 15:31:41 dhcp.range-stop=192.168.1.11
2018/07/25 15:31:41 share.disabled=false
2018/07/25 15:31:41 share.network-interface=eth0
Configuration applied succesfully
# Check status
sudo wifi-ap.status
# --> ap.active: false
# Enable manually
sudo wifi-ap.config set disabled=false
# Check status again...
sudo wifi-ap.status
# --> ap.active: true

Then after 1 minute…I printed these logs

myuser@pi3:~$ sudo journalctl --no-pager -u snap.wifi-ap.management-service
-- Logs begin at Wed 2018-01-03 03:41:28 EST, end at Wed 2018-07-25 15:38:27 EDT. --
Jul 25 15:37:49 pi3 systemd[1]: Started Service for snap application wifi-ap.management-service.
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]: 2018/07/25 15:37:50 Adding route for  /v1/configuration
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]: 2018/07/25 15:37:50 Adding route for  /v1/status
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]: wlan0     Link encap:Ethernet  HWaddr b8:27:eb:ed:de:ec
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]:           inet addr:192.168.1.74  Bcast:192.168.1.255  Mask:255.255.255.0
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]:           inet6 addr: 2600:1700:3170:44c0:ba27:ebff:feed:deec/64 Scope:Global
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]:           inet6 addr: fe80::ba27:ebff:feed:deec/64 Scope:Link
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]:           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]:           RX packets:532 errors:0 dropped:153 overruns:0 frame:0
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]:           TX packets:144 errors:0 dropped:0 overruns:0 carrier:0
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]:           collisions:0 txqueuelen:1000
Jul 25 15:37:50 pi3 wifi-ap.management-service[1207]:           RX bytes:45159 (45.1 KB)  TX bytes:23189 (23.1 KB)
Jul 25 15:37:51 pi3 wifi-ap.management-service[1207]: Error: NetworkManager is not running.
Jul 25 15:37:51 pi3 wifi-ap.management-service[1207]: SIOCSIFNETMASK: Invalid argument
Jul 25 15:37:54 pi3 wifi-ap.management-service[1207]: net.ipv4.ip_forward = 1
Jul 25 15:37:54 pi3 dnsmasq[1364]: started, version  cachesize 150
Jul 25 15:37:54 pi3 dnsmasq[1364]: compile time options: IPv6 GNU-getopt no-DBus no-i18n no-IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset auth no-DNSSEC loop-detect inotify
Jul 25 15:37:54 pi3 dnsmasq-dhcp[1364]: DHCP, IP range 192.168.1.2 -- 192.168.1.11, lease time 12h
Jul 25 15:37:54 pi3 dnsmasq-dhcp[1364]: DHCP, sockets bound exclusively to interface wlan0
Jul 25 15:37:54 pi3 dnsmasq[1364]: reading /etc/resolv.conf
Jul 25 15:37:54 pi3 dnsmasq[1364]: using nameserver 192.168.1.254#53
Jul 25 15:37:54 pi3 dnsmasq[1364]: read /etc/hosts - 7 addresses
Jul 25 15:37:54 pi3 wifi-ap.management-service[1207]: Configuration file: /var/snap/wifi-ap/210/hostapd.conf
Jul 25 15:37:54 pi3 wifi-ap.management-service[1207]: Failed to create interface mon.wlan0: -95 (Operation not supported)
Jul 25 15:37:54 pi3 wifi-ap.management-service[1207]: wlan0: interface state UNINITIALIZED->COUNTRY_UPDATE
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: Could not set channel for kernel driver
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: Interface initialization failed
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: wlan0: interface state COUNTRY_UPDATE->DISABLED
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: wlan0: AP-DISABLED
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: wlan0: interface state DISABLED->DISABLED
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: wlan0: AP-DISABLED
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: hostapd_free_hapd_data: Interface wlan0 wasn't started
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: nl80211: deinit ifname=wlan0 disabled_11b_rates=0
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: /snap/wifi-ap/210/bin/ap.sh: line 49: kill: (1368) - No such process
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: net.ipv4.ip_forward = 0
Jul 25 15:37:59 pi3 wifi-ap.management-service[1207]: Error: NetworkManager is not running.

Looks like it needs network manager, but that doesn’t seem right as your tutorial clearly didn’t need it. I’m not entirely clear what network-manager does in addition to wifi-ap.

Any ideas what could cause the issues shown in the logs?

https://docs.ubuntu.com/core/en/stacks/network/network-manager/docs/configure-wifi-connections

hmm, this is weird, i havent used that setup in a while but will try to check it out on the weekend to see whats going on there …

Thanks for checking it out, I’ll be available this weekend too for more testing if you can think of anything else I could try.

My next test is to use ethernet connection on initial Ubuntu Core setup, then try to go through your guide. I know it probably shouldn’t matter but I want to rule out the possibility of the initial setup wifi connection doing something that prevents a new SSID.

I will report back anything I find.

Since I have a similar use-case as @swhamilton, I’ll post my question here. Please correct me if I should open a new thread instead.
@ogra: Is it possible to configure DNS settings for wifi-ap through the REST API? Like @swhamilton, I need a setup where I can open/close the AP on-demand depending if there’s a WiFi connection or not. Using a local DNS resolver would allow me to have the AP act as a captive portal. The snap contains dnsmasq, but I cannot find an option to specify its settings.

i’m not owning the wifi-ap snap, but looking at the code it seems you can modify the dnsmasq configs in $SNAP_DATA:

1 Like

Thanks for sharing this information. It’s useful. You may also refer router login guide to get some more information about this.