Skip to content

Instantly share code, notes, and snippets.

@luispedro
Last active July 8, 2023 19:09
Show Gist options
  • Save luispedro/c51d9a4f93bd7d0df94e77f7431325be to your computer and use it in GitHub Desktop.
Save luispedro/c51d9a4f93bd7d0df94e77f7431325be to your computer and use it in GitHub Desktop.
Connect to a WiFi networks using a QR code
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