3 回复
// pages/ceshi/ceshi.js
import {
connect
} from "./mqtt"
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
//接受用户数据
console.log("准备连接服务器");
let client = connect('wxs://fangguohua.top:8084/mqtt')
client.on('connect', function () {
console.log("成功连接服务器");
client.subscribe("/mysmarthome/pu", function (err) {
if (!err) {
console.log("成功订阅设备上行数据");
} else {
console.log("失败");
}
})
});
console.log("延迟时间1秒");
setTimeout(function () {
//要延时执行的代码
}, 1000) //延迟时间 这里是1秒
var that = this;
client.on("message", function (topic, massage) {
//console.log('收到消息:' + message.toString())
console.log("接收参数");
console.log("主题topic",topic);
let dataFromDev = {};
dataFromDev = JSON.parse(massage);
console.log(dataFromDev);
var Temp1 = dataFromDev.Temp;
var Hum1 = dataFromDev.Hum;
var Light1 = dataFromDev.Light;
var led1 = dataFromDev.led;
var Beep1 = dataFromDev.Beep;
console.log(Temp1);
console.log(Hum1);
console.log(Light1);
console.log(led1);
console.log(Beep1);
//传参
});
},
})