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:
commit
ddd867eb00
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal 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
|
||||
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal 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
8
.idea/modules.xml
generated
Normal 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
6
.idea/vcs.xml
generated
Normal 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
11
.idea/weatherDisplay.iml
generated
Normal 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
44
main.py
Normal 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
55
weather_requests.py
Normal 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]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user