My experience using python to manipulate Wi-Fi hardware is very limited.
my script below works fine on a raspberry pi model 3 B+ connecting to Wi-Fi ‘tiphone’ (iPhone hotspot) but when it goes to connect back to the other Wi-Fi (gooberbest) it presents a dialog box for the user to enter passphrase. Why is this?
this a a small function I’m working on for a portable data collection device. In the office it connects to gooberbest, when it leaves it tethers to iPhone hot spot via Wi-Fi, upon return it reconnects to gooberbest.
my understanding is the iPhone hotspot does not constantly broadcast, so my thought is to loop thru reading active SSIDs until i find the iPhone SSID. The iPhone is set to maximum compatibility. The results I had tethering via USB led me to believe that was not a reliable connection method.
this is my code:
# -*- coding:UTF-8 -*-
import os, time, re, sys
from wireless import Wireless
#### empty list for ESSIDs
active_wireless_networks = []
#### SSID and Password for target WiFi network
goal_ssid="tiphone"
goal_pw="---------"
#### Boolean to use if target WiFi found
target_wifi_found=False
#### loop to run scanning WiFi until target SSID found
while target_wifi_found==False:
wifi_count=0 # for number of SSID's found
unique_wifi_count=0 # to count unique SSID's found
#### scan wlan0
lines = os.popen("sudo iwlist wlan0 scan").read().split("n")
for line in lines: #iterate through each line returned by previous command
line = line.rstrip() #remove spaces a end of each line
if line.find("SSID:") >= 0: #filter to just lines that contain 'SSID:'
line=line.lstrip() #remove spaces a beginning of each line
wifi_count =wifi_count+1 # increment the number of SSID's found
ssid=line[7:-1] # remove the first seven (ESSID:") and the last character (") from the current line
if ssid=="tiphone": # check if this is the target SSID
target_wifi_found=True # if yes set boolean to True
active_wireless_networks.append(ssid) # add the current SSID to the list of active_wireless_networks list
print (wifi_count, " SSID's have been found, ", len(set(active_wireless_networks)), "are unique")
if target_wifi_found==False: #checking if target SSID is found
print ("tiphone NOT one of them, sleeping 5 seconds then rescanningn")
### loop starts over if tiphone SSID not found ###
print ("Wi-Fi tiphone FOUND,going to connect")
wire = Wireless() # define wire
wire.connect(ssid=goal_ssid,password=goal_pw) #connect to target wifi
time.sleep(20) #pause program to allow wifi to connect
### computer connecting to target wifi now ###
print ("going to confirm which wifi now connected to")
if 'tiphone' in str(subprocess.check_output(['iwgetid -r'], shell=True)).split(''')[1][:-2]:
print("Connected to target wifi,tiphonenn")
wifi="tiphone"
if 'gooberbest' in str(subprocess.check_output(['iwgetid -r'], shell=True)).split(''')[1][:-2]:
print('I am connected to gooberbest Wi-Fi!')
time.sleep(60) # pause profram to allow thigs to settle
### for testing purposes reconnect to other WiFi - gooberbest
print("reconnecting to gooberbestnn")
wire.connect(ssid="gooberbest",password="--------")
time.sleep(60)
print ("going to check which wifi now connected to")
if 'tiphone' in str(subprocess.check_output(['iwgetid -r'], shell=True)).split(''')[1][:-2]:
print('I am connect to iphone Wi-Fi!')
wifi="tiphone"
if 'gooberbest' in str(subprocess.check_output(['iwgetid -r'], shell=True)).split(''')[1][:-2]:
print('I am connected to gooberbest Wi-Fi!')
there’s no error message, just when it tries to reconnect to the Wi-Fi it was originally connected to, one of my saved Wi-Fi connections the Raspberry OS opens a dialog box for the user to enter the passphrase. This is undesirable.
Lastly, the results the script produces are not always consistent, sometimes it fails to connect to either Wi-Fi. I am I using the best method to establish a WIFI connection?