1.演示视频
2.PPT
本来是打算用这次的比赛的机会,去完善以前做过的一个停车场车位云检测项目,但是,由于疫情的原因,很多主要的硬件设备,如NB及传感器之类的都放在了学校。所以,本次作品仅利用手上的这套官方送的开发板进行创作与学习。
背景:
目前,待机能耗问题已成为国际社会普遍关注的话题。待机能耗是指具有待机功能的电器设备的不使用的时候,没有断开电源所发生的电能消耗。而且电源插头待机状态下甚至还可能会引起灾难性事件,给人们的生命和财产安全造成了巨大的破坏。企业员工要养成电器使用之后及时断开电源的好习惯,但说归说,真正能够做到的确很不容易。所以为解决企业待机能耗状况,通过实时监测电器用电状态,以及通过时段管理来控制企业用电,就能够避免待机能耗,节省公司用电成本。
#define LED_Pin GPIO_PIN_9
#define LED_GPIO_Port GPIOB
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin ,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_GPIO_Port,LED_Pin ,GPIO_PIN_RESET);
#ifdef USE_ESP8266
extern int esp8266_sal_init(hal_uart_port_t uart_port);
extern int esp8266_join_ap(const char *ssid, const char *pwd);
esp8266_sal_init(HAL_UART_PORT_0);
esp8266_join_ap("WIFI名", "wWIFI密码");
#endif
设置产品id,设备名,设备密钥。在HAL_Device_tencentos_tiny.c里
/* product Id */
static char sg_product_id[MAX_SIZE_OF_PRODUCT_ID + 1] = "ACZSE3GAQ7";
/* device name */
static char sg_device_name[MAX_SIZE_OF_DEVICE_NAME + 1] = "dev001";
/* device secret of PSK device */
static char sg_device_secret[MAX_SIZE_OF_DEVICE_SECRET + 1] = "5ZcfaFYTFjvccY8xVC1JyA==";
选择要控制开关的电器
获取并显示自己的openid
index.js里获取openid的函数
onGetOpenid: function () {
// 调用云函数
wx.cloud.callFunction({
name: 'login',
data: {},
success: res => {
console.log('[云函数] [login] user openid: ', res.result.openid)
app.globalData.openid = res.result.openid
// wx.navigateTo({
// url: '../userConsole/userConsole',
// })
this.setData({
isShowbt: false,
isShowlb:true,
openid: app.globalData.openid
})
},
fail: err => {
console.error('[云函数] [login] 调用失败', err)
wx.navigateTo({
url: '../deployFunctions/deployFunctions',
})
}
})
},
led.js里采集数据的函数
queryDeviceData() {
let that = this
wx.showLoading()
// 实例化一个请求对象
let req = new iotmodel.DescribeDeviceDataRequest();
req.DeviceName = app.globalData.deviceName;
req.ProductId = app.globalData.productId;
// 通过client对象调用想要访问的接口,需要传入请求对象以及响应回调函数
this.data.client.DescribeDeviceData(req, function (err, response) {
if (err) {
console.log("err:", err);
wx.showToast({
title: "采集数据失败",
icon: 'none',
duration: 3000
})
return;
}
wx.showToast({
title: "采集数据成功",
icon: 'none',
duration: 4000
})
let deviceData = JSON.parse(response.Data)
var time = util.formatTime(new Date())
that.setData({
deviceData: deviceData,
userN: deviceData,
time:time
})
console.log(that.data.deviceData);
});
},
bindViewTap: function () {
//保留当前页面,跳转到应用内某个界面
wx.navigateTo({
url: '../index/index',
})
//关闭当前页面,跳转到应用内某个页面
wx.redirectTo({
url: '../index/index',
})
//跳转到tabBar页面,并关闭其他所有tabBar页面
wx.switchTab({
url: '../inded/index',
})
//返回到上一页面或多级页面
wx.navigateBack({
delta: 1
})
},
led.js里发送控制数据的函数
controlDeviceData(e) {
let that = this
var data = { power_switch:0};
wx.showLoading()
console.log('form data:', e)
if (e.detail.value == true) {
data.power_switch = 1
} else {
data.power_switch=0
}
console.log('form data:', data.power_switch)
// 实例化一个请求对象
let req = new iotmodel.ControlDeviceDataRequest();
req.DeviceName = app.globalData.deviceName;
req.ProductId = app.globalData.productId;
req.Data = JSON.stringify(data);
// 通过client对象调用想要访问的接口,需要传入请求对象以及响应回调函数
this.data.client.ControlDeviceData(req, function (err, response) {
if (err) {
console.log(err);
wx.showToast({
title: "发送控制指令失败",
icon: 'none',
duration: 3000,
})
return;
}
console.log(err)
wx.showToast({
title: "发送控制指令成功",
icon: 'none',
duration: 3000,
})
//这段语句用来在操作者控制设备成功后,
//将其信息写入小程序云开发的数据库
if (data.power_switch == 1) {
that.onAdd()
} else {
that.onde()
}
});
},
data: {
},
led.js里写入数据库的函数(onAdd 当时p开关为1时)
onAdd: function () {
const db = wx.cloud.database()
db.collection('counters').add({
data: {
power_switch: 1,
time: util.formatTime(new Date()),
head: "教室灯",
deviceName: app.globalData.deviceName,
productId :app.globalData.productId
},
success: res => {
// 在返回结果中会包含新创建的记录的 _id
this.setData({
counterId: res._id,
power_switch: 1,
head: "教室灯",
deviceName: app.globalData.deviceName,
productId: app.globalData.productId
})
wx.showToast({
title: '新增记录成功',
})
console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '新增记录失败'
})
console.error('[数据库] [新增记录] 失败:', err)
}
})
},
led.js里写入数据库的函数(onde 当时p开关为0时)
onde: function () {
const db = wx.cloud.database()
db.collection('counters').add({
data: {
power_switch: 0,
time: util.formatTime(new Date()),
head: "教室灯",
deviceName: app.globalData.deviceName,
productId: app.globalData.productId
},
success: res => {
// 在返回结果中会包含新创建的记录的 _id
this.setData({
counterId: res._id,
power_switch: 0,
head:"教室灯",
deviceName: app.globalData.deviceName,
productId: app.globalData.productId
})
wx.showToast({
title: '新增记录成功',
})
console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '新增记录失败'
})
console.error('[数据库] [新增记录] 失败:', err)
}
})
},
history.js里的查询数据库的历史数据的函数
onQuery: function () {
const db = wx.cloud.database()
// 查询当前用户所有的 counters
db.collection('counters').where({
productId: app.globalData.productId
}).get({
success: res => {
this.setData({
queryResult: JSON.stringify(res.data, null, 2),
})
console.log('[数据库] [查询记录] 成功: ', res)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '查询记录失败'
})
console.error('[数据库] [查询记录] 失败:', err)
}
})
},
1.板子的参考资料 https://github.com/Tencent/TencentOS-tiny
2.微信小程序连入腾讯云IOT的demo https://share.weiyun.com/5u2vejl 密码:8szgyr
3.微信小程序云开发自动生成的demo
这次作品主要的将引入了腾讯云IOT与微信小程序云开发相结合,利用腾讯云IOT提供的API,快速的实现云平台与小程序对接,再这基础之上再利用微信小程序的云开发功能将操作者的信息存入云数据库,实现了操作者可视化,对于每一次操作都能找出对应的操作者,在节能方面还是有很大的用处的。
整体来说这个作品很简单,根据官方提供的文档就可以完成,但自己也在官方提供的demo中加入了一些自己的想法,在这次的学习过程中,也看了很多大神的作品,发现自己会的东西真的很微不足道,以后一定会继续加油的!跟上大神的步伐。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。