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")
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")

View File

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

View File

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

View File

@@ -1,26 +1,26 @@
/* Includes */
#include "gatt_svc.h"
#include "common.h"
#include "heart_rate.h"
#include "temp.h"
#include "led.h"
/* 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);
static int led_chr_access(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg);
/* Private variables */
/* Heart rate service */
static const ble_uuid16_t heart_rate_svc_uuid = BLE_UUID16_INIT(0x180D);
/* temp rate service */
static const ble_uuid16_t temp_svc_uuid = BLE_UUID16_INIT(0x180D);
static uint8_t heart_rate_chr_val[2] = {0};
static uint16_t heart_rate_chr_val_handle;
static const ble_uuid16_t heart_rate_chr_uuid = BLE_UUID16_INIT(0x2A37);
static uint8_t temp_chr_val[2] = {0};
static uint16_t temp_chr_val_handle;
static const ble_uuid16_t temp_chr_uuid = BLE_UUID16_INIT(0x2A37);
static uint16_t heart_rate_chr_conn_handle = 0;
static bool heart_rate_chr_conn_handle_inited = false;
static bool heart_rate_ind_status = false;
static uint16_t temp_chr_conn_handle = 0;
static bool temp_chr_conn_handle_inited = false;
static bool temp_ind_status = false;
/* Automation IO service */
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 */
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
/* Heart rate service */
/* Temp rate service */
{.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = &heart_rate_svc_uuid.u,
.uuid = &temp_svc_uuid.u,
.characteristics =
(struct ble_gatt_chr_def[]){
{/* Heart rate characteristic */
.uuid = &heart_rate_chr_uuid.u,
.access_cb = heart_rate_chr_access,
{/* Temp rate characteristic */
.uuid = &temp_chr_uuid.u,
.access_cb = temp_chr_access,
.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. */
}}},
@@ -64,13 +64,13 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
};
/* 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) {
/* Local variables */
int rc;
/* Handle access events */
/* Note: Heart rate characteristic is read only */
/* Note: Temp rate characteristic is read only */
switch (ctxt->op) {
/* Read characteristic event */
@@ -85,11 +85,11 @@ static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle,
}
/* Verify attribute handle */
if (attr_handle == heart_rate_chr_val_handle) {
if (attr_handle == temp_chr_val_handle) {
/* Update access buffer value */
heart_rate_chr_val[1] = get_heart_rate();
rc = os_mbuf_append(ctxt->om, &heart_rate_chr_val,
sizeof(heart_rate_chr_val));
temp_chr_val[1] = get_temp();
rc = os_mbuf_append(ctxt->om, &temp_chr_val,
sizeof(temp_chr_val));
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
goto error;
@@ -102,7 +102,7 @@ static int heart_rate_chr_access(uint16_t conn_handle, uint16_t attr_handle,
error:
ESP_LOGE(
TAG,
"unexpected access operation to heart rate characteristic, opcode: %d",
"unexpected access operation to temp rate characteristic, opcode: %d",
ctxt->op);
return BLE_ATT_ERR_UNLIKELY;
}
@@ -161,9 +161,9 @@ error:
/* Public functions */
void send_heart_rate_indication(void) {
if (heart_rate_ind_status && heart_rate_chr_conn_handle_inited) {
ble_gatts_indicate(heart_rate_chr_conn_handle,
heart_rate_chr_val_handle);
if (temp_ind_status && temp_chr_conn_handle_inited) {
ble_gatts_indicate(temp_chr_conn_handle,
temp_chr_val_handle);
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
* 1. Update heart rate subscription status
* 1. Update temp rate subscription status
*/
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 */
if (event->subscribe.attr_handle == heart_rate_chr_val_handle) {
/* Update heart rate subscription status */
heart_rate_chr_conn_handle = event->subscribe.conn_handle;
heart_rate_chr_conn_handle_inited = true;
heart_rate_ind_status = event->subscribe.cur_indicate;
if (event->subscribe.attr_handle == temp_chr_val_handle) {
/* Update temp rate subscription status */
temp_chr_conn_handle = event->subscribe.conn_handle;
temp_chr_conn_handle_inited = true;
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);
}