import time
time.sleep(0.1) # Wait for USB to become ready
print("Hello, Pi Pico W!")
import network
import urequests
import ujson
import time
# Your Wi-Fi credentials
#SSID = 'Verizon-MiFi8800L-14D8'
#PASSWORD = 'FF1FA5A8'
SSID = 'Wokwi-GUEST'
PASSWORD = ''
# Your API keys
OPENWEATHERMAP_API_KEY = '039487592f6580da661b6194ed72570a'
# The zip code for which you want to get the data
ZIP_CODE = '95032'
COUNTRY_CODE = '1'
"""
# Function to connect to Wi-Fi
def connect_to_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print('Connected to WiFi')
print(wlan.ifconfig())
"""
def connect_to_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
print('Connected to WiFi')
print('network config:', wlan.ifconfig())
# Function to get temperature from OpenWeatherMap
def get_temperature(zip_code, country_code, api_key):
url = f'http://api.openweathermap.org/data/2.5/weather?zip={zip_code},{country_code}&appid={api_key}&units=metric'
response = urequests.get(url)
data = ujson.loads(response.text)
temperature = data['main']['temp']
response.close()
return temperature
"""
# Function to get AQI from BreezoMeter
def get_aqi(zip_code, country_code, api_key):
url = f'http://api.breezometer.com/air-quality/v2/current-conditions?lat={lat}&lon={lon}&key={api_key}'
response = urequests.get(url)
data = ujson.loads(response.text)
aqi = data['data']['indexes']['baqi']['aqi']
response.close()
return aqi
"""
def main():
connect_to_wifi(SSID, PASSWORD)
try:
temperature = get_temperature(ZIP_CODE, COUNTRY_CODE, OPENWEATHERMAP_API_KEY)
print(f'Temperature: {temperature}°C')
except Exception as e:
print(f'Failed to get temperature: {e}')
"""
try:
aqi = get_aqi(ZIP_CODE, COUNTRY_CODE, BREEZOMETER_API_KEY)
print(f'Air Quality Index: {aqi}')
except Exception as e:
print(f'Failed to get AQI: {e}')
"""
if __name__ == '__main__':
main()