1、实验背景
随着城市的快速发展和城市化进程的加快,伴随而来的环境、社会、交通问题也日益严重。利用先进的信息技术构建智慧城市成为解决城市问题的重要途径。作为智慧城市重要的组成部分,智慧园区的建设尤为重要。
2、实验目标
1.采集温度、湿度、光照、气压值,并由OneNET平台订阅;
2.光照高于设定值,LED灯亮;光照低于设定值,LED灯灭
3.轻应用展示温度、湿度、光照、气压值。
4.可视化展示温度、湿度、光照、气压值。
3、实验步骤
3.1平台控制
添加新的对象
nbiot_value_t press; /* 气压*/
nbiot_value_t led; /* LED灯*/
extern float result_lx;
extern _bmp180 bmp180;
编写写回调函数
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))
{
Led1_Set(data->value.as_bool);
}
}
编写读回调函数
void read_callback(
uint16_t objid,
uint16_t instid,
uint16_t resid,
nbiot_value_t *data)
{
if (objid == 3303 && instid == 0 && resid == 5700) {
SHT20_INFO sht20 = { 0 };
sht20 = SHT20_GetValue();
temp.value.as_float = (int64_t)sht20.tempreture;
} else if (objid == 3304 && instid == 0 && resid == 5700) {
SHT20_INFO sht20 = { 0 };
sht20 = SHT20_GetValue();
humi.value.as_float = (int64_t)sht20.humidity;
} else if (objid == 3301 && instid == 0 && resid == 5700) {
BH1750_test();
illumi.value.as_float=result_lx;
} else if (objid == 3323 && instid == 0 && resid == 5700) {
BMP180_test();
press.value.as_float=bmp180.p;
}
}
添加资源
void res_update(time_t interval)
{
SHT20_INFO sht20;
if (cur_time >= last_time + interval) {
cur_time = 0;
last_time = 0;
temp.flag |= NBIOT_UPDATED;
humi.flag |= NBIOT_UPDATED;
sht20 = SHT20_GetValue();
temp.value.as_float = ((float)((int)((sht20.tempreture + 0.05) * 100)) / 100);
humi.value.as_float = sht20.humidity;
illumi.flag|=NBIOT_UPDATED;
BH1750_test();
illumi.value.as_float=result_lx;
press.flag|=NBIOT_UPDATED;
BMP180_test();
press.value.as_float=bmp180.p;
led.flag|=NBIOT_UPDATED;
led.value.as_bool=ledStatus.Led1Sta;
} else if (cur_time == 0 && last_time == 0) {
cur_time = nbiot_time();
last_time = cur_time;
} else {
cur_time = nbiot_time();
}
}
// 添加光照资源
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);
}
// 添加气压资源
press.type = NBIOT_FLOAT;
press.flag = NBIOT_READABLE;
ret = nbiot_resource_add(dev,
3323, /* objId */
0, /* instId */
5700, /* resId */
&press);
if (ret)
{
nbiot_device_destroy(dev);
printf("device add resource(press) failed, code = %d.\r\n", ret);
}
// 添加led灯资源
led.type = NBIOT_BOOLEAN;
led.flag = NBIOT_READABLE|NBIOT_WRITABLE;
ret = nbiot_resource_add(dev,
3311, /* objId */
0, /* instId */
5850, /* resId */
&led);
if (ret)
{
nbiot_device_destroy(dev);
printf("device add resource(led) failed, code = %d.\r\n", ret);
}
代码编写完之后,终端固件烧写运行。
进入OneNET平台,设备上线后可以看到已经有五个资源
打开Light Control资源,通过写的操作,输入true,LED灯亮着
3.2光照值控制
为设备添加触发器
在光照值高于60时Led灯灭
在光照值低于50时亮灯
3.3Postman控制
调用API返回温度值和资源ID
返回值
{
"errno": 0,
"data": {
"count": 5,
"datastreams": [
{
"datapoints": [
{
"at": "2021-06-08 16:16:38.236",
"value": 30.870001
}
],
"id": "3303_0_5700"
},
{
"datapoints": [
{
"at": "2021-06-08 16:16:38.236",
"value": 57.674973
}
],
"id": "3304_0_5700"
},
{
"datapoints": [
{
"at": "2021-06-08 16:16:38.026",
"value": 121.666664
}
],
"id": "3301_0_5700"
},
{
"datapoints": [
{
"at": "2021-06-08 16:16:38.555",
"value": 98022
}
],
"id": "3323_0_5700"
},
{
"datapoints": [
{
"at": "2021-06-08 16:16:38.252",
"value": false
}
],
"id": "3311_0_5850"
}
]
},
"error": "succ"
}
使用Postman发送请求,向终端设备写入资源
资源参数
写入(其中输入1,表示亮灯,0表示关灯)
3.4轻应用
4、总结
本次实验步骤比较繁琐但是比较简单,需要耐心。遇到问题时要多与同学讨论!