前言
我去年发过一个SDDC
嗅探器的文章,这次学习其余协定的时候发现,官网又没有相干的开发调试工具,于是我又本人写了一个,除了之前的SDDC
协定外,这次我也将Lora
、Mqtt
以及Coap
协定的通信测试退出了其中,让整个工具性能变得更加丰盛全面,这个工具利用测试实现后将会去申请到爱智世界中进行一个公布,大家到时候感兴趣的话能够去下载一下,当然有任何问题也欢送大家能够踊跃的反馈给自己,在此先感激一下大家!*
界面&形容
上面这张图就是最新的嵌入式设施开发与设施进行通信测试的工具Eap
。
和之前的相比,Device
模块通信测试放在了Sddc/Zddc
选项中,用户能够通过切换tab标签
来进行功能模块的切换。如果有嵌入式终端设备开发的小伙伴,想要在开发过程中测试与精灵一号的连贯通信状态,能够用这个工具帮助开发,具体应用办法基本上在每个tab
标签页中大家都能看懂,这里就不过多啰嗦了。
利用代码
对于利用波及的相干代码,前端局部不再给大家演示了,中规中矩的VUE
利用,次要还是给大家看一下JSRE
给咱们提供的一些协定通信模块相干代码,这里的话,我在写的过程中也发现了一些小问题,曾经和官网沟通过了,根本的性能大抵是有了,等自己前面有工夫的话,会再优化下利用,同时丰盛利用的其余性能。
【Coap
应用】:
依照爱智官网的形容,JSRE
提供的Coap
模块是基于根本的RESTful
格调的申请/响应模式,反对GET
,POST
,PUT
,DELETE
;
在测试工具中,次要提供了俩个入口,一个通过GET
来获取指定门路内容,另一个就是通过PUT
来对指定门路的内容进行批改,具体能够参考以下代码,能够看出通过JSRE
开发物联网通信的话还是比拟不便的;
.../* 获取coap服务指定门路的内容 */ get(url: string, path = '/', cb: ICallBack) { coap.request(url, (client) => { client.on('response', ({ }, res: any) => { const data = Buffer.isBuffer(res.payload) ? res.payload.toString() : res.payload cb({ result: true, message: '操作胜利!', data }) }) client.on('error', (e) => { console.log('coap error.', e) cb({ result: false, message: '获取Coap指定门路内容失败!' }) }) }, { method: 'GET', path }) } /* 批改coap服务指定门路的内容 */ put(url: string, path = '/', data: string, cb: ICallBack) { coap.request(url, (client) => { client.on('response', ({ }, res: any) => { const data = Buffer.isBuffer(res.payload) ? res.payload.toString() : res.payload cb({ result: true, message: '操作胜利!' }) }) client.on('error', (e) => { console.log('coap put error.', e) cb({ result: false, message: '批改Coap指定门路内容失败!' }) }) }, { method: 'PUT', path, payload: data }) }...
【Sddc/Zddc
应用】:
这里在之前的嗅探器文章中曾经具体的介绍过了,大家如果感兴趣的话能够去翻一下往期的文章。
【Lora
应用】:
待补充...
【Mqtt
应用】:
Mqtt
被设计为一个极其轻量级的公布/订阅音讯传输。JSRE
为大家提供的Mqtt
模块是一个客户端模块,次要用来实例化连贯到爱智中配置的Mqtt
服务,并且连贯爱智的Mqtt
终端设备也是作为客户端进行通信,咱们的利用与终端设备作为订阅者,而爱智外部的Mqtt
服务就是被订阅的主题对象。
首先咱们须要在默认设施Eap
中配置好Mqtt
服务。
配置好之后咱们就能够在利用后端中连贯上咱们的Mqtt
服务,这样当Mqtt
服务接管到数据后,会向所有订阅的客户端公布message
事件,在事件回调中会携带以后的音讯主题和音讯内容等。
我在测试工具中默认订阅了message
主题(是主题,不是事件,仅仅同名而已)进行测试。
.../* 连贯MQTT服务 */connectServer(params: IMqttConnectParames = { hostname: '192.168.128.1', port: 1883, client: 'Spirit', user: 'user', passwd: 'passwd' }, cb: ICallBack) { if (this.getMqttClientStatus()) { return cb && cb({ result: true, message: 'Successfully connected to mqtt' }) } const { hostname, port } = params try { const res = this.openMqtt(hostname, port) if (res.result) { this.client = res.data this.client.connect(params, () => { console.log(`${hostname}:${port} MQTT server connected!`); }); this.client.on('connect', () => { this.emit('connect') this.client.subscribe('message', { qos: 1 }, (error: any) => { if (error) { // 阐明mqtt监听message事件失败! console.log('[MQTT subscribe message]: error') } else { console.log(`${hostname}:${port} MQTT server subscribed!`); } }); cb && cb({ result: true, message: 'Successfully connected to mqtt' }) }); this.client.on('disconnect', () => { this.emit('disconnect') this.connectServer(params, null) const t = setTimeout(() => { if (!this.client || !this.client.isConnected()) { this.connectServer(params, null) } else { clearTimeout(t) } }, 3000); }); this.client.on('close', () => { console.info('mqtt client close!'); }); this.client.on('error', () => { console.error('mqtt client error!'); }); this.client.on('message', (data) => { console.log(`recevied a message from mqtt: ${JSON.stringify(data)}`); this.emit(data.topic, data.message.toString()) }); } else { cb && cb(res) } } catch (error) { console.error('[MQTT connectServer]: ', error) cb && cb({ result: false, message: 'Failed to connect to mqtt.' }) } } /* 关上MQTT */ private openMqtt(hostname = '192.168.128.1', port = 1883) { console.log('start open mqtt: ', hostname, port) try { const serAddr = socket.sockaddr(hostname, port); const client = mqtt.open(serAddr, undefined, 5000); if (!client) { return { result: false, message: 'Can not connect to broker!' } } return { result: true, message: 'connect success!', data: client } } catch (error) { console.error('[MQTT open]: ', error) return { result: false, message: 'Failed to connect to broker!' } } } /* 公布音讯 */ sendMessage(topic: string, message: string) { this.client && this.client.publish(topic, message, { qos: 1 }, (error) => { if (error) { console.error('MQTT publish error:', error); } else { console.log('MQTT publish success: ', topic, message); } }); }...
总结
下面就是通信测试工具波及的JSRE
的物联网通信协议模块的相干内容了,因为这个测试工具的用处,它的总体性能比拟繁多,大家前面能够在爱智世界中下载下来尝试一下,我平时开发设施的时候都会去用这个工具进行测试。
以上内容如有形容谬误,能够在上面留言评论哈!