Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4863

MicroPython • Re: Raspberry Pi Pico WH does not refresh Web Server

$
0
0
I looked at switching to Weather Underground API, but thought I'd do a quick trick to see if I could still get the error without needing to code a new API
I went into CommonCode.fetch_weather (below) and commented out the openWeatherMap api call and put in dummy data and then ran the program. Same thing. Here is thonny output in shell

MPY: soft reboot
Connected on 192.168.4.144
9/24/2024, 19:36:38, WHITE, Clear, 82, 4, ENE, 32, 1000, nice day, America/Denver, []
1 9/24/2024 19:36:38
2
9/24/2024, 19:40:48, WHITE, Clear, 82, 4, ENE, 32, 1000, nice day, America/Denver, []
1 9/24/2024 19:40:48
2
9/24/2024, 19:44:49, WHITE, Clear, 82, 4, ENE, 32, 1000, nice day, America/Denver, []
1 9/24/2024 19:44:49


it hung up and around 19:45:15 I did a manual refresh on browser and browser updated to 19:44:49 and thonny showed "2" and then 4 minutes later gave 2 more fake API weather update readings before hanging up again.
SO removing API call and using dummy data still gives the 2 issues
1) after initial web display, display does not update with cl.send(response) command
2) program will run 3 "api" queries and then hang without manual browser refresh.

Here is commonCode with dummy data

Code:

# this is code that will work with either the Raspberry Pi OR the Raspberry Pi Pico WHimport timeimport urequestsDEBUG = Truedef fetch_weather(lat, lon, api_key):#     url = f'http://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={api_key}&units=imperial'#     try:#         data = urequests.get(url).json()#     except Exception as e:#         print(f"API call failed: {e}")#         return None  # Return None if the call fails# #     weather_data = {#         'condition': data['current']['weather'][0]['main'],#         'description': data['current']['weather'][0]['description'],#         'pressure': data['current']['pressure'],#         'wind_speed': data['current']['wind_speed'],#         'wind_direction': calc_wind_direction(data['current']['wind_deg']),#         'humidity': data['current']['humidity'],#         'temp': data['current']['temp'],#         'timezone': data['timezone'],#         'alerts': extract_alerts(data.get('alerts', [])),#         'timestamp': time.localtime()#     }        weather_data = {    'condition': 'Clear',    'description': 'nice day',    'pressure': 1000,    'wind_speed': 4,    'wind_direction': calc_wind_direction(65),    'humidity': 32,    'temp': 82,    'timezone': 'America/Denver',    'alerts': [],    'timestamp': time.localtime()    }    return weather_data# Convert Wind Direction to Compassdef calc_wind_direction(deg):    directions = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW']    return directions[int((deg + 11.25) // 22.5) % 16]# Log and print weather informationdef log_weather(weather, color):    if not DEBUG:        return    today = f"{weather['timestamp'][1]}/{weather['timestamp'][2]}/{weather['timestamp'][0]}"    time_str = f"{weather['timestamp'][3]}:{weather['timestamp'][4]}:{weather['timestamp'][5]}"    print(f"{today}, {time_str}, {color}, {weather['condition']}, {weather['temp']}, {weather['wind_speed']}, {weather['wind_direction']}, {weather['humidity']}, {weather['pressure']}, {weather['description']}, {weather['timezone']}, {weather['alerts']}")# Determine traffic light color based on weatherdef determine_light_color(description, pressure, wind_speed, alerts):    if any(phrase in description for phrase in ['heavy', 'extreme']) or wind_speed >= 35 or 'tornado' in alerts:        return 'BLACK'    elif 'moderate rain' in description or pressure < 965 or wind_speed >= 20 or 'warning' in alerts:        return 'YELLOW'    return 'WHITE'# Extract alerts from API datadef extract_alerts(alerts):    return '; '.join(alert['event'].lower() for alert in alerts)# Load location data from filedef load_location_data(filepath="myLatitudeLongitude.txt"):    try:        with open(filepath, "r") as file:            lat, lon, location = file.readline().strip().split(',')            return float(lat), float(lon), location    except Exception as e:        raise Exception("Error loading location data: " + str(e))
so if you can help me, I'd be much obliged.
You should be able to reproduce my results without needing to access the OpenWeatherMap API.

thank you one and all.

Statistics: Posted by merino — Wed Sep 25, 2024 2:06 am



Viewing all articles
Browse latest Browse all 4863

Trending Articles