1 /*
2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdarg.h>
9 #include "aos/kernel.h"
10 #include "sensor/sensor.h"
11 #include "ulog/ulog.h"
12 #include "netmgr.h"
13 #include <uservice/uservice.h>
14 #include <uservice/eventid.h>
15
16 #define SENSOR_SAMPLE_TIME 1000 /* sensor sampling period is 1000 ms*/
17 #define PROP_POST_FORMAT_TEMP "{\"CurrentTemperature\":%.1f}"
18 #define PROP_POST_FORMAT_HUMI "{\"Humidity\":%.1f}"
19
sensor_cloud_test_init(void)20 void sensor_cloud_test_init(void)
21 {
22 int ret = 0;
23
24 i2c_dev_t i2c_dev;
25 i2c_dev.port = 1;
26 i2c_dev.config.address_width = I2C_HAL_ADDRESS_WIDTH_7BIT;
27 i2c_dev.config.freq = I2C_BUS_BIT_RATES_100K;
28 i2c_dev.config.mode = I2C_MODE_MASTER;
29 sensor_i2c_init(&i2c_dev);
30
31 /* Sensor Hal start */
32 ret = sensor_hal_init();
33 if (ret != 0) {
34 return;
35 }
36
37 /* Open the Humidity sensor device */
38 ret = sensor_hal_open(TAG_DEV_HUMI, 0);
39 if (ret != 0) {
40 //return;
41 }
42
43 /* Open the temperature sensor device */
44 ret = sensor_hal_open(TAG_DEV_TEMP, 0);
45 if (ret != 0) {
46 //return;
47 }
48
49 /* Set the sampling period for sensors */
50 sensor_hal_ioctl(TAG_DEV_HUMI, 0, SENSOR_IOCTL_ODR_SET, SENSOR_SAMPLE_TIME);
51 sensor_hal_ioctl(TAG_DEV_TEMP, 0, SENSOR_IOCTL_ODR_SET, SENSOR_SAMPLE_TIME);
52 }
53
sensor_cloud_test_start(void * dm_handle)54 void sensor_cloud_test_start(void *dm_handle)
55 {
56 int ret = 0;
57 char param[128];
58 temperature_data_t temp;
59 humidity_data_t humi;
60
61 /* Read the Humidity sensor data */
62 ret = sensor_hal_read(TAG_DEV_HUMI, 0, &humi, sizeof(humi));
63 if (ret > 0) {
64 /* Print the Humidity sensor data */
65 printf("Humidity value : %.1f H\n", ((float)humi.h)/10);
66
67 memset(param, 0, 128);
68
69 /* build the report payload */
70 sprintf(param, PROP_POST_FORMAT_HUMI, ((float)humi.h)/10);
71
72 /* Report the Humidity data to cloud */
73 ret = demo_send_property_post(dm_handle, (char *)param);
74 if (ret == -1) {
75 LOG("%s %d fail\n", __func__,__LINE__);
76 }
77 }
78
79 /* Read the temperature sensor data */
80 ret = sensor_hal_read(TAG_DEV_TEMP, 0, &temp, sizeof(temp));
81 if (ret > 0) {
82 /* Print the temperature sensor data */
83 printf("Temperature value : %.1f centidegree\n", ((float)temp.t)/10);
84
85 memset(param, 0, 128);
86
87 /* build the report payload */
88 sprintf(param, PROP_POST_FORMAT_TEMP, ((float)(temp.t))/10);
89
90 /* Report the temperature data to cloud */
91 ret = demo_send_property_post(dm_handle, (char *)param);
92 if (ret == -1) {
93 LOG("%s %d fail\n", __func__,__LINE__);
94 }
95 }
96 }
97