Hello All,
I'm tried to interface nordic with "peripheral_lbs" with arduino nano BLE with the example of "Led Control". Arduino nano ble is requesting attributes and nordic is disconnected after 10 seconds.
/* * Copyright (c) 2018 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ #include <zephyr/types.h> #include <stddef.h> #include <string.h> #include <errno.h> #include <sys/printk.h> #include <sys/byteorder.h> #include <zephyr.h> #include <drivers/gpio.h> #include <soc.h> #include <bluetooth/bluetooth.h> #include <bluetooth/hci.h> #include <bluetooth/conn.h> #include <bluetooth/uuid.h> #include <bluetooth/gatt.h> #include <bluetooth/services/lbs.h> #include <settings/settings.h> #include <dk_buttons_and_leds.h> #define DEVICE_NAME CONFIG_BT_DEVICE_NAME #define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1) #define RUN_STATUS_LED DK_LED1 #define CON_STATUS_LED DK_LED2 #define RUN_LED_BLINK_INTERVAL 1000 #define USER_LED DK_LED3 #define USER_BUTTON DK_BTN1_MSK static bool app_button_state; static const struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)), BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN), }; static const struct bt_data sd[] = { BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_LBS_VAL), }; static void connected(struct bt_conn *conn, uint8_t err) { if (err) { printk("Connection failed (err %u)\n", err); return; } printk("Connected\n"); dk_set_led_on(CON_STATUS_LED); } static void disconnected(struct bt_conn *conn, uint8_t reason) { printk("Disconnected (reason %u)\n", reason); dk_set_led_off(CON_STATUS_LED); } #ifdef CONFIG_BT_LBS_SECURITY_ENABLED static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); if (!err) { printk("Security changed: %s level %u\n", addr, level); } else { printk("Security failed: %s level %u err %d\n", addr, level, err); } } #endif static struct bt_conn_cb conn_callbacks = { .connected = connected, .disconnected = disconnected, #ifdef CONFIG_BT_LBS_SECURITY_ENABLED .security_changed = security_changed, #endif }; #if defined(CONFIG_BT_LBS_SECURITY_ENABLED) static void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Passkey for %s: %06u\n", addr, passkey); } static void auth_cancel(struct bt_conn *conn) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Pairing cancelled: %s\n", addr); } static void pairing_complete(struct bt_conn *conn, bool bonded) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Pairing completed: %s, bonded: %d\n", addr, bonded); } static void pairing_failed(struct bt_conn *conn, enum bt_security_err reason) { char addr[BT_ADDR_LE_STR_LEN]; bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr)); printk("Pairing failed conn: %s, reason %d\n", addr, reason); } static struct bt_conn_auth_cb conn_auth_callbacks = { .passkey_display = auth_passkey_display, .cancel = auth_cancel, .pairing_complete = pairing_complete, .pairing_failed = pairing_failed }; #else static struct bt_conn_auth_cb conn_auth_callbacks; #endif static void app_led_cb(bool led_state) { //dk_set_led(USER_LED, led_state); printk("\n----------- led changed cb -----------\n"); } static bool app_button_cb(void) { printk("\n----------- button changed cb -----------\n"); return app_button_state; } static struct bt_lbs_cb lbs_callbacs = { .led_cb = app_led_cb, .button_cb = app_button_cb, }; static void button_changed(uint32_t button_state, uint32_t has_changed) { if (has_changed & USER_BUTTON) { uint32_t user_button_state = button_state & USER_BUTTON; bt_lbs_send_button_state(user_button_state); app_button_state = user_button_state ? true : false; } printk("----------- button changed function -----------\n"); } static int init_button(void) { int err; err = dk_buttons_init(button_changed); if (err) { printk("Cannot init buttons (err: %d)\n", err); } return err; } void main(void) { int blink_status = 0; int err; printk("Starting Bluetooth Peripheral LBS example\n"); //err = dk_leds_init(); //if (err) { // printk("LEDs init failed (err %d)\n", err); // return; //} //err = init_button(); //if (err) { // printk("Button init failed (err %d)\n", err); // return; //} bt_conn_cb_register(&conn_callbacks); if (IS_ENABLED(CONFIG_BT_LBS_SECURITY_ENABLED)) { bt_conn_auth_cb_register(&conn_auth_callbacks); } err = bt_enable(NULL); if (err) { printk("Bluetooth init failed (err %d)\n", err); return; } printk("Bluetooth initialized\n"); if (IS_ENABLED(CONFIG_SETTINGS)) { settings_load(); } err = bt_lbs_init(&lbs_callbacs); if (err) { printk("Failed to init LBS (err:%d)\n", err); return; } err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd)); if (err) { printk("Advertising failed to start (err %d)\n", err); return; } printk("Advertising successfully started\n"); int i=0; for (;;) { //dk_set_led(RUN_STATUS_LED, (++blink_status) % 2); k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL)); printk("."); } }
Below code is from arduino
#include <ArduinoBLE.h> // variables for button const int buttonPin = 2; int oldButtonState = LOW; void setup() { Serial.begin(9600); while (!Serial); // configure the button pin as input pinMode(buttonPin, INPUT); Serial.println("Set button as i/p"); // initialize the Bluetooth® Low Energy hardware BLE.begin(); Serial.println("Bluetooth® Low Energy Central - LED control"); Serial.println("--------- Start scanning for particular sercive id --> 19b10000-e8f2-537e-4f6c-d104768a1214"); // start scanning for peripherals // BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); //standard BLE.scanForUuid("00001523-1212-efde-1523-785feabcd123"); // nordic led uuid //00001525, 0x1212, 0xefde, 0x1523, 0x785feabcd123 } void loop() { Serial.println("Checking peripheral is available or not"); // check if a peripheral has been discovered BLEDevice peripheral = BLE.available(); Serial.print("-------- peripheral = "); Serial.print(peripheral); if (peripheral) { // discovered a peripheral, print out address, local name, and advertised service Serial.print("Found and address is "); Serial.println(peripheral.address()); Serial.print(" ', local name is "); Serial.print(peripheral.localName()); Serial.println("' advertise service uuid is "); Serial.print(peripheral.advertisedServiceUuid()); Serial.println(); if (peripheral.localName() != "Nordic_LBS") { Serial.println("------Local name is not matched with 'LED'"); return; } Serial.println("-----------Stop Scanning ---------------"); // stop scanning BLE.stopScan(); Serial.println("------------- Controlled led is calling --------"); controlLed(peripheral); Serial.println("------------- peripheral disconnected, start scanning again for service id --------"); // peripheral disconnected, start scanning again BLE.scanForUuid("00001523-1212-efde-1523-785feabcd123"); } } void controlLed(BLEDevice peripheral) { // connect to the peripheral Serial.println("Connecting ..."); if (peripheral.connect()) { Serial.println("Connected"); } else { Serial.println("Failed to connect!"); return; } // discover peripheral attributes Serial.println("Discovering attributes ..."); if (peripheral.discoverAttributes()) { Serial.println("Attributes discovered"); } else { Serial.println("Attribute discovery failed!"); peripheral.disconnect(); return; } // retrieve the LED characteristic BLECharacteristic ledCharacteristic = peripheral.characteristic("00001523-1212-efde-1523-785feabcd123"); if (!ledCharacteristic) { Serial.println("Peripheral does not have LED characteristic!"); peripheral.disconnect(); return; } else if (!ledCharacteristic.canWrite()) { Serial.println("Peripheral does not have a writable LED characteristic!"); peripheral.disconnect(); return; } while (peripheral.connected()) { // while the peripheral is connected // read the button pin int buttonState = digitalRead(buttonPin); if (oldButtonState != buttonState) { // button changed oldButtonState = buttonState; if (buttonState) { Serial.println("button pressed"); // button is pressed, write 0x01 to turn the LED on ledCharacteristic.writeValue((byte)0x01); // { // // ledCharacteristic.write("hello world"); // // if(ledCharacteristic.available()){ // // Serial.print( ledCharacteristic.read()); // } // // // // } } else { Serial.println("button released"); // button is released, write 0x00 to turn the LED off ledCharacteristic.writeValue((byte)0x00); } } } Serial.println("Peripheral disconnected"); }
Please help me,
Thanks.