Add initial weather display application with API integration

Includes weather data retrieval from Open-Meteo API and basic WiFi setup for connectivity. IntelliJ project configuration files and a `.gitignore` for IDE-specific files are also added.
This commit is contained in:
Alexander Berry-Roe 2025-05-11 12:32:38 +01:00
commit ddd867eb00
7 changed files with 138 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/weatherDisplay.iml" filepath="$PROJECT_DIR$/.idea/weatherDisplay.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

11
.idea/weatherDisplay.iml generated Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.11 (weatherDisplay)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="MicroPythonTools" level="project" />
</component>
</module>

44
main.py Normal file
View File

@ -0,0 +1,44 @@
import network
import socket
import time
import weather_requests
# --- WiFi Connection Setup ---
SSID = 'octopod' # Replace with your WiFi SSID
PASSWORD = 'amniotic-duo-portfolio' # Replace with your WiFi Password
#Set current location
latitude = 50.9097
longitude = -1.4043
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(SSID, PASSWORD)
print("Connecting to WiFi...")
timeout = 10
start_time = time.time()
while not station.isconnected():
if time.time() - start_time > timeout:
print("Failed to connect to WiFi. Check your SSID and password.")
break
time.sleep(1)
if station.isconnected():
ip = station.ipconfig("addr4")
time.sleep(1)
ipv6_addr = station.ipconfig("addr6")
print("Connected to WiFi! IPv4 address:", ip)
print("Connected to WiFi! IPv6 address:", ipv6_addr)
else:
print("WiFi connection not established. Restart and try again.")
raise SystemExit
weather = weather_requests.get_daily(latitude, longitude)
print(weather_requests.get_today_forecast(weather))

55
weather_requests.py Normal file
View File

@ -0,0 +1,55 @@
import urequests as requests
def get_hourly(latitude, longitude):
base_url = "https://api.open-meteo.com/v1/forecast"
query = (
f"latitude={latitude}&"
f"longitude={longitude}&"
"hourly=temperature_2m,precipitation_probability,weathercode&"
"timezone=Europe%2FLondon"
)
full_url = f"{base_url}?{query}"
try:
response = requests.get(full_url)
if response.status_code == 200:
return response.json()
else:
print(f"Error: Status {response.status_code}")
print(response.text)
return None
except Exception as e:
print(f"Request failed: {e}")
return None
def get_daily(latitude, longitude):
base_url = "https://api.open-meteo.com/v1/forecast"
query = (
f"latitude={latitude}&"
f"longitude={longitude}&"
"daily=temperature_2m_max,temperature_2m_min,precipitation_sum,weathercode&"
"timezone=Europe%2FLondon"
)
full_url = f"{base_url}?{query}"
try:
response = requests.get(full_url)
if response.status_code == 200:
return response.json()
else:
print(f"Error: Status {response.status_code}")
print(response.text)
return None
except Exception as e:
print(f"Request failed: {e}")
return None
def get_today_forecast(data):
daily = data["daily"]
return {
"date": daily["time"][0],
"max_temp": daily["temperature_2m_max"][0],
"min_temp": daily["temperature_2m_min"][0],
"precip_mm": daily["precipitation_sum"][0],
"weathercode": daily["weathercode"][0]
}