/* main.c - Application main entry point */ /* * Copyright (c) 2015-2016 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include #include #include #include #include #include #include #include #include #include #define DEVICE_NAME CONFIG_BT_DEVICE_NAME #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define SLEEP_TIME_MS 1000 //#define SW0_NODE DT_ALIAS(sw0) #define LED0_NODE DT_ALIAS(led4) #define SW0_NODE DT_ALIAS(sw6) uint8_t door_status = 0; uint8_t d[3] = {0x11, 0x66, 0x00}; static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios); static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(SW0_NODE, gpios); /* * Set Advertisement data. Based on the Eddystone specification: * https://github.com/google/eddystone/blob/master/protocol-specification.md * https://github.com/google/eddystone/tree/master/eddystone-url */ static struct bt_data ad1[] = { BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_NO_BREDR), BT_DATA_BYTES(BT_DATA_UUID16_ALL, 0x58, 0x36), BT_DATA_BYTES(BT_DATA_SVC_DATA16,0x11,0x66,0x00) }; static const struct bt_data sd[] = { BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static void bt_ready(int err) { char addr_s[BT_ADDR_LE_STR_LEN]; bt_addr_le_t addr = {0}; size_t count = 1; if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } printk("Bluetooth initialized\n"); /* Start advertising */ err = bt_le_adv_start(BT_LE_ADV_NCONN_IDENTITY, ad1, ARRAY_SIZE(ad1), sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; } /* For connectable advertising you would use * bt_le_oob_get_local(). For non-connectable non-identity * advertising an non-resolvable private address is used; * there is no API to retrieve that. */ bt_id_get(&addr, &count); bt_addr_le_to_str(&addr, addr_s, sizeof(addr_s)); printk("Beacon started, advertising as %s\n", addr_s); } int main(void) { int err,ret; if (!device_is_ready(led.port)) { return ; } if (!device_is_ready(button.port)) { return ; } ret = gpio_pin_configure_dt(&button, GPIO_INPUT); if (ret < 0) { return ; } ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); if (ret < 0) { return ; } printk("Starting Beacon Demo\n"); /* Initialize the Bluetooth Subsystem */ err = bt_enable(bt_ready); if (err) { printk("Bluetooth init failed (err %d)\n", err); } while(1) { bool val = gpio_pin_get_dt(&button); gpio_pin_set_dt(&led,val); printk("Button Value: %d\n", val); door_status = (val == 1) ? 1 : 0; printk("Door Status: %s\n", door_status ? "Open" : "Closed"); d[2]=door_status; ad1[2].data = (const uint8_t *)(d); err = bt_le_adv_update_data(ad1, ARRAY_SIZE(ad1),sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising update with (err %d)\n", err); } } return 0; }