2020年12月18日 星期五

Yocto WiFi configuration for automatic connection at boot

Objective: This writeup will cover WiFi configuration on Raspberry Pi using the Yocto tool. The goal is to have an automatic WiFi connection on boot-up

Recommended prerequisite articles to read:

  1. Custom Raspberry Pi Image Build with Yocto

In the previous article, we built a console-only image for the Raspberry Pi 4. For such a headless set up, we need a smooth way to interact with the device. One of the most convenient way is the use of the SSH protocol that can allow us to remotely access the raspberry pi and even transfer files to it via scp. However, to use these tools the board and the host machine need to be connected in a way such as being in the same local network or connected to the internet. Hence to connect the Pi to the internet, we need to enable the WiFi.

We don’t want to be manually configuring the WiFi every time a reboot occurs. At the time being, we can assign a static IP address to our board since this is for in-house development. We will also hard code the router/network SSID and our password. All these can be performed after bootup but we want to have all the network configurations performed automatically at boot up.

1. Configure SSID and passphrase

Edit the wpa_supplicant.conf-sane in the following path … /meta/recipes-connectivity/wpa-supplicant/wpa-supplicant/wpa_supplicant.conf-sane

ctrl_interface = /var/run/wpa_supplicant
ctrl_interface_group = 0
update_config = 1
 
Network = {
               ssid = "SSID"
               psk = "PASSWD"
}

2 . Script to initialize the wpa_supplicant and configure WiFi at boot up

Create a custom script “setup-wifi.sh” at the following path …/meta/recipes-core/initscripts/initscripts-1.0/setup-wifi.sh

#!/bin/sh
ifconfig wlan0 10.233.174.16   #Set the static IP address, should be unique
wpa_passphrase SSID  PASSWD> /etc/wpa_supplicant.conf
route add default gw 10.233.174.254  #Router IP address
wpa_supplicant -B -i   wlan0    -c    /etc/wpa_supplicant.conf
echo “nameserver 8.8.8.8” >> /etc/resolv.conf
echo “nameserver 10.233.174.254” >> /etc/resolv.conf
: exit 0

3. Edit initscript recipe to enable WiFi configuration at boot up

Edit the initscript bitbake recipe at … /meta/recipes-core/initscripts/initscripts-1.0.bb that incorporates the “setup-wifi.sh” and installs it in /etc/initscripts directory after build.

SRC_URI = "
file://setup-wifi.sh \
"
do_install () {
install -m 0755    ${WORKDIR}/setup-wifi.sh     ${D}${sysconfdir}/init.d
  
update-rc.d -r ${D} setup-wifi.sh start 99 2 3 4 5 .
}
  
MASKED_SCRIPTS = " \
setup-wifi \
"

SSH

With the above configurations, rebuild the core-image as explained in the previous article. On bootup, you can test the WiFi configuration by issuing the commands:

ifconfig            //should show the IP address of the Pi as hard coded in the Yocto image
ping google.com        //shows that the DNS server(s) hardcoded are working

With the above network configurations, we can access the board remotely via ssh and transfer files via scp from a Linux machine.

On the Linux machine, we enter the IP address we assigned to the board as below:

ssh root@10.233.174.16

This opens a terminal console of the board remotely and we can access all the files remotely.

 

資料來源: https://reiwaembedded.com/raspberry-pi-yocto-wifi-configuration-for-automatic-connection-at-boot/

沒有留言: