Yesterday, the 11th, was the day I tied all my little WiFi scripts together into the wifi-wrap.py wrapper.

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.

 

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):

 

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

To Do:

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

x