Commit 82955862 authored by Yongjia Niu's avatar Yongjia Niu
Browse files

Add humidity and temperature attribute and pub to IoTConnect

parent 3da366e5
https://os.mbed.com/teams/ST/code/HTS221/#ccf7f36492ae
#include "HumTempAttr.h"
#include "mbed_trace.h"
#define TRACE_GROUP "HumTempAttr"
HumTempAttr::HumTempAttr(HTS221Sensor* _sensor) :
sensor(_sensor),
hum(NULL),
temp(NULL),
interval_ms(0),
thread(osPriorityNormal, HTS221_SENSOR_UPDATE_THREAD_STACK_SIZE)
{
init_attr();
}
HumTempAttr::~HumTempAttr()
{
if (hum) {
cJSON_Delete(hum);
}
if (temp) {
cJSON_Delete(temp);
}
}
int HumTempAttr::sensor_init()
{
uint8_t id = 0;
sensor->init(NULL);
sensor->enable();
sensor->read_id(&id);
// sensor->get_temperature(&val);
tr_debug("HTS221 sensor init done, get id: 0x%02x", id);
return 0;
}
void HumTempAttr::init_attr()
{
hum = cJSON_CreateNumber(0);
temp = cJSON_CreateNumber(0);
}
cJSON* HumTempAttr::get_hum()
{
return hum;
}
cJSON* HumTempAttr::get_temp()
{
return temp;
}
int HumTempAttr::start_sensor_update(int _interval_ms)
{
osStatus ret;
interval_ms = _interval_ms;
ret = thread.start(callback(this, &HumTempAttr::sensor_update_main_loop));
if (ret != osOK) {
tr_error("Start thread failed with osStatus: %d", ret);
return -1;
}
return 0;
}
void HumTempAttr::sensor_update_main_loop()
{
float hum_data = 0;
float temp_data = 0;
while(1) {
sensor->get_temperature(&temp_data);
sensor->get_humidity(&hum_data);
cJSON_SetNumberValue(temp, temp_data);
cJSON_SetNumberValue(hum, hum_data);
// tr_debug("Read Date:temp=%2d, hum=%2d",
// (int)temp_data,
// (int)hum_data);
ThisThread::sleep_for(interval_ms);
}
}
\ No newline at end of file
#ifndef __HUM_TEMP_ATTR_H__
#define __HUM_TEMP_ATTR_H__
#include "mbed.h"
#include "HTS221Sensor.h"
#include "cJSON.h"
#define HTS221_SENSOR_UPDATE_THREAD_STACK_SIZE 2048
class HumTempAttr
{
public:
HumTempAttr(HTS221Sensor* _sensor);
~HumTempAttr();
int sensor_init();
// NOTE: Use cJSON_AddItemToObject to add Hum / Temp to a parent Json object
// Do not use cJSON_AddItemReferenceToObject because Hum, Temp is just a Number Type, not a object type
cJSON* get_hum();
cJSON* get_temp();
int start_sensor_update(int _interval_ms);
void sensor_update_main_loop();
private:
void init_attr();
private:
HTS221Sensor* sensor;
cJSON *hum;
cJSON *temp;
int interval_ms;
Thread thread;
};
#endif
......@@ -5,9 +5,15 @@
#include "IoTConnectClient.h"
#include "IoTConnectHttpClient.h"
#include "devices_settings.h"
#include "HumTempAttr.h"
#define TRACE_GROUP "Main"
// Blinking rate in milliseconds
#define BLINKING_RATE_MS 500
#define PUB_PERIOD_MS 10000
#define SENSOR_UPDATE_RATE_MS 2000
NetworkInterface* network = NULL;
......@@ -16,6 +22,16 @@ IoTConnectDevice* stm32_dev;
IoTConnectClient* client = NULL;
IoTConnectHttpClient* http_client = NULL;
#ifdef TARGET_DISCO_L475VG_IOT01A
DevI2C i2c(PB_11, PB_10);
#endif
#ifdef TARGET_B_L4S5I_IOT01A
DevI2C i2c(PB_11, PB_10);
#endif
HTS221Sensor* hum_temp = NULL;
HumTempAttr* hum_temp_attr = NULL;
NetworkInterface* connect_network() {
NetworkInterface* net = NULL;
......@@ -67,6 +83,8 @@ int sync_rtc_time(NetworkInterface* net, const char* ntp_server = NULL) {
int main() {
int ret = 0;
cJSON *attr = NULL;
char *js = NULL;
mbed_trace_init();
......@@ -74,7 +92,18 @@ int main() {
printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
#endif
hum_temp = new HTS221Sensor(&i2c);
hum_temp_attr = new HumTempAttr(hum_temp);
stm32_dev = new IoTConnectDevice(EMBEST_STM32L4_KIT_DEBUG_ID, EMBEST_STM32L4_KIT_DEBUG_NAME, &embest);
attr = stm32_dev->get_attr();
cJSON_AddItemToObject(attr, "humidity", hum_temp_attr->get_hum());
cJSON_AddItemToObject(attr, "temperature", hum_temp_attr->get_temp());
hum_temp_attr->sensor_init();
hum_temp_attr->start_sensor_update(SENSOR_UPDATE_RATE_MS);
// Connect to network
network = connect_network();
......@@ -111,5 +140,18 @@ int main() {
return -1;
}
// Initialise the digital pin LED1 as an output
DigitalOut led(LED1);
while (true) {
client->pub();
for(int i = 0; i < PUB_PERIOD_MS; i += BLINKING_RATE_MS) {
led = !led;
ThisThread::sleep_for(BLINKING_RATE_MS);
}
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment