Compare commits

..

4 Commits

Author SHA1 Message Date
30cccee5f6 set temp service 2025-12-02 23:30:57 +01:00
615637e24c add dht dependency 2025-12-02 20:07:04 +01:00
6dd232fb83 rename heart variables and function 2025-12-01 12:18:38 +01:00
3ba29199c4 rename files 2025-12-01 11:22:29 +01:00
6 changed files with 62 additions and 49 deletions

View File

@@ -1,5 +1,5 @@
file(GLOB_RECURSE srcs "main.c" "src/*.c") file(GLOB_RECURSE srcs "main.c" "src/*.c")
idf_component_register(SRCS "${srcs}" idf_component_register(SRCS "${srcs}"
PRIV_REQUIRES bt nvs_flash esp_driver_gpio PRIV_REQUIRES bt nvs_flash esp_driver_gpio dht
INCLUDE_DIRS "./include") INCLUDE_DIRS "./include")

View File

@@ -4,12 +4,16 @@
/* Includes */ /* Includes */
/* ESP APIs */ /* ESP APIs */
#include "esp_random.h" #include "esp_random.h"
#include "dht.h"
/* Defines */ /* Defines */
#define HEART_RATE_TASK_PERIOD (1000 / portTICK_PERIOD_MS) #define HEART_RATE_TASK_PERIOD (1000 / portTICK_PERIOD_MS)
#define DHT_GPIO_PIN 23
#define DHT_SENSOR_TIPO DHT_TYPE_DHT11
/* Public function declarations */ /* Public function declarations */
uint8_t get_heart_rate(void); uint8_t get_temp(void);
void update_heart_rate(void); void update_temp(void);
void init_sensor(void);
#endif // HEART_RATE_H #endif // HEART_RATE_H

View File

@@ -2,7 +2,7 @@
#include "common.h" #include "common.h"
#include "gap.h" #include "gap.h"
#include "gatt_svc.h" #include "gatt_svc.h"
#include "heart_rate.h" #include "temp.h"
#include "led.h" #include "led.h"
/* Library function declarations */ /* Library function declarations */
@@ -59,8 +59,8 @@ static void heart_rate_task(void *param) {
/* Loop forever */ /* Loop forever */
while (1) { while (1) {
/* Update heart rate value every 1 second */ /* Update heart rate value every 1 second */
update_heart_rate(); update_temp();
ESP_LOGI(TAG, "heart rate updated to %d", get_heart_rate()); ESP_LOGI(TAG, "heart rate updated to %d", get_temp());
/* Send heart rate indication if enabled */ /* Send heart rate indication if enabled */
send_heart_rate_indication(); send_heart_rate_indication();

View File

@@ -1,26 +1,26 @@
/* Includes */ /* Includes */
#include "gatt_svc.h" #include "gatt_svc.h"
#include "common.h" #include "common.h"
#include "heart_rate.h" #include "temp.h"
#include "led.h" #include "led.h"
/* Private function declarations */ /* Private function declarations */
static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle, static int temp_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg); struct ble_gatt_access_ctxt *ctxt, void *arg);
static int led_chr_access(uint16_t conn_handle, uint16_t attr_handle, static int led_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg); struct ble_gatt_access_ctxt *ctxt, void *arg);
/* Private variables */ /* Private variables */
/* Heart rate service */ /* temp rate service */
static const ble_uuid16_t heart_rate_svc_uuid = BLE_UUID16_INIT(0x180D); static const ble_uuid16_t temp_svc_uuid = BLE_UUID16_INIT(0x180D);
static uint8_t heart_rate_chr_val[2] = {0}; static uint8_t temp_chr_val[2] = {0};
static uint16_t heart_rate_chr_val_handle; static uint16_t temp_chr_val_handle;
static const ble_uuid16_t heart_rate_chr_uuid = BLE_UUID16_INIT(0x2A37); static const ble_uuid16_t temp_chr_uuid = BLE_UUID16_INIT(0x2A37);
static uint16_t heart_rate_chr_conn_handle = 0; static uint16_t temp_chr_conn_handle = 0;
static bool heart_rate_chr_conn_handle_inited = false; static bool temp_chr_conn_handle_inited = false;
static bool heart_rate_ind_status = false; static bool temp_ind_status = false;
/* Automation IO service */ /* Automation IO service */
static const ble_uuid16_t auto_io_svc_uuid = BLE_UUID16_INIT(0x1815); static const ble_uuid16_t auto_io_svc_uuid = BLE_UUID16_INIT(0x1815);
@@ -31,16 +31,16 @@ static const ble_uuid128_t led_chr_uuid =
/* GATT services table */ /* GATT services table */
static const struct ble_gatt_svc_def gatt_svr_svcs[] = { static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
/* Heart rate service */ /* Temp rate service */
{.type = BLE_GATT_SVC_TYPE_PRIMARY, {.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = &heart_rate_svc_uuid.u, .uuid = &temp_svc_uuid.u,
.characteristics = .characteristics =
(struct ble_gatt_chr_def[]){ (struct ble_gatt_chr_def[]){
{/* Heart rate characteristic */ {/* Temp rate characteristic */
.uuid = &heart_rate_chr_uuid.u, .uuid = &temp_chr_uuid.u,
.access_cb = heart_rate_chr_access, .access_cb = temp_chr_access,
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_INDICATE, .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_INDICATE,
.val_handle = &heart_rate_chr_val_handle}, .val_handle = &temp_chr_val_handle},
{ {
0, /* No more characteristics in this service. */ 0, /* No more characteristics in this service. */
}}}, }}},
@@ -64,13 +64,13 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
}; };
/* Private functions */ /* Private functions */
static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle, static int temp_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg) { struct ble_gatt_access_ctxt *ctxt, void *arg) {
/* Local variables */ /* Local variables */
int rc; int rc;
/* Handle access events */ /* Handle access events */
/* Note: Heart rate characteristic is read only */ /* Note: Temp rate characteristic is read only */
switch (ctxt->op) { switch (ctxt->op) {
/* Read characteristic event */ /* Read characteristic event */
@@ -85,11 +85,11 @@ static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle,
} }
/* Verify attribute handle */ /* Verify attribute handle */
if (attr_handle == heart_rate_chr_val_handle) { if (attr_handle == temp_chr_val_handle) {
/* Update access buffer value */ /* Update access buffer value */
heart_rate_chr_val[1] = get_heart_rate(); temp_chr_val[1] = get_temp();
rc = os_mbuf_append(ctxt->om, &heart_rate_chr_val, rc = os_mbuf_append(ctxt->om, &temp_chr_val,
sizeof(heart_rate_chr_val)); sizeof(temp_chr_val));
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} }
goto error; goto error;
@@ -102,7 +102,7 @@ static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle,
error: error:
ESP_LOGE( ESP_LOGE(
TAG, TAG,
"unexpected access operation to heart rate characteristic, opcode: %d", "unexpected access operation to temp rate characteristic, opcode: %d",
ctxt->op); ctxt->op);
return BLE_ATT_ERR_UNLIKELY; return BLE_ATT_ERR_UNLIKELY;
} }
@@ -161,9 +161,9 @@ error:
/* Public functions */ /* Public functions */
void send_heart_rate_indication(void) { void send_heart_rate_indication(void) {
if (heart_rate_ind_status && heart_rate_chr_conn_handle_inited) { if (temp_ind_status && temp_chr_conn_handle_inited) {
ble_gatts_indicate(heart_rate_chr_conn_handle, ble_gatts_indicate(temp_chr_conn_handle,
heart_rate_chr_val_handle); temp_chr_val_handle);
ESP_LOGI(TAG, "heart rate indication sent!"); ESP_LOGI(TAG, "heart rate indication sent!");
} }
} }
@@ -213,7 +213,7 @@ void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg) {
/* /*
* GATT server subscribe event callback * GATT server subscribe event callback
* 1. Update heart rate subscription status * 1. Update temp rate subscription status
*/ */
void gatt_svr_subscribe_cb(struct ble_gap_event *event) { void gatt_svr_subscribe_cb(struct ble_gap_event *event) {
@@ -227,11 +227,11 @@ void gatt_svr_subscribe_cb(struct ble_gap_event *event) {
} }
/* Check attribute handle */ /* Check attribute handle */
if (event->subscribe.attr_handle == heart_rate_chr_val_handle) { if (event->subscribe.attr_handle == temp_chr_val_handle) {
/* Update heart rate subscription status */ /* Update temp rate subscription status */
heart_rate_chr_conn_handle = event->subscribe.conn_handle; temp_chr_conn_handle = event->subscribe.conn_handle;
heart_rate_chr_conn_handle_inited = true; temp_chr_conn_handle_inited = true;
heart_rate_ind_status = event->subscribe.cur_indicate; temp_ind_status = event->subscribe.cur_indicate;
} }
} }

View File

@@ -1,11 +0,0 @@
/* Includes */
#include "common.h"
#include "heart_rate.h"
/* Private variables */
static uint8_t heart_rate;
/* Public functions */
uint8_t get_heart_rate(void) { return heart_rate; }
void update_heart_rate(void) { heart_rate = 60 + (uint8_t)(esp_random() % 21); }

20
main/src/temp.c Normal file
View File

@@ -0,0 +1,20 @@
/* Includes */
#include "common.h"
#include "temp.h"
#include "driver/gpio.h"
/* Private variables */
static uint8_t heart_rate;
/* Public functions */
uint8_t get_temp(void) { return heart_rate; }
void update_temp(void) { heart_rate = 60 + (uint8_t)(esp_random() % 21); }
void init_sensor(void) {
// DHT PULL up resistor config
gpio_set_direction(DHT_GPIO_PIN, GPIO_MODE_INPUT);
gpio_pullup_en(DHT_GPIO_PIN);
printf("DHT sensor config init, pull up resistor and pin: %d\n", DHT_GPIO_PIN);
}