Create a hotspot when no Access Point available

Hi,

i have a working magic mirror using docker on a raspberry pi 3B. It flawlessly works as long as i am in my home network.
This is about to be a present, so my overall goal is to be able to dynamically host an access Point in case no known AP are in range. This way i can have my friend to still access the mirror and configure it without having me come around and trying to fix the network issue he has, when changing his SSID or WPA-Key

When i try to activate hotspot using dietpi-config i get the following result

sudo dietpi-config
[  OK  ] DietPi-Config | systemctl start hostapd
[  OK  ] DietPi-Config | Desired setting in /boot/dietpi.txt was already set: SOFTWARE_WIFI_HOTSPOT_SSID=magic-mirror
[  OK  ] DietPi-Config | Desired setting in /boot/dietpi.txt was already set: SOFTWARE_WIFI_HOTSPOT_CHANNEL=9
[  OK  ] DietPi-Config | Desired setting in /boot/dietpi.txt was already set: SOFTWARE_WIFI_HOTSPOT_KEY=SECRET_PHRASE
[  OK  ] DietPi-Config | Desired setting in /etc/hostapd/hostapd.conf was already set: ssid=magic-mirror
[  OK  ] DietPi-Config | Desired setting in /etc/hostapd/hostapd.conf was already set: channel=9
[  OK  ] DietPi-Config | Desired setting in /etc/hostapd/hostapd.conf was already set: wpa_passphrase=SECRET_PHRASE
[  OK  ] DietPi-WiFiDB | mkdir -p /etc/wpa_supplicant
[  OK  ] DietPi-WiFiDB | eval > /var/lib/dietpi/dietpi-wifi.db
[ INFO ] DietPi-WiFiDB | Applied WiFi DB slot 0 with SSID "CLEARNET"
[ INFO ] DietPi-WiFiDB | Applied WiFi DB slot 1 with SSID ""
[ INFO ] DietPi-WiFiDB | Applied WiFi DB slot 2 with SSID ""
[ INFO ] DietPi-WiFiDB | Applied WiFi DB slot 3 with SSID ""
[ INFO ] DietPi-WiFiDB | Applied WiFi DB slot 4 with SSID ""
[  OK  ] DietPi-WiFiDB | chown root:root /var/lib/dietpi/dietpi-wifi.db /etc/wpa_supplicant/wpa_supplicant.conf
[  OK  ] DietPi-WiFiDB | chmod 600 /var/lib/dietpi/dietpi-wifi.db /etc/wpa_supplicant/wpa_supplicant.conf
[  OK  ] DietPi-Config | systemctl stop isc-dhcp-server
[ SUB1 ] DietPi-Set_hardware > wifimodules (enable)
[ INFO ] DietPi-Set_hardware | Please wait, enabling WiFi modules...
[ INFO ] DietPi-Set_hardware | Checking for required APT packages: iw wireless-tools wpasupplicant wireless-regdb
[  OK  ] wifimodules enable | Completed
[ INFO ] DietPi-Config | Dropping network connections, please wait...
[ INFO ] DietPi-Config | Restarting network connections, please wait...
[  OK  ] DietPi-Config | systemctl daemon-reload
Internet Systems Consortium DHCP Client 4.4.1
Copyright 2004-2018 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth0/b8:27:eb:23:f0:44
Sending on   LPF/eth0/b8:27:eb:23:f0:44
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 4
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 8
DHCPOFFER of 192.168.1.169 from 192.168.1.1
DHCPREQUEST for 192.168.1.169 on eth0 to 255.255.255.255 port 67
DHCPACK of 192.168.1.169 from 192.168.1.1
bound to 192.168.1.169 -- renewal in 18890 seconds.
[  OK  ] DietPi-Config | systemctl start isc-dhcp-server
[ INFO ] DietPi-Config | Reloading networking data, please wait...
[  OK  ] DietPi-Config | Network restarted

What’s weird on this, is that the subnet is not what i configured. (should be .42.0/27 subnet not .1.0/27 subnet)
Correct me if i am wrong:
My generali assumption on DHCP

  • each subnet can have 0, 1 dhcp servers.
  • having multiple dhcp servers will conflict (authorative config)
    So i guess it is possible to have my actual network (this one uses eth0 for simplicity reasons, as i don’t want to lose network connections all the time i am messing around with that config) using subnet 192.168.1.0/27 and host a Hotspot using subnet 192.168.1.0/27

The hotspot is not hosted though.
All my services seems to be running:

  console-setup.service                                       loaded active exited  Set console font and keymap
  containerd.service                                          loaded active running containerd container runtime
  cron.service                                                loaded active running Regular background program processing daemon
  dbus.service                                                loaded active running D-Bus System Message Bus
  dietpi-postboot.service                                     loaded active exited  DietPi-PostBoot
  dietpi-preboot.service                                      loaded active exited  DietPi-PreBoot
  dietpi-ramlog.service                                       loaded active exited  DietPi-RAMlog
  docker.service                                              loaded active running Docker Application Container Engine
  fake-hwclock.service                                        loaded active exited  Restore / save the current clock
  getty@tty1.service                                          loaded active running Getty on tty1
  ifup@eth0.service                                           loaded active exited  ifup for eth0
  ifup@wlan0.service                                          loaded active exited  ifup for wlan0
  ifupdown-pre.service                                        loaded active exited  Helper to synchronize boot up for ifupdown
  isc-dhcp-server.service                                     loaded active running LSB: DHCP server
  keyboard-setup.service                                      loaded active exited  Set the console keyboard layout
  kmod-static-nodes.service                                   loaded active exited  Create list of static device nodes for the current kernel
  networking.service                                          loaded active exited  Raise network interfaces
  rngd.service                                                loaded active running Start entropy gathering daemon (rngd)
  ssh.service                                                 loaded active running OpenBSD Secure Shell server
  systemd-fsck-root.service                                   loaded active exited  File System Check on Root Device
  systemd-fsck@dev-disk-by\x2dpartuuid-2a5aca42\x2d01.service loaded active exited  File System Check on /dev/disk/by-partuuid/2a5aca42-01
  systemd-journal-flush.service                               loaded active exited  Flush Journal to Persistent Storage
  systemd-journald.service                                    loaded active running Journal Service
  systemd-logind.service                                      loaded active running User Login Management
  systemd-modules-load.service                                loaded active exited  Load Kernel Modules
  systemd-random-seed.service                                 loaded active exited  Load/Save Random Seed
  systemd-remount-fs.service                                  loaded active exited  Remount Root and Kernel File Systems
  systemd-sysctl.service                                      loaded active exited  Apply Kernel Variables
  systemd-sysusers.service                                    loaded active exited  Create System Users
  systemd-tmpfiles-setup-dev.service                          loaded active exited  Create Static Device Nodes in /dev
  systemd-tmpfiles-setup.service                              loaded active exited  Create Volatile Files and Directories
  systemd-udev-trigger.service                                loaded active exited  Coldplug All udev Devices
  systemd-udevd.service                                       loaded active running Rule-based Manager for Device Events and Files
  systemd-update-utmp.service                                 loaded active exited  Update UTMP about System Boot/Shutdown
  systemd-user-sessions.service                               loaded active exited  Permit User Sessions
  unattended-upgrades.service                                 loaded active running Unattended Upgrades Shutdown
  user-runtime-dir@1000.service                               loaded active exited  User Runtime Directory /run/user/1000
  user@1000.service                                           loaded active running User Manager for UID 1000
$ cat /etc/network/interfaces
# Location: /etc/network/interfaces
# Please modify network settings via: dietpi-config
# Or create your own drop-ins in: /etc/network/interfaces.d/

# Drop-in configs
source interfaces.d/*

# Ethernet
allow-hotplug eth0
iface eth0 inet dhcp
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
#dns-nameservers 192.168.1.1

# WiFi
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.0.1
netmask 255.255.255.0
#gateway 192.168.0.1
#dns-nameservers 9.9.9.9 149.112.112.112
pre-up iw dev wlan0 set power_save off
post-down iw dev "wlan0 set power_save on

# iptables NAT rules
up iptables-restore < /etc/iptables.ipv4.nat
up ip6tables-restore < /etc/iptables.ipv6.nat

Not sure if this one conflicts with /etc/network/interfaces

$ cat /etc/network/interfaces.d/wlan0
iface wlan0 inet static
  address 192.168.42.99
  network 192.168.42.0
  netmask 255.255.255.0
  broadcast 192.168.42.255
  wpa-ssid "magic-mirror"
  wpa-psk "SECRET_PHRASE"
  wpa-group TKIP CCMP
  wpa-key-mgmt WPA-PSK
$cat /etc/dhcp/dhcpd.conf
ddns-update-style none;
default-lease-time 600;
max-lease-time 7200;
log-facility local7;

subnet 192.168.42.0 netmask 255.255.255.0 {
        range 192.168.42.10 192.168.42.50;
        option broadcast-address 192.168.42.255;
        option routers 192.168.42.1;
        option domain-name "local";
        option domain-name-servers 9.9.9.9, 149.112.112.112;
}

Any help appreciated

Hi, did you manage to get this working? I am looking for this solution for some time now…

I could not find a way to enable an access point on the pi.
If I remember correctly dietpi uses other components of WiFi support than other distros (I am used to Debian based like Ubuntu)… But don’t nail me on that.

I found a workaround, which I would not consider as secure though, but for the purpose it’s ‘good enough’…
Instead of hosting an Access point on the pi, you can configure a second AP connection in the pi, which does not exist with a pre known wpa key.

In case you need to reconfigure the device you can spawn a hotspot with those credentials.

I tried it with an android phone, which worked flawless. Had to use a laptop, to then also connect to that network in order to reconfigure the pi.

Hope this helps.

hostapd (and ifupdown) which we use, is THE Debian standard stack for APs/hotspots (and network in general). And of course you can use any tool/stack on DietPi, that works on vanilla Debian and Ubuntu.

The question is not so much how to setup a hotspot (dietpi-software has an option for that), but how to toggle it on each boot, depending on whether an AP is detected. Not sure whether there is a native ifupdown/wpasupplicant implementation for this, at least returning somehow when no AP was found. Worst case would be to parse journalctl -u ifup@wlan0, and depending on its result stop the interface, exchange the config, restart interface hostapd and DHCP server.

My idea was this:

I was use a cron job to check for an available gateway…
If the gateway is gone I enable the AP config.

Example for the shell script

#!/bin/bash
# get Gateway IP Address
gateway_ip=$(ip route | awk '/default/ { print $3 }')

# Check gateway availability
gateway_available=false
ping -c 1 -W 1 $gateway_ip > /dev/null 2>&1 && gateway_available=true

# Enable hotspot if gateway is unavailable
if ! $gateway_available; then
    echo "Gateway ($gateway_ip) is not reachable. Activating hotspot..."
    sudo systemctl stop hostapd
    sudo systemctl stop dnsmasq
    sudo systemctl start hostapd
    sudo systemctl start dnsmasq
else
    echo "Gateway ($gateway_ip) is reachable. Hotspot not activated."
fi

done
EOT

I really can’t remember the circumstance, which blocked me from enabling the AP. This gift is already delivered. Still I would consider another try in a few month, since I want to do it with a rpi zero w in a slightly different setup.

1 Like

I have found this solution: (Raspberry Connect - Raspberry Pi Automatic Hotspot and Static Hotspot Installer). Unfortunately, it seems it doesn’t work with ifupdown, but maybe it will provide some inspiration :wink:

1 Like

Probably better to check the version for Debian Bookworm Raspberry Connect - Automated Switching - Access Point or WiFi Network

Anyway, you can use NetworkManager and remove ifupdown. At least if you don’t care about some DietPi own tools not working anymore. Means you can’t use DietPi own network config, once switched to NetworkManager. But in theory it’s working.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.