Configuring WiFi on a Raspberry Pi
WiFi on a Raspberry Pi
Adding WiFi to a Raspberry Pi (RPi) is a very simple process if you are using the latest version of Raspian, and can be very helpful if you want to use your RPi in an outdoor environment where being connected via an Ethernet cable is not possible. If you have a RPi 3 then the WiFi is already built in for you, if not then you will need to plugin a WiFi dongle to one of the usb ports. This guide will take you through the steps to set up the WiFi via the graphical user interface (GUI) and command line (CLI).
WiFi via the Graphical User Interface (GUI)
In the Desktop environment locate the network icon on the top right had side of your menu bar.
Click on the icon to see a list of available WiFi networks.
Select your network and you will presented with a popup requesting your router password (This can normally be found on the back of your router).
Enter your password and you should now see your network icon change to a WiFi icon.
Your RPi should now be configured for WiFi, if not then configuration via the command line interface may be necessary.
WiFi via the Command Line Interface (CLI)
If you are using a headless configuration then you will need to SSH to your RPi, if you are connected via a monitor then select Menu/Accessories/Terminal. To scan for your WiFi network use the following command
This command will provide you with the ESSID, again you will need to know the password for your router.
Next we will need to edit the wpa_supplicant.conf file by entering the following command
Go to the bottom of this file and enter the following statement including your ESSID and password
ssid=”Your_ESSID_from_previous_step”
psk=”Your_Wi-Fi_password”
}
Ensure that you include the double quotes.
The wpa_supplicant should pick up within a few seconds that your WiFi network has been added and make the connection, if not reboot the RPi
If you still don’t have a WiFi connection double check that you have entered you ESSID and password correctly.
Check For a WiFi Connection
If you want your RPi to operate autonomously in a headless state, we need to ensure that in the event of losing our Wi-Fi connection that the RPi will attempt to reconnect on it’s own. To achieve this we will add a shell script called Wifi_Check.sh and edit our crontab file. We can use the crontab to repeatedly execute a check that the Wi-Fi is active, and if it isn’t to attempt reconnection.
Wifi_Check.sh was written by Kevin Reed and is free to use provided that the preamble and licensing information at the top of the script is maintained. You can download a copy from my HydroPi GitHub Repository.
Click the RAW button at the top right hand side of the code, then select and save the code to notepad (or similar). Save the file as WiFi_Check.sh and then FTP using Filezilla or similar it to the RPi folder /home/pi/.
Below is a screenshot of the Filezilla login screen, enter your host IP address, username, password, set the port to 22 and select Quickconnect.
By uploading it to the /home/pi folder it will keep the code consistent with the location in the script. If you do choose to save the code somewhere else then you will need to change line 36 to reflect this.
Finally you will need to edit your crontab file, open a CLI session and enter the following command
If this is the first time you have run crontab then it may present you with 3 options, select 2 /bin/nano
Add the following command at the bottom of the file, this will execute the Wifi_Check.sh script every 5 minutes.
(If you saved the script elsewhere replace /home/pi/ with your chosen location)
To check that you have set everything up properly try executing the script.
You should see the following results.
That’s it, you should now be connected to your Wi-Fi network.
Any thought’s, improvements or errors let me know in the comments below.
Hi Dominic,
I will be bookmarking your site basically on the grounds that it has a great deal of good data. Also, you have portrayed it in such a straightforward method for how to associate Wi-Fi on a Raspberry Pi. Truly had no clue about how simple it is. Much obliged to you for such a brilliant article loaded with data.
Yours Truly,
Carlton
No worries Carlton, glad you found it useful