Skip to content

Commit 2a94e67

Browse files
committed
v1
1 parent 41a2579 commit 2a94e67

7 files changed

+105
-37
lines changed

README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,35 @@
22

33
Daily AI generated patriotic images on the Adafruit MagTag
44

5-
## Requirements
5+
## Client Requirements
6+
7+
This assumes that the MagTag storage is remounted read/write after boot using boot.py.
8+
9+
```python
10+
import storage
11+
12+
storage.remount("/", readonly=False)
13+
```
14+
15+
MagTag code is written in CircuitPython. The following libraries are required:
616

717
- Adafruit MagTag
818
- Adafruit MagTag Libraries
9-
- adafruit_magtag
1019
- adafruit_bitmap_font
20+
- adafruit_imageload
1121
- adafruit_io
22+
- adafruit_magtag
1223
- adafruit_minimqtt
24+
- adafruit_requests
1325
- adafruit_ticks
1426
- simpleio
1527

28+
## Server Requirements
29+
30+
See `server/requirements.txt`
31+
1632
## Usage
1733

18-
The server-side code runs as a cron job once every 24 hours. It generates a new patriotic image via the OpenAI DALL-E-3 API.
34+
The server-side code runs as a systemd timer once every 24 hours. It generates a new patriotic image via the OpenAI DALL-E-3 API.
1935

2036
The MagTag reaches out to the server every 24 hours to get the latest image and display it.

magtag/code.py

+57-33
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,72 @@
11
import time
2-
import board
3-
from adafruit_magtag.magtag import MagTag
42
import displayio
5-
6-
display = board.DISPLAY
3+
import wifi
4+
import os
5+
from adafruit_magtag.magtag import MagTag
6+
import adafruit_requests
7+
import socketpool
8+
import ssl
9+
from adafruit_imageload import load
10+
from io import BytesIO
711

812
magtag = MagTag()
913

10-
# Load the image
11-
bitmap = displayio.OnDiskBitmap("usa.bmp")
14+
# Connect to WiFi
15+
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
16+
17+
# URL of the bitmap image
18+
url = "http://137.184.19.28:80/murica.bmp"
19+
20+
# Download the image
21+
pool = socketpool.SocketPool(wifi.radio)
22+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
23+
response = requests.get(url)
24+
if response.status_code == 200:
25+
image_data = response.content
26+
else:
27+
print("Failed to download the image")
28+
image_data = None
29+
30+
if image_data:
31+
# Save the image to a file
32+
with open("/murica.bmp", "wb") as file:
33+
file.write(image_data)
34+
35+
# Load the image
36+
bitmap = displayio.OnDiskBitmap("murica.bmp")
1237

13-
# Create a TileGrid to hold the image
14-
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
38+
# Create a TileGrid to hold the image
39+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
1540

16-
# Calculate the position to center the image
17-
# MagTag display is 296x128 pixels
18-
image_width = bitmap.width
19-
image_height = bitmap.height
20-
display_width = magtag.display.width
21-
display_height = magtag.display.height
41+
# Calculate the position to center the image
42+
# MagTag display is 296x128 pixels
43+
image_width = bitmap.width
44+
image_height = bitmap.height
45+
display_width = magtag.display.width
46+
display_height = magtag.display.height
2247

23-
x = (display_width - image_width) // 2
24-
y = (display_height - image_height) // 2
48+
x = (display_width - image_width) // 2
49+
y = (display_height - image_height) // 2
2550

26-
# Set the position of the TileGrid
27-
tile_grid.x = x
28-
tile_grid.y = y
51+
# Set the position of the TileGrid
52+
tile_grid.x = x
53+
tile_grid.y = y
2954

30-
# Create a Group to hold the TileGrid
31-
group = displayio.Group()
55+
# Create a Group to hold the TileGrid
56+
group = displayio.Group()
3257

33-
# Add the TileGrid to the Group
34-
group.append(tile_grid)
58+
# Add the TileGrid to the Group
59+
group.append(tile_grid)
3560

36-
# Show the Group on the display
37-
magtag.splash.append(group)
61+
# Show the Group on the display
62+
magtag.splash.append(group)
3863

39-
# Refresh the display to show the image
40-
magtag.display.refresh()
64+
# Refresh the display to show the image
65+
magtag.display.refresh()
4166

42-
# Wait for the display to finish updating
43-
time.sleep(10)
67+
# Wait for the display to finish updating
68+
time.sleep(10)
4469

45-
# go into sleep mode until we rotate
46-
# the photo 24 hours later
47-
#magtag.exit_and_deep_sleep(86400)
48-
magtag.exit_and_deep_sleep(5)
70+
# go into sleep mode until we rotate
71+
# the photo 24 hours later
72+
magtag.exit_and_deep_sleep(86400)

server/murica-generator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
indexed_image = resized_image.convert("P", palette=Image.ADAPTIVE)
3838

3939
# Save the indexed bitmap
40-
indexed_image.save("indexed_image.bmp")
40+
indexed_image.save("/opt/murica.bmp")

server/murica-generator.service

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Unit]
2+
Description=Run murica-generator.py
3+
4+
[Service]
5+
Type=simple
6+
ExecStart=/usr/bin/python3 /opt/murica-generator.py
7+
Environment="OPENAI_API_KEY=your_openai_api_key"

server/murica-generator.timer

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Run murica-generator.py daily at midnight Central Time
3+
4+
[Timer]
5+
OnCalendar=*-*-* 00:00:00
6+
Persistent=true
7+
8+
[Install]
9+
WantedBy=timers.target

server/nginx.conf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
server {
2+
listen 80;
3+
server_name localhost;
4+
5+
location / {
6+
root /opt;
7+
try_files /murica.bmp =404;
8+
}
9+
}

server/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
requests
2+
openai
3+
pillow

0 commit comments

Comments
 (0)