Skip to content

Commit ad59d3f

Browse files
committed
First Commit
1 parent f9be51b commit ad59d3f

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
import os
5+
import ssl
6+
import time
7+
import wifi
8+
import board
9+
import displayio
10+
import terminalio
11+
import socketpool
12+
import adafruit_requests
13+
14+
from adafruit_display_text import bitmap_label
15+
16+
# Initialize Wi-Fi connection
17+
try:
18+
wifi.radio.connect(
19+
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
20+
)
21+
print("Connected to %s!" % os.getenv("CIRCUITPY_WIFI_SSID"))
22+
# Wi-Fi connectivity fails with error messages, not specific errors, so this except is broad.
23+
except Exception as e: # pylint: disable=broad-except
24+
print(
25+
"Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds."
26+
)
27+
28+
29+
# Create a socket pool and session object for making HTTP requests
30+
pool = socketpool.SocketPool(wifi.radio)
31+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
32+
33+
# Set location and units for weather data
34+
UNITS = "metric"
35+
LOCATION = os.getenv("LOCATION")
36+
print("Getting weather for {}".format(LOCATION))
37+
38+
# Set up the URL for fetching weather data
39+
DATA_SOURCE = (
40+
"http://api.openweathermap.org/data/2.5/weather?q="
41+
+ LOCATION
42+
+ "&units="
43+
+ UNITS
44+
+ "&mode=json"
45+
+ "&appid="
46+
+ os.getenv("OPENWEATHER_KEY")
47+
)
48+
49+
# Define time interval between requests
50+
time_interval = 3000 # set the time interval to 30 minutes
51+
52+
# Set up display a default image
53+
display = board.DISPLAY
54+
bitmap = displayio.OnDiskBitmap("/images/sunny.bmp")
55+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=bitmap.pixel_shader)
56+
57+
group = displayio.Group()
58+
group.append(tile_grid)
59+
60+
# Create label for displaying temperature data
61+
text_area = bitmap_label.Label(terminalio.FONT, scale=3)
62+
text_area.anchor_point = (0.5, 0.5)
63+
text_area.anchored_position = (display.width // 2, display.height // 2)
64+
65+
# Create main group to hold all display groups
66+
main_group = displayio.Group()
67+
main_group.append(group)
68+
main_group.append(text_area)
69+
# Show the main group on the display
70+
display.show(main_group)
71+
72+
# Define function to get the appropriate weather icon
73+
def get_weather_condition_icon(weather_condition):
74+
if "cloud" in weather_condition.lower():
75+
return "/images/cloudy.bmp"
76+
elif "rain" in weather_condition.lower():
77+
return "/images/rain.bmp"
78+
elif "snow" in weather_condition.lower():
79+
return "/images/snowy.bmp"
80+
elif "clear" in weather_condition.lower():
81+
return "/images/sunny.bmp"
82+
else:
83+
return "/images/sunny.bmp"
84+
85+
86+
# Define function to update the background image based on weather conditions
87+
def set_background(weather_condition, background_tile_grid):
88+
bitmap_path = get_weather_condition_icon(weather_condition)
89+
background_bitmap = displayio.OnDiskBitmap(bitmap_path)
90+
background_tile_grid.bitmap = background_bitmap
91+
92+
93+
# Main loop to continuously fetch and display weather data
94+
while True:
95+
96+
# Fetch weather data from OpenWeatherMap API
97+
print("Fetching json from", DATA_SOURCE)
98+
response = requests.get(DATA_SOURCE)
99+
print(response.json())
100+
101+
# Extract temperature and weather condition data from API response
102+
current_temp = response.json()["main"]["temp"]
103+
max_temp = response.json()["main"]["temp_max"]
104+
min_temp = response.json()["main"]["temp_min"]
105+
current_weather_condition = response.json()["weather"][0]["main"]
106+
107+
print("Weather condition: ", current_weather_condition)
108+
109+
# Convert temperatures to Fahrenheit
110+
max_temp = (max_temp * 9 / 5) + 32
111+
min_temp = (min_temp * 9 / 5) + 32
112+
current_temp = (current_temp * 9 / 5) + 32
113+
114+
# Convert temperatures to Fahrenheit to Celsius
115+
# max_temp = (max_temp - 32) * 5/9
116+
# min_temp = (min_temp - 32) * 5/9
117+
# current_temp = (current_temp - 32) * 5/9
118+
print("Current temperature: {:.1f} °F".format(current_temp))
119+
120+
# Update label for displaying temperature data
121+
text_area.text = "{}\n {:.0f}°F\nH:{:.0f}°F L:{:.0f}°F".format(
122+
LOCATION, round(current_temp), round(max_temp), round(min_temp))
123+
124+
# Update background image
125+
set_background(current_weather_condition, tile_grid)
126+
127+
time.sleep(time_interval)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)