Final, corrected version, is here:
# Date and Time from jsontest.com display on Pico Display V1
# Tony Goodhew 24th July 2022
from picographics import PicoGraphics, DISPLAY_PICO_DISPLAY, PEN_P4
import urequests
# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY, pen_type=PEN_P4, rotate=0)
display.set_backlight(0.5)
display.set_font("bitmap8")
WIDTH, HEIGHT = display.get_bounds()
RED = display.create_pen(255,0,0)
BLACK = display.create_pen(0,0,0)
WHITE = display.create_pen(255,255,255)
YELLOW = display.create_pen(255,255,0)
GREEN = display.create_pen(0,255,0)
display.set_pen(GREEN)
display.set_pen(BLACK)
display.clear()
display.set_pen(RED)
# Connect to network
import network
from secret import ssid,password
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'IP = ' + status[0] )
display.set_pen(GREEN)
display.text('IP = ' + status[0], 0,0,135, 2, 90, 3)
display.update()
display.set_pen(RED)
display.text("date.jsontest.com", 0,25,135, 2, 90, 2)
display.update()
# Make GET request
r = urequests.get("http://date.jsontest.com")
print("\nTime from 'http://date.jsontest.com'\n")
s = str(r.json())
# Get date
p = s.find("date")
datestr = s[p+8:p+18]
# Get time
p = s.find("time")
timestr = s[p+8:p+17]
display.set_pen(YELLOW)
display.text("Date: "+datestr, 0,50,135, 2, 90, 3)
display.set_pen(YELLOW)
display.text("Time: "+timestr, 0,95,135, 2, 90, 3)
display.update()
r.close()
I hope that answers your question.