Inky Frame 4.0 - Weather Data import (similar to Badger!)

I am trying to port the Badger weather.py code over to my Inky Frame 4.0 but i’m new to Python and have got stuck with the json parsing of the Meteo api data. It doesn’t seem to like the j = r.json() line but i am not sure why. Am i missing a module that needs importing to use json to parse the data? Any help would be gratefully received! :) (URL already defined in earlier code!)
John

    print(f"Requesting URL: {URL}")    #working
    r = urequest.urlopen(URL)          # working now modded to News one.
    # open the json data
    j = r.json()                  #NOT WORKING  it is basically saying j is urequest.urlopen(URL).json()  It's lile its trying to jsonise the the data returned by the URL
    print("Data obtained!")
    print(j)

Can you post your full code?

Are you using urequests or urllib.urequest to handle your HTTP request? I think the command to parse the json will look a bit different depending on which library you’re using?

This example uses urllib.urequest, for example :

and this one uses urequests:

Thanks Hel, i’ll look through those and see which will do the job.

Thanks Hel for the tips! I realised that i was short of Wifi connection info too. Now i have it functioning but the font is rather small and the weather icon also small so getting there but needs more work! I have tried adding scale = 2 to the text display line but that doesn’t seem to change the size of the displayed font.

Also i read that jpegdec doesn’t support image upscaling so i tried to resize the original weather icons (64x64 px i think they were) to 150x150 (and adding a splash of colour to them) but these don’t load and calling them causes the program to stop. I did export them properly (not progressive or enhanced) so i don’t think it’s that.
I have included the relevant part of the code (draw_page) regarding the text displaying and image rendering in case anyone has any suggestions as to how i can increase font sizing and render double (ish) sized images.

Ignore the lower panel, i am adding an instant drone flying status panel too which will take wind and possibly precipitation into account and give a symbol for the day. These are WIP and need more contrast and would be better at 150x150 if i can get them to work.

When it’s a bit more sorted i’ll post the whole code if anyone is interested in it (without typos on screen :) )

def draw_page():
    
    graphics.set_pen(inky_frame.WHITE) #Set white pen
    graphics.clear() #Set all screen to white
    graphics.set_font("bitmap12") #set font for weather header
    graphics.set_pen(inky_frame.BLUE) # set colour for top box
    graphics.rectangle(0, 0, 640, 20)
    graphics.rectangle(0, 200, 640, 20) # draw top box rectangle - x,y,width, height
    graphics.set_pen(inky_frame.WHITE)
    graphics.text("Weather", 3, 4, scale = 2) #x,y,scaling from 8 pixel
    graphics.text("Drone Operting Status", 3, 204, scale = 2) # Write 'Weather' in top box  - Text parameters are position offset x,y from 0,0 (top left)


    graphics.set_pen(inky_frame.BLACK) # set colour to black
    graphics.set_font("bitmap8")  #set font for next bit
  #  graphics.update() # Refresh Display  - remd out as seems too many updates
    
    
    if temperature is not None:
        # Choose an appropriate icon based on the weather code
        # Weather codes from https://open-meteo.com/en/docs
        # Weather icons from https://fontawesome.com/
        if weathercode in [71, 73, 75, 77, 85, 86]:  # codes for snow
            jpeg.open_file("/icons/icon-snow.jpg")
        elif weathercode in [51, 53, 55, 56, 57, 61, 63, 65, 66, 67, 80, 81, 82]:  # codes for rain
            jpeg.open_file("/icons/icon-rain.jpg")
        elif weathercode in [1, 2, 3, 45, 48]:  # codes for cloud
            jpeg.open_file("/icons/icon-cloud.jpg")
        elif weathercode in [0]:  # codes for sun
            jpeg.open_file("/icons/icon-sun.jpg")
        elif weathercode in [95, 96, 99]:  # codes for storm
            jpeg.open_file("/icons/icon-storm.jpg")
        jpeg.decode(13, 60, jpegdec.JPEG_SCALE_FULL) #x,y position of jpeg
        graphics.set_pen(0)
        graphics.text(f"Temperature: {temperature}°C", int(WIDTH / 3), 48, WIDTH - 105, 2)
        graphics.text(f"Wind Speed: {windspeed}kmph", int(WIDTH / 3), 68, WIDTH - 105, 2)
        graphics.text(f"Wind Direction: {winddirection}", int(WIDTH / 3), 88, WIDTH - 105, 2)
        graphics.text(f"Last update: {date}, {time}", int(WIDTH / 3), 108, WIDTH - 105, 2)
        if windspeed > 10:
            jpeg.open_file("/icons/no-drone.jpg") #windspeed is too high for drone flight
        elif weathercode <=10:  # windsspeed is low enough for drone flight
            jpeg.open_file("/icons/go-drone.jpg")
        jpeg.decode(13, 250, jpegdec.JPEG_SCALE_FULL) #x,y position of jpeg
        graphics.update()

    else:
        graphics.set_pen(0)
        graphics.rectangle(0, 60, WIDTH, 25)
        graphics.set_pen(15)
        graphics.text("Unable to display weather! Check your network settings in WIFI_CONFIG.py", 5, 65, WIDTH, 1)
        graphics.update()

Here’s some of the larger weather icons i have used for testing.
icon-rain150
icon-snow150
icon-sun150

An update here, it would seem that one of the 150x150 images (icon_cloud150.jpg - not called in the code above as i had given up and switched back to the smaller ones at that point!) was corrupted and as it happened, that was the one that the programme happened to want to call due to the weather at the time i was testing. So i have solved that issue and jpegdec is providing me correctly with larger icons. hooray!

I have not yet resolved the issue with tiny fonts though, any comments on my use of scaling in the code would be appreciated.

Then i want to make a version that will run under the launcher (and do a refresh on button press and maybe hourly) but i will look at the other ‘under launcher’ programmes as a guide. Launcher seems to handle the wifi connection, is there anything else i need to be wary of?

Cheers
D
20230328_131730 Weatherv2

1 Like

Looking good!

I think scale=2 is the default for bitmap text - try scale=3 or scale=4?

1 Like

Thanks Hel, will try that!

Well that’s done the trick with that many thanks for the nudges in the right direction :) . Now onto the Launcher based version!

Cheers

D
20230328_170259weatherv3

1 Like