ESP32/ESP8266之MicroPython獲取本地區最新疫情數據

疫情期間在家上班,偶爾刷抖音看到有人使用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掌控板獲取浙江肺炎疫情並帶文字查詢功能

於鑫用掌控板+Python播報新冠肺炎疫情數據

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章