Last active
July 8, 2023 19:09
-
-
Save luispedro/c51d9a4f93bd7d0df94e77f7431325be to your computer and use it in GitHub Desktop.
Connect to a WiFi networks using a QR code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import cv2 | |
import subprocess | |
pat = re.compile(r'^WIFI:S:([^;]+);T:WPA;P:([^;]+);;') | |
detect = cv2.QRCodeDetector() | |
cv2.namedWindow("preview") | |
vc = cv2.VideoCapture(0) | |
rval, frame = vc.read() | |
while rval: | |
cv2.imshow("preview", frame) | |
rval, frame = vc.read() | |
value, points, straight_qrcode = detect.detectAndDecode(frame) | |
if value: | |
print(value) | |
if mat := pat.match(value): | |
ap, pwd = mat.groups() | |
print(f'Connecting to {ap} (password: {pwd})') | |
subprocess.check_call( | |
['nmcli', 'd', 'wifi', | |
'connect', ap, | |
'password', pwd]) | |
break | |
key = cv2.waitKey(20) | |
if key == 27: # exit on ESC | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment