feat(nodes): WIP adding first implementation for BLE advertising.

wip: Not tested for now

Refs: #3
This commit is contained in:
adrien balleyguier
2026-04-28 16:44:23 +02:00
committed by Klagarge
parent 71ed4c4bf9
commit 53ccda306b
5 changed files with 58 additions and 13 deletions

View File

@@ -1,3 +1,2 @@
CONFIG_GPIO=y
CONFIG_SENSOR=y
CONFIG_I2C=y
CONFIG_BT=y

View File

@@ -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;
}

View File

@@ -1,15 +1,19 @@
#ifndef BLE_ADVERTISER_H
#define BLE_ADVERTISER_H
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/assigned_numbers.h>
#include <zephyr/bluetooth/bluetooth.h>
#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

View File

@@ -7,6 +7,7 @@ enum error_code{
success = 0,
init_failed,
read_failed,
write_failed,
error_unknown,
error_code_last, // iteration purpose
};

View File

@@ -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;