RPi Zero mesh network setup - overthesun/simoc-sam GitHub Wiki

Create OS image

To flash the SD card with the OS image:

  • Download, install, and launch the latest version of the Raspberry Pi Imager
  • Select the following options and press next:
    • Raspberry Pi Device: RASPBERRY PI ZERO
    • Operating System: RASPBERRY PI OS (LEGACY, 32 BIT) (recommended)
      • If you already have an image, select Use custom instead
    • Storage: select the SD card
  • Would you like to apply OS customization settings?
    • Select EDIT SETTINGS
  • In the GENERAL tab set:
    • hostname: samrpi1/2/3/N
    • Username: pi
    • Password: ...
    • Configure wireless LAN (optional)
    • Set locale settings (optional)
  • In the SERVICES tab set:
    • Enable SSH
    • Use password authentication
    • Click on SAVE
  • Would you like to apply OS customization settings?
    • Select YES
  • Are you sure you want to continue?
    • Select YES
  • When it's done click on CONTINUE and remove the SD card

Connect to the RPi Zero

  • Attach the RPi Zero to the RPi 3 extension (a USB hub with Ethernet should work too)
  • Insert the SD card and connect the Ethernet cable
  • Boot the RPi and find its IP address from the router interface or with a ping sweep, e.g.:
    nmap -sn 192.168.0.0/24
    
  • SSH into the device with ssh [email protected] or ssh [email protected]

Setting up Batman on the RPi Zero

  • Install batctl with
    sudo apt-get install -y batctl
    
  • Create the file ~/start-batman-adv.sh with the following content:
    #!/bin/bash
    # Tell batman-adv which interface to use
    sudo batctl if add wlan0
    # Activates the interfaces for batman-adv
    sudo ifconfig wlan0 up
    sudo ifconfig bat0 up # bat0 is created via the first command
    
  • Make it executable
    chmod +x ~/start-batman-adv.sh
    
  • Create the file /etc/network/interfaces.d/wlan0 with the following content (use sudo):
    auto wlan0
    iface wlan0 inet manual
    	wireless-channel 1
    	wireless-essid sam-mesh
     	wireless-mode ad-hoc
    	wireless-ap e4:5f:01:a1:93:90
    
    You might see a warning that says "bat0: The MTU of interface wlan0 is too small (1500) to handle the transport of batman-adv packets.". This can be fixed by adding mtu 1532 above (above wireless-channel 1, however on the RPi Zero it gives the error "Error: mtu greater than device maximum.". Everything still works with an mtu of 1500.
  • Create the file /etc/network/interfaces.d/bat0 with the following content:
     auto bat0
     iface bat0 inet static
    	address 172.27.0.250
    	netmask 255.255.0.0
    	pre-up /usr/sbin/batctl if add wlan0 
    
  • Change the IP address in the file above so that the last digit corresponds to the hostname (e.g. samrpi4 -> 172.27.0.4)
  • Ensure that the batman-adv kernel module is loaded at boot time:
    echo 'batman-adv' | sudo tee --append /etc/modules
    
  • Stop the DHCP process from trying to manage the wireless lan interface:
    echo 'denyinterfaces wlan0' | sudo tee --append /etc/dhcpcd.conf
    
  • Make sure the IPs in bat0 are updated and the startup script called by adding the following to /etc/rc.local (use sudo):
    # check/update bat0 IP
    python3 /home/pi/simoc-sam/simoc-sam.py fix-ip
    
    /home/pi/start-batman-adv.sh &
    
  • Reboot

Setting up wlan1 on an RPi 4

On an RPi 4, it is possible to connect a WiFi dongle to have an additional network interface (wlan1). With this configuration, wlan0 will still be used for the mesh network, whereas wlan1 can be used to connect to a router and get internet access.

  • Insert the WiFi dongle (TP-Link TL-WN823N)
  • Follow the procedure above
  • Add the following lines at the end of ~/start-batman-adv.py:
    # Startup the wifi dongle (wlan1)
    sudo ifdown wlan1
    sudo ifup wlan1
    
  • Create the file /etc/network/interfaces.d/wlan1 with the following content:
    auto wlan1
    iface wlan1 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    
  • Edit the file /etc/wpa_supplicant/wpa_supplicant.conf with the following content:
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    
    network={
       ssid="NETWORKSSID"
       scan_ssid=1
       key_mgmt=WPA-PSK
       psk="THEVERYSECRETPASSWORD"
    }
    

Troubleshooting

To check the connections:

ifconfig bat0
ifconfig wlan0
iwconfig

sudo batctl n
sudo batctl o
sudo batctl ping

To trace the route to another device:

sudo batctl traceroute 172.27.0.2

Other standard commands work, e.g.:

ping 172.27.0.2
ssh [email protected]

Note that attempting to ping a hostname directly doesn't work, unless you add the .local suffix:

ping samrpi1.local
sudo batctl ping samrpi1.local

In case you get problems like: SIOCSIFFLAGS: Operation not possible due to RF-kill it can be solved by a: sudo rfkill unblock all