Connecting Bela to Wifi

Because Bela systems are full Linux computers, you also have access to a USB connection for a WiFi dongle if you want your Bela system to connect to the internet or a local wireless network.

This article details how to set up the network connection on your Bela to use a Wifi dongle.

Table of contents

  1. Types of Wifi dongles
  2. Setting up your network connection with Wifi
  3. Sharing wifi over USB with Bela (Mac OS X)
  4. Using Bela as a wi-fi hotspot

Types of Wifi dongles

We tested Bela with these Wifi dongles:

  • Ralink RT5370
  • Ralink MT7601U 148f:7601

Most Linux-compatible dongles should work, provided that their drivers are included in the 4.4.113 Linux kernel running on Bela. If the drivers are not included, you will have to compile the drivers for this kernel to get the dongle to work.

Setting up your network connection with Wifi

1. ssh into your Bela system

From a terminal on your computer, connect to your Bela board by running:

$ ssh root@bela.local

You’ll know you’ve connected to your Bela board when you see this prompt:

root@bela:~#

2. Make sure your dongle is visible on your board

Run this command to list your devices:

root@bela:~# lsusb

If your dongle does not show up, reboot your Bela board.

3. Get the name of your wifi device

Run this command:

root@bela:~# ip a

One of the devices that is listed will be called wlanX, where X is a number - this is the name of your network device.

If no wlan device shows up, you need to install drivers for it.

4. Edit your interface list

In this example we use wlan1 as the name of your interface. Replace this with the device name you found in the previous step.

Here you’ll edit the /etc/network/interfaces file. Open it in the command line text editor with this command:

root@bela:~# nano /etc/network/interfaces

Instructions on how to configure the file are included as comments at the bottom of the file. Make sure you set the settings using the interface name of your device that you found in the previous step.

When you’re ready to exit, click CTRL + X to edit, and Y to save any changes you made.

5. Set your network name and password

Run the following command, replacing YourNetworkName with the name of your Wifi network and YourNetworkPassword with the Wifi network’s password:

root@bela:~# wpa_passphrase YourNetworkName YourNetworkPassword >>/etc/wpa_supplicant/wpa_supplicant.conf

Then, reset your device for these changes to take effect (don’t forget to replace wlan1 with your device name):

root@bela:~# ifdown wlan1; ifup wlan1;

6. Verify your settings

Make sure your settings are correct by running the following:

root@bela:~# ip a

If your network is configured correctly you’ll see your device name listed with an IP address.

Sharing wifi over USB with Bela (Mac OS X)

This is a way of connecting your Bela system to the internet via the USB connection with your laptop.

1. Enable Internet Sharing

Go to System Preferences > Sharing, and enable Internet sharing:

Important

Enabling Internet Sharing will interrupt the IPv4 connection between your laptop and your Bela system. This means you won't be able to reach the Bela IDE or the Bela command line using its IP address. However, bela.local is still advertising its IPv6 address, so you can access the IDE and the command line using bela.local.

2. Edit the interfaces config file

Run the following command:

root@bela:~# pico /etc/network/interfaces

This will open the file in the Pico editor.

Comment out the usb0 static config IP from /etc/network/interfaces by adding a # at the beginning of the line.

Then add these lines:

iface usb0 inet dhcp
iface usb1 inet dhcp

The interface file should then look like this:

auto usb0
iface usb0 inet dhcp

#iface usb0 inet static
#    address 192.168.6.2
#    netmask 255.255.255.0
#    network 192.168.6.0

# USB ECM gadget (macOS / Linux compatible)
auto usb1
iface usb1 inet dhcp

#iface usb1 inet static
#    address 192.168.7.2
#    netmask 255.255.255.0
#    network 192.168.7.0

Save and exit Pico when you’re done with Ctrl + X.

3. Restart the network service

To restat the networking service, run:

root@bela:~# systemctl restart networking.service

Using Bela as a wi-fi hotspot

Here are instructions on how to set up your Bela as a wireless access point which means you will be able to connect to your board and bring up the IDE – very useful for reprogramming boards which are mounted in projects. This assumes you have an image v0.3.3 or above and that you have a Linux-compatible wifi dongle plugged into Bela (see above for discussion of suitable dongles).

  1. Edit the file /etc/default/hostapd (e.g.: using nano /etc/default/hostapd), find the line that starts with DAEMON_CONF , uncomment it and change it so it looks like this:
    DAEMON_CONF="/etc/hostapd/hostapd.conf"
    

    Save and close the file.

  2. The above edit told hostapd to look for a configuration file, so now we need to create it:
    nano /etc/hostapd/hostapd.conf
    
  3. Put the following into this file:
    interface=wlan0
    hw_mode=g
    channel=1
    wmm_enabled=1
    ssid=MyBelaAccessPoint
    auth_algs=1
    wpa=2
    wpa_passphrase=bela1234
    wpa_key_mgmt=WPA-PSK
    rsn_pairwise=CCMP
    ieee80211d=1
    country_code=DK
    ieee80211n=1
    

    Note: you may want to change the ssid=(network name) and wpa_passphrase=(password) lines. Ensure your passphrase is at least 8 characters long. Save and close the file.

  4. Next we need to change the network configuration so that Bela advertises its network instead of trying to connect to one. To do that, edit /etc/network/interfaces and replace the wlan0 configuration section (if any) with the following:
    allow-hotplug wlan0
    iface wlan0 inet static
     hostapd /etc/hostapd/hostapd.conf
     address 192.168.30.1
     netmask 255.255.255.0
    

    Again, you may want to change the address to suit your taste. This is the IP address of your board on this network.

  5. We then want to ensure the board acts as a DHCP server, assigning suitable IP addresses to the devices that connect to it. To do this, first edit /etc/default/isc-dhcp-server and change this line from
    INTERFACESv4="usb0 usb1"
    

    to

    INTERFACESv4="usb0 usb1 wlan0"
    

    then edit /etc/dhcp/dhcpd.conf and add in these extra lines:

    subnet 192.168.30.0 netmask 255.255.255.0 {
       range 192.168.30.2 192.168.30.254;
    }
    

    Ensure that the range here (the range of IP addresses that are assigned to connected devices) is compatible with the address and netmask values set in /etc/network/interfaces above.

Testing the hotspot

Now you can test that the network can be successfully started:

ifdown wlan0; ifup wlan0 --verbose

The output of the above command should give something like:

ifup: configuring interface wlan0=wlan0 (inet)
/bin/run-parts --exit-on-error --verbose /etc/network/if-pre-up.d
run-parts: executing /etc/network/if-pre-up.d/hostapd
hostapd: /usr/sbin/hostapd -B -P /run/hostapd.wlan0.pid /etc/hostapd/hostapd.conf
Configuration file: /etc/hostapd/hostapd.conf
wlan0: interface state UNINITIALIZED->COUNTRY_UPDATE
run-parts: executing /etc/network/if-pre-up.d/wireless-tools
run-parts: executing /etc/network/if-pre-up.d/wpasupplicant
/bin/ip addr add 192.168.30.1/255.255.255.0 broadcast 192.168.30.255 	  dev wlan0 label wlan0
/bin/ip link set dev wlan0   up

/bin/run-parts --exit-on-error --verbose /etc/network/if-up.d
run-parts: executing /etc/network/if-up.d/avahi-daemon
run-parts: executing /etc/network/if-up.d/openssh-server
run-parts: executing /etc/network/if-up.d/upstart
run-parts: executing /etc/network/if-up.d/wpasupplicant

If you had any errors in your files, they would hopefully show up here.

Now you should be able to connect to the MyBelaAccessPoint network (or whatever you called it) with the password bela1234 (or whatever you set it to above). Once connected, your device should be assigned an address in the range 192.168.30.2 to 192.168.30.254 (or whatever range specified above), and you should be able to access the board on 192.168.30.1 (or whatever you set it to above).

A few tests:

  • open the address 192.168.30.1 in the browser: does the Bela IDE show up?
  • open a terminal, run ssh root@192.168.30.1 then echo $SSH_CLIENT. To me it shows 192.168.30.2 62879 22. In your case it will be slightly different, but the important part to check is that the IP address displayed there is of the range you specified in dhcpd.conf. If it is something else (e.g.: a completely different subnetwork, such as 192.168.6.x or 192.168.7.x, then something went wrong).
  • power off the board and reboot it. Verify that the wireless networks still comes up, and you can still connect to it.
  • power the board from an external power source (i.e.: do not connect it to your computer over USB). Verify that the wireless network still comes up, and you can still connect to it. In this configuration, depending on your system, you may also be able to access the Bela board at bela.local/ (try that in a browser and/or via ssh: ssh root@bela.local).