-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize_time.py
43 lines (35 loc) · 1.3 KB
/
initialize_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import urequests
import time
import machine
import network
led = machine.Pin('LED', machine.Pin.OUT)
current_hour = 0
current_minute = 0
current_second = 0
current_year = 0
current_month = 0
current_day = 0
def get_time_from_api():
global current_hour, current_minute, current_second, current_year, current_month, current_day
led.on()
try:
print("Fetching current time ...")
wlan = network.WLAN(network.STA_IF)
print(wlan.status())
response = urequests.get("https://timeapi.io/api/time/current/zone?timeZone=Europe%2FBerlin")
print(response.status_code)
if response.status_code == 200:
led.off()
data = response.json()
print(data)
current_hour= data["hour"]
current_minute = data["minute"]
current_second =data["seconds"]
current_year = data["year"]
current_month = data["month"]
current_day = data["day"]
print(current_hour, current_minute, current_second, current_year, current_month, current_day)
return current_hour, current_minute, current_second, current_year, current_month, current_day
except:
print("Error while fetching current time.")
return None, None, None, None, None, None