Last active
January 19, 2024 09:38
-
-
Save martinschierle/f4b21fde167b60c7d4631d8150367a62 to your computer and use it in GitHub Desktop.
EInkNewsViewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gc | |
import uos | |
import machine | |
import jpegdec | |
import sdcard | |
from urllib import urequest | |
import network | |
from picographics import PicoGraphics, DISPLAY_INKY_FRAME_7 as DISPLAY # 7.3" | |
import time | |
from machine import Pin | |
import random | |
import mrequests | |
import ntptime | |
import inky_frame | |
""" | |
Daily news frontpage | |
You *must* insert an SD card into Inky Frame! | |
We need somewhere to save the jpg for display. | |
Fetches a jpg of a news frontpage from the newseum website | |
Docs for graphics lib here: | |
https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/modules/picographics/README.md#jpeg-files | |
Get started guide here: | |
https://learn.pimoroni.com/article/getting-started-with-inky-frame | |
Run this code through Thonny with device connected through USB | |
""" | |
inky_frame.led_busy.on() | |
gc.collect() # We're really gonna need that RAM! | |
#if needed, overwrite default time server | |
ntptime.host = "0.us.pool.ntp.org" | |
ENDPOINTS = [ | |
"https://cdn.freedomforum.org/dfp/jpg#DAY/lg/NY_NYT.jpg", #NYTimes | |
"https://cdn.freedomforum.org/dfp/jpg#DAY/lg/WSJ.jpg", #WSJ | |
"https://cdn.freedomforum.org/dfp/jpg#DAY/lg/NEWZ_TP.jpg", #the press, new zealand | |
"https://cdn.freedomforum.org/dfp/jpg#DAY/lg/CA_SFC.jpg", #san francisco chronicle | |
"https://cdn.freedomforum.org/dfp/jpg#DAY/lg/GER_RZ.jpg", #rhein zeitung | |
"https://cdn.freedomforum.org/dfp/jpg#DAY/lg/GER_AA.jpg", #augsburger allgemeine | |
"https://cdn.freedomforum.org/dfp/jpg#DAY/lg/GER_FAZ.jpg" #frankfurter allgemeine | |
] | |
REQ_HEADERS = { | |
"Accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8", | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", | |
"Accept-Encoding": "", | |
"Cache-Control": "no-cache", | |
"Pragma": "no-cache" | |
} | |
wlan = network.WLAN(network.STA_IF) | |
ssid = '' | |
password = '' | |
graphics = PicoGraphics(display=DISPLAY) | |
FILENAME = "/sd/frontpage.jpg" | |
wlan.active(True) | |
wlan.connect(ssid, password) | |
# Wait for connection to establish | |
max_wait = 10 | |
while max_wait > 0: | |
if wlan.status() < 0 or wlan.status() >= 3: | |
break | |
max_wait -= 1 | |
print('waiting for connection...') | |
time.sleep(1) | |
# Manage connection errors | |
if wlan.status() != 3: | |
print('Network Connection has failed') | |
else: | |
print('connected') | |
status = wlan.ifconfig() | |
print( 'ip = ' + status[0] ) | |
try: | |
print("Local time before synchronization:%s" %str(time.localtime())) | |
#make sure to have internet connection | |
ntptime.settime() | |
print("Local time after synchronization:%s" %str(time.localtime())) | |
except: | |
print("Error syncing time") | |
print("Mounting SD Card...") | |
sd_spi = machine.SPI(0, sck=machine.Pin(18, machine.Pin.OUT), mosi=machine.Pin(19, machine.Pin.OUT), miso=machine.Pin(16, machine.Pin.OUT)) | |
sd = sdcard.SDCard(sd_spi, machine.Pin(22)) | |
uos.mount(sd, "/sd") | |
gc.collect() # Claw back some RAM! | |
while True: | |
url = random.choice(ENDPOINTS) | |
year, month, day, hour, minute, second, dow, _ = time.localtime() | |
if hour < 10: | |
day = day-1 #adjust from europe to US plus add time so taht website has newest data - micropython doesn't seem to have great tiemzone support | |
url = url.replace("#DAY", str(day)) | |
print("Downloading File: " + url) | |
try: | |
r = mrequests.get(url, headers=REQ_HEADERS) | |
r.save(FILENAME,1024) | |
gc.collect() # We really are tight on RAM! | |
print("Decoding and Rendering...") | |
jpeg = jpegdec.JPEG(graphics) | |
gc.collect() # For good measure... | |
graphics.set_pen(1) | |
graphics.clear() | |
jpeg.open_file(FILENAME) | |
gc.collect() # We really are tight on RAM! | |
jpeg.decode(50, 0, jpegdec.JPEG_SCALE_FULL, dither=True) | |
graphics.update() | |
except Exception as e: | |
print("Error downloading and showing file") | |
print(e) | |
# Time to have a little nap until the next update | |
print("Going to sleep...") | |
inky_frame.led_busy.off() | |
inky_frame.sleep_for(60) #in minutes | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment