|
本帖最后由 xiyuesaves 于 2021-10-8 03:03 编辑
如题.我想使用一台服务器来控制某个设备的启动和关闭,我尝试用MQTT.js来向开发板发布信息,但是我似乎只是把开发板挤下线了,这是nodejs代码
- const mqtt = require("mqtt");
- const axios = require("axios");
- axios({
- method: 'get',
- url: 'https://iot.diandeng.tech/api/v1/user/device/diy/auth',
- data: {
- authKey: '**********************'
- }
- }).then(function(res) {
- var data = res.data.detail;
- console.log(data)
- if (res.data.message === 1000) {
- var client = mqtt.connect(data.host, {
- port: data.port,
- clientId: data.deviceName,
- username: data.iotId,
- password: data.iotToken
- });
- client.on('connect', function() {
- console.log("发送关闭信息")
- client.publish(`/device/${data.deviceName}/s`, `{"toDevice":"${data.uuid}","data":{"btn-wm7":"on"}})`)
- })
- client.on('message', function(topic, message) {
- console.log("返回数据", topic, message.toString())
- })
- client.on('error', function(error) {
- console.log("出现错误", error)
- client.end()
- })
- }
- })
复制代码
点灯库版本是最新版 0.3.80210803
请问下我该怎么实现我的这个需求?
|
|