Yesterday, the 11th, was the day I tied all my little WiFi scripts together into the wifi-wrap.py wrapper.
- I made wifi-chek.py to check if the WiFi is connected.
In the wifi-wrap.py file, the wifi-chek.py script runs immediately after wifi-pw.py sends the P4wnP1_cli command to Connect to WiFi.
- I also wrote a 1-line script to reset the WiFi to the Startup settings. This script is wifi-reset.py.
P4wnP1 OLED Connect-to-WiFi List of Scripts (JoyFi?)
Here is the current list of scripts included in the OLED P4wnP1 Connect-to-WiFi script package (in order of where they're placed in the wrapper .py file):
- Runs all the scripts
- Scans for WiFi networks
- Displays the scanned WiFi networks
- Asks the user for WiFi password (using OLED screen for output and Joystick / buttons for input). Also runs a Python3 subprocess to launch the P4wnP1_cli connect-to-WiFi command.
- Checks the WiFi status, pings google.com, and offers options.
- Resets the WiFi to its Startup condition.
How does wifi-chek.py work?
Wifi-chek.py runs the classic linux command iwconfig to fetch internet wireless configuration data from the Raspberry Pi (or any Linux computer).
Why use iwconfig to check WiFi status instead of a more Python-native code block such as the one at shown at this kite.com page?
I wanted to use a "raw" Linux command. I also wanted Signal Strength and the other info that comes with the output of iwconfig.
Note: There's also this way to check active connections in Python.
Step 1: Run the iwconfig Command
First, wifi-chek runs "iwconfig" Linux command in a Python3 subprocess.
I use the Python "try" command to attempt running iwconfig and simultaneously handling errors how I want it to.
Step 2: Get the Data
Then it parses the output of iwconfig using the Python3 ".strip" method.
Step 3: Do the WiFi Network Check
After that, it looks for "SSID:" in the parsed output.
TIP: To look for text in a string using Python, check out this simple method on Stackexchange.
Why? If the device is connected to a WiFi network, then the text "SSID:" will appear in the output of iwconfig.
Therefore, if the string "SSID:" is found, then wifi-chek.py determines that the WiFi is connected to a network.
If "SSID" is not found, then wifi-chek.py _____________.
Step 4: Run the Ping Check Command
After the SSID is checked, wifi-chek.py pings google.com.
Wifi-chek runs a Python3 subprocess to "ping -c3 google.com".
If google.com returns data to us, then wifi-chek.py determines that P4wnP1 is now connected to the Internet (DNS is good!).
If google.com does NOT return data to us, then wifi-chek.py ______________.
Step 5: Offer Options
- Turn off WiFi
- Reset WiFi
- Speed Test
- Check Internet / Ping
- If wifi-scan.py detects 0 APs, then sleep 3 seconds and scan again X times until a network is found. Otherwise exit.
- Why? If the system isn't ready to scan, the scan could fail, resulting in a false negative "0 APs detected".
- Add OLED display screens for every stage of the process
- Please wait
Untested alternative to iwlist:
iw dev wlan0 scan
Switch to WiFi "Managed" mode:
Another name for this is Client mode.
Make a script that runs these commands:
sudo ifconfig wlan0 down
sudo iwconfig wlan0 mode Managed
sudo ifconfig wlan0 up
sudo iwlist wlan0 scan