赞
踩
import urequests as requests
from machine import Pin,I2C,SPI
import time
import network
import ujson
import ssd1306
import HZK
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(18), mosi=Pin(23), miso=Pin(19))
cs = Pin(25,Pin.OUT)
HZK.init(spi,cs)
lcd=ssd1306.SSD1306_I2C(128,64,i2c)
API_KEY = 'mggxhv8zbogqbmqj'
UID = "UF239A86CE"
LOCATION = 'chengdu'
URL = 'https://api.seniverse.com/v3/weather/now.json'
UNIT = 'c'
LANGUAGE = 'zh-Hans'
SSID="YOURSSID"
PASSWORD="YOURPASSWORD"
wlan=None
s=None
def connectWifi(ssid,passwd):
global wlan
wlan=network.WLAN(network.STA_IF)
wlan.active(True)
wlan.disconnect()
wlan.connect(ssid,passwd)
while(wlan.ifconfig()[0]=='0.0.0.0'):
time.sleep(1)
return True
def fetchWeather(location):
result = requests.get(URL, params={
'key': API_KEY,
'location': location,
'language': LANGUAGE,
'unit': UNIT
})
return result.text
if __name__ == '__main__':
connectWifi(SSID,PASSWORD)
result = fetchWeather(LOCATION)
print(result)
j=ujson.loads(result)
print("\r\n\r\n")
print(j['results'][0]['location']['name'])
print(j['results'][0]['now']['text'])
print(j['results'][0]['now']['temperature'])
addr=j['results'][0]['location']['name']
weather=j['results'][0]['now']['text']
temperature=j['results'][0]['now']['temperature']
lcd.text("地点:%s"%addr,5,0)
lcd.text("天气:%s"%weather,5,16)
lcd.text("温度:%s"%temperature,5,32)
lcd.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。