Python help - changes to a Raspberry Pi script

This project makes it easier to connect a headless Raspberry Pi to a wifi network.

So, if I take my Pi to a new environment, it does the following:

  1. Looks in wpa_supplicant.conf for a password for all visible wifi networks in the room.
  2. Fails to find a suitable entry (because I have never been there before)
  3. Turns the Pi into a local server and wireless Access Point and broadcasts its SSID

On my phone, I:

  1. Connect to the Access Point
  2. Browse to 10.0.0.1
  3. Access the RaspiWifi page running on the Pi
  4. Select the room SSID
  5. Enter the password

The Pi then reboots and connects to the room SSID.
Sweet!

The problem is that the current code overwrites the wpa_supplicant.conf instead of appending to it.
So, when I get home, I have to repeat the whole process to reconnect, and will have to repeat again when I go back to the remote location.

I know just enough programming to brief a programmer, but not enough to actually do the work!
Can anyone help? @Kyle?

Here’s what I know:

“\RaspiWiFi-master\libs\configuration_app\app.py” lines 87 to 105 are responsible for creating the new wpa_supplicant.conf and overwriting the existing one.

There is a command line script (wpa_cli) which is native to RaspbianOS that allows one to programatically interact with the existing wpa_supplicant.conf - adding and deleting entries.

The manual for wpa_cli is here.

A forum post demonstrating its use is here

I just tried wpa_cli in interactive mode and it worked (added another wifi).

I guess one challenge would be knowing what network entry to add.
It appears that the command “add network” returns the numerical identifier of the next entry that should be added. That identifier would need to be used to construct the command line that actually enters the network credentials.

The changes to the RaspiWifi code seem relatively trivial:
Line 88 to 105 are replaced with (warning, PSEUDOCODE):

// return the numerical id of the next network to be added
wpa_cli add_network

// turn the returned id into a variable $ID
code here

// Enter a new entry for the wifi
wpa_cli set_network $ID ssid “$SSID”
wpa_cli set_network $ID psk “$PASSWORD” (this may have to be in HEX)

// Write to file
wpa_cli save_config

1 Like

One thing you could try is changing line 88 in app.py to replace the ‘w’ with ‘a’

As far as I can remember, ‘w’ mode overwrites anything in the file whereas ‘a’ mode appends new info to the end

Thanks for taking a look Sean

Lines 90 to 94 are a header section in the wpa_supplicant.conf file, so appending would result in multiple headers. Also, the temporary file is deleted during the move operation on line 105, so there would be nothing to append to.

However, your approach will be useful if it proves to be impossible to use wpa_cli - I could duplicate current conf to temp, and then strip out the header rows (these will already be in the conf file, so they don’t need to be written again).

There’s a bug in that file, line 103, temp_conf_file.close should be temp_conf_file.close() but I don’t think that will fix it.
I don’t think it’s as simple as just appending that text to the file instead of replacing it, but I’ll have to look later.

I haven’t had a chance to download the code and give a proper fix a try but there’s an outstanding pull request that creates a base conf and then appends the new network. https://github.com/jasbur/RaspiWiFi/pull/52
In the vein of Sean’s reply, I would think checking if the real file exists and has a valid header would be a reasonable case to append. Not a python expert so not sure if the tmp file method was used in order to avoid weird behavior in writing partial changes interacting with file buffering. Personally, I would build a string of the new network config and append directly in that that case.