疫情期间在家上班,偶尔刷抖音看到有人使用ESP32掌控板获取各地疫情信息,感觉挺好的,由于手头只有一个ESP8266,就不能使用OLED显示了,直接串口print打印简单粗暴,显示后续如果我还想起来这篇文章再加上。
奉上全部代码,喜欢的点个赞带走,欢迎转载。
#boot.py
import network
import gc
gc.collect()
ssid = 'ChinaNet-TymJ'
password = '@66668888@'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
print(station.ifconfig())
#main.py
import urequests
import network
import json
import math
import time
#获取数据
#本接口为全国新型肺炎疫情实时数据接口,数据来源为丁香园,开放给所有有需要的人。
#接口说明网址:https://lab.isaaclin.cn/nCoV/
get_url = 'https://lab.isaaclin.cn/nCoV/api/area?&province=河南省'
_response = urequests.get(get_url, headers={"Content-Type":"application/json"},)
resp_json = _response.json()
_response.close()
data = resp_json['results'][0]['cities'] # 城市疫情数据
confirm = resp_json['results'][0]['confirmedCount'] # 确诊历史数据
suspect = resp_json['results'][0]['suspectedCount'] # 疑似历史数据
dead = resp_json['results'][0]['deadCount'] # 死亡历史数据
heal = resp_json['results'][0]['curedCount'] # 治愈历史数据
total = resp_json['results'][0] # 全国数据
date = resp_json['results'][0]['updateTime']# 数据日期(只要年月日)
time.sleep_ms(200)
index = 0 # 城市索引
HeNan = {'南阳': 411300,'郑州': 410100,'驻马店': 411700,'商丘': 411400,'周口': 411600,'平顶山': 410400,'新乡': 410700,'安阳': 410500,'许昌': 411000,'漯河': 411100,'洛阳': 410300,'焦作': 410800,'开封': 410200,'鹤壁': 410600,'濮阳': 410900,'三门峡': 411200,'济源': 419001}
def print_province():
Time_Epoch = 946656000000
now_timestamp = (date - Time_Epoch) // 1000
x = time.localtime(now_timestamp)
update_time = '{}-{:0>2d}-{:0>2d} {}:{}:{}'.format(x[0], x[1], x[2], x[3], x[4], x[5])
print(total['provinceName'])
print('确诊:%d' % (total['confirmedCount']))
print('疑似:%d' % (total['suspectedCount']))
print('治愈:%d' % (total['curedCount']))
print('死亡:%d' % (total['deadCount']))
print('更新日期:' + update_time)
def print_prefecture():
global index
d = data[index]
print(d['cityName'])
print('确诊 %d 例' % (d['confirmedCount']))
print('治愈 %d 例' % (d['curedCount']))
print('死亡 %d 例' % (d['deadCount']))
def function_a():
global index
if index == None:
index = 0
for index in range(len(data)):
index += 1
#print("'" + data[index]['cityName'] + "': " + "%d"%data[index]['locationId'] + ",")
if data[index]['locationId'] == HeNan['周口']:
print_prefecture()
break
print_province()
while True:
function_a()
time.sleep_ms(10000)
boot.py中上电连接WIFI。
main.py将各省内的地区及对应ID制作成字典,方便调用。
打印结果
参考:
kylinpoet:掌控板获取浙江肺炎疫情并带文字查询功能