wxml页面
<view class="zong">
<!--注释--><!-- 头-->
<view class="header">
<view class="one"> <text>空气质量:良好</text> <text>广州市</text> </view> <!--注释--> <view class="two"> <text>55</text> <text>晴天</text> </view> <!--注释--> <view class="three"> <text>空气质量良好,可以出行</text> </view> <!--注释--> </view> <!-- 身体--> <view class="body"> <view class="onebody"> <!--注释001--> <view class="data"> <view class="data-logo"> <image src="../image/温度.png" ></image> </view> <!--注释--> <view class="data-text"> <text class="data-title">温度</text> <text class="data-value">{{temperature}}℃</text> </view> <!--注释--> </view> <!--注释--><!--注释--> <view class="data"> <view class="data-logo"> <image src="../image/湿度.png" ></image> </view> <!--注释--> <view class="data-text"> <text class="data-title">湿度</text> <text class="data-value">{{humidity}}%</text> </view> <!--注释--> </view> </view> <view class="onebody"> <!--注释002--> <view class="data"> <view class="data-logo"> <image src="../image/阳光.png" ></image> </view> <!--注释--> <view class="data-text"> <text class="data-title">亮度</text> <text class="data-value">{{light}}LX</text> </view> <!--注释--> </view> <!--注释--><!--注释--> <view class="data"> <view class="data-logo"> <image src="../image/手电筒.png" ></image> </view> <!--注释--> <view class="data-text"> <text class="data-title">开关灯</text> <switch bindchange="switchChange"></switch> </view> <!--注释--> </view> </view> <view class="onebody"> <!--注释003--> <view class="data"> <view class="data-logo"> <image src="../image/阳光.png" ></image> </view> <!--注释--> <view class="data-text"> <text class="data-title">光照</text> <text class="data-value">{{ps}}</text> </view> <!--注释--> </view> <view class="data"> <view class="data-logo"> <image src="../image/报警.png" ></image> </view> <!--注释--> <view class="data-text"> <text class="data-title">蜂鸣器</text> <switch bindchange="switchChange1"></switch> </view> <!--注释--> </view> </view> <view class="data"> <!-- 注释 --> <view class="data-text"> <!-- Replace static text with an input field --> <input class="data-value" type="text" id="psInput" placeholder="Enter value" /> </view> <!-- 注释 --> <button onclick="submitData()">Submit</button> </view> </view> </view> |
Page({ data: { temperature:0, humidity:0, light:0, // ps:0, ps:0 }, // 事件处理函数 getinfo(){ var that = this wx.request({ url: "https://api.heclouds.com/devices/1164262344/datapoints", //将请求行中的数字换成自己的设备ID header: { 'content-type': 'application/json', "api-key": "NfCuMOPF7dQV0fmxjNxG1CzrrYA=" //自己的api-key }, method: "GET", success: function (e) { console.log("获取成功", e); // 检查所需属性是否存在 if (e.data.data.datastreams && e.data.data.datastreams.length > 0) { let streams = e.data.data.datastreams; // 找到对应数据点 streams.forEach(stream => { switch (stream.id) { case 'temperature': that.setData({ temperature: stream.datapoints[0].value }); break; case 'humidity': that.setData({ humidity: stream.datapoints[0].value }); break; case 'light': that.setData({ light: stream.datapoints[0].value }); break; case 'ps': that.setData({ ps: stream.datapoints[0].value }); break; default: break; } }); } // 在这里进行数据的输出验证 console.log(e); console.log("temperature==", that.data.temperature); console.log("humidity==", that.data.humidity); console.log("light==", that.data.light); console.log("ps==", that.data.ps); } }); }, switchChange: function (e) { let command; if (e.detail.value) { command = "ledon"; // 字符串命令,表示打开 } else { command = "ledoff"; // 字符串命令,表示关闭 } console.log("Command to be sent:", command); // 发送命令到 OneNet wx.request({ url: 'https://api.heclouds.com/cmds?device_id=1164262344', header: { 'content-type': 'application/json', 'api-key': 'NfCuMOPF7dQV0fmxjNxG1CzrrYA=' }, method: 'POST', data: // 目标设备的 ID command // 要发送的命令内容 , success(res) { console.log("成功", res.data); // 处理成功的情况 }, fail(res) { console.log("失败", res); // 处理失败的情况 if (res.data && res.data.errno === 3) { console.error("Authentication failed. Check API Key and permissions."); } } }); }, switchChange1: function (e) { let command; if (e.detail.value) { command = "beepon"; // 字符串命令,表示打开 } else { command = "beepoff"; // 字符串命令,表示关闭 } console.log("Command to be sent:", command); // 发送命令到 OneNet wx.request({ url: 'https://api.heclouds.com/cmds?device_id=1164262344', header: { 'content-type': 'application/json', 'api-key': 'NfCuMOPF7dQV0fmxjNxG1CzrrYA=' }, method: 'POST', data: // 目标设备的 ID command // 要发送的命令内容 , success(res) { console.log("成功", res.data); // 处理成功的情况 }, fail(res) { console.log("失败", res); // 处理失败的情况 if (res.data && res.data.errno === 3) { console.error("Authentication failed. Check API Key and permissions."); } } }); }, submitData: function (e) { const inputValue = e.detail.value; console.log( inputValue); // Check if the input matches the expected value if (inputValue === "123") { let command; if (e.detail.value) { command = "beepon"; // Command to send when the input is "123" and the switch is on } else { command = "beepoff"; // Command to send when the input is "123" and the switch is off } console.log("Command to be sent:", command); // Sending the command to OneNet wx.request({ url: 'https://api.heclouds.com/cmds?device_id=1164262344', header: { 'content-type': 'application/json', 'api-key': 'NfCuMOPF7dQV0fmxjNxG1CzrrYA=' }, method: 'POST', data: command, // Sending the command content success(res) { console.log("成功", res.data); // Handling successful cases }, fail(res) { console.log("失败", res); // Handling failure cases if (res.data && res.data.errno === 3) { console.error("Authentication failed. Check API Key and permissions."); } } }); } else { // Input value doesn't match the expected value console.log("Input value does not match. Command not sent."); } }, onLoad() { var that = this setInterval(function(){ that.getinfo() },3000) } }) |
标签:onenet,console,log,res,value,联网,command,data,连接 From: https://www.cnblogs.com/azwz/p/17893342.html