import machine
import network
import utime
import urequests
import ssd1306
import time
# Replace with your WiFi credentials
#WIFI_SSID = "OPPO F11 PRO"
#WIFI_PASSWORD = "123456789"
# Open Meteo API URL with latitude and longitude
API_URL = "https://api.open-meteo.com/v1/forecast?latitude=2.52&longitude=13.41¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m"
# OLED Display setup
i2c = machine.I2C(0, scl=machine.Pin(22), sda=machine.Pin(21))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
def connect_to_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.disconnect()
wifi.connect('Wokwi-GUEST', '')
if not wifi.isconnected():
print('connecting..')
timeout = 0
while (not wifi.isconnected() and timeout < 5):
print(5 - timeout)
timeout = timeout + 1
time.sleep(1)
if(wifi.isconnected()):
print('connected')
else:
print('not connected')
def get_weather(latitude, longitude):
api_url = API_URL.format(latitude, longitude)
response = urequests.get(api_url)
weather_data = response.json()
response.close()
return weather_data
def display_info(date, time, temperature):
oled.fill(0)
oled.text(date, 0, 0)
oled.text(time, 0, 16)
oled.text(f"Temp: {temperature}C", 0, 32)
#oled.text(f"Humidity: {humidity}%", 0, 48)
#oled.text(f"Wind Speed: {wind_speed} m/s", 0, 64)
oled.show()
def main():
connect_to_wifi()
# Take user input for location
latitude = input("Enter latitude: ")
longitude = input("Enter longitude: ")
while True:
# Get weather information
weather_data = get_weather(latitude, longitude)
if weather_data:
current_weather = weather_data["current"]
temperature = current_weather["temperature_2m"]
# Get date and time
date = current_weather["time"]
print(date)
# Parse the input string into a tuple representing date and time
year, month, day, hour, minute = map(int, date.split("T")[0].split("-") + date.split("T")[1].split(":"))
# Create a tuple with the parsed date and time components
parsed_datetime = (year, month, day, hour, minute, 0, 0, 0)
# Get the local time in seconds since the epoch
timestamp = utime.mktime(parsed_datetime)
# Convert the timestamp to a tuple representing the local time
local_time = utime.localtime(timestamp)
# Extract date and time components
date_component = local_time[:3]
time_component = local_time[3:6]
# Print the results
print("Date:", date_component)
print("Time:", time_component)
# Format date and time components into strings
date_str = "{:04d}-{:02d}-{:02d}".format(*date_component)
time_str = "{:02d}:{:02d}:{:02d}".format(*time_component)
# Display information on OLED
display_info(date_str, time_str, temperature)
# Update every 5 minutes
utime.sleep(300)
if __name__ == "__main__":
main()