Never share your Wi-Fi Password Again

An easy way to share your Wi-Fi details using a QR code generated by Python

Vinod Dhole
CodeX
6 min readAug 31, 2022

--

image source : https://unsplash.com/photos/2HWkORIX3II/share

Introduction

How many times has this happened to you, when your friend visits home and asks for Wi-Fi details, and you have to run around to find the password… Let me help ease this process.

In this article I am going to show you how to store the Wi-Fi details in QR code using Python, and One can easily scan the QR code to get the details.

What is a QR Code?

A Quick Response code is a two-dimensional pictographic code used for its fast readability and comparatively large storage capacity. The code consists of black modules arranged in a square pattern on a white background. The information encoded can be made up of any kind of data (e.g., binary, alphanumeric, or symbols)

How to run the code

You can execute the code using “Google Colab” or “Run Locally”

The code is available on Github: https://github.com/vinodvidhole/wifi-qr-code/blob/main/wifi-qr-code.ipynb

Setup and Tools

Run on Colab : You will need to provide the Google login to run this notebook on Colab.
Run Locally : Download and install Anaconda framework, We will be using Jupyter Notebook for writing & executing code.

How to create QR code using Python

Generating QR code using Python is fairly simple, just one line of code and QR code will be ready.

We will use Python library qrcode, Lets Install & Import the Library.

!pip install qrcode --quiet
import qrcode

We will use the qrcode.make() method to generate the QR code. In this method you can pass the data you need to share via a QR code which can be scanned by compatible devices. In the following example I have created a QR code pointing to my medium.com profile.

qrcode.make('https://medium.com/@vinodvidhole')
png

Let’s try to scan this QR code and see if this works.

Scanning was done smoothly and it pointed to the correct url (https://medium.com/@vinodvidhole).

QR code creation is was like a cake walk, let’s move ahead with the next step

QR Code Generation for Wi-Fi

Joining a Wi-Fi network

Generally end-users point of view to connect any Wi-Fi network you will need 2 things

  1. Network Name (SSID) of the current connection.
  2. Password to connect the network.

Users can select the available network and then enter the password manually to join the Wi-Fi network.

QR code to automatically join a Wi-Fi network

We would need to pass the Network Name (SSID), encryption type (WEP, WPA or blank) and password in the specific format (as mentioned below) in the qrcode.make method. Mobile device users can quickly scan the QR code and join networks without having to manually enter the data.

Common format: WIFI:S:<SSID>;T:<WEP|WPA|blank>;P:<PASSWORD>;H:<true|false|blank>;;
Sample: WIFI:S:MySSID;T:WPA;P:MyPassW0rd;;

Let’s implement this.

First we will generate the Wi-Fi details in the above mentioned required format. In Python there is a way to enter the sensitive information like user id, passwords etc. So no one can accidentally share the info in the cloud.

I am going to use getpass() from getpass Python module

from getpass import getpasssensitive_info = getpass()········print(sensitive_info)test

Please see below how this works, I ran the code sensitive_info = getpass(). Then the system prompts me to enter the information. and for demonstration purposes I have printed the values which I entered.

Now let’s enter the Network Name (SSID) and password information

ssid =  getpass()········password = getpass()········security = 'WPA' # (one of WPA or WEP)

Now creating the required Wi-Fi format string

wifi_data = 'WIFI:S:{};T:{};P:{};;'.format(ssid, security, password)

Now you can generate the QR code as shown above and pass the wifi_data

qrcode.make(wifi_data)
png

And there you have it, your scannable Wi-Fi QR code is ready. Let’s check if it’s working.

I was able to join the Wi-Fi network using the QR code!!!

Save the QR code as Image

Let’s move ahead and try to implement it in a way that everyone can use it. Here I am trying to add a nice “Wi-Fi Logo” in the QR code and then we will save the QR code as an image file (.png).

The idea is that anyone can enter Wi-Fi details and generate an image file which can be printed for practical use.

from PIL import Image
from urllib.request import urlretrieve

Here I have created the QR code in a slightly different way since we need to store it in an image, also a function is created such a way the user can customize the QR code (Like changing color, logo etc.)

def generate_qr_code(logo_url, QR_Code_file_name = 'wifi_qr.png', QR_color = 'Black', QR_Back_color = 'white', pixels = 256):

ssid = getpass("Type Network Name (SSID) and press Enter ")
password = getpass("Type Password and press Enter ")
security = getpass("Type Security (WPA or WEP) and press Enter ")

#generate wifi data string
wifi_data = 'WIFI:S:{};T:{};P:{};;'.format(ssid, security, password)

#download logo
urlretrieve(logo_url,'wifi_logo.png')
wifi_logo = Image.open('wifi_logo.png')

# taking base width
basewidth = 100

# adjust image size
wpercent = (basewidth/float(wifi_logo.size[0]))
hsize = int((float(wifi_logo.size[1])*float(wpercent)))
wifi_logo = wifi_logo.resize((basewidth, hsize), Image.ANTIALIAS)
#QRCode object
QR = qrcode.QRCode(version=1, #integer from 1 to 40 that controls the size of the QR Code (the smallest, version 1, is a 21x21 matrix)
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10
)

# adding wifi data
QR.add_data(wifi_data)

# generating QR code
QR.make(fit=True)

# adding color to QR code
QR_image = QR.make_image(
fill_color=QR_color,
back_color=QR_Back_color
).convert('RGBA')


# set size
pos = ((QR_image.size[0] - wifi_logo.size[0]) // 2,
(QR_image.size[1] - wifi_logo.size[1]) // 2)

QR_image.paste(wifi_logo, pos)

QR_image = QR_image.resize((pixels, pixels), Image.NEAREST)

# save the image file
QR_image.save(QR_Code_file_name)

print('QR code generated!')

Let’s call the function. I have uploaded logo image here

logo_url = 'https://i.imgur.com/GeIxGmO.png'
generate_qr_code(logo_url,
QR_Code_file_name = 'wifi_qr.png')
Type Network Name (SSID) and press Enter ········
Type Password and press Enter ········
Type Security (WPA or WEP) and press Enter ········
QR code generated!

The wifi_qr.png file should be available in the File → Open Menu. You can download the file and print the image.

This concludes the tutorial.

References

References to some useful links.

Conclusion

So next time someone asks for the Wi-Fi details just show them the QR code!!! Usability of QR code is limitless. You can use this technique to share Contact details, Resume link, Website link etc. Hope you can build great projects using this article.

If you have any questions or any feedback feel free to post a comment or contact me on LinkedIn. Thank you for reading and Until next time… Happy coding !!

--

--

Vinod Dhole
CodeX
Writer for

Sr. Technical Architect | Machine Learning | Python