import network
import time
from time import sleep
import urequests
from urequests import Response
try:
import urequests as requests
except:
import requests
import ujson
import esp
esp.osdebug(None)
import gc
gc.collect()
import machine
from machine import Pin
led = Pin(25, mode=Pin.OUT)
ssid = 'WiFi-2.4-D47F' # Replace with Your SSID
password = '0F6473B613' # Replace with Your Password
appid = 'fb2cc3893aa581ce1cdb9c9dd696549b'; #id api openweathermap
lon = '4.7082129'; #longitude et lattitude Fosses-la-Ville
lat = '50.4074273';
open_weather_map_api_key = 'fb2cc3893aa581ce1cdb9c9dd696549b'
#station = network.WLAN(network.STA_IF)
#station.active(True)
#station.connect(ssid, password)
#while station.isconnected() == False:
#pass
#print('Connection successful')
#print(station.ifconfig())
#print('')
print("Connecting to WiFi", end="")
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.1)
print(" Connected!\n")
world_time_url ='http://worldtimeapi.org/api/timezone/Europe/Brussels'
world_time_data = requests.get(world_time_url)
print(world_time_data.json())
print('')
open_weather_map_url = 'https://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&units=metric&lang=fr&appid=' + appid
open_weather_map_data = requests.get(open_weather_map_url)
print(open_weather_map_data.json())
print('')
sunrise = 'Lever:' + str(open_weather_map_data.json().get('sys').get('sunrise'))
sunset = 'Coucher:' + str(open_weather_map_data.json().get('sys').get('sunset'))
dt = 'unixtime:' + str(world_time_data.json().get('unixtime'))
print(sunrise)
print(sunset)
while True:
world_time_data = requests.get(world_time_url)
unixtime = world_time_data.json().get('unixtime')
_unixtime = int(unixtime)
_unixtime70 = _unixtime - (946684800 - 3600)
datetime = time.localtime(_unixtime70)
print(datetime)
led.value(not led.value())
sleep(0.5)