-1

I'm trying to replicate Joe Wreford's desktop weather display, this is the link to that; https://mustardcorner.com/desktop-weather, but i'm trying to use my pico w instead of the pi zero w he was using,

I've managed to get the pico to display the current weather but it won't display any forecasts yet and I have no clue how to get it to I would much appreciate any help, thanks, My code is not showing any errors

This is my code

import json
import urequests as requests
import utime as datetime
import urequests
import network
import time
from servo import Servo
from machine import Pin, PWM
import utime
FOG = 500000
SNOW = 1000000
THUNDER = 1400000
RAIN = 1900000
CLOUDS = 2200000
SUN = 2600000
TWODAYS = 500000
ONEDAY = 1000000
TWELVEHRS = 1400000
SIXHRS = 1900000
THREEHRS = 2200000
NOW = 2600000
# servo positions

pwm = PWM(Pin(1))
pwm2 = PWM(Pin(0))
pwm.freq(60)
pwm2.freq(60)
pwm.duty_ns(FOG)  
   
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("SSID","password")
openweather_api_key = "api_key"

# connect the network       
wait = 10
while wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    wait -= 1
    print('waiting for connection...')
    time.sleep(1) 
# Handle connection error
if wlan.status() != 3:
    raise RuntimeError('wifi connection failed')
else:
    print('connected')
    ip=wlan.ifconfig()[0]
    print('IP: ', ip) 
# syncing the time
import ntptime

while True:
    try:
        ntptime.settime()
        print('Time Set Successfully')
        break
    except OSError:
        print('Time Setting...')
        continue
# Open Weather
TEMPERATURE_UNITS = {
    "standard": "K",
    "metric": "°C",
    "imperial": "°F",
} 
SPEED_UNITS = {
    "standard": "m/s",
    "metric": "m/s",
    "imperial": "mph",
} 
units = "metric"
def get_weather(city, api_key, units='metric', lang='en'):
    '''
    Get weather data from openweathermap.org
        city: City name, state code and country code divided by comma, Please, refer to ISO 3166 for the state codes or country codes. https://www.iso.org/obp/ui/#search
        api_key: Your unique API key (you can always find it on your openweather account page under the "API key" tab https://home.openweathermap.org/api_keys
        unit: Units of measurement. standard, metric and imperial units are available. If you do not use the units parameter, standard units will be applied by default. More: https://openweathermap.org/current#data
        lang: You can use this parameter to get the output in your language. More: https://openweathermap.org/current#multi
    '''
    url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units={units}&lang={lang}"
    print(url)
    res = urequests.post(url)
    return res.json()
def print_weather(weather_data):
    print(f'Timezone: {int(weather_data["timezone"] / 3600)}')
    sunrise = time.localtime(weather_data['sys']['sunrise']+weather_data["timezone"])
    sunset = time.localtime(weather_data['sys']['sunset']+weather_data["timezone"])
    print(f'Sunrise: {sunrise[3]}:{sunrise[4]}')
    print(f'Sunset: {sunset[3]}:{sunset[4]}')   
    print(f'Country: {weather_data["sys"]["country"]}')
    print(f'City: {weather_data["name"]}')
    print(f'Coordination: [{weather_data["coord"]["lon"]}, {weather_data["coord"]["lat"]}')
    print(f'Visibility: {weather_data["visibility"]}m')
    print(f'Weather: {weather_data["weather"][0]["main"]}')
    print(f'Temperature: {weather_data["main"]["temp"]}{TEMPERATURE_UNITS[units]}')
    print(f'Temperature min: {weather_data["main"]["temp_min"]}{TEMPERATURE_UNITS[units]}')
    print(f'Temperature max: {weather_data["main"]["temp_max"]}{TEMPERATURE_UNITS[units]}')
    print(f'Temperature feels like: {weather_data["main"]["feels_like"]}{TEMPERATURE_UNITS[units]}')
    print(f'Humidity: {weather_data["main"]["humidity"]}%')
    print(f'Pressure: {weather_data["main"]["pressure"]}hPa')
    print(f'Wind speed: {weather_data["wind"]["speed"]}{SPEED_UNITS[units]}')
    print(f'Wind gust: {weather_data["wind"]["gust"]}{SPEED_UNITS[units]}')
    print(f'Wind direction: {weather_data["wind"]["deg"]}°')
    if "clouds" in weather_data:
        print(f'Cloudiness: {weather_data["clouds"]["all"]}%')
    elif "rain" in weather_data:
        print(f'Rain volume in 1 hour: {weather_data["rain"]["1h"]}mm')
        print(f'Rain volume in 3 hour: {weather_data["rain"]["3h"]}mm')
    elif "snow" in weather_data:
        print(f'Snow volume in 1 hour: {weather_data["snow"]["1h"]}mm')
        print(f'Snow volume in 3 hour: {weather_data["snow"]["3h"]}mm')    
while True:
    # get weather
    weather_data = get_weather('eastbourne', openweather_api_key, units=units)
    weather=weather_data["weather"][0]["main"]
    t=weather_data["main"]["temp"]
    rh=weather_data["main"]["humidity"]
    hours=time.localtime()[3]+int(weather_data["timezone"] / 3600)
    mins=time.localtime()[4]
    pwm2.duty_ns(NOW)
    print_weather(weather_data)
    if "rain" in weather_data:
        pwm.duty_ns(RAIN)
    elif "clouds" in weather_data:
        pwm.duty_ns(CLOUDS)
    elif "snow" in weather_data:
        pwm.duty_ns(SNOW)
    elif "fog" in weather_data:
        pwm.duty_ns(FOG)
    # refresh every 60s
    time.sleep(60)

Don't worry about finishing the whole code, but if you could just tell me how to get it to show future weather that would be great thanks

  • So what exactly is the problem? Can you [edit] your question and add details? What happens when you run your code and what did you expect to happen instead? Any errors? Please see [ask]. – Robert Aug 04 '23 at 14:39
  • So the code works fine when showing the current weather but im not sure how to get future weather forecasts e.g. in 6 hours time – Jesse gudgeon Aug 04 '23 at 18:41
  • This, with `cnt=6` and taking last `dt` I guess... https://openweathermap.org/api/hourly-forecast#5days – Mark Setchell Aug 06 '23 at 08:54

0 Answers0