1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/bluetooth.h>
8 #include <zephyr/bluetooth/uuid.h>
9 
10 #define SERVICE_DATA_LEN        9
11 #define SERVICE_UUID            0xfcd2      /* BTHome service UUID */
12 #define IDX_TEMPL               4           /* Index of lo byte of temp in service data*/
13 #define IDX_TEMPH               5           /* Index of hi byte of temp in service data*/
14 
15 #define ADV_PARAM BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_IDENTITY, \
16 				  BT_GAP_ADV_SLOW_INT_MIN, \
17 				  BT_GAP_ADV_SLOW_INT_MAX, NULL)
18 
19 
20 static uint8_t service_data[SERVICE_DATA_LEN] = {
21 	BT_UUID_16_ENCODE(SERVICE_UUID),
22 	0x40,
23 	0x02,	/* Temperature */
24 	0xc4,	/* Low byte */
25 	0x00,   /* High byte */
26 	0x03,	/* Humidity */
27 	0xbf,	/* 50.55%  low byte*/
28 	0x13,   /* 50.55%  high byte*/
29 };
30 
31 static struct bt_data ad[] = {
32 	BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR),
33 	BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
34 	BT_DATA(BT_DATA_SVC_DATA16, service_data, ARRAY_SIZE(service_data))
35 };
36 
main(void)37 int main(void)
38 {
39 	int err;
40 	int temp = 0;
41 
42 	printk("Starting BTHome sensor template\n");
43 
44 	/* Initialize the Bluetooth Subsystem */
45 	err = bt_enable(NULL);
46 	if (err) {
47 		printk("Bluetooth init failed (err %d)\n", err);
48 		return 0;
49 	}
50 
51 	printk("Bluetooth initialized\n");
52 
53 	/* Start advertising */
54 	err = bt_le_adv_start(ADV_PARAM, ad, ARRAY_SIZE(ad), NULL, 0);
55 	if (err) {
56 		printk("Advertising failed to start (err %d)\n", err);
57 		return 0;
58 	}
59 
60 	for (;;) {
61 		/* Simulate temperature from 0C to 25C */
62 		service_data[IDX_TEMPH] = (temp * 100) >> 8;
63 		service_data[IDX_TEMPL] = (temp * 100) & 0xff;
64 		if (temp++ == 25) {
65 			temp = 0;
66 		}
67 		err = bt_le_adv_update_data(ad, ARRAY_SIZE(ad), NULL, 0);
68 		if (err) {
69 			printk("Failed to update advertising data (err %d)\n", err);
70 		}
71 		k_sleep(K_MSEC(BT_GAP_ADV_SLOW_INT_MIN));
72 	}
73 	return 0;
74 }
75