from machine import RTC
import ntptime
import time
import urequests
import ujson
def do_connect(ssid, password):
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...', ssid)
wlan.connect(ssid, password)
while not wlan.isconnected():
print(".", end="")
time.sleep_ms(200)
print('network config:', wlan.ifconfig())
SERVER_URL = "api.openweathermap.org/data/2.5/weather"
API_KEY = "b2f8ca9d62d6aa46fb38d00e32d7e809"
CITY_NAME = "tokyo"
CONVERT_C = 273.15
do_connect("TP-Link_C848", "27363447")
param = "?q="+ CITY_NAME + "&appid=" + API_KEY
url = "https://" + SERVER_URL + param
res = urequests.get(url)
json_res = res.json()
print(json_res)
print("天気: ", json_res["weather"][0]["main"])
print("最低気温: ", round(json_res["main"]["temp_min"] - CONVERT_C, 1), "℃")
print("最高気温: ", round(json_res["main"]["temp_max"] - CONVERT_C, 1), "℃")
print("湿度: ", json_res["main"]["humidity"],"%")
print("気圧: ", json_res["main"]["pressure"],"hPa")
res.close()