# Micropython, current weather from open-meteo.com, Fort Worth, TX
import network, json
import time
from I2c_lcd import I2cLcd
from machine import Pin, SoftI2C
import urequests
addr_lcd = 0x27
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
# connect scl to GPIO 22, sda to GPIO 21
lcd = I2cLcd(i2c, addr_lcd, 4, 20)
print("Connecting to WiFi", end="")
lcd.move_to(0,0) # col, row
lcd.putstr("Connecting...")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.5)
print(" Connected!")
lcd.clear()
lcd.putstr("Connected")
URL = "http://api.open-meteo.com/v1/gfs?latitude=32.73&longitude=-97.32"
URL += "&hourly=temperature_2m,relativehumidity_2m,apparent_temperature,"
URL += "precipitation_probability,precipitation,"
URL += "cloudcover,windspeed_10m,windgusts_10m&daily=sunrise,sunset,temperature_2m_max,temperature_2m_min"
URL += "&temperature_unit=fahrenheit"
URL += "&windspeed_unit=mph&forecast_days=1&timezone=America%2FChicago¤t_weather=True"
URL += "&precipitation_unit=inch"
resp = urequests.get(URL)
data = resp.json()
print(data)
# This is raw full stamp to be used in the for loop next...
asofStamp = str(data['current_weather']['time'])
# print(asofDate)
# and isolate just the Time
asof = asofStamp[11:16]
# convert away from 24-hr time
hourRaw = asof[0:2]
hrNum = int(hourRaw)
AM_PM = "AM"
print(hrNum)
if hrNum > 12:
hrNum -= 12
AM_PM = "PM"
if hrNum == 12:
AM_PM = "PM"
timeStamp = str(hrNum) + asofStamp[13:16] + " " + AM_PM
currTemp = str(round(data['current_weather']['temperature']))
wind = str(round(data['current_weather']['windspeed']))
curr_hr = str(asofStamp[11:13])
print("curr_hr " + curr_hr)
# use current HOUR as index...
idx = int(curr_hr) + 1
print("index into hourly: " + str(idx))
# forecastIndex = idx
gust = str(round(data['hourly']['windgusts_10m'][idx]))
feel = str(round(data['hourly']['apparent_temperature'][idx]))
clouds = int(data['hourly']['cloudcover'][idx])
if int(clouds < 20):
condition = "Clear"
else:
condition = "Clouds " + str(clouds) + "%"
sunrise = str(data['daily']['sunrise'])
sunrise = sunrise[14:18]
sunset = str(data['daily']['sunset'])
# isolate sunset hour
sunsetHour = int(sunset[13:15]) - 12
sunset = str(sunsetHour) + sunset[15:18] # stick on colon and minute
maxtemp = str(round(data['daily']['temperature_2m_max'][0]))
mintemp = str(round(data['daily']['temperature_2m_min'][0]))
humi = str(round(data['hourly']['relativehumidity_2m'][idx]))
def Page1():
lcd.clear()
lcd.move_to(0,0) # col, row
lcd.putstr(timeStamp + " " + currTemp + " Feel " + feel)
lcd.move_to(0,1)
lcd.putstr("Wind " + wind + " Gust " + gust)
lcd.move_to(0,2)
lcd.putstr(condition)
lcd.move_to(0,3)
lcd.putstr("Sunrise " + sunrise + " - " + sunset)
def Page2():
lcd.clear()
lcd.move_to(0,0) # col, row
lcd.putstr("Range " + maxtemp + " - " + mintemp)
lcd.move_to(0,1)
lcd.putstr("Humi " + humi + "%")
while True:
Page1()
time.sleep(4)
Page2()
time.sleep(4)