4637| 4
|
[高级教程] 微信小程序+OneNET物联+掌控板 |
本帖最后由 云天 于 2019-12-5 22:32 编辑 index.zip 要实现,通过微信小程序语音控制掌控板。语音识别可通过微信小程序中加载官方推出微信同声传译插件实现。在掌控板上亮灯及显示相应控制指令。 暂未控制电机,因发现mPython X不能使用掌控板扩展板(也许是自己不会用,请各位指教)。 那么如何使用该款插件呢? 开发者可登录小程序后台管理,依次选择【设置】【第三方服务】【添加插件】输入“同声传译”或者“语音识别”、“翻译”等搜索关键字搜索插件即可添加。 只要你添加了该款插件,便能利用该插件开放的语音识别接口、语音合成接口、文本翻译接口实现小程序的语音功能。 1、index.js中代码 //index.js //获取应用实例 const app = getApp() Page({ data: { button:'语音识别', tts:'' }, //事件处理函数 OneNet: function (ml) {//向掌控板下发命令 wx.request({ url: 'https://api.heclouds.com/cmds?device_id=566544929', //接口地址,自己设备ID header: { 'api-key': 'wVCSX14SwmBSuS=DIU2M=eHqVb0=' // 填写自己设备ID的'api-key' }, method: 'POST', data: ml, success(res) { console.log(res.data) } }) }, tts:function(){ var that = this; if(that.data.button==="语音识别"){ that.setData({ button: "退出语音" }); var plugin = requirePlugin("WechatSI")//在app.json中加载“微信同声传译”插件 let manager = plugin.getRecordRecognitionManager() manager.onRecognize = function (res) {//语音识别过程中,判断是否出现控制指令,如“向左” console.log("current result", res.result) if (res.result != "" || res.result !=NULL){ if(res.result.indexOf("向左")!=-1){//下达控制指令时,可以使用如“向左转”、“向前进”等,增加识别率 that.setData({ tts: "向左" }); that.OneNet("向左"); manager.stop(); } if (res.result.indexOf("向右") != -1) { that.setData({ tts: "向右" }); that.OneNet("向右"); manager.stop(); } if (res.result.indexOf("前进") != -1) { that.setData({ tts: "前进" }); that.OneNet("前进"); manager.stop(); } if (res.result.indexOf("后退") != -1) { that.setData({ tts: "后退" }); that.OneNet("后退"); manager.stop(); } if (res.result.indexOf("停止") != -1) { that.setData({ tts: "停止" }); that.OneNet("停止"); manager.stop(); } if (res.result.indexOf("退出") != -1) { that.setData({ tts: "退出" }); manager.stop(); } } } manager.onStop = function (res) {//在识别完成时,判断是退出还是进行下一次语音识别,这样可连续识别,也可通过“退出”指令结束语音识别。 console.log("record file path", res.tempFilePath); console.log("result", res.result) if (res.result != "" || res.result != NULL) { if (res.result.indexOf("退出")===-1){ manager.start({ duration: 30000, lang: "zh_CN" }) } else{ that.setData({ button: "语音识别" }); } } else{ manager.start({ duration: 30000, lang: "zh_CN" }) } } manager.onStart = function (res) { console.log("成功开始录音识别", res) } manager.onError = function (res) { console.error("error msg", res.msg) manager.start({ duration: 30000, lang: "zh_CN" }) } manager.start({ duration: 30000, lang: "zh_CN" }) } else { that.setData({ button: "语音识别" }); } } }) 2、页面布局 <!--index.wxml--> <view class="container"> <view class="userinfo"> <text class="user-motto">语音结果:{{tts}}</text> </view> <view class="usermotto"> <button bindtap="tts"> {{button}} </button> </view> </view> 3、插件加载,app.json { "pages": [ "pages/index/index", "pages/logs/logs" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "小程序语音控制掌控板", "navigationBarTextStyle": "black" }, "style": "v2", "sitemapLocation": "sitemap.json", "plugins": { "WechatSI": { "version": "0.0.7", "provider": "wx069ba97219f66d99" } } } 4、微信小程序开发平台 5、mPython x代码 from mpython import * import time import network from umqtt.simple import MQTTClient from machine import Timer import machine import ubinascii my_wifi = wifi() my_wifi.connectWiFi("sxs", "smj080823") def OneNET_recv(_msg):pass _OneNET_msg_list = [] def OneNET_callback(_topic, _msg): global _OneNET_msg_list try: _msg = _msg.decode('utf-8', 'ignore') except: print(_msg);return OneNET_recv(_msg) if _msg in _OneNET_msg_list: eval('OneNET_recv_' + bytes.decode(ubinascii.hexlify(_msg)) + '()') tim14 = Timer(14) _iot_count = 0 def timer14_tick(_): global _iot, _iot_count _iot_count = _iot_count + 1 if _iot_count == 1000: _iot.ping(); _iot_count = 0 try: _iot.check_msg() except: machine.reset() _iot = None def OneNET_setup(): global _iot _iot = MQTTClient("566544929", "183.230.40.39", 6002, "292081", "wVCSX14SwmBSuS=DIU2M=eHqVb0=", keepalive=300) _iot.set_callback(OneNET_callback) if 1 == _iot.connect(): print('Successfully connected to MQTT server.') tim14.init(period=200, mode=Timer.PERIODIC, callback=timer14_tick) def OneNET_recv(_msg): oled.fill(0) oled.DispChar(_msg, 42, 22, 1) oled.show() _OneNET_msg_list.append('向左') def OneNET_recv_e59091e5b7a6(): rgb.fill( (0, 0, 0) ) rgb.write() time.sleep_ms(1) rgb[0] = (int(255/30), int(0/30), int(0/30)) rgb.write() time.sleep_ms(1) oled.DispChar("向左", 42, 22, 1) _OneNET_msg_list.append('向右') def OneNET_recv_e59091e58fb3(): rgb.fill( (0, 0, 0) ) rgb.write() time.sleep_ms(1) rgb[2] = (int(255/30), int(0/30), int(0/30)) rgb.write() time.sleep_ms(1) oled.DispChar("向右", 42, 22, 1) _OneNET_msg_list.append('向前') def OneNET_recv_e59091e5898d(): rgb.fill( (0, 0, 0) ) rgb.write() time.sleep_ms(1) rgb[0] = (int(255/30), int(0/30), int(0/30)) rgb.write() time.sleep_ms(1) rgb[2] = (int(255/30), int(0/30), int(0/30)) rgb.write() time.sleep_ms(1) oled.DispChar("向前", 42, 22, 1) _OneNET_msg_list.append('向后') def OneNET_recv_e59091e5908e(): rgb.fill( (0, 0, 0) ) rgb.write() time.sleep_ms(1) rgb[1] = (int(255/30), int(0/30), int(0/30)) rgb.write() time.sleep_ms(1) oled.DispChar("向后", 42, 22, 1) _OneNET_msg_list.append('停止') def OneNET_recv_e5819ce6ada2(): rgb.fill( (0, 0, 0) ) rgb.write() time.sleep_ms(1) oled.DispChar("停止", 42, 22, 1) rgb.fill( (0, 0, 0) ) rgb.write() time.sleep_ms(1) while not my_wifi.sta.isconnected(): pass oled.fill(0) oled.DispChar("Wifi_OK", 0, 0, 1) oled.show() OneNET_setup() oled.fill(0) oled.DispChar("OneNet_OK", 42, 22, 1) oled.show() 图形代码 6、演示视频 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed