In the last few days, I downloaded the OLED-P4wnP1 ALOA .img file and flashed it onto a MicroSD card.
After tinkering around with various WiFi-config Python scripts without success, I wandered toward the P4wnP1 CLI.
P4wnP1 CLI (Simply amazing)
Within a minute or two, I was able to instantly connect my P4wnP1 to my WiFi network using P4wnP1 CLI.
For me, this was a huge success in the scope of my current goal: Allow a user to connect to a WiFi network using only the buttons on the front of the OLED P4wnP1.
Since I've essentially already put the pieces together to get the WiFi network name and ask the user for the WiFi password, getting the P4wnP1 to use that info to connect to WiFi was the last big piece of the process.
P4wnP1 Connect to WiFi Using OLED Buttons ONLY
To review, here's the flow of how I see the OLED P4wnP1 connect-to-WiFi process happening:
- Scan for WiFi networks
- [DONE] I just finished the "core" of this function last night. The "iwlist-parser" script from Github does the actual WiFi scanning using the "iwlist" command. I modified the iwlist-parser script to export the Top 10 highest-signal WiFi networks (sorted by signal strength) to a text file.
sudo python3 wifi-scan.py wlan0
- Display WiFi networks on OLED screen
- [DONE] After the scanned WiFi networks are sent to a text file, my custom-written "oledHorizMenu" Python script reads the same text file and displays the contents of the file on the OLED screen.
sudo python3 wifi-display.py
- User presses a physical button on OLED screen to select WiFi network name
IN PROGRESS] Have the script return the WiFi network name - and/or replace the text file contents with just the 1 selected WiFi network name. Selected WiFi network name is sent to ssid.txt.
- [TO DO] Ask user for WiFi password using my custom-written OLED-joystick input script
- [TO DO] Use P4wnP1-CLI to connect P4wnP1 to WiFi using selected WiFi network name and user-submitted password
- [TO DO] Test connection / show connection status using WiFi icon.
- [BONUS] Add CLI speedtest function
Progress made today:
- wifi-scan.py successfully scans Top 10 highest-strength WiFi networks.
- Then outputs the list to ssids.txt (plural, not singular) - sorted by signal strength.
- wifi-display.py successfully loads the WiFi networks from ssids.txt.
- Then it displays the list on the OLED screen starting with the first item.
- After that, the user can select a WiFi network on the OLED screen using the OLED joystick.
- Press the Joystick center button to select a WiFi network.
- Pressing this button writes the name of the selected WiFi network to ssid.txt (singular, not plural).
- Done! The scripts (when combined) scan for WiFi, display to user, and write selection to txt file.
- Next: Run my custom oledP4wnP1 user-input script to ask the user for WiFi password.
- After that: Run P4wnP1-CLI to connect P4wnP1 to WiFi Internet using WiFI network name from ssid.txt and password from user input.
- After that: Test connection with "ping -c3 google.com"
- After that: Add speed test
WiFi Scan-and-Select Linux-CLI DEBUGGING Workflow:
- python3 wifi-scan.py
- tail ssids.txt # To make sure the scan script sent to txt properly
- python3 wifi-display.py # Now use the joystick to select a network
- Ctrl+C to close the display program
- tail ssid.txt # To make sure the display script sent to txt properly
To Do: Write a wrapper script to execute Steps 1 and 3 above, in order to combine everything into 1 easy script.
Update 1: Added code to skip empty SSIDs
if myessid == '':
print('Skipping Empty SSID when writing to file')
elif myessid != '':
f.write(myessid + '\n')
And I changed the wifi-display.py script in a way that writes the SSID to text file without extra info (such as Signal strength).
Ex: Now shows "NETGEAR69" instead of "1) NETGEAR69 -53".
Since the list is auto-sorted by signal strength anyway, it's unnecessary and confusing to display the signal strength. That makes it too complex and can hide some of the SSID.
So the WiFi connect command can simply read the text file without stripping or removing any characters.
Update 2: Wrapper Script wrap.py
I made a wrap.py python script which opens a subprocess to launch wifi-scan.py and wifi-disp.py.
So now the WiFi Scan-and-Select package includes 3 files:
Just run the command below to launch the whole shebang:
sudo python3 wrap.py
The result is the user-selected SSID written to ssid.txt in the current folder.
Here's where I'm currently at in the scope of the Connect-OLEDP4wnP1-to-WiFi project:
0. DONE - Take the iwlist-parser script, output the top 10 network SSIDs to ssids.txt file.
1. DONE - Take oledhorizmenu and load the ssids.txt file as the menu
2. DONE - Change oledhorizmenu to show the FIRST 3 lines of txt file instead of LAST
3. DONE - On center button press, write ssid to ssid.txt
3B. Make a wrapper script (wrap.py) to call all the other scripts (wifi-scan.py, wifi-disp.py, wifi-pwin.py etc)
4. IN PROGRESS - Launch the oledp4wnp1 input script from within wrap.py
5. TO DO - Have the input script temporarily store the password in a variable
5. TO DO - From within the input script, launch the "P4wnP1_cli wifi set sta SSIDgoeshere PWhere or whatever" to connect to the wifi!
Replaced Code with Loop in wifi-scan.py
I also replaced a long chunk of 10 chunks of code with ONE loop. This also completely circumented the "index out of range" python error I kept getting (when trying to access a variable that wasn't there). Hooray for loops!
Update 3: Added wifi-pw.py into wifi-wrap.py!
Now the wrapper script scans, selects, and then asks for password.
Now just to get the input script to store the Password in a variable.
Then run the P4wnP1_CLI command to connect to WiFi!
Then run a ping command to see if Google responds?
If so, say "Connected". If not, then blah.
Update 4: Boned the WiFi & ReFlashing My OLED P4wnP1 Image (Do over)
On trying to complete the Connect-to-WiFi process, my p4wnp1 installation was unable to properly manage the WiFi connection. This was true even though just a few hours earlier it worked fine.
So my conclusion was that I changed some p4wnp1 setting which boned the WiFi.
I collected my python scripts, flashed a new image of a p4wnp1 installation that I know works onto a fresh SD card, and I'm currently deploying the Connect-to-Wifi environment onto that new installation.
I edited the BeBox gui.py file by adding a new menu item under Wireless Things.
The new menu item is "Connect to WiFi"
I told the gui.py file to load my connect-to-wifi scripts (in the same exact way my wifi-wrap.py script does).
But I needed to install a dependency: numpy (pip3 install numpy)
I'm currently waiting for that to finish (takes forever since it builds a wheel or whatever, which is slow on RPi 0 W). Building/installing numpy on Raspberry Pi 0 W is so slow that I may re-build the .img file after it's done, so I don't have to do it again.
After this finishes, I'll complete moving the connect-to-wifi package to the fresh BeBox install. I do this by testing the wifi-wrap.py script in /P4wnP1/myscripts/oledhorizmenu/ and see if there are any unmet dependences (other than numpy) left to be installed. Then I'll move the connect-to-WiFi script "package" to /P4wnP1/ and test it there.
Finally, test it using the actual BeBox menu script.
Hopefully the BeBox menu actually launches my scripts.
Update 5: Set Up My WiFi Connect Package in P4wnP1 BeBox + Test CLI
I currently have 2 mini objectives:
- Successfully use the P4wnP1_cli in BeBox to connect to WiFi (client mode) using specified SSID (network name) and PSK (password)
- Get BeBox to run my scripts successfully
- Finish the last piece by passing the password-variable from wifi-pw.py to the subprocess command which runs P4wnP1_cli.
After installing numpy on Bebox's OLEDP4wnP1, install fontawesome and PIL (Pillow):
- pip3 install fontawesome
- Fast install
- pip3 install Pillow
- Builds a wheel, so takes a long time to install (and is a 49MB .tar.gz file download)
The Raspberry Pi Zero W finally finished installing Pillow, fontawesome, and numpy on BeBox P4wnP1 OLED.
I ran my script-wrapper (wifi-wrap.py) and it executed seemingly-well, except the BeBox OLED menu script interfered with it (wrestles with it). Basically I need to kill the gui.py process. Then run my script.
So here's how to kill a process in Python by name:
How to Kill BeBoX gui.py Process
ps aux | grep gui.py
Then run the "ps aux..." part again to make sure gui.py was killed.
Finally, I re-ran my wifi-wrap.py script - and - Voila! It worked!
At this point, I want to do two things:
1) Test P4wnP1_cli
- Manually run the P4wnP1_cli command to put the RPi 0 W into client mode and connect it to my WiFi network.
2) Insert the successfully-tested command into wifi-pw.py
- Add the same command into the end of wifi-pw.py to connect P4wnP1 to WiFi using the WiFi network name chosen by the user - and password entered using joystick.
Result 1: Test P4wnP1_cli
Manually-Running P4wnP1_cli to Connect to WiFi Worked!
I tested this command:
P4wnP1_cli wifi set sta -n -s MyWiFiNetworkName -k MyPassword
and almost instantly received this output:
mode:STA_CONNECTED channel:4 ssid:"home"..
Additionally, the P4wnP1 ALOA web GUI auto-refreshed to show the new WiFi network name (and password - eesh - so be careful on that).
Note: I used my Android Google Pixel 3 phone's WiFi hotspot for this test. I made the WiFi network name nice and short ("home") so it's quick and easy to type in. Keep this in mind (time to enter info using joystick) when creating your WiFi hotspot, if you choose to do that instead of connect P4wnP1 to a land-line type WiFi network.
Last, but not least, I ran a ping on google.com to make sure DNS works right:
ping -c3 google.com
Result 2: (IN PROGRESS) Add the p4wnp1_cli command into wifi-pw.py
Not yet done. TBD.
This would accomplish the goal of connecting P4wnP1 to WiFi, but it would skip past the BeBox GUI. That's fine though - could even run BeBox GUI after connecting to WiFi.
Extra: Testing BeBox's Gui.py with My WiFi-Connect Scripts
So I tried using BeBox's gui.py to run my scripts, but nothing happened.
I'll investigate in the morning and start a new post on it. Hopefully tomorrow I at least get my script to successfully run the P4wnP1_cli command to connect RPi 0 W to WiFi using only the OLED buttons!