import network
import time
import os
import urequests
import ujson
from machine import Pin, I2C
import ssd1306
ssid = 'Danny'
password ='97811822'
def ConnectNet(ssid ,password):
mynetwork=network.WLAN(network.STA_IF)
mynetwork.active(True)
mynetwork.connect(ssid,password)
while True:
if(mynetwork.isconnected()):
break
else :
time.sleep(1)
print(mynetwork.ifconfig())
def OLEDInit():
res=Pin(19, mode=Pin.OUT, pull=None, value=1)
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000)
addr=i2c.scan()
for x in addr:
if x==60 :
print('OLED exist!')
break
else:
print('OLED not exist!')
global oled
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
if __name__=='__main__':
ConnectNet(ssid,password)
OLEDInit()
oled.text('Micropython', 20, 5)
oled.show()
oled.invert(False)
while True:
result1=urequests.get('https://api.seniverse.com/v3/weather/now.json?key=SZQ692OfAQcUERtD4&location=SHENZHEN&language=zh-Hans&unit=c')
j1=ujson.loads(result1.text)
print(j1['results'][0]['location']['name'],end=' ')
print(j1['results'][0]['now']['text'],end=' ')
print(j1['results'][0]['now']['temperature'],end='℃ ')
print(j1['results'][0]['last_update'])
# oled.text(' shenzhen:'+j1['results'][0]['now']['temperature'],5,30)
# oled.show()
result2=urequests.get('https://api.seniverse.com/v3/weather/now.json?key=SZQ692OfAQcUERtD4&location=beijing&language=zh-Hans&unit=c')
j2=ujson.loads(result2.text)
print(j2['results'][0]['location']['name'],end=' ')
print(j2['results'][0]['now']['text'],end=' ')
print(j2['results'][0]['now']['temperature'],end='℃ ')
print(j2['results'][0]['last_update'])
# oled.text(' beijing:'+j2['results'][0]['now']['temperature'],5,40)
# oled.show()
result3=urequests.get('https://api.seniverse.com/v3/weather/now.json?key=Sd0epKp6o3tqxTqQ-&location=%E6%B7%B1%E5%9C%B3&language=zh-Hans&unit=c')
j3=ujson.loads(result3.text)
print(j3['results'][0]['location']['name'],end=' ')
print(j3['results'][0]['now']['text'],end=' ')
print(j3['results'][0]['now']['temperature'],end='℃ ')
print(j3['results'][0]['now']['pressure'],end='Pa ')
print(j3['results'][0]['now']['humidity'],end='% ')
print(j3['results'][0]['now']['wind_speed'],end=' ')
print(j3['results'][0]['last_update'])
oled.fill(0);
oled.show();
oled.text('Shenzhen',25,5)
oled.text('T:'+j3['results'][0]['now']['temperature']+'`',30,20)
oled.text('H:'+j3['results'][0]['now']['humidity']+'%',30,30)
oled.text('P:'+j3['results'][0]['now']['pressure']+'Pa',30,40)
oled.text('W:'+j3['results'][0]['now']['wind_speed']+'km/h',30,50)
oled.show()
time.sleep(20)