|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import re |
| 5 | +import sys |
| 6 | +import qrcode |
| 7 | +from PIL import Image, ImageDraw, ImageFont |
| 8 | + |
| 9 | +def get_esp32_mac(): |
| 10 | + try: |
| 11 | + result = subprocess.run(['esptool.py', 'read_mac'], capture_output=True, text=True, check=True) |
| 12 | + mac_addresses = re.findall(r'MAC: ([0-9A-Fa-f:]{17})', result.stdout) |
| 13 | + |
| 14 | + if len(mac_addresses) == 0: |
| 15 | + print("Error: No ESP32 device found.") |
| 16 | + sys.exit(1) |
| 17 | + elif len(mac_addresses) == 2 and mac_addresses[0] == mac_addresses[1]: |
| 18 | + return mac_addresses[0] # Return the single MAC address if both are identical |
| 19 | + elif len(mac_addresses) > 1: |
| 20 | + print("Error: Multiple ESP32 devices detected. Please connect only one device.") |
| 21 | + sys.exit(1) |
| 22 | + |
| 23 | + return mac_addresses[0] |
| 24 | + except subprocess.CalledProcessError: |
| 25 | + print("Error: Failed to read MAC address. Make sure esptool.py is installed and the device is connected.") |
| 26 | + sys.exit(1) |
| 27 | + |
| 28 | +def generate_qr_code_with_text(url): |
| 29 | + # Generate QR code |
| 30 | + qr = qrcode.QRCode(version=1, box_size=10, border=4) |
| 31 | + qr.add_data(url) |
| 32 | + qr.make(fit=True) |
| 33 | + qr_img = qr.make_image(fill_color="black", back_color="white") |
| 34 | + |
| 35 | + # Convert to RGB mode if it's not already |
| 36 | + if qr_img.mode != 'RGB': |
| 37 | + qr_img = qr_img.convert('RGB') |
| 38 | + |
| 39 | + # Set up font |
| 40 | + try: |
| 41 | + font = ImageFont.truetype("Arial.ttf", 20) |
| 42 | + except IOError: |
| 43 | + font = ImageFont.load_default() |
| 44 | + |
| 45 | + # Calculate text size |
| 46 | + draw = ImageDraw.Draw(qr_img) |
| 47 | + bbox = draw.textbbox((0, 0), url, font=font) |
| 48 | + text_width = bbox[2] - bbox[0] |
| 49 | + text_height = bbox[3] - bbox[1] |
| 50 | + |
| 51 | + # Create a new image with space for QR code and text |
| 52 | + padding = 20 # Padding around the QR code and text |
| 53 | + img_width = max(qr_img.width, text_width) + (padding * 2) |
| 54 | + img_height = qr_img.height + text_height + (padding * 3) # Extra padding between QR and text |
| 55 | + img = Image.new('RGB', (img_width, img_height), color='white') |
| 56 | + |
| 57 | + # Paste QR code |
| 58 | + qr_x = (img_width - qr_img.width) // 2 |
| 59 | + qr_y = padding |
| 60 | + img.paste(qr_img, (qr_x, qr_y)) |
| 61 | + |
| 62 | + # Add text below QR code |
| 63 | + draw = ImageDraw.Draw(img) |
| 64 | + text_x = (img_width - text_width) // 2 |
| 65 | + text_y = qr_y + qr_img.height + padding |
| 66 | + draw.text((text_x, text_y), url, font=font, fill="black") |
| 67 | + |
| 68 | + # Display the image |
| 69 | + img.show() |
| 70 | + |
| 71 | + # Optionally, save the image |
| 72 | + # img.save("qr_code_with_url.png") |
| 73 | + |
| 74 | +def main(): |
| 75 | + mac_address = get_esp32_mac() |
| 76 | + last_six_digits = mac_address.replace(':', '')[-6:] |
| 77 | + url = f"http://openspool-{last_six_digits.lower()}.local" |
| 78 | + print(f"Generated URL: {url}") |
| 79 | + |
| 80 | + generate_qr_code_with_text(url) |
| 81 | + print("QR code with URL text has been generated and displayed.") |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments