import network
import time
import urequests # handles making and servicing network requests
from machine import I2C, Pin # since I2C communication would be used, I2C class is imported
# very important
# this module needs to be saved in the Raspberry Pi Pico in order for the LCD I2C to be used
from pico_i2c_lcd import I2cLcd
# creating an I2C object, specifying the data (SDA) and clock (SCL) pins used in the Raspberry Pi Pico
# any SDA and SCL pins in the Raspberry Pi Pico can be used (check documentation for SDA and SCL pins)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# getting I2C address
I2C_ADDR = i2c.scan()[0]
# creating an LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 2, number of columns = 16
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
print("Connecting to WiFi", end="")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
#wlan.connect('Bbox-0488C286', 'Ua5fxZpMraJLdLF3jE')
while not wlan.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print(wlan.ifconfig())
"""# Example 1. Make a GET request for google.com and print HTML
# Print the html content from google.com
print("1. Querying google.com:")
r = urequests.get("http://www.google.com")
print(r.content)
r.close()"""
"""
# Example 2. urequests can also handle basic json support! Let's get the current time from a server
print("\n\n2. Querying the current GMT+0 time:")
r = urequests.get("http://date.jsontest.com") # Server that returns the current GMT+0 time.
print(r.json())
a = r.json()
print(a['milliseconds_since_epoch'])
#t= float(int(a['milliseconds_since_epoch']))
print(time.time())
print(time.localtime(time.time()))"""
def get_time():
response = urequests.get("http://worldtimeapi.org/api/timezone/Europe/Paris")
data = response.json()
print(data)
timestamp = data["unixtime"]
return timestamp
current_time = get_time()
print(current_time )
formatted_time = time.localtime(current_time)
#Print the time to the console or perform any other actions with it:
print(formatted_time)
lcd.putstr(str(formatted_time))