Python Script to Configure WiFi On Raspberry Pi Without Reboot

Vivek Maskara
2 min readAug 13, 2022

--

Photo by Harrison Broadbent on Unsplash

In this short post, I will walk you through the steps of configuring Wifi on a Raspberry Pi device using a Python script. Generally, a reboot is required after a new Wifi config is added to the config file. We will learn how to avoid this step so that the newly added wifi config can be applied without a reboot.

Background

The wifi configs on a Raspberry Pi are stored in the following file.

/etc/wpa_supplicant/wpa_supplicant.conf

The wpa_supplicant.conf file contents should look something like this:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid="<YOUR_WIFI_NAME>"
psk="<YOUR_WIFI_PASSWORD>"
}

Required Steps

Here’s a list of steps that we need to perform

  • Make the /etc/wpa_supplicant/wpa_supplicant.conf file editable by modifying its permissions
  • Update the wpa_supplicant.conf file with the desired wifi credentials.
  • Refresh the Wifi configs on the Raspberry Pi to apply the changes. Once we refresh the configs, the Raspberry Pi will connect to the newly added config.

Python Script

Here’s the python script that will do all of the above steps for you. It contains the configure_wifi that takes in the wifi credentials and performs the required steps.

That’s it for this post. Please leave a clap if you found this article useful. Consider subscribing to Medium to read more of my stories.

--

--