I’ve been playing with the new PRESTO board and thought I would share my latest project. It looks at a US website which records world-wide earthquakes. The program downloads the last 10 recoded quakes and displays the more interesting details on the screen. Extracting the data and getting it into a more useful format takes a fair bit of processing but is not too difficult.
Here is a link to the video: https://youtu.be/MOoyETQSido
The code is below:
# Test WIFI network DEMO on Pimoroni PRESTO
# Uses data from https://earthquake.usgs.gov
# ==== Tony Goodhew 29th Dec 2024 ====
# Shows how to download data from a website, process it to
# extract what you need and display results
# on the 480x480 pixel screen
# Video at https://youtu.be/MOoyETQSido
import time
from random import randint
from presto import Presto
import network
import rp2
import requests
rp2.country("GB")
from secrets import WIFI_SSID, WIFI_PASSWORD
# Setup for the Presto display
presto = Presto(full_res=True)
display = presto.display
WIDTH, HEIGHT = display.get_bounds()
# Create some colours
BLUE = display.create_pen(20,0,255)
WHITE = display.create_pen(255, 255, 255)
RED = display.create_pen(255,0,0)
ORANGE = display.create_pen(245, 165, 4)
GREEN = display.create_pen(0,255,0)
PINK = display.create_pen(250, 125, 180)
CYAN = display.create_pen(0,255,255)
MAGENTA = display.create_pen(255,0,255)
BLACK = display.create_pen(0, 0, 0)
YELLOW = display.create_pen(255, 255, 0)
# A few procedures to be used later
def wait(z): # delay a while
time.sleep(z)
def clean(): # Clear the screen to Black
display.set_pen(BLACK)
display.clear()
presto.update()
# Routine to separate and extract data items from a single downloaded line
def splitup(s):
string = ""
items = []
for p in range(len(s)):
c = s[p]
# print(type(c))
if c != chr(124):
string = string + c
else:
items.append(string)
string = ""
items.append(string)
# Extract date and time from second item
dt = items[1]
date = dt[0:10]
ttime = dt[11:19]
return(items,date,ttime)
display.set_font("bitmap8") # Change the font
clean()
display.set_pen(RED)
display.text("EarthQuake",40,200,460,8)
display.set_pen(BLUE)
display.text("Data from: https://earthquake.usgs.gov",70,400,480,2)
display.text("Tony Goodhew, Leicester UK",120,450,480,2)
presto.update()
time.sleep(1)
for p in range(35):
display.set_pen(BLACK)
display.rectangle(0,190,480,100)
xx = randint(0,16)-8
yy = randint(0,16)-8
display.set_pen(ORANGE)
display.text("EarthQuake",40+xx,200+yy,460,8)
presto.update()
time.sleep(0.07)
clean()
display.set_pen(RED)
display.text("EarthQuake",40,200,460,8)
presto.update()
time.sleep(0.6)
clean()
display.set_pen(RED)
# Activate WiFi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
max_wait = 30
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print("Waiting for Wi-Fi connection...")
display.set_pen(RED)
display.text("Waiting for Wi-Fi connection...",10,200,460,5)
presto.update()
time.sleep(1)
if wlan.status() != 3:
raise RuntimeError("Network connection failed")
else:
print("Connected to Wi-Fi network.")
print(wlan.ifconfig())
clean()
display.set_pen(GREEN)
display.text("Connected to WiFi",50,200,460,5)
display.set_pen(BLUE)
display.text(" Processing",70,260,460,5)
presto.update()
# ===== Main Loop ====
while True:
# Access EarthQuake website and get first 10 lines
response = requests.get("https://earthquake.usgs.gov/fdsnws/event/1/query?format=text&limit=10")
# Separate into lines
lines = []
for x in response.content.splitlines():
xs = str(x)
# print(str(xs))
lines.append(str(x))
response.close()
clean()
events = []
for i in range(1,11):
k = splitup(lines[i]) # This does the major lifting using the proc above
# print()
# print(k)
events.append(k)
'''
# List of items for information
names = ['EventID', 'Time', 'Latitude', 'Longitude', 'Depth/km', 'Author', 'Catalog', 'Contributor',
'ContributorID', 'MagType', 'Magnitude', 'MagAuthor', 'EventLocationName', 'Date', 'Time']
'''
# Show recent event high-lights
for i in range(10):
clean()
display.set_pen(CYAN)
if i == 0:
display.set_pen(RED) # Most recent quake
k,date,ttime = events[i] # Fetch decoded quake data line
display.text(date + " -- " + ttime,30,0,460,4)
display.set_pen(YELLOW)
place = k[12]
place = place[:-1] # remove final quote character
display.text(place,0,40,460,4)
lat = round(float(k[2]),3)
long = round(float(k[3]),3)
display.text("Lat/Lon: "+str(lat) + " "+ str(long),0,200,460,4)
display.text("Magnitude: " + k[10],0,260,460,4)
display.text("Type: " + k[9],0,320,460,4)
deep = round(float(k[4]),3)
display.text("Depth/km: "+ str(deep),0,380,460,4)
display.set_pen(ORANGE)
display.text(str(i),460,440,100,4)
presto.update()
wait(4)
I’m delighted with this board. Great graphics with so much screen space, plenty of memory, wifi, and i2c port. Software may be Beta but is working well.!
I was surprised to find out how many earthquakes are happening. The RED date and time at the top of the screen is the latest quake and the numbers at the bottom right count quakes backwards.
Comments and discussion welcome. Let’s share our projects to help others.