Qr code wifi python

Creating 3D Printed WiFi Access QR Codes with Python

Over the weekend, I embarked on a project to create a 3D printed QR code that guests at our house could scan to gain access to our guest wireless network. Why 3D you might ask? Well, that’s how geeks like myself like to impress their guests! Also, let’s be real, I have a 3D printer at home, and I was looking for a fun way to put it to practical use. It turns out that it makes for some nice wall artwork as well. In this first blog post I detail how I generate a QR code using Python, then how to build 3D printable blocks and, finally, how to convert that model into a file 3D printers can read. In a follow-up blog post, I will specify how I created a hybrid command line app and Flask app from the same code base, using click and Flask. It will take the code that we write here and turn it into an app that can be used from the command line and from a web interface — it’s a great exercise in showing the similarities between the CLI and Flask. Coming soon!

Why a 3D QR code for my WIFI password?

There are a ton of QR code generators out there on the web and more than a handful of WiFi QR code generators too – so why did I embark on this project? Mainly, it was me wanting to scratch my itch surrounding QR codes. The last time I went to China (Xi’an and Shanghai, specifically), I saw QR codes everywhere. There surely had to be something good we could use this for at home that didn’t involve just packing and storage. Now that I know how simple it is to create a QR code using Python, I’m sure I’ll find myriad uses for them!

Getting Set Up

Ok, let’s get started! To create QR codes, you need to install pyqrcode and pypng in your environment:
pip install pyqrcode
pip install pypng If you want to do the 3D printing part, you’ll also need to install SolidPython and NumPy:
pip install SolidPython
pip install numpy Finally, to build a command line app and a web app, you’ll need to install click and Flask:
pip install click
pip install Flask If you are using the conda package manager, you should know that numpy, click and Flask are conda-installable if you prefer to do so. I also used Kite in the Atom text editor: this allowed me to view documentation and common usage patterns for the packages I imported.

Kite is a plugin for PyCharm, Atom, Vim, VSCode, Sublime Text, and IntelliJ that uses machine learning to provide you with code completions in real time sorted by relevance. Start coding faster today.

Step 1: Encoding WiFi credentials in a QR code

Let’s start by creating a QR code for our WiFi guest network. Let’s say that these are the security credentials for the network:
SSID (a.k.a. Network Name): Family Guest Network
Password: vn8h2sncu093y3nd!
Security Type (one of WPA or WEP): WPA QR codes are merely two-dimensional barcodes that encode a string that can be parsed by another program. In order to create a QR-code that is readable for accessing WiFi, we need a string that can be parsed by our devices. This string is structured as follows:
WIFI:S:;T:;P:;; So in our case, we would want a string that looks like:
WIFI:S:Family Guest Network;T:WPA;P:vn8h2sncu093y3nd!;; Now, we can code up our Python program to encode the QR code for us. I’ll assume you’re running Python 3.6 or later.

import pyqrcode as pq 
ssid = 'Family Guest Network'
security = 'WPA'
password = 'vn8h2sncu093y3nd!'
qr = pq.create(f'WIFI:S:;T:;P:;;')
print(qr.terminal())

With that block of code, you should get a QR code printed to your terminal, just like that! Let’s say you wanted to do the simple thing, and just have a regular laser/inkjet printer make a printout of the QR code. To do so, you can save the QR code to disk as a PNG file:

Читайте также:  Есть wi fi подключение настроить пароль

And just like that, you’ve used Python to create a WiFi QR code! Just scan this code using your phone’s camera and you (or your guests) will be able to join your WiFi network. Now, if you remember that QR codes are just “ASCII strings encoded in a 2D barcode”, then you’ll know that you can pass any arbitrary string into the pyqrcode.create() function. That means you can come up with any creative use of a short string that would make sense to scan with your phone! For example, you can create business cards with your LinkedIn profile URL embedded in the QR code, use it to encode a serial number information on your possessions, or more! Next, we’ll turn the QR code into a 3D-printable model using our imported Python packages and free online CAD software.

Step 2: 3D Printing a QR Code

For this, we will need a package called SolidPython, and optionally numpy to help us with some array processing (it can also be done entirely using built-in lists if needed). To start, I defined a convenience function that let me create and return a QRCode object that can be passed around and manipulated.

def create_wifi_qr(ssid: str, security: str, password: str): 
qr = pq.create(f'WIFI:S:;T:;P:;;')
return qr

Create Text Representation

qr = create_wifi_qr(ssid, security, password) 
print(qr.text())

This will essentially give a series of 1s and 0s. This is a string, though, not a numpy array. Hence, we may have to convert this into a list of lists, or a numpy array (as a user of the scientific Python stack, I prefer using arrays where possible, but in this case there is no real practical advantage to doing so because we are not doing linear algebra).

Create Array Representation

Let’s now define a function that takes in the QRCode object and returns an array version of the text rendering.

def qr2array(qr): 
arr = []
for line in qr.text().split('\n'):
if len(line) != 0:
arr.append([int(bit) for bit in line])
return np.vstack(arr)

Create 3D Model

Now, we’re ready to play with SolidPython! SolidPython is a Python package that provides an interface to the OpenSCAD language. OpenSCAD allows a programmer to programmatically define 3D models using the language of geometry. This includes the creation of cubes and other 3D objects, as well as object manipulations, such as translation, coloring, and union-ing. Take a look at the code below for an example of how we create the 3D object.

from solid import color, cube, scad_render, translate, union 

SCALE = 2 # output defaults to 1 mm per unit; this lets us increase the size of objects proportionally.
cubes = [translate([i*SCALE, j*SCALE, 0])(color('black')(cube(size=[SCALE, SCALE, HEIGHT])))
for i, row in enumerate(arr)
for j, col in enumerate(row)
if arr[i, j] == 1]

base_plate = color('white')(cube(size=(arr.shape[0] * SCALE, arr.shape[1] * SCALE, HEIGHT / 2)))
qrobj = union()(*cubes, base_plate)

print(scad_render(qrobj))

What we’re doing here is taking the 1s and creating cubes where they are supposed to be, but leaving the zeros empty. Then, we add a “base plate” so that everything stays nice and connected, and finally union all of the cubes with the base plate, so that we get one solid piece that is 3D printed. If you observe the output of the function scad_render, it will essentially be valid OpenSCAD text. Once you download the free software here, you can paste our function’s output into OpenSCAD to render and export as an STL file. The STL file then needs to be converted into a .gcode file, which gives a 3D printer the necessary instructions to move its printhead around to print the QR code. In short, the flow is:
SolidPython -> OpenSCAD -> STL -> .gcode That’s it! You have everything you need to 3D print a QR code of your WiFi credentials.

Читайте также:  Wifi антенна main aux

Conclusions

  1. How to create a QR code using Python.
  2. How to create a text representation of the QR code.
  3. How to convert the text representation into an array.
  4. How to create a 3D model of the QR code using the array.

Now that you have a rendered 3D model, you can either 3D print it at home, or send it to a friend to 3D print it for you. You’ll no longer have to give plain text WiFi passwords to your guests – they can just scan the aesthetically-pleasing 3D printed QR code instead!

With this example of how to create an OpenSCAD file from Python using SolidPython, I hope you’ll go forth and make a ton of fun stuff! Please share your experiences in the comment section below.

Kite added the associated code from this blog post to a public Github repository. You can find the source code from this and other posts in their Python series here.

In my next post, I’ll show how I took this code base to create a hybrid command line app and Flask app. Stay tuned for more details and make sure to subscribe to the Kite blog so you don’t miss it!

This post is a part of Kite’s new series on Python. You can check out the code from this and other posts on our GitHub repository.

Источник

Let’s generate your Wifi qr code with flask Python

Welcome to the world of QR code generators! Today, we’re going to build our very own WiFi QR code generator using the Flask web framework and the qrcode library. But before we get started, let’s make sure we have everything we need. First, make sure you have Flask installed. You can install it by running pip install flask in your terminal. Next, we’ll need to install the qrcode library. You can do this by running pip install qrcode==6.0 . This is an important step because it ensures that we’re using the right version of the library that is compatible with the rest of our code. Now that we have everything we need, let’s get started on our code! We’ll start by importing Flask, request, and send_file from the flask module, as well as qrcode and BytesIO from the io module.

from flask import Flask, request, send_file import qrcode from io import BytesIO 

Next, we’ll create an instance of the Flask class, which we’ll use to define and run our web application.

Читайте также:  Связь работает только при вай фай

Now, we’ll define a route for our application using the @app.route decorator. This route will handle requests to the «/» endpoint, and call the generate_qr() function that we’ll define next.

Inside the generate_qr() function, we’ll create two variables, ssid and password, which will store the name and password for our WiFi network, respectively. We’ll then use these variables to create a string in the format that qrcode library uses to encode WiFi information into a QR code.

 ssid = "Get Your Own Wifi" password = "ItIsMyWifi69" wifi_data = 'WIFI:S:<>;T:WPA;P:<>;;'.format(ssid, password) 

Next, we’ll create an instance of the QRCode class, which will allow us to generate a QR code with the information we’ve provided. We’ll set the version, error correction, box size, and border properties to configure the QR code.

 qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=5) 
 qr.add_data(wifi_data) qr.make(fit=True) 

We’ll create an image of the QR code with the fill_color and back_color to be black and white respectively.

 img = qr.make_image(fill_color="black", back_color="white") 

We’ll now create a BytesIO object to hold the image in memory and save the image to the BytesIO object in PNG format

 byteIO = BytesIO() img.save(byteIO, 'PNG') 

We’ll reset the seek to the start of the byteIO object and return the image file with ‘image/png’ mimetype.

 byteIO.seek(0) return send_file(byteIO, mimetype='image/png') 

Finally, we’ll add a check to run the application only if the script is being run directly (and not imported as a module).

if __name__ == '__main__': app.run(port=5000,debug=True) 

And that’s it! Our WiFi QR code generator is now complete. You can now run the script and visit «http://localhost:5000/» to see your very own QR code for your WiFi network. With this handy tool, you can easily share your WiFi network with friends and family without having to reveal your password. Just remember, never share your wifi passwords with anyone you don’t trust. Have fun and happy coding! Full Code :

from flask import Flask, request, send_file import qrcode from io import BytesIO app = Flask(__name__) @app.route('/') def generate_qr(): ssid = "Get Your Own Wifi" password = "ItIsMyWifi69" wifi_data = 'WIFI:S:<>;T:WPA;P:<>;;'.format(ssid, password) qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=5) qr.add_data(wifi_data) qr.make(fit=True) img = qr.make_image(fill_color="black", back_color="white") byteIO = BytesIO() img.save(byteIO, 'PNG') byteIO.seek(0) return send_file(byteIO, mimetype='image/png') if __name__ == '__main__': app.run(port=5000,debug=True) 

Источник

Оцените статью
Adblock
detector