【得捷电子Follow me第2期】任务4:分任务1:日历&时钟
[复制链接]
这篇来学习下网络获取日历时钟和天气信息,CircuitPython编程语言还是第一次使用,还有许多不太明白的,后续再慢慢学习。
这个测试代码部分参考坛友的,下面来学习下网络测试功能。
1、复制文件到开发板盘符/lib文件下
2、测试代码
copy.py
import board, os, wifi
import time
import ssl
import socketpool
import adafruit_requests
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
color = 0xFFFFFF
font = bitmap_font.load_font("fonts\wenquanyi_10pt.pcf")
display = board.DISPLAY
display.brightness = 1
display.rotation = 0
JSON_TIME_URL = "http://quan.suning.com/getSysTime.do"
JSON_weather_URL = "http://t.weather.sojson.com/api/weather/city/{}"
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
def refresh_screen(print_text):
text_area = label.Label(font, text=print_text, scale=1)
text_area.x = 5
text_area.y = 15
text_area.scale = 1
text_area.color = color
display.show(text_area)
def get_data():
try:
response = requests.get(JSON_TIME_URL)
except ConnectionError as e:
print("Connection Error:", e)
print("Retrying in 60 seconds")
time_data = response.json()['sysTime2'][:-3]
try:
city_code = "101180101"
url = JSON_weather_URL.format(city_code)
response = requests.get(url)
except ConnectionError as e:
print("connect error:{},retry in 60s".format(e))
weather_data = response.json()
cityInfo = weather_data['cityInfo']
city_weather = weather_data['data']
forecast = city_weather['forecast']
dis_str = cityInfo['city'] + ' ' + forecast[0]['week']
dis_str += "\n时间:"+time_data
dis_str += "\n温度:"+city_weather['wendu']+"℃"
dis_str += "\n湿度:" + city_weather['shidu']
dis_str += '\n空气质量:' + city_weather['quality']
return dis_str
while not wifi.radio.ipv4_address:
try:
wifi.radio.connect('TP-LINK', '66668888')
except ConnectionError as e:
print("connecti error:{},retry in 2s".format(e))
time.sleep(2)
print("get ip:", wifi.radio.ipv4_address)
while True:
info = get_data()
refresh_screen(info)
time.sleep(60)
3、运行
获取的天气信息显示在LCD上
|