智慧園區(qū)能源管控系統(tǒng)
1.實(shí)驗(yàn)?zāi)康?/h3>
1.1采集溫度忿檩、濕度、光照值介蛉,并上傳OneNET平臺(tái)
1.2實(shí)現(xiàn)公共區(qū)域照明系統(tǒng)的燈光自動(dòng)控制萌庆。當(dāng)光照充足的時(shí)候,燈光熄滅币旧。光照暗時(shí)践险,照明燈亮起。
(LED燈模擬照明燈吹菱,通過HTTP數(shù)據(jù)推送將光照值推送給應(yīng)用程序巍虫,由應(yīng)用程序控制LED開關(guān))
2.實(shí)驗(yàn)步驟
2.1代碼更改
在程序中設(shè)置接入機(jī)的地址和鑒權(quán)信息(即IMEI和IMSI)
char uri[] = "coap://183.230.102.118:5683"; // 引導(dǎo)機(jī)服務(wù)
char *serv_addr = "183.230.102.118"; // 接入機(jī) IP 地址,暫時(shí)無用
const char endpoint_name[] = "318790567959252;318790567959252"; //IMEI;IMSI
向SDK添加外圍硬件驅(qū)動(dòng)鳍刷,本項(xiàng)目Demo中已經(jīng)做好LED和光照傳感器驅(qū)動(dòng)程序
#include "BH1750.h" /* 光照傳感器 */
#include "Lcd_Driver.h" /* lcd屏 */
nbiot_value_t illumi; /* 光照 */
nbiot_value_t led; /* led燈*/
extern float result_lx; /*光照*/
光照傳感器的對(duì)象ID:3301
燈光控制的對(duì)象ID:3311
寫回調(diào)函數(shù)
// "寫"回調(diào)函數(shù)
void write_callback(
uint16_t objid,
uint16_t instid,
uint16_t resid,
nbiot_value_t *data)
{
printf("write /%d/%d/%d:%d\r\n",
objid,
instid,
resid, data->value.as_bool);
if (objid == 3311 && instid == 0 && resid == 5850)
{
if(data->value.as_bool)
{
Lcd_Clear(WHITE);
ledStatus.Led1Sta=1;
}else
{
Lcd_Clear(BLACK);
ledStatus.Led1Sta=0;
}
}
}
讀回調(diào)函數(shù)
// "讀"回調(diào)函數(shù)
void read_callback(
uint16_t objid,
uint16_t instid,
uint16_t resid,
nbiot_value_t *data)
{ if (objid == 3301 && instid == 0 && resid == 5700) { /* 光照 */
BH1750_test();
illumi.value.as_float=result_lx;
} else if (objid == 3303 && instid == 0 && resid == 5700) { /* 溫度 */
SHT20_INFO sht20 = { 0 };
sht20 = SHT20_GetValue();
temp.value.as_float = sht20.tempreture;
} else if (objid == 3304 && instid == 0 && resid == 5700) { /* 濕度 */
SHT20_INFO sht20 = { 0 };
sht20 = SHT20_GetValue();
humi.value.as_float = sht20.humidity;
}
}
添加溫濕度垫言,光照資源
// 添加溫度資源
temp.type = NBIOT_FLOAT;
temp.flag = NBIOT_READABLE;
ret = nbiot_resource_add(dev,
3303, /* objId */
0, /* instId */
5700, /* resId */
&temp);
if (ret)
{
nbiot_device_destroy(dev);
printf("device add resource(temp) failed, code = %d.\r\n", ret);
}
// 添加濕度資源
humi.type = NBIOT_FLOAT;
humi.flag = NBIOT_READABLE;
ret = nbiot_resource_add(dev,
3304,
0,
5700,
&humi);
if (ret)
{
nbiot_device_destroy(dev);
printf("device add resource(humi) failed, code = %d.\r\n", ret);
}
// 添加光照資源
illumi.type = NBIOT_FLOAT;
illumi.flag = NBIOT_READABLE;
ret = nbiot_resource_add(dev,
3301,
0,
5700,
&illumi);
if (ret)
{
nbiot_device_destroy(dev);
printf("device add resource(illumi) failed, code = %d.\r\n", ret);
}
// 添加LED燈資源
led.type = NBIOT_BOOLEAN;
led.flag = NBIOT_READABLE | NBIOT_WRITABLE;
ret = nbiot_resource_add(dev,
3311,
0,
5850,
&led);
if (ret)
{
nbiot_device_destroy(dev);
printf("device add resource(led) failed, code = %d.\r\n", ret);
}
溫濕度資源更新
// 更新資源
void res_update(time_t interval)
{
SHT20_INFO sht20;
if (cur_time >= last_time + interval) {
cur_time = 0;
last_time = 0;
/* 更新溫濕度數(shù)值 */
temp.flag |= NBIOT_UPDATED;
humi.flag |= NBIOT_UPDATED;
sht20 = SHT20_GetValue();
temp.value.as_float = sht20.tempreture;
humi.value.as_float = sht20.humidity;
} else if (cur_time == 0 && last_time == 0) {
cur_time = nbiot_time();
last_time = cur_time;
} else {
cur_time = nbiot_time();
}
}
int main(int argc, char *argv[])
{
int life_time = 300;
int ret;
2.2平臺(tái)控制
2.3Postman
LCD屏現(xiàn)象
實(shí)驗(yàn)總結(jié)
通過此次項(xiàng)目的實(shí)施,能夠?qū)崿F(xiàn)對(duì)園區(qū)內(nèi)的溫濕度實(shí)時(shí)檢測
認(rèn)知了南向通信協(xié)議和北調(diào)API
熟悉了北調(diào)API
在這次項(xiàng)目中更熟悉了postman的用法
了解到了通過數(shù)據(jù)推送的方式控制LCD屏幕