diff --git a/nodes/prj.conf b/nodes/prj.conf index 4210a15..55962f0 100644 --- a/nodes/prj.conf +++ b/nodes/prj.conf @@ -1,3 +1,2 @@ -CONFIG_GPIO=y CONFIG_SENSOR=y -CONFIG_I2C=y +CONFIG_BT=y diff --git a/nodes/src/ble_advertiser.c b/nodes/src/ble_advertiser.c index 46b857e..465ce09 100644 --- a/nodes/src/ble_advertiser.c +++ b/nodes/src/ble_advertiser.c @@ -1,14 +1,55 @@ #include "ble_advertiser.h" +static const int MS_ADV_UP = 500; // time during which the advertising is active + +static const char BT_KEY_WINDOW = 0x01; +static const char BT_KEY_HUMIDITY = 0x02; +static const char BT_KEY_TEMP = 0x03; +static const char BT_KEY_CO2_LVL = 0x04; + +// value size [B] +static const char BT_VALUE_SIZE_WINDOW = 1; +static const char BT_VALUE_SIZE_HUMIDITY = 1; +static const char BT_VALUE_SIZE_TEMP = 2; +static const char BT_VALUE_SIZE_CO2_LVL = 4; + +static const int BT_AD_DATA_INDEX_WINDOW = 1; +static const int BT_AD_DATA_INDEX_HUMIDITY = 3; +static const int BT_AD_DATA_INDEX_TEMP = 5; +static const int BT_AD_DATA_INDEX_CO2_LVL = 8; + +// sum of all value size + size for all keys +static const char BT_AD_TOTAL_SIZE = + BT_VALUE_SIZE_WINDOW + BT_VALUE_SIZE_HUMIDITY + BT_VALUE_SIZE_TEMP + BT_VALUE_SIZE_CO2_LVL + 4; + +static uint8_t ad_data[] = { + BT_KEY_WINDOW, 0x00, BT_KEY_HUMIDITY, 0x00, BT_KEY_TEMP, 0x00, 0x00, BT_KEY_CO2_LVL, 0x00, 0x00, 0x00, 0x00 +}; +static const struct bt_data ad[] = { + BT_DATA(BT_DATA_MANUFACTURER_DATA, ad_data, BT_AD_TOTAL_SIZE), +}; + enum error_code ble_init(){ - return success; + return ((0 == bt_enable(NULL)) ? success : init_failed); } enum error_code ble_advertise( - int co2_lvl_value_ptr, - int hygro_value_ptr, - int thermo_value_ptr, - enum window_status window_value_ptr + enum window_status window_value, + int hygro_value, + int thermo_value, + int co2_lvl_value ){ - return success; + enum error_code ret = write_failed; + // set values here + ad_data[BT_AD_DATA_INDEX_WINDOW] = (uint8_t)(window_value == open ? 1 : 0); + ad_data[BT_AD_DATA_INDEX_HUMIDITY] = (uint8_t)(hygro_value); + ad_data[BT_AD_DATA_INDEX_TEMP] = (uint16_t)(thermo_value); + ad_data[BT_AD_DATA_INDEX_CO2_LVL] = (uint32_t)(co2_lvl_value); + if(0 == bt_le_adv_start(BT_LE_ADV_NCONN, ad, ARRAY_SIZE(ad), NULL, 0)){ + k_msleep(MS_ADV_UP); + if(0 == bt_le_adv_stop()){ + ret = success; + }else{} + }else{} + return ret; } diff --git a/nodes/src/ble_advertiser.h b/nodes/src/ble_advertiser.h index 5b8c329..f31a107 100644 --- a/nodes/src/ble_advertiser.h +++ b/nodes/src/ble_advertiser.h @@ -1,15 +1,19 @@ #ifndef BLE_ADVERTISER_H #define BLE_ADVERTISER_H +#include +#include +#include + #include "error_code.h" #include "window_status.h" enum error_code ble_init(); enum error_code ble_advertise( - int co2_lvl_value_ptr, - int hygro_value_ptr, - int thermo_value_ptr, - enum window_status window_value_ptr + enum window_status window_value, + int hygro_value, + int thermo_value, + int co2_lvl_value ); #endif //BLE_ADVERTISER_H diff --git a/nodes/src/error_code.h b/nodes/src/error_code.h index ea69fd1..ca53f8d 100644 --- a/nodes/src/error_code.h +++ b/nodes/src/error_code.h @@ -7,6 +7,7 @@ enum error_code{ success = 0, init_failed, read_failed, + write_failed, error_unknown, error_code_last, // iteration purpose }; diff --git a/nodes/src/hygrometer.c b/nodes/src/hygrometer.c index 471ecfe..1706a65 100644 --- a/nodes/src/hygrometer.c +++ b/nodes/src/hygrometer.c @@ -14,7 +14,7 @@ enum error_code hygro_get_value(int* holder){ enum error_code ret = read_failed; struct sensor_value humidity; if(sensor_channel_get(dev, SENSOR_CHAN_AMBIENT_TEMP, &humidity) >= 0){ - *holder = sensor_value_to_deci(&humidity); + *holder = humidity.val1; //taking only the integer part ret = success; }else{} return ret;