import urequests
import network
import time
import ntptime
from lcd1602 import LCD
lcd=LCD()
lcd.clear()
lcd.message("Weather Station\n")
time.sleep(2)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("IoT Network","1234abcd")
openweather_api_key = "7a805f6f042f52f30e0b0078bc951f03"
city = "Bhubaneswar,Odisha,India"
update_freq = 60
while True:
lcd.clear()
lcd.message("Connecting to\nWiFi...")
time.sleep(1)
if wlan.status() >= 3:
break
time.sleep(1)
lcd.clear()
lcd.message("Connected to\nWiFi :)")
time.sleep(2)
lcd.clear()
lcd.message(f'IP Address :\n{wlan.ifconfig()[0]}')
time.sleep(2)
while True:
lcd.clear()
lcd.message("Syncing Time...")
time.sleep(1)
try:
ntptime.settime()
lcd.clear()
lcd.message("Time synced\nsuccessfully :)")
time.sleep(2)
break
except:
continue
def get_weather():
lcd.clear()
lcd.message(f"Weather Update :\n{city.split(',')[0]}")
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={openweather_api_key}&units=metric&lang=en"
print(url)
res = urequests.post(url)
return res.json()
while True:
try:
weather_data = get_weather()
weather=weather_data["weather"][0]["main"]
t=weather_data["main"]["temp"]
rh=weather_data["main"]["humidity"]
hours=time.localtime()[3]+int(weather_data["timezone"] / 3600)
mins=time.localtime()[4]
lcd.clear()
time.sleep_ms(200)
string = f'{hours:02d}:{mins:02d} {weather}\n'
lcd.message(string)
string = f'{t}°C {rh}% Humid'
lcd.message(string)
time.sleep(update_freq)
except:
lcd.clear()
lcd.message("Failed to\nupdate weather:(")
time.sleep(10)