It's me again....
Been up all night reading up and researching on Python and kind of sloppily came up with a code for other codes here and there. I'm positive it won't work so before I even attempt it I'll drop it here so someone can point and laugh then correct me. I don't mind the criticism one bit. I know it's not fully complete but gotta start somewhere.
I'm basically wanting a simple forecast that will refresh every thirty minutes from openweather.
import time
import requests
from PIL import Image, ImageDraw, ImageFont
import ST7789 as ST7789
# Replace with your own OpenWeatherMap API key and city
API_KEY = "YOUR_API_KEY"
CITY = "YOUR_CITY"
UNITS = "metric" # or 'imperial'
# Initialize the display
disp = ST7789.ST7789(
rst=25, dc=24, sck=11, sdi=10,
width=240, height=240, rotation=180
)
disp.begin()
def get_weather_data():
url = f"http://api.openweathermap.org/data/2.5/ ... d={API_KEY}"
response = requests.get(url)
data = response.json()
return data
def draw_weather_forecast(forecast):
# Create an image with a black background
image = Image.new('RGB', (240, 240), "black")
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
# Draw the title
draw.text((5, 5), f"Weather in {CITY}", fill="white", font=font)
# Draw the forecast
y_offset = 30
for day in range(0, 3): # Display three days
day_data = forecast['list'][day * 8] # Every 8 entries is a new day
date = time.strftime('%Y-%m-%d', time.localtime(day_data['dt']))
temp = day_data['main']['temp']
description = day_data['weather'][0]['description']
draw.text((5, y_offset), f"{date}: {temp}°C, {description}", fill="white", font=font)
y_offset += 30
disp.display(image)
def main():
while True:
try:
weather_data = get_weather_data()
draw_weather_forecast(weather_data)
time.sleep(1800) # Sleep for 30 minutes (1800 seconds)
except Exception as e:
print(f"Error: {e}")
time.sleep(60) # If there's an error, wait 1 minute before retrying
if __name__ == "__main__":
main()
Been up all night reading up and researching on Python and kind of sloppily came up with a code for other codes here and there. I'm positive it won't work so before I even attempt it I'll drop it here so someone can point and laugh then correct me. I don't mind the criticism one bit. I know it's not fully complete but gotta start somewhere.
I'm basically wanting a simple forecast that will refresh every thirty minutes from openweather.
import time
import requests
from PIL import Image, ImageDraw, ImageFont
import ST7789 as ST7789
# Replace with your own OpenWeatherMap API key and city
API_KEY = "YOUR_API_KEY"
CITY = "YOUR_CITY"
UNITS = "metric" # or 'imperial'
# Initialize the display
disp = ST7789.ST7789(
rst=25, dc=24, sck=11, sdi=10,
width=240, height=240, rotation=180
)
disp.begin()
def get_weather_data():
url = f"http://api.openweathermap.org/data/2.5/ ... d={API_KEY}"
response = requests.get(url)
data = response.json()
return data
def draw_weather_forecast(forecast):
# Create an image with a black background
image = Image.new('RGB', (240, 240), "black")
draw = ImageDraw.Draw(image)
font = ImageFont.load_default()
# Draw the title
draw.text((5, 5), f"Weather in {CITY}", fill="white", font=font)
# Draw the forecast
y_offset = 30
for day in range(0, 3): # Display three days
day_data = forecast['list'][day * 8] # Every 8 entries is a new day
date = time.strftime('%Y-%m-%d', time.localtime(day_data['dt']))
temp = day_data['main']['temp']
description = day_data['weather'][0]['description']
draw.text((5, y_offset), f"{date}: {temp}°C, {description}", fill="white", font=font)
y_offset += 30
disp.display(image)
def main():
while True:
try:
weather_data = get_weather_data()
draw_weather_forecast(weather_data)
time.sleep(1800) # Sleep for 30 minutes (1800 seconds)
except Exception as e:
print(f"Error: {e}")
time.sleep(60) # If there's an error, wait 1 minute before retrying
if __name__ == "__main__":
main()
Statistics: Posted by DaBeard — Wed Oct 09, 2024 9:47 am