【2020】掌控板获取浙江肺炎疫情并带文字查询功能
一、缘起论坛有多篇内容提到疫情信息的获取问题,笔者也很关注疫情情况,所以这里也想用掌控板进行获取了解。
当然为了以示区别,主要说明和别人不同的地方。至于为什么用代码而不用积木块,是因为习惯了代码编写。但代码的内容都是可以用积木块还原的。有时候,用积木块确实能加速实现过程,比如说,本文中的按键事件代码
就是由积木块直接生成的。
二、代码解析
1.获取的API,是使用别人已经提供的地址:https://lab.isaaclin.cn/nCoV/,
因为有其他文章已经介绍过了,这里不展开讲。主要是获取json化后的内容的获取:
get_url = 'https://lab.isaaclin.cn/nCoV/api/area?&province=浙江省'
_response = urequests.get(get_url, headers={"Content-Type":"application/json"},)
resp_json= _response.json()
micropython提供的 urequests模块有提供 json()方法,可以直接使用。
拿到了数据后,剩下的就是字典和列表的嵌套解析了,需要了解的读者请自行搜索吧,这里不展开讲了。
provinceShortName_Count = resp_json['results']['confirmedCount']
provinceShortName_curedCount = resp_json['results']['curedCount']
provinceShortName_suspectedCount = resp_json['results']['suspectedCount']
provinceShortName_deadCount = resp_json['results']['deadCount']
provinceShortName_updateTime = resp_json['results']['updateTime']
2.这里先说第一个坑:
因为API获取的信息里有updateTime的字段,这个字段是用timestamp(时间戳)表示的,
当然时间戳是可以转换为当前时间的,还好掌控板的mpy导入的time库有time.localtime()
方法可以直接调用。但是查找资料发现,mpy里自带的时间戳计算初始值不是从 1970.1.1开始的,而是从 2000.1.1开始的,所以只好进行转换了
{:6_211:}
Time_Epoch = 946656000000
now_timestamp = (provinceShortName_updateTime - Time_Epoch) // 1000
x = time.localtime(now_timestamp)
update_time = '{}-{:0>2d}-{:0>2d} {}:{}:{}'.format(x, x, x, x, x, x)
其中的 Time_Epoch = 946656000000就是 2000.1.1的正确时间戳。
因为获取的内容是毫秒级的,所以结果还要 除以 1000,这样就获得了正确的时间。所以严格意义上来讲,
这不是坑,而是一个学习的机会。
3.第二个坑就是信息的查询交互:
我们知道只有交互才有吸引,在掌控2.0里有语音功能,但在掌控1.0怎么进行信息的输入呢?
还好,掌控板提供了A+B+PYTHON 八个输入状态,这八个输入状态的组合已经基本上能满足要求了。
但怎么输入汉字呢?最简单的方法就是使用区位码。当然能获取掌控内置字库的地址会更好,但懒得翻github上的代码了
依稀记得,掌控的字库就是用区位地址编码的,不知道说得对不对 @李工,@吴工
这里预设了两个字的字库字典当做演示,有需要可以直接添加。
font_info = {2628: '杭', 5461: '州'}
font_info.get(a*1000+b*100+c*10+d,"温")
a,b,c,d是用户输入的区位码的顺序4位数,用字典的get方法,防止区位码错误,预设默认值
三、演示视频:
https://www.bilibili.com/video/av87225043/
四、完整代码
刷入掌控板即可:
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']['cities']):
if c['cityName'] == first_char + second_char:
break
cities_Count = resp_json['results']['cities']['confirmedCount']
cities_suspectedCount = resp_json['results']['cities']['suspectedCount']
cities_curedCount = resp_json['results']['cities']['curedCount']
cities_deadCount = resp_json['results']['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, 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']['confirmedCount']
provinceShortName_curedCount = resp_json['results']['curedCount']
provinceShortName_suspectedCount = resp_json['results']['suspectedCount']
provinceShortName_deadCount = resp_json['results']['deadCount']
provinceShortName_updateTime = resp_json['results']['updateTime']
Time_Epoch = 946656000000
now_timestamp = (provinceShortName_updateTime - Time_Epoch) // 1000
x = time.localtime(now_timestamp)
update_time = '{}-{:0>2d}-{:0>2d} {}:{}:{}'.format(x, x, x, x, x, x)
for i, city in enumerate(resp_json['results']['cities']):
if city['cityName'] == '温州':
break
cities_Count = resp_json['results']['cities']['confirmedCount']
cities_suspectedCount = resp_json['results']['cities']['suspectedCount']
cities_curedCount = resp_json['results']['cities']['curedCount']
cities_deadCount = resp_json['results']['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()
在家防疫情,好的做法 kylinpoet 发表于 2020-2-6 15:36
一、缘起
论坛有多篇内容提到疫情信息的获取问题,笔者也很关注疫情情况,所以这里也想用掌控板进行获取了 ...
武汉加油,中国加油 我看到条条大道通罗马{:6_213:} 多谢分享,学习了。 武汉加油 中国加油我们都加油 希望疫情早点结束
页:
[1]