# -*- coding: UTF-8 -*-'''硬件环境:EC600S软件环境:QuecPython'''import _threadimport ujsonimport utime# import modemimport simimport netimport dataCallimport audio# from misc import Powerfrom aLiYun import aLiYunfrom machine import Pinfrom machine import ExtInt# MQTT CONNECT CONFIGclientId = "QuecPython_yunlaba"productKey = "a1A5W32fexl"# productSecret = "Trks8gtR82AUm2lX"productSecret = NonedeviceName = "yunlaba"deviceSecret = "b79eb8c46bc88f6e8f6559b572de7786"# MQTT CONNECT CONFIG END!# mqtt topictopic_conn_aliyun = "/a1A5W32fexl/yunlaba/user/ylb_conn_test"topic_trade_info = "/a1A5W32fexl/yunlaba/user/topic_trade_info"# 预置播报消息str_connect_network = ",正在连接网络,"str_connect_net_ok = ",网络连接成功,"str_connect_net_failed = ",网络连接失败,正在重新连接,"str_network_anomaly = ",移动网络异常,无法连接,"str_wait_connect = ",即将进入等待连接模式,"str_connect_server = ",正在连接服务器,"str_connect_server_ok = ",服务器连接成功,"str_connect_server_failed = ",服务器连接失败,正在重新连接,"str_dev_low_power = ",电量不足,请及时充电,"str_power_start = ",开始充电,"str_power_end = ",充电结束,"str_current_volume = ",音量{},"str_poweron = ",欢迎使用云喇叭智能语音设备,祝您生意兴隆,"str_poweroff = ",正在关机,"str_no_sim_card = ",请插入流量卡,并重启设备"tts = audio.TTS(0)# lock = _thread.allocate_lock()TTS_EN = Pin(Pin.GPIO10, Pin.OUT, Pin.PULL_PD, 1)net_led = Pin(Pin.GPIO9, Pin.OUT, Pin.PULL_DISABLE, 0)aliyun = aLiYun(productKey, productSecret, deviceName, deviceSecret)# 全局变量key_mode = 0 # 0-V+按键功能为音量调节,1-V+按键功能为查看交易历史记录,最多支持播报最近5条交易net_led_mode = 0run_mode = 0query_index = -1history_cnt = 0history_list = []def trade_info_history_list(info):global query_indexglobal history_cntglobal history_listquery_index = -1history_cnt += 1if history_cnt > 5:history_cnt = 5history_list[4] = history_list[3]history_list[3] = history_list[2]history_list[2] = history_list[1]history_list[1] = history_list[0]history_list[0] = infoelse:history_list.insert(0, info)'''音量加'''def key_event_volume_up(args):global run_modeglobal key_modeglobal query_indexglobal history_cntglobal history_listif key_mode == 0:vol = tts.getVolume()if vol != 9:vol = vol + 1tts.setVolume(vol)print('[++]current volume:{}'.format(vol))if run_mode != 0:tts.play(4, 1, 2, str_current_volume.format(vol))elif key_mode == 1:if history_cnt != 0:query_index += 1if query_index < history_cnt:tts.play(3, 0, 2, history_list[query_index])elif query_index >= history_cnt:query_index = history_cnt - 1else:tts.play(3, 0, 2, "无历史记录")print('history_list:{}'.format(history_list))'''音量减'''def key_event_volume_down(args):global run_modeglobal key_modeglobal query_indexglobal history_cntglobal history_listif key_mode == 0:vol = tts.getVolume()if vol != 0:vol = vol - 1tts.setVolume(vol)print('[--]current volume:{}'.format(vol))if run_mode != 0:tts.play(4, 1, 2, str_current_volume.format(vol))elif key_mode == 1:if history_cnt != 0:if query_index > 0:query_index -= 1tts.play(3, 0, 2, history_list[query_index])elif query_index == 0:query_index = -1else:tts.play(3, 0, 2, "无历史记录")print('history_list:{}'.format(history_list))'''按键功能选择key_mode = 0,V+、V-用于音量调节key_mode = 1,V+、V-用于最近交易查询,最多支持查询最近5条记录'''def key_event_mode_select(args):global key_modeglobal query_indexif key_mode == 0:key_mode = 1tts.play(3, 0, 2, "查询历史模式")elif key_mode == 1:key_mode = 0query_index = -1tts.play(3, 0, 2, "音量调节模式")print('[F]key mode:{}'.format(key_mode))def volume_key_event_handler():vol_up = ExtInt(ExtInt.GPIO6, ExtInt.IRQ_FALLING, ExtInt.PULL_PU, key_event_volume_up)vol_dw = ExtInt(ExtInt.GPIO8, ExtInt.IRQ_FALLING, ExtInt.PULL_PU, key_event_volume_down)key_fun = ExtInt(ExtInt.GPIO7, ExtInt.IRQ_FALLING, ExtInt.PULL_PU, key_event_mode_select)vol_up.enable()vol_dw.enable()key_fun.enable()'''网络状态指示灯控制任务网络正常,网络状态指示灯长亮网络异常,网络状态指示灯闪烁'''def net_status_led_control_task():global net_led_modewhile True:if net_led_mode == 0:net_led.write(1)utime.sleep_ms(500)elif net_led_mode == 1:net_led.write(1)utime.sleep_ms(500)net_led.write(0)utime.sleep_ms(500)'''检测当前是否已拨号成功已拨号成功:返回1;否则返回0'''def check_data_call():for pdp in range(1, 9):nw_state = dataCall.getInfo(pdp, 0)# print(nw_state)if (nw_state != -1) and (nw_state[2][0] == 1):print(nw_state)return 1return 0'''网络连接,如果连不上会一直在尝试连接,直到拨号联网成功才会退出该函数拨号成功:返回1;否则返回0'''def connnect_to_network():nw_state = 0try_count = 0wait_mode = 0global net_led_modesim_state = sim.getStatus()if sim_state == 0:tts.play(4, 0, 2, str_no_sim_card)return 0while nw_state == 0:try_count += 1if try_count > 1000:try_count = 7nw_state = dataCall.getInfo(1, 0)print(nw_state)if (nw_state != -1) and (nw_state[2][0] == 1):wait_mode = 0net_led_mode = 0tts.play(4, 0, 2, str_connect_net_ok)return 1else:if try_count == 6:nw_state = 0wait_mode = 1tts.play(4, 0, 2, str_network_anomaly)tts.play(4, 0, 2, str_wait_connect)net_led_mode = 1if wait_mode == 0:tts.play(4, 0, 2, str_connect_network)net_sta = net.getState()if net_sta != -1 and net_sta[1][0] == 1:ret = dataCall.start(1, 0, 'cmnet', '', '', 0)if ret == 0:nw_state = dataCall.getInfo(1, 0)if (nw_state != -1) and (nw_state[2][0] == 1):wait_mode = 0tts.play(4, 0, 2, str_connect_net_ok)return 1else:nw_state = 0if wait_mode == 0:tts.play(4, 0, 2, str_connect_net_failed)utime.sleep(5)else:nw_state = 0if wait_mode == 0:tts.play(4, 0, 2, str_connect_net_failed)utime.sleep(5)else:nw_state = 0if wait_mode == 0:utime.sleep(10)elif wait_mode == 1:utime.sleep(60)'''消息推送回调,接收到云端推送的msg时,会调用该函数'''def aliyun_mqtt_callback(topic, msg):print('recv msg from topic: %s' % topic.decode())print('msg : %s' % msg.decode())global run_modeglobal net_led_modeif topic.decode() == topic_conn_aliyun:if msg.decode() == 'connect aliyun succeed!':run_mode = 1net_led_mode = 0tts.play(4, 0, 2, str_connect_server_ok)# tts.play(4, 0, 2, str_connect_net_ok)if msg.decode() == 'connect aliyun test':run_mode = 1if topic.decode() == topic_trade_info:# 订单模板# trade_info = {"orderID": 1,"productID": 11111111,"msg": "支付宝到账100元","PayStatus": 1,"createTime": "2020/10/31 13:45:52"}tts_priority = 3json_data = ujson.loads(msg)tts_str = json_data.get('msg')trade_info_history_list(tts_str)ret = tts.play(tts_priority, 0, 2, tts_str)if ret == -2:tts_priority -= 1if tts_priority == -1:tts_priority = 3tts.play(tts_priority, 0, 2, tts_str)'''定时检测与服务器连接状态'''def network_status_monitor():global run_modeglobal clientIdglobal net_led_modeglobal str_connect_serverglobal topic_conn_aliyunglobal topic_trade_infowhile True:if run_mode == 0:print('connect to aliyun...')print('network_status_monitor: running mode = 0')tts.play(4, 0, 2, str_connect_server)aliyun.setMqtt(clientId, clean_session=False, keepAlive=300)aliyun.setCallback(aliyun_mqtt_callback)print('subscribe topic: %s' % topic_conn_aliyun)aliyun.subscribe(topic_conn_aliyun)print('subscribe topic: %s' % topic_trade_info)aliyun.subscribe(topic_trade_info, 1)print('publish \'connect aliyun succeed!\' to aliyun')aliyun.publish(topic_conn_aliyun, "connect aliyun succeed!")aliyun.start()run_mode = 2utime.sleep(10)elif run_mode == 1:utime.sleep(50)print('network_status_monitor: running mode = 1')sta = tts.getState()if sta == 0:aliyun.publish(topic_conn_aliyun, "connect aliyun test")run_mode = 2utime.sleep(10)elif run_mode == 2:print('network_status_monitor: running mode = 2')net_led_mode = 1tts.play(4, 0, 2, "网络异常,正在重新连接")net_sta = net.getState()print(net_sta)if net_sta != -1 and net_sta[1][0] == 1:print('try datacall 11111')ret = dataCall.start(1, 0, '3gnet', '', '', 0)print('try datacall 22222')if ret == 0:call_state = dataCall.getInfo(1, 0)if (call_state != -1) and (call_state[2][0] == 1):print('try datacall succeed!')print('aliyun.setMqtt 11111')aliyun.setMqtt(clientId, clean_session=False, keepAlive=300)print('aliyun.setMqtt 22222')aliyun.publish(topic_conn_aliyun, "connect aliyun succeed!")elif ret == -1:print('try datacall failed!')elif net_sta[1][0] == 0:print('network disconnected!')print('will sleep 60s')utime.sleep(60)print('sleep 60s end')'''检测充电状态,该函数需要单独放在一个线程中执行'''def check_charge_status():passif __name__ == '__main__':utime.sleep(5)_thread.start_new_thread(net_status_led_control_task, ())tts.play(4, 0, 2, str_poweron)print('connect to network...')ret = connnect_to_network()if ret == 0:print('connect to network failed.')else:print('connect to network succeed.')_thread.start_new_thread(network_status_monitor, ())volume_key_event_handler()while 1:pass
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。