1.IDE:微信开发者工具
2.实验:微信小程序设计两个按钮,对onenet平台数据流(旧版)来进行http post上传,下位机再http get得到控制信息
3.记录
(1)bind-tap(绑定函数)
(2)关键代码
//按键1kai:function(){let data={"datastreams":[{"id":"button_ctrl","datapoints":[{"value":1}]}, //button_ctrl为数据流名称]}//按钮发送命令控制硬件wx.request({url: 'https://api.heclouds.com/devices/1105985351/datapoints',header:{'content-type': 'application/json','api-key':'AdbrV5kCRsKsRCfjboYOCVcF9FY='},method: 'POST',data: JSON.stringify(data), //data数据转换成JSON格式success(res){console.log("成功",res.data)},fail(res){console.log("失败",res)}})},
/********************************************************************************/ //按键0guan:function(){let data={"datastreams":[{"id":"button_ctrl","datapoints":[{"value":0}]}, //button_ctrl为数据流名称]}//按钮发送命令控制硬件wx.request({url: 'https://api.heclouds.com/devices/1105985351/datapoints',header:{'content-type': 'application/json','api-key':'AdbrV5kCRsKsRCfjboYOCVcF9FY='},method: 'POST',data: JSON.stringify(data), //data数据转换成JSON格式success(res){console.log("成功",res.data)},fail(res){console.log("失败",res)}})},
(3)函数绑定
<button class="ctrl" bindtap="kai">按键1</button>
4.代码
index.js
Page({data: {ppm:0},//事件处理函数getinfo(){ //javascript中函数与函数之间需要用逗号隔开var that =this
wx.request({url: "https://api.heclouds.com/devices/1105985351/datapoints",header:{"api-key": "AdbrV5kCRsKsRCfjboYOCVcF9FY=" //自己的apikey},method: "GET",success: function(e){ //获取成功将所有数据传入econsole.log("获取成功",e) //控制台打印获取回来的数值that.setData({ppm:e.data.data.datastreams[0].datapoints[0].value})console.log("ppm=",that.data.ppm) //打印从json返回数据中解码出来的数据ppm}
})},//按键1kai:function(){let data={"datastreams":[{"id":"button_ctrl","datapoints":[{"value":1}]}, //button_ctrl为数据流名称]}//按钮发送命令控制硬件wx.request({url: 'https://api.heclouds.com/devices/1105985351/datapoints',header:{'content-type': 'application/json','api-key':'AdbrV5kCRsKsRCfjboYOCVcF9FY='},method: 'POST',data: JSON.stringify(data), //data数据转换成JSON格式success(res){console.log("成功",res.data)},fail(res){console.log("失败",res)}})},
/********************************************************************************/ //按键0guan:function(){let data={"datastreams":[{"id":"button_ctrl","datapoints":[{"value":0}]}, //button_ctrl为数据流名称]}//按钮发送命令控制硬件wx.request({url: 'https://api.heclouds.com/devices/1105985351/datapoints',header:{'content-type': 'application/json','api-key':'AdbrV5kCRsKsRCfjboYOCVcF9FY='},method: 'POST',data: JSON.stringify(data), //data数据转换成JSON格式success(res){console.log("成功",res.data)},fail(res){console.log("失败",res)}})},
/***************************************************************************/ onLoad(){ //相当于主函数var that =this// setInterval(function(){// that.getinfo() //获取一次数据// },5000) //3s执行一次// setInterval(function(){// that.kai() //获取一次数据// },3000) //3s执行一次}})