3294浏览
查看: 3294|回复: 6

[入门教程] 【2020】掌控板获取浙江肺炎疫情并带文字查询功能

[复制链接]
一、缘起
论坛有多篇内容提到疫情信息的获取问题,笔者也很关注疫情情况,所以这里也想用掌控板进行获取了解。
当然为了以示区别,主要说明和别人不同的地方。至于为什么用代码而不用积木块,是因为习惯了代码编写。但代码的内容都是可以用积木块还原的。有时候,用积木块确实能加速实现过程,比如说,本文中的按键事件代码
就是由积木块直接生成的。

二、代码解析
1.获取的API,是使用别人已经提供的地址:https://lab.isaaclin.cn/nCoV/  ,
因为有其他文章已经介绍过了,这里不展开讲。主要是获取json化后的内容的获取:
【2020】掌控板获取浙江肺炎疫情并带文字查询功能图1

[mw_shl_code=python,false]get_url = 'https://lab.isaaclin.cn/nCoV/api/area?&province=浙江省'
_response = urequests.get(get_url, headers={"Content-Type":"application/json"},)
resp_json  = _response.json()[/mw_shl_code]
micropython提供的 urequests模块有提供 json()方法,可以直接使用。

拿到了数据后,剩下的就是字典和列表的嵌套解析了,需要了解的读者请自行搜索吧,这里不展开讲了。
[mw_shl_code=python,false]provinceShortName_Count = resp_json['results'][0]['confirmedCount']
provinceShortName_curedCount = resp_json['results'][0]['curedCount']
provinceShortName_suspectedCount = resp_json['results'][0]['suspectedCount']
provinceShortName_deadCount = resp_json['results'][0]['deadCount']
provinceShortName_updateTime = resp_json['results'][0]['updateTime'][/mw_shl_code]【2020】掌控板获取浙江肺炎疫情并带文字查询功能图2


2.这里先说第一个坑:

因为API获取的信息里有updateTime的字段,这个字段是用timestamp(时间戳)表示的,
当然时间戳是可以转换为当前时间的,还好掌控板的mpy导入的time库有[mw_shl_code=python,false]time.localtime()[/mw_shl_code]
方法可以直接调用。但是查找资料发现,mpy里自带的时间戳计算初始值不是从 1970.1.1开始的,而是从 2000.1.1开始的,所以只好进行转换了

[mw_shl_code=python,false]Time_Epoch = 946656000000
now_timestamp = (provinceShortName_updateTime - 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])[/mw_shl_code]
其中的 [mw_shl_code=python,false]Time_Epoch = 946656000000[/mw_shl_code]就是 2000.1.1的正确时间戳。
因为获取的内容是毫秒级的,所以结果还要 除以 1000,这样就获得了正确的时间。所以严格意义上来讲,

这不是坑,而是一个学习的机会。

3.第二个坑就是信息的查询交互:

我们知道只有交互才有吸引,在掌控2.0里有语音功能,但在掌控1.0怎么进行信息的输入呢?
【2020】掌控板获取浙江肺炎疫情并带文字查询功能图3
还好,掌控板提供了A+B+PYTHON 八个输入状态,这八个输入状态的组合已经基本上能满足要求了。

但怎么输入汉字呢?最简单的方法就是使用区位码。当然能获取掌控内置字库的地址会更好,但懒得翻github上的代码了

依稀记得,掌控的字库就是用区位地址编码的,不知道说得对不对 @李工,@吴工

这里预设了两个字的字库字典当做演示,有需要可以直接添加。

[mw_shl_code=python,false]font_info = {2628: '杭', 5461: '州'}
font_info.get(a*1000+b*100+c*10+d,"温")[/mw_shl_code]
a,b,c,d是用户输入的区位码的顺序4位数,用字典的get方法,防止区位码错误,预设默认值
【2020】掌控板获取浙江肺炎疫情并带文字查询功能图4【2020】掌控板获取浙江肺炎疫情并带文字查询功能图5

三、演示视频:


四、完整代码

刷入掌控板即可:
[mw_shl_code=python,false]from mpython import *
import urequests
import network
import time

def on_button_a_down(_):
    time.sleep_ms(10)
    if button_a.value() == 1: return
    oled.fill(0)
    oled.DispChar('温州:', 0, 0, 1)
    oled.DispChar('确诊:{},  疑似:{}'.format(cities_Count,cities_suspectedCount),0, 16, 1)
    oled.DispChar('治愈:{:>3d},  死亡:{}'.format(cities_curedCount,cities_deadCount),0, 32, 1)
    oled.DispChar('按PYTHON键查询.'.format(cities_curedCount,cities_deadCount),0, 48, 1)
    oled.show()

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)

def on_button_b_down(_):
    time.sleep_ms(10)
    if button_b.value() == 1: return

    for i, c in enumerate(resp_json['results'][0]['cities']):
        if c['cityName'] == first_char + second_char:
            break
    cities_Count = resp_json['results'][0]['cities']['confirmedCount']
    cities_suspectedCount = resp_json['results'][0]['cities']['suspectedCount']
    cities_curedCount = resp_json['results'][0]['cities']['curedCount']
    cities_deadCount = resp_json['results'][0]['cities']['deadCount']
    oled.fill(0)
    oled.DispChar(first_char + second_char + ':', 0, 0, 1)
    oled.DispChar('确诊:{},  疑似:{}'.format(cities_Count,cities_suspectedCount),0, 16, 1)
    oled.DispChar('治愈:{:>3d},  死亡:{}'.format(cities_curedCount,cities_deadCount),0, 32, 1)
    oled.DispChar('按PYTHON键查询.'.format(cities_curedCount,cities_deadCount),0, 48, 1)
    oled.show()

button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)

from mpython import *

from machine import Timer

_status_p = _status_y = _status_t = _status_h = _status_o = _status_n = 0
def on_touchpad_P_pressed():pass
def on_touchpad_P_unpressed():pass
def on_touchpad_Y_pressed():pass
def on_touchpad_Y_unpressed():pass
def on_touchpad_T_pressed():pass
def on_touchpad_T_unpressed():pass
def on_touchpad_H_pressed():pass
def on_touchpad_H_unpressed():pass
def on_touchpad_O_pressed():pass
def on_touchpad_O_unpressed():pass
def on_touchpad_N_pressed():pass
def on_touchpad_N_unpressed():pass

tim12 = Timer(12)

def timer12_tick(_):
    global _status_p, _status_y, _status_t, _status_h, _status_o, _status_n
    try:
        touchPad_P.read();pass
    except:
        return
    if touchPad_P.read() < 400:
        if 1 != _status_p:_status_p = 1;on_touchpad_P_pressed()
    elif 0 != _status_p:_status_p = 0;on_touchpad_P_unpressed()
    if touchPad_Y.read() < 400:
        if 1 != _status_y:_status_y = 1;on_touchpad_Y_pressed()
    elif 0 != _status_y:_status_y = 0;on_touchpad_Y_unpressed()
    if touchPad_T.read() < 400:
        if 1 != _status_t:_status_t = 1;on_touchpad_T_pressed()
    elif 0 != _status_t:_status_t = 0;on_touchpad_T_unpressed()
    if touchPad_H.read() < 400:
        if 1 != _status_h:_status_h = 1;on_touchpad_H_pressed()
    elif 0 != _status_h:_status_h = 0;on_touchpad_H_unpressed()
    if touchPad_O.read() < 400:
        if 1 != _status_o:_status_o = 1;on_touchpad_O_pressed()
    elif 0 != _status_o:_status_o = 0;on_touchpad_O_unpressed()
    if touchPad_N.read() < 400:
        if 1 != _status_n:_status_n = 1;on_touchpad_N_pressed()
    elif 0 != _status_n:_status_n = 0;on_touchpad_N_unpressed()

tim12.init(period=100, mode=Timer.PERIODIC, callback=timer12_tick)

a = 0
b = 0
c = 0
d = 0
first_char = ''
second_char = ''
font_info = {2628: '杭', 5461: '州'}
def on_touchpad_P_pressed():
    global a,b,c,d,first_char,second_char
    a = a + 1
    if a>=10:
        a=0
    oled.fill(0)
    oled.DispChar("请输入区位码:{}{}{}{}".format(a,b,c,d), 0, 0, 1)
    oled.DispChar("按O输入第一个字,按N输入", 0, 16, 1)
    oled.DispChar("第二个字,按B查询.", 0, 32, 1)
    oled.DispChar("你输入的字是:{}{}".format(first_char,second_char), 0, 48, 1)
    oled.show()
   
def on_touchpad_Y_pressed():
    global a,b,c,d,first_char,second_char
    b = b + 1
    if b>=10:
        b=0
    oled.fill(0)
    oled.DispChar("请输入区位码:{}{}{}{}".format(a,b,c,d), 0, 0, 1)
    oled.DispChar("按O输入第一个字,按N输入", 0, 16, 1)
    oled.DispChar("第二个字,按B查询.", 0, 32, 1)
    oled.DispChar("你输入的字是:{}{}".format(first_char,second_char), 0, 48, 1)
    oled.show()
   
def on_touchpad_T_pressed():
    global a,b,c,d,first_char,second_char
    c = c + 1
    if c>=10:
        c=0
    oled.fill(0)
    oled.DispChar("请输入区位码:{}{}{}{}".format(a,b,c,d), 0, 0, 1)
    oled.DispChar("按O输入第一个字,按N输入", 0, 16, 1)
    oled.DispChar("第二个字,按B查询.", 0, 32, 1)
    oled.DispChar("你输入的字是:{}{}".format(first_char,second_char), 0, 48, 1)
    oled.show()
   
def on_touchpad_H_pressed():
    global a,b,c,d,first_char,second_char
    d = d + 1
    if d>=10:
        d=0
    oled.fill(0)
    oled.DispChar("请输入区位码:{}{}{}{}".format(a,b,c,d), 0, 0, 1)
    oled.DispChar("按O输入第一个字,按N输入", 0, 16, 1)
    oled.DispChar("第二个字,按B查询.", 0, 32, 1)
    oled.DispChar("你输入的字是:{}{}".format(first_char,second_char), 0, 48, 1)
    oled.show()


def on_touchpad_O_pressed():
    global first_char,second_char
    first_char = font_info.get(a*1000+b*100+c*10+d,"温")
    oled.fill(0)
    oled.DispChar("请输入区位码:{}{}{}{}".format(a,b,c,d), 0, 0, 1)
    oled.DispChar("按O输入第一个字,按N输入", 0, 16, 1)
    oled.DispChar("第二个字,按B查询.", 0, 32, 1)
    oled.DispChar("你输入的字是:{}{}".format(first_char,second_char), 0, 48, 1)
    oled.show()

def on_touchpad_N_pressed():
    global first_char,second_char
    second_char = font_info.get(a*1000+b*100+c*10+d,"州")
    oled.fill(0)
    oled.DispChar("请输入区位码:{}{}{}{}".format(a,b,c,d), 0, 0, 1)
    oled.DispChar("按O输入第一个字,按N输入", 0, 16, 1)
    oled.DispChar("第二个字,按B查询.", 0, 32, 1)
    oled.DispChar("你输入的字是:{}{}".format(first_char,second_char), 0, 48, 1)
    oled.show()

my_wifi = wifi()
my_wifi.connectWiFi('1404-2', '2222222x')
while not my_wifi.sta.isconnected():
    print(1)

oled.fill(0)
oled.DispChar(my_wifi.sta.ifconfig()[0], 0, 0, 1)
oled.show()

get_url = 'https://lab.isaaclin.cn/nCoV/api/area?&province=浙江省'
_response = urequests.get(get_url, headers={"Content-Type":"application/json"},)
resp_json  = _response.json()

provinceShortName_Count = resp_json['results'][0]['confirmedCount']
provinceShortName_curedCount = resp_json['results'][0]['curedCount']
provinceShortName_suspectedCount = resp_json['results'][0]['suspectedCount']
provinceShortName_deadCount = resp_json['results'][0]['deadCount']
provinceShortName_updateTime = resp_json['results'][0]['updateTime']

Time_Epoch = 946656000000
now_timestamp = (provinceShortName_updateTime - 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])

for i, city in enumerate(resp_json['results'][0]['cities']):
    if city['cityName'] == '温州':
        break
cities_Count = resp_json['results'][0]['cities']['confirmedCount']
cities_suspectedCount = resp_json['results'][0]['cities']['suspectedCount']
cities_curedCount = resp_json['results'][0]['cities']['curedCount']
cities_deadCount = resp_json['results'][0]['cities']['deadCount']

print(provinceShortName_Count)
pass

oled.fill(0)

oled.DispChar('浙江肺炎疫情动态', 0, 0, 1)
oled.DispChar('时间:{}'.format(update_time), 0, 16, 1)
oled.DispChar('浙江确诊:{},疑似:{}.'.format(provinceShortName_Count,provinceShortName_suspectedCount), 0, 32, 1)
oled.DispChar('请按A键查看城市信息.', 0, 48, 1)
oled.show()
[/mw_shl_code]









DFrJ5KYVQaH  中级技匠

发表于 2020-2-7 10:02:21

在家防疫情,好的做法
回复

使用道具 举报

gray6666  初级技神 来自手机

发表于 2020-2-8 18:08:30

kylinpoet 发表于 2020-2-6 15:36
一、缘起
论坛有多篇内容提到疫情信息的获取问题,笔者也很关注疫情情况,所以这里也想用掌控板进行获取了 ...

武汉加油,中国加油
回复

使用道具 举报

且歌且行  中级技师

发表于 2020-2-15 21:15:31

我看到条条大道通罗马
回复

使用道具 举报

kylinpoet  初级技神
 楼主|

发表于 2020-2-18 03:19:22

多谢分享,学习了。
回复

使用道具 举报

风悠扬0539  初级技匠

发表于 2020-2-18 22:30:24

武汉加油 中国加油  我们都加油
回复

使用道具 举报

gada888  版主

发表于 2020-2-23 12:11:18

希望疫情早点结束
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail