首页 > 编程语言 >小程序物联网连接onenet

小程序物联网连接onenet

时间:2023-12-10 22:12:19浏览次数:36  
标签:onenet console log res value 联网 command data 连接

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

相关文章

  • java中C3P0、Druid、HikariCP 、DBCP连接池的jar包下载与IDEA配置
    ##一、什么是连接池连接池是应用程序与数据库之间的一个缓冲区,它存储了一定数量的空闲数据库连接,当应用程序需要连接数据库时,可以从连接池中获取一个可用连接,使用完毕后再将连接归还给连接池,从而避免了每次连接都需要创建和销毁连接的开销,提高了应用程序的性能和可伸缩性。连接池......
  • mp实现一个自连接查询
    起因是我设置了一个考核表结构,其中包含指标值,指标当前值,是主副指标等列。后面我要进行考核的验收的时候,我发现验收要取得的是主当前指标值/主指标值以及副指标当前值/副指标值。如果想要让这两条数据一次都被查到,那么就需要进行自连接查询(查询同一个表两次,但是有些需要的输出内容......
  • 虚拟机突然连接不上xshell的解决方案
    今天我打开虚拟机和xshell的时候,发现我的node1连接不上xshell,但是node2、node3依旧可以链接,我在网上找了很多方法,但是是关于全部虚拟机连接不上xshell,但是,我只有一个连接不上,然后我发现我在虚拟机上输入ifconfig的时候显示它不显示我虚拟机的ip地址,查找后我发现是我的ens33这个......
  • flinkcdc连接oracle的报错汇总
    报错一:原因分析:字面原因,找不到 org.apache.flink.table.api.ValidationException类。解决办法:根据类名可知,应该 org.apache.flink.table.api包下面的,然后去阿里云maven仓库搜索,添加如下依赖即可 报错二:原因分析:ORA-16331:容器"ORCLPDB1"未打开。解决办法:使用命令打......
  • ESP8266 01s,连接usb to ttl,结果电脑上不显示串口回应
    ESP826601s,连接usbtottl,结果电脑上不显示串口回应。     ———————————————————————————————————————————————————————————————————————————————————————————————......
  • K8s构建的mysql无法远程连接
    最近在写一个老师布置的大作业,都是老师写好的yaml文件,都是没问题的,但是构建的mysql无法远程连接。尝试了网上的很多方法,都失败了,我的构建过程应该是没什么错误的,所以网上的方法并不奏效,这里提供一个很容易被忽视的思路。我的k8s是三台虚拟机构成的集群,如果远程连接失败很可能是......
  • 6.连接查询
    一、含义当查询中涉及到了多个表的字段,需要使用多表连接select字段1,字段2from表1,表2,...;笛卡尔乘积:当查询多个表时,没有添加有效的连接条件,导致多个表所有行实现完全连接如何解决:添加有效的连接条件二、分类按年代分类:sql92: 等值 非等值 自连接 也支持一部分外连......
  • 聊聊数据库连接池 Druid
    在SpringBoot项目中,数据库连接池已经成为标配,然而,我曾经遇到过不少连接池异常导致业务错误的事故。很多经验丰富的工程师也可能不小心在这方面出现问题。在这篇文章中,我们将探讨数据库连接池,深入解析其实现机制,以便更好地理解和规避潜在的风险。1为什么需要连接池假如没有......
  • linux系统如何连接网络
    1、登入进入linux桌面 2、在上方的系统下拉菜单中点击首选项  3、点击网络连接 4、进入编辑页。打开IPV4设置,将方法改为手动 5、选择添加,填写ip地址,子网掩码,网关,DNS服务器等信息,填写完成后,点击应用即可 ......
  • JDBC连接数据库
    JDBC连接数据库共六步1.Class.forName()加载数据库连接驱动2.DriverManager.getConnection()获取数据连接对象3.根据SQL获取sq|会话对象4.执行SQL,执行SQL前如果有参数值就设置参数值setXXX()5.处理结果集6.释放资源0.前期工作0.0文件jar包下载,配置,删除目前常用的驱动......