0

I'm trying a solution that can change or connect to a new Wi-Fi network with a Python script. After executing the code below that changes the content of 'wpa_supplicant.conf', the change of the file works by inspecting the file with sudo nano /etc/wpa_supplicant/wpa_supplicant.conf. But after rebooting the Pi to get it to connect to the new network, the Pi couldn't connect to any networks since then.

SSID = input('Enter SSID: ')
password = input("Enter password: ")

with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w') as file:
    content = \
        "country = MY\n" + \
        "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev\n" + \
        "network={\n" + \
            '\tssid="' + SSID + '"\n' + \
            '\tpsk="' + password + '"\n' + \
            "\tkey_mgmt=WPA-PSK\n" + \
        '}'

    file.write(content)

When executing the sudo iwlist wlan0 scan command line, I got an error of

wlan0 interface doesn't support scanning network is down

"Network is down" was removed after executing ifconfig wlan0 up. But the error still shows "wlan0 interface doesn't support scanning" when I execute sudo iwlist wlan0 scan.

There are no soft or hard blocks seen for Wireless LAN when executing rfkill list.

The only way I could think of solving this issue was to reformat and reinstall Raspbian, but I've spent countless hours setting up the OpenCV environment and other third-party libraries, and I don't want to go through that tedious setup process again. Is there any other way?

2 Answers2

0

It is far from clear what you are trying to do, however if your Question is how to restore wireless networking after corrupting your wpa_supplicant file all you have to do is put a correctly formatted wpa_supplicant file in the boot partition and reboot.

Of course you could just restore from your backup.

Milliways
  • 54,718
  • 26
  • 92
  • 182
  • I've already tried that by creating a 'wpa_supplicant.conf' file with my Windows PC, just like the first time when I install Raspbian on the SD card. But it still doesn't work after creating the file and rebooting the Pi. – Shawn Khoo Sep 30 '21 at 03:26
  • Use an editor that doesn't use CR/LF as line terminators. If this doesn't work you have damaged something else. See [How to set up networking/WiFi](https://raspberrypi.stackexchange.com/a/37921/8697) – Milliways Sep 30 '21 at 03:34
0

I've spent hours on trying to get this issue resolved but ended up no luck. I even tried creating a new 'wpa_supplicant.conf' file on the boot drive of the SD card on my Windows PC but still, my Pi could not connect to any of the Wi-Fi networks. So, I reformatted and reinstalled Raspbian OS on it.

My plan was to connect to a new Wi-Fi network by rewriting the 'wpa_supplicant.conf' file from a Python script, but this method will break the Wi-Fi system. Luckily, I found a solution to do this.

Solution: Instead of rewriting 'wpa_supplicant.conf' using with open('/etc/wpa_supplicant/wpa_supplicant.conf', 'w') as file:, rewrite the 'interfaces' file from the /etc/network/interfaces path.

You will see this initially in the 'interfaces' file: enter image description here

Add this code below the source-directory /etc/network/interfaces.d line:

auto wlan0
iface wlan0 inet dhcp
wpa-ssid "WIFI NETWORK NAME HERE"
wpa-psk "WIFI NETWORK PASSWORD HERE"

Save the 'interfaces' file and open the 'wpa_supplicant.conf' file:

Add update_config=1 between the lines of ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev and network={.... From my observation, this kind of ignores the contents of network={... and divert its retrieval of Wi-Fi information to the /etc/network/interfaces file.

Save the file and reboot.

Once rebooted, you can rewrite the /etc/network/interfaces file by executing a Python script whenever you want to change to a new network (the script must lead to a system reboot at the end to allow the new Wi-Fi changes to take effect). (I don't know whether the 'interfaces' file allows multiple networks to be added into, so far I only tested with one network at a time, since this has already fulfilled my goal.) Here's a Python code sample to change the network information in the 'interfaces' file:

import os
import time

SSID = input('Enter SSID: ')
password = input("Enter password: ")

with open('/etc/network/interfaces', 'w') as file:
    content = \
        '# interfaces(5) file used by ifup(8) and ifdown(8)\n\n' + \
        '# Please note that this file is written to be used with dhcpcd\n' + \
        "# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'\n" + \
        '# Include files from /etc/network/interfaces.d:\n' + \
        'source-directory /etc/network/interfaces.d\n' + \
        'auto wlan0\n' + \
        'iface wlan0 inet dhcp\n' + \
        'wpa-ssid "' + SSID + '"\n' + \
        'wpa-psk "' + password + '"\n'

    file.write(content)

print("Write successful. Rebooting now.")
time.sleep(2)
os.system('sudo reboot now')