Connect to WIFI with specific BSSID (parsing issue)

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
cchhsu
Posts: 1
Joined: Fri Sep 14, 2018 6:04 am

Connect to WIFI with specific BSSID (parsing issue)

Post by cchhsu » Fri Sep 14, 2018 6:38 am

When I parsed the BSSID string from a.config file to nic.connect("SSID", "password", bssid=BSSID bytes variable) using the micropython version: 1.9.4, I always got the error message. (ValueError)

:a.config
key="305A3A51B448"

code:
f = open('a.config')
f_read = f.read()
f.close()
key = bytes(f_read.split('\n')[0].split('=')[1], 'utf-8')
x =['\\x' + key[i:i+2] for i in range(0, len(key), 2)]
b_ssid=bytes(''.join(x), 'utf-8)
import network
nic = network.WLAN(network.STA_IF)
nic.active(True)
nic.connect("AC66", "12345678", bssid=b_ssid)
After running this code, show error messages as below:
Trackback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError:

But, I input the bssid value as \x30\x5A\x3A\x51\xB4\x48 manually, I am able to connect to AP successfully.
nic.connect("AC66", "12345678", bssid=b'\x30\x5A\x3A\x51\xB4\x48')

Have anybody know how should I solve this issue?
Thanks.

jarekd
Posts: 13
Joined: Mon Aug 21, 2017 3:54 pm

Re: Connect to WIFI with specific BSSID (parsing issue)

Post by jarekd » Tue Sep 25, 2018 10:41 am

Not sure about your method. Just posting the one I created - for now it works flawlessly, connecting to strongest from known networks.
If someone spots anything that can be optimised in the code, feel free to post your sugestions :)

Code: Select all

# wifi.py

import network, utime

_wifi_list = {'network1':'password1', 'network2':'password2'}

network.WLAN(network.AP_IF).active(False)
wifi = network.WLAN(network.STA_IF)
wifi.active(True)

def enable():
	#print("Scanning networks...")
	if wifi.isconnected():
		print("[WIFI] Already connected")
		i = wifi.ifconfig()
		print("[WIFI] IP address: " + str(i[0]))
	else:
		wifi_scan = wifi.scan()
		if wifi_scan:
			wifi_scan = sorted( wifi_scan, key = lambda x: -x[3]) # sorts by fourth element in wifi tuple (rssi)
			for w in wifi_scan:
				w = w[0].decode('utf8')
				if w in _wifi_list:
					print("[WIFI] Connecting to: '"+str(w)+"'")
					wifi.connect(w, _wifi_list[w])
					t0 = utime.time()+10 #timeout after 10 secs
					while not wifi.isconnected(): #enable to wait for connection
						utime.sleep(0.5)
						if t0 < utime.time():
							print("[WIFI] Connection timed out")
							break
				if wifi.isconnected():
					break
			if wifi.isconnected():
				print("[WIFI] Connected")
				i = wifi.ifconfig()
				#j = i[0].split('.')[2]
				#wifi.ifconfig(('192.168.'+str(j)+'.120', i[1], i[2], i[3]))
				print("[WIFI] IP address: " + str(i[0]))
			else:
				print("[WIFI] No known AP found")
		else:
			print("[WIFI] No APs found")

def disable():
	wifi.disconnect()

def check():
	return wifi.isconnected()

SpotlightKid
Posts: 377
Joined: Wed Apr 08, 2015 5:19 am

Re: Connect to WIFI with specific BSSID (parsing issue)

Post by SpotlightKid » Fri Sep 28, 2018 10:00 am

To the OP: the code you posted contains several errors:

1. There is a syntax error inline 6, the closing single quote after ut-8 is missing.
2. You are not parsing your config file correctly, you don't account for the double quotes around the key value.
3. This is the big one: byte strings do not work theway you think they do. The \xNN notation is for literal strings or the representation of unprintable characters in strings/byte strings when outputting them. You can't contruct a string dynamically with them.

Here's how I would parse your config:

Code: Select all

import ubinascii

config = dict()
with open('a.config') as fp:
    for line in fp:
        if not line.strip():
            continue
        key, val = line.strip().split('=', 1)
        config[key.strip()] = val.strip().strip('"')

bssid = ubinascii.unhexlify(config['key'])
print(bssid)

Post Reply