Skip to content

Instantly share code, notes, and snippets.

@edwork
Forked from vwillcox/weather2.py
Last active January 25, 2025 16:01
Show Gist options
  • Select an option

  • Save edwork/a39c8b076901ac1b8ff42d542982b515 to your computer and use it in GitHub Desktop.

Select an option

Save edwork/a39c8b076901ac1b8ff42d542982b515 to your computer and use it in GitHub Desktop.

Revisions

  1. edwork renamed this gist Jan 25, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @vwillcox vwillcox revised this gist Dec 1, 2023. 1 changed file with 45 additions and 33 deletions.
    78 changes: 45 additions & 33 deletions weather2.py
    Original file line number Diff line number Diff line change
    @@ -20,53 +20,59 @@
    last_cheerlight_check = time.time()
    utc_offset = 0

    def sync_time():
    global wlan
    if not wifi_available:
    return

    if not wlan.isconnected():
    wifi_connect()

    try:
    ntptime.settime()
    #print("Time set")
    except OSError as e:
    print(e)
    pass


    def sync_time_if_reqd():
    global last_time_sync
    if time.time() - last_time_sync > 86400: #Sync once per day
    #print ("Running Time Sync")
    sync_time()
    last_time_sync = time.time()


    WHITE = graphics.create_pen(255, 255, 255)
    BLACK = graphics.create_pen(0, 0, 0)
    BACKGROUND_COLOUR = (10, 0, 96) # Blue
    OUTLINE_COLOUR = (0, 0, 0)
    MESSAGE_COLOUR = (255, 255, 255)
    BG_COLOUR = graphics.create_pen(0, 0, 0)
    TEXT_COLOUR = graphics.create_pen(50, 0, 255)

    city = 'Colchester'
    city = 'Mars'
    country_code = 'UK'
    #example
    #city = 'Lahore'
    #country_code = 'PAK'
    width = GalacticUnicorn.WIDTH
    height = GalacticUnicorn.HEIGHT

    open_weather_map_api_key = '<API_KEY>'

    station = network.WLAN(network.STA_IF)

    station.active(True)
    station.connect(secrets.WIFI_SSID, secrets.WIFI_PASSWORD)
    open_weather_map_api_key = '<your_API_Key>'

    def clear_display():
    # does what it says on the tin
    graphics.set_pen(BG_COLOUR)
    graphics.clear()
    galactic.update(graphics)

    def connect_to_wifi():
    station = network.WLAN(network.STA_IF)
    station.active(True)
    station.connect(secrets.WIFI_SSID, secrets.WIFI_PASSWORD)
    while not station.isconnected() and station.status() >= 0:
    print("Waiting to connect:")
    clear_display()
    graphics.set_pen(TEXT_COLOUR)
    graphics.text("Connecting", 1, 2, 0, scale=1);
    galactic.update(graphics)
    time.sleep(1)
    if(station.isconnected()):
    network_config = station.ifconfig()
    ip = network_config[0]
    print("Connected:" + ip)
    clear_display()
    # draw the text
    graphics.set_pen(TEXT_COLOUR)
    graphics.text("Active", 5, 2, 0, scale=1);

    # update the display
    galactic.update(graphics)
    time.sleep(5)
    try:
    ntptime.settime()
    except OSError:
    pass

    while station.isconnected() == False:
    pass

    def outline_text(text, x, y):
    graphics.set_pen(BLACK)
    @@ -89,7 +95,8 @@ def set_background():
    for y in range(0,height):
    graphics.pixel(x, y)

    print('Connection successful')



    def redraw_weather():
    while True:
    @@ -119,6 +126,7 @@ def redraw_weather():
    outline_text(temperature,13, 2)
    galactic.update(graphics)
    time.sleep(120)
    #sync_time_if_reqd()

    def redraw_display_if_reqd():
    global year, month, day, wd, hour, minute, second, last_second, blink
    @@ -149,7 +157,9 @@ def redraw_display_if_reqd():
    last_second = second

    def redraw_time():
    #sync_time()
    while True:
    #sync_time_if_reqd()
    redraw_display_if_reqd()
    galactic.update(graphics)
    time.sleep(0.1)
    @@ -158,5 +168,7 @@ def redraw_time():
    galactic.set_brightness(0.5)

    #set_background()
    connect_to_wifi()
    second_thread = _thread.start_new_thread(redraw_time, ())
    redraw_weather()

  3. @vwillcox vwillcox revised this gist Mar 11, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion weather2.py
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ def sync_time_if_reqd():
    width = GalacticUnicorn.WIDTH
    height = GalacticUnicorn.HEIGHT

    open_weather_map_api_key = 'dd97959d2409e6e1556c37a189fca344'
    open_weather_map_api_key = '<API_KEY>'

    station = network.WLAN(network.STA_IF)

  4. @vwillcox vwillcox revised this gist Mar 11, 2023. 1 changed file with 24 additions and 14 deletions.
    38 changes: 24 additions & 14 deletions weather2.py
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,12 @@
    import jpegdec, math, ntptime, time
    import _thread

    blink=0
    machine.freq(200000000)
    rtc = machine.RTC()
    year, month, day, wd, hour, minute, second, _ = rtc.datetime()
    last_second = second
    blink = 0
    galactic = GalacticUnicorn()
    graphics = PicoGraphics(DISPLAY)
    #Timer global variables
    @@ -47,15 +50,15 @@ def sync_time_if_reqd():
    OUTLINE_COLOUR = (0, 0, 0)
    MESSAGE_COLOUR = (255, 255, 255)

    city = '<SOMEPLACE>'
    country_code = '<SOMECOUNTRY>'
    city = 'Colchester'
    country_code = 'UK'
    #example
    #city = 'Lahore'
    #country_code = 'PAK'
    width = GalacticUnicorn.WIDTH
    height = GalacticUnicorn.HEIGHT

    open_weather_map_api_key = '<YOUR_API_KEY>'
    open_weather_map_api_key = 'dd97959d2409e6e1556c37a189fca344'

    station = network.WLAN(network.STA_IF)

    @@ -97,28 +100,31 @@ def redraw_weather():
    location = weather_data.json().get('name')
    # Weather Description
    description = weather_data.json().get('weather')[0].get('main')
    weather_icon = weather_data.json().get('weather')[0].get('icon')
    print(description)
    if (description == "Clouds"):
    icon="greycloud.jpeg"
    else:
    icon="sunny.jpeg"
    print(weather_icon)
    icon=weather_icon+'.jpg'
    raw_temperature = weather_data.json().get('main').get('temp')-273.15
    temperature = str(round(raw_temperature)) + '°'
    print(temperature)

    graphics.set_pen(graphics.create_pen(10,0,96))
    for x in range(11,30):
    for y in range(0,height):
    graphics.pixel(x, y)
    j = jpegdec.JPEG(graphics)
    j.open_file(icon)
    j.decode(0, 0, jpegdec.JPEG_SCALE_FULL)
    graphics.set_font("bitmap8")
    #graphics.set_pen(WHITE)
    outline_text(temperature,16, 2)
    outline_text(temperature,13, 2)
    galactic.update(graphics)
    time.sleep(120)

    def redraw_display_if_reqd():
    global year, month, day, wd, hour, minute, second, last_second
    global year, month, day, wd, hour, minute, second, last_second, blink

    year, month, day, wd, hour, minute, second, _ = rtc.datetime()

    if second != last_second:
    hour += utc_offset
    if hour < 0:
    @@ -128,11 +134,16 @@ def redraw_display_if_reqd():

    #set_background()
    graphics.set_pen(graphics.create_pen(10,0,96))
    for x in range(32,width):
    for x in range(30,width):
    for y in range(0,height):
    graphics.pixel(x, y)

    clock = "{:02}:{:02}".format(hour, minute)
    if blink == 0:
    clock = "{:02}:{:02}".format(hour, minute)
    blink = 1
    else:
    clock = "{:02}|{:02}".format(hour, minute)
    blink = 0
    graphics.set_font("bitmap8")
    outline_text(clock, 32, 2)
    last_second = second
    @@ -143,10 +154,9 @@ def redraw_time():
    galactic.update(graphics)
    time.sleep(0.1)


    #while True:
    galactic.set_brightness(0.5)

    set_background()
    #set_background()
    second_thread = _thread.start_new_thread(redraw_time, ())
    redraw_weather()
  5. @vwillcox vwillcox revised this gist Feb 5, 2023. 1 changed file with 28 additions and 13 deletions.
    41 changes: 28 additions & 13 deletions weather2.py
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@

    rtc = machine.RTC()
    year, month, day, wd, hour, minute, second, _ = rtc.datetime()
    last_second = second
    galactic = GalacticUnicorn()
    graphics = PicoGraphics(DISPLAY)
    #Timer global variables
    @@ -54,7 +55,7 @@ def sync_time_if_reqd():
    width = GalacticUnicorn.WIDTH
    height = GalacticUnicorn.HEIGHT

    open_weather_map_api_key = '<YourAPIKey>'
    open_weather_map_api_key = '<YOUR_API_KEY>'

    station = network.WLAN(network.STA_IF)

    @@ -114,24 +115,38 @@ def redraw_weather():
    galactic.update(graphics)
    time.sleep(120)

    def redraw_time():
    while True:

    global year, month, day, wd, hour, minute, second, last_second

    # Display the result
    year, month, day, wd, hour, minute, second, _ = rtc.datetime()
    def redraw_display_if_reqd():
    global year, month, day, wd, hour, minute, second, last_second

    year, month, day, wd, hour, minute, second, _ = rtc.datetime()
    if second != last_second:
    hour += utc_offset
    if hour < 0:
    hour += 24
    elif hour >= 24:
    hour -= 24

    #set_background()
    graphics.set_pen(graphics.create_pen(10,0,96))
    for x in range(32,width):
    for y in range(0,height):
    graphics.pixel(x, y)

    clock = "{:02}:{:02}".format(hour, minute)

    # set the font
    #graphics.set_font("bitmap8")
    # calculate text position so that it is centred
    graphics.set_font("bitmap8")
    outline_text(clock, 32, 2)
    last_second = second

    def redraw_time():
    while True:
    redraw_display_if_reqd()
    galactic.update(graphics)
    time.sleep(0.01)
    time.sleep(0.1)


    #while True:
    galactic.set_brightness(0.5)

    set_background()
    second_thread = _thread.start_new_thread(redraw_time, ())
    redraw_weather()
  6. @vwillcox vwillcox revised this gist Feb 5, 2023. 1 changed file with 11 additions and 23 deletions.
    34 changes: 11 additions & 23 deletions weather2.py
    Original file line number Diff line number Diff line change
    @@ -46,15 +46,15 @@ def sync_time_if_reqd():
    OUTLINE_COLOUR = (0, 0, 0)
    MESSAGE_COLOUR = (255, 255, 255)

    city = 'SOMEPLACE'
    country_code = 'SOMECOUNTRY'
    city = '<SOMEPLACE>'
    country_code = '<SOMECOUNTRY>'
    #example
    #city = 'Lahore'
    #country_code = 'PAK'
    width = GalacticUnicorn.WIDTH
    height = GalacticUnicorn.HEIGHT

    open_weather_map_api_key = '<YOUR API KEY>'
    open_weather_map_api_key = '<YourAPIKey>'

    station = network.WLAN(network.STA_IF)

    @@ -80,7 +80,7 @@ def outline_text(text, x, y):

    def set_background():
    # global cheercolor
    graphics.set_pen(graphics.create_pen(10,0,16))
    graphics.set_pen(graphics.create_pen(10,0,96))
    for x in range(14,width):
    for y in range(0,height):
    graphics.pixel(x, y)
    @@ -101,19 +101,15 @@ def redraw_weather():
    icon="greycloud.jpeg"
    else:
    icon="sunny.jpeg"

    #icon="thunder.jpg"
    raw_temperature = weather_data.json().get('main').get('temp')-273.15
    temperature = str(round(raw_temperature)) + '°'
    raw_min = weather_data.json().get('main').get('temp_min')-273.17
    mintemp = str(round(raw_min))+'°'
    print(temperature)
    set_background()

    j = jpegdec.JPEG(graphics)
    j.open_file(icon)
    j.decode(0, 0, jpegdec.JPEG_SCALE_FULL)
    graphics.set_font("bitmap8")
    graphics.set_pen(WHITE)
    #graphics.set_pen(WHITE)
    outline_text(temperature,16, 2)
    galactic.update(graphics)
    time.sleep(120)
    @@ -129,21 +125,13 @@ def redraw_time():

    # set the font
    #graphics.set_font("bitmap8")

    # calculate text position so that it is centred
    w = graphics.measure_text(clock, 1)
    #x = int(width / 2 - w / 2 + 1)
    x = 10
    y = 2

    outline_text(clock, 32, y)
    outline_text(clock, 32, 2)
    galactic.update(graphics)
    time.sleep(0.01)


    while True:
    second_thread = _thread.start_new_thread(redraw_time, ())
    redraw_weather()
    #galactic.update(graphics)
    #graphics.clear()
    time.sleep(30)
    #while True:
    set_background()
    second_thread = _thread.start_new_thread(redraw_time, ())
    redraw_weather()
  7. @vwillcox vwillcox created this gist Feb 5, 2023.
    149 changes: 149 additions & 0 deletions weather2.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,149 @@
    import urequests as requests
    import json
    import network
    import secrets
    from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
    from galactic import GalacticUnicorn
    import jpegdec, math, ntptime, time
    import _thread

    rtc = machine.RTC()
    year, month, day, wd, hour, minute, second, _ = rtc.datetime()
    galactic = GalacticUnicorn()
    graphics = PicoGraphics(DISPLAY)
    #Timer global variables
    last_time_sync = time.time()
    last_cheerlight_check = time.time()
    utc_offset = 0

    def sync_time():
    global wlan
    if not wifi_available:
    return

    if not wlan.isconnected():
    wifi_connect()

    try:
    ntptime.settime()
    #print("Time set")
    except OSError as e:
    print(e)
    pass


    def sync_time_if_reqd():
    global last_time_sync
    if time.time() - last_time_sync > 86400: #Sync once per day
    #print ("Running Time Sync")
    sync_time()
    last_time_sync = time.time()


    WHITE = graphics.create_pen(255, 255, 255)
    BLACK = graphics.create_pen(0, 0, 0)
    BACKGROUND_COLOUR = (10, 0, 96) # Blue
    OUTLINE_COLOUR = (0, 0, 0)
    MESSAGE_COLOUR = (255, 255, 255)

    city = 'SOMEPLACE'
    country_code = 'SOMECOUNTRY'
    #example
    #city = 'Lahore'
    #country_code = 'PAK'
    width = GalacticUnicorn.WIDTH
    height = GalacticUnicorn.HEIGHT

    open_weather_map_api_key = '<YOUR API KEY>'

    station = network.WLAN(network.STA_IF)

    station.active(True)
    station.connect(secrets.WIFI_SSID, secrets.WIFI_PASSWORD)

    while station.isconnected() == False:
    pass

    def outline_text(text, x, y):
    graphics.set_pen(BLACK)
    graphics.text(text, x - 1, y - 1, -1, 1)
    graphics.text(text, x, y - 1, -1, 1)
    graphics.text(text, x + 1, y - 1, -1, 1)
    graphics.text(text, x - 1, y, -1, 1)
    graphics.text(text, x + 1, y, -1, 1)
    graphics.text(text, x - 1, y + 1, -1, 1)
    graphics.text(text, x, y + 1, -1, 1)
    graphics.text(text, x + 1, y + 1, -1, 1)

    graphics.set_pen(WHITE)
    graphics.text(text, x, y, -1, 1)

    def set_background():
    # global cheercolor
    graphics.set_pen(graphics.create_pen(10,0,16))
    for x in range(14,width):
    for y in range(0,height):
    graphics.pixel(x, y)

    print('Connection successful')

    def redraw_weather():
    while True:
    #set your unique OpenWeatherMap.org URL
    open_weather_map_url = 'http://api.openweathermap.org/data/2.5/weather?q=' + city + ',' + country_code + '&APPID=' + open_weather_map_api_key
    weather_data = requests.get(open_weather_map_url)
    # Location (City and Country code)
    location = weather_data.json().get('name')
    # Weather Description
    description = weather_data.json().get('weather')[0].get('main')
    print(description)
    if (description == "Clouds"):
    icon="greycloud.jpeg"
    else:
    icon="sunny.jpeg"

    #icon="thunder.jpg"
    raw_temperature = weather_data.json().get('main').get('temp')-273.15
    temperature = str(round(raw_temperature)) + '°'
    raw_min = weather_data.json().get('main').get('temp_min')-273.17
    mintemp = str(round(raw_min))+'°'
    print(temperature)
    set_background()
    j = jpegdec.JPEG(graphics)
    j.open_file(icon)
    j.decode(0, 0, jpegdec.JPEG_SCALE_FULL)
    graphics.set_font("bitmap8")
    graphics.set_pen(WHITE)
    outline_text(temperature,16, 2)
    galactic.update(graphics)
    time.sleep(120)

    def redraw_time():
    while True:

    global year, month, day, wd, hour, minute, second, last_second

    # Display the result
    year, month, day, wd, hour, minute, second, _ = rtc.datetime()
    clock = "{:02}:{:02}".format(hour, minute)

    # set the font
    #graphics.set_font("bitmap8")

    # calculate text position so that it is centred
    w = graphics.measure_text(clock, 1)
    #x = int(width / 2 - w / 2 + 1)
    x = 10
    y = 2

    outline_text(clock, 32, y)
    galactic.update(graphics)
    time.sleep(0.01)


    while True:
    second_thread = _thread.start_new_thread(redraw_time, ())
    redraw_weather()
    #galactic.update(graphics)
    #graphics.clear()
    time.sleep(30)