import time
import neopixel
import network
import urequests
import json
from neopixel import Neopixel
# Neopixel ring configuration
pixels = Neopixel(17, 0, 6, "GRB")
num_pixels = 17
# Weather API configuration for forecast
api_key = "9ab0e440abed79d253bff3463d1594cd"
city = "Kolkata"
forecast_api_url = f"http://api.openweathermap.org/data/2.5/forecast?cnt=2&q={city}&appid={api_key}&units=metric"
# Weather condition colors
weather_colors = {
"Clear": (255, 255, 0), # Yellow for clear sky
"Clouds": (128, 128, 128), # Gray for cloudy
"Rain": (0, 0, 255), # Blue for rain
"Snow": (255, 255, 255), # White for snow
"Thunderstorm": (255, 0, 0), # Red for thunderstorm
"Drizzle": (0, 255, 255), # Cyan for drizzle
"Mist": (128, 128, 128) # Gray for mist
}
def get_weather_forecast():
try:
response = urequests.get(forecast_api_url)
data = json.loads(response.text)
# Find the forecast for the next day (assuming it's the first entry for simplicity)
forecast = data["list"][1]["weather"][0]["main"]
print(f"\nWeather forecast for the next day: {forecast}")
return forecast
except:
return "Error"
def update_neopixel_ring(weather_forecast):
if weather_forecast in weather_colors:
color = weather_colors[weather_forecast]
else:
color = (128, 128, 128) # Default to gray for unknown weather conditions
for i in range(num_pixels):
pixels.set_pixel(i, color)
pixels.show()
def wait_until_midnight():
current_time = time.localtime()
seconds_until_midnight = 86400 - (current_time[3] * 3600 + current_time[4] * 60 + current_time[5])
time.sleep(seconds_until_midnight)
print("Connecting to WiFi", end="")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print(wlan.ifconfig())
wait = 10
while wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
wait -= 1
print('Waiting for connection...')
time.sleep(1)
if wlan.status() != 3:
raise RuntimeError('Network connection failed')
else:
print('Connected')
ip = wlan.ifconfig()[0]
print('IP:', ip)
while True:
wait_until_midnight() # Wait until midnight
weather_forecast = get_weather_forecast()
update_neopixel_ring(weather_forecast)
time.sleep(86400) # Update every 24 hours