Fun Circuit Python project
I made an actually useful program with circuit python on the adafruit magtag.
I'm starting to fall back in love with micro-controllers. I haven't touched anything like an Arduino since my college days, and there is something to be said about the simplicity of the environment. I have a hard time with what to do on them, which is probably why I've owned an Adafruit MagTag for nearly 3 years and haven't used it in any projects.
One day, I saw it on my shelf and decided to get back into it. I was going to make something that is actually useful, and then maybe learn something along the way! The Adafruit library has an interesting function that generates a QR code, and QR codes and E-ink was an interesting combo in my head, so I wanted to find a way to use it.
I finally settled on a random password generator. Simply click a button and the device would create a random and secure password for you to scan, then use.
The Code
The Circuit Pythoncode below is used to generate a random value and display it as a QR code on an e-ink display using an ESP32 board for use as a password.
# import wifi # type: ignore
import time
import os
from adafruit_magtag.magtag import MagTag
import binascii
magtag = MagTag()
magtag.add_text(
text_position=(
50,
(magtag.graphics.display.height // 2) - 1,
),
text_scale=1,
)
password = binascii.b2a_base64(binascii.hexlify(os.urandom(16)))
def update_qr(code):
magtag.graphics.qrcode(password, qr_size=4, x=13, y=3,qr_color=1)
magtag.graphics.set_background("/img/background.bmp")
time.sleep(0.01)
magtag.refresh()
while True:
for i, b in enumerate(magtag.peripherals.buttons):
if not b.value:
print("Button %c pressed" % chr((ord("A") + i)))
if(i==0):
password = binascii.b2a_base64(binascii.hexlify(os.urandom(8)))
magtag.peripherals.neopixel_disable = False
if(i==1):
password = binascii.b2a_base64(binascii.hexlify(os.urandom(16)))
magtag.peripherals.neopixel_disable = False
time.sleep(0.1)
break
else:
magtag.peripherals.neopixel_disable = True
update_qr(password)
time.sleep(0.01)
- Import necessary libraries (e.g.
adafruit_magtag
forMagTag
class) and initialize an instance of theMagTag
class to access the e-ink display - Define a function
update_qr()
that generates a random value usingos.urandom()
and converts it to a base64 string for encoding as a QR code - Inside a
while True
loop, check if any buttons on the display are pressed, generate new random values accordingly, and update the QR code using theupdate_qr()
function - Use button press events to change random value generation length and enable/disable neopixel LEDs.
Finally
And there it is. A completely offline way to generate passwords for use by devices that can scan a QR code. There are several ways I think this should be improved, like adding symbols to the strings, randomizing the ratio of letters to numbers, and making use of the other 2 buttons. I can't get over how much I enjoy the bitmap image in the background with the QR code. It's such an interesting aesthetic and I absolutely love it.
What's next?
I would like to make more projects with this hardware. I feel like there are some other boards I'd like to play with, but this projects gives me the confidence I felt I was missing to get back into microcontroller projects.
If you like our content, please consider subscribing to our weekly newsletter. I'm biased, but it's pretty great.Sign Up