<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/116075/sending-data-from-lsm6dso-over-bluetooth-using-nrf52832</link><description>Hello, 
 My goal is to combine the Peripheral UART sample code and the LSM6DSO I2C sample code with the objective of sending data from the sensor over bluetooth. Based on my knowledge of C code, I should be able to copy and paste necessary #includes,</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 19 Nov 2024 08:26:35 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/116075/sending-data-from-lsm6dso-over-bluetooth-using-nrf52832" /><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/511015?ContentTypeID=1</link><pubDate>Tue, 19 Nov 2024 08:26:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:13fc94d2-c1d3-42bd-b807-76ed9d21c7f5</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you make your own service, then you can decide which data type each characteristics are. If you use the NUS service, it is up to you to know how to intepret the data. With nus, you can send a number of unsigned 8 bit integers (uint8_t). The data is not processed in any way, so this can be used to send any data, as long as both sides know how to interpret the data (you could for instance use 4 bytes to send a float, or a signed integer, or anything else. Of 8 bytes to senda double, or anything else you need.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/510981?ContentTypeID=1</link><pubDate>Tue, 19 Nov 2024 00:52:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:89adf768-6c9f-4169-956a-047ab6916cd5</guid><dc:creator>connorshannon</dc:creator><description>&lt;p&gt;Hello, this was very helpful, thank you! I am now able to send data over bluetooth using the sensor. I ended up adding the bt_nus_send code to the fetch_and_display loop, which worked.&lt;/p&gt;
&lt;p&gt;A follow up question I have is this: does anyone know what data types can be sent over bluetooth? Obviously, the accelerometer data isn&amp;#39;t best represented as an 8 bit integer, but it appears that when the bt_nus_send function is defined in the code its argument is of this data type. Is that something I can change?&lt;/p&gt;
&lt;p&gt;Also, the sensor that I am using has both acceleration and gyroscope readings on all 3 axes. My plan was to send this data by just calling the bt_nus_send six times in a row, but it feels like there could be a better way to do this. Let me know if anyone out there has a better grasp on the bluetooth capabilities than I do and can help.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/510875?ContentTypeID=1</link><pubDate>Mon, 18 Nov 2024 11:46:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0007ba60-d430-469e-a3f7-65bb8a43db21</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;It is good you got the sensor working in the NUS sample code. To procede, you first need to remove one of the loops, as obviously nothing happens after the first eternal loop. So all continious work in one loop (you can also use separate threads, but in this case that does not seem appropriate).&lt;/p&gt;
&lt;p&gt;When it comes to sending the data over BLE via NUS you can do it the same way as in the example Ther eyou can see that there is a separate thread&amp;nbsp;ble_write_thread, which get data from a FIFO -&amp;nbsp;fifo_uart_rx_data, and send it via&amp;nbsp;bt_nus_send(). You could use this approach, and populate&amp;nbsp;fifo_uart_rx_data with your sensor data instead using&amp;nbsp;k_fifo_put() as is done for UART (remove the UART related code as you don&amp;#39;t need that). A simpler approach would be to simply call&amp;nbsp;bt_nus_send() from the main loop whear you poll the sensor.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/510795?ContentTypeID=1</link><pubDate>Sat, 16 Nov 2024 21:13:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d8525704-3dbb-4dbc-8f64-429ddcfe97b4</guid><dc:creator>connorshannon</dc:creator><description>&lt;p&gt;Alright, I&amp;#39;ve combined the UART and LSM6DSO code as well as updated the overlay and configuration files, and the code builds with no issues. Right now, I&amp;#39;ve inserted the code from the sensor sample into the main of the uart sample such that the infinite loop of the sensor code (test_polling_mode(dev)) occurs right before the infinite loop of the uart code. As such, the code works exactly how the sensor code would normally work, with it outputting the sensor data to the terminal on repeat. My goal is to now send this data over bluetooth each time it is collected from the sensor. I assume that I will need to add code into the loop that is continuously printing sensor data to the console, but I don&amp;#39;t know exactly what to add. I&amp;#39;m hoping that there are people out there who understand the bluetooth code and process more than I do that can help. Any comments or advice would be appreciated, thank you!&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

/** @file
 *  @brief Nordic UART Bridge Service (NUS) sample
 */
#include &amp;lt;uart_async_adapter.h&amp;gt;

#include &amp;lt;zephyr/types.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/drivers/uart.h&amp;gt;
#include &amp;lt;zephyr/usb/usb_device.h&amp;gt;

#include &amp;lt;zephyr/device.h&amp;gt;
#include &amp;lt;zephyr/devicetree.h&amp;gt;
#include &amp;lt;soc.h&amp;gt;

#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/uuid.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/gatt.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/hci.h&amp;gt;

#include &amp;lt;bluetooth/services/nus.h&amp;gt;

#include &amp;lt;dk_buttons_and_leds.h&amp;gt;

#include &amp;lt;zephyr/settings/settings.h&amp;gt;

#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;

#include &amp;lt;zephyr/logging/log.h&amp;gt;

#define LOG_MODULE_NAME peripheral_uart
LOG_MODULE_REGISTER(LOG_MODULE_NAME);

#define STACKSIZE CONFIG_BT_NUS_THREAD_STACK_SIZE
#define PRIORITY 7

#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN	(sizeof(DEVICE_NAME) - 1)

#define RUN_STATUS_LED DK_LED1
#define RUN_LED_BLINK_INTERVAL 1000

#define CON_STATUS_LED DK_LED2

#define KEY_PASSKEY_ACCEPT DK_BTN1_MSK
#define KEY_PASSKEY_REJECT DK_BTN2_MSK

#define UART_BUF_SIZE CONFIG_BT_NUS_UART_BUFFER_SIZE
#define UART_WAIT_FOR_BUF_DELAY K_MSEC(50)
#define UART_WAIT_FOR_RX CONFIG_BT_NUS_UART_RX_WAIT_TIME

//add sensor code below
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/device.h&amp;gt;
#include &amp;lt;zephyr/drivers/sensor.h&amp;gt;

static inline float out_ev(struct sensor_value *val)
{
	return (val-&amp;gt;val1 + (float)val-&amp;gt;val2 / 1000000);
}

static void fetch_and_display(const struct device *dev)
{
	struct sensor_value x, y, z;
	static int trig_cnt;

	trig_cnt++;

	/* lsm6dso accel */
	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_X, &amp;amp;x);
	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Y, &amp;amp;y);
	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Z, &amp;amp;z);

	printf(&amp;quot;accel x:%f ms/2 y:%f ms/2 z:%f ms/2\n&amp;quot;,
			(double)out_ev(&amp;amp;x), (double)out_ev(&amp;amp;y), (double)out_ev(&amp;amp;z));

	/* lsm6dso gyro */
	sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ);
	sensor_channel_get(dev, SENSOR_CHAN_GYRO_X, &amp;amp;x);
	sensor_channel_get(dev, SENSOR_CHAN_GYRO_Y, &amp;amp;y);
	sensor_channel_get(dev, SENSOR_CHAN_GYRO_Z, &amp;amp;z);

	printf(&amp;quot;gyro x:%f rad/s y:%f rad/s z:%f rad/s\n&amp;quot;,
			(double)out_ev(&amp;amp;x), (double)out_ev(&amp;amp;y), (double)out_ev(&amp;amp;z));

	printf(&amp;quot;trig_cnt:%d\n\n&amp;quot;, trig_cnt);
}

static int set_sampling_freq(const struct device *dev)
{
	int ret = 0;
	struct sensor_value odr_attr;

	/* set accel/gyro sampling frequency to 12.5 Hz */
	odr_attr.val1 = 12.5;
	odr_attr.val2 = 0;

	ret = sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ,
			SENSOR_ATTR_SAMPLING_FREQUENCY, &amp;amp;odr_attr);
	if (ret != 0) {
		printf(&amp;quot;Cannot set sampling frequency for accelerometer.\n&amp;quot;);
		return ret;
	}

	ret = sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ,
			SENSOR_ATTR_SAMPLING_FREQUENCY, &amp;amp;odr_attr);
	if (ret != 0) {
		printf(&amp;quot;Cannot set sampling frequency for gyro.\n&amp;quot;);
		return ret;
	}

	return 0;
}

#ifdef CONFIG_LSM6DSO_TRIGGER
static void trigger_handler(const struct device *dev,
			    const struct sensor_trigger *trig)
{
	fetch_and_display(dev);
}

static void test_trigger_mode(const struct device *dev)
{
	struct sensor_trigger trig;

	if (set_sampling_freq(dev) != 0)
		return;

	trig.type = SENSOR_TRIG_DATA_READY;
	trig.chan = SENSOR_CHAN_ACCEL_XYZ;

	if (sensor_trigger_set(dev, &amp;amp;trig, trigger_handler) != 0) {
		printf(&amp;quot;Could not set sensor type and channel\n&amp;quot;);
		return;
	}
}

#else
static void test_polling_mode(const struct device *dev)
{
	if (set_sampling_freq(dev) != 0) {
		return;
	}

	while (1) {
		fetch_and_display(dev);
		k_sleep(K_MSEC(10000));
	}
}
#endif


//add sensor code above



static K_SEM_DEFINE(ble_init_ok, 0, 1);

static struct bt_conn *current_conn;
static struct bt_conn *auth_conn;

static const struct device *uart = DEVICE_DT_GET(DT_CHOSEN(nordic_nus_uart));
static struct k_work_delayable uart_work;

struct uart_data_t {
	void *fifo_reserved;
	uint8_t data[UART_BUF_SIZE];
	uint16_t len;
};

static K_FIFO_DEFINE(fifo_uart_tx_data);
static K_FIFO_DEFINE(fifo_uart_rx_data);

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_NUS_VAL),
};

#ifdef CONFIG_UART_ASYNC_ADAPTER
UART_ASYNC_ADAPTER_INST_DEFINE(async_adapter);
#else
#define async_adapter NULL
#endif

static void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data)
{
	ARG_UNUSED(dev);

	static size_t aborted_len;
	struct uart_data_t *buf;
	static uint8_t *aborted_buf;
	static bool disable_req;

	switch (evt-&amp;gt;type) {
	case UART_TX_DONE:
		LOG_DBG(&amp;quot;UART_TX_DONE&amp;quot;);
		if ((evt-&amp;gt;data.tx.len == 0) ||
		    (!evt-&amp;gt;data.tx.buf)) {
			return;
		}

		if (aborted_buf) {
			buf = CONTAINER_OF(aborted_buf, struct uart_data_t,
					   data[0]);
			aborted_buf = NULL;
			aborted_len = 0;
		} else {
			buf = CONTAINER_OF(evt-&amp;gt;data.tx.buf, struct uart_data_t,
					   data[0]);
		}

		k_free(buf);

		buf = k_fifo_get(&amp;amp;fifo_uart_tx_data, K_NO_WAIT);
		if (!buf) {
			return;
		}

		if (uart_tx(uart, buf-&amp;gt;data, buf-&amp;gt;len, SYS_FOREVER_MS)) {
			LOG_WRN(&amp;quot;Failed to send data over UART&amp;quot;);
		}

		break;

	case UART_RX_RDY:
		LOG_DBG(&amp;quot;UART_RX_RDY&amp;quot;);
		buf = CONTAINER_OF(evt-&amp;gt;data.rx.buf, struct uart_data_t, data[0]);
		buf-&amp;gt;len += evt-&amp;gt;data.rx.len;

		if (disable_req) {
			return;
		}

		if ((evt-&amp;gt;data.rx.buf[buf-&amp;gt;len - 1] == &amp;#39;\n&amp;#39;) ||
		    (evt-&amp;gt;data.rx.buf[buf-&amp;gt;len - 1] == &amp;#39;\r&amp;#39;)) {
			disable_req = true;
			uart_rx_disable(uart);
		}

		break;

	case UART_RX_DISABLED:
		LOG_DBG(&amp;quot;UART_RX_DISABLED&amp;quot;);
		disable_req = false;

		buf = k_malloc(sizeof(*buf));
		if (buf) {
			buf-&amp;gt;len = 0;
		} else {
			LOG_WRN(&amp;quot;Not able to allocate UART receive buffer&amp;quot;);
			k_work_reschedule(&amp;amp;uart_work, UART_WAIT_FOR_BUF_DELAY);
			return;
		}

		uart_rx_enable(uart, buf-&amp;gt;data, sizeof(buf-&amp;gt;data),
			       UART_WAIT_FOR_RX);

		break;

	case UART_RX_BUF_REQUEST:
		LOG_DBG(&amp;quot;UART_RX_BUF_REQUEST&amp;quot;);
		buf = k_malloc(sizeof(*buf));
		if (buf) {
			buf-&amp;gt;len = 0;
			uart_rx_buf_rsp(uart, buf-&amp;gt;data, sizeof(buf-&amp;gt;data));
		} else {
			LOG_WRN(&amp;quot;Not able to allocate UART receive buffer&amp;quot;);
		}

		break;

	case UART_RX_BUF_RELEASED:
		LOG_DBG(&amp;quot;UART_RX_BUF_RELEASED&amp;quot;);
		buf = CONTAINER_OF(evt-&amp;gt;data.rx_buf.buf, struct uart_data_t,
				   data[0]);

		if (buf-&amp;gt;len &amp;gt; 0) {
			k_fifo_put(&amp;amp;fifo_uart_rx_data, buf);
		} else {
			k_free(buf);
		}

		break;

	case UART_TX_ABORTED:
		LOG_DBG(&amp;quot;UART_TX_ABORTED&amp;quot;);
		if (!aborted_buf) {
			aborted_buf = (uint8_t *)evt-&amp;gt;data.tx.buf;
		}

		aborted_len += evt-&amp;gt;data.tx.len;
		buf = CONTAINER_OF((void *)aborted_buf, struct uart_data_t,
				   data);

		uart_tx(uart, &amp;amp;buf-&amp;gt;data[aborted_len],
			buf-&amp;gt;len - aborted_len, SYS_FOREVER_MS);

		break;

	default:
		break;
	}
}

static void uart_work_handler(struct k_work *item)
{
	struct uart_data_t *buf;

	buf = k_malloc(sizeof(*buf));
	if (buf) {
		buf-&amp;gt;len = 0;
	} else {
		LOG_WRN(&amp;quot;Not able to allocate UART receive buffer&amp;quot;);
		k_work_reschedule(&amp;amp;uart_work, UART_WAIT_FOR_BUF_DELAY);
		return;
	}

	uart_rx_enable(uart, buf-&amp;gt;data, sizeof(buf-&amp;gt;data), UART_WAIT_FOR_RX);
}

static bool uart_test_async_api(const struct device *dev)
{
	const struct uart_driver_api *api =
			(const struct uart_driver_api *)dev-&amp;gt;api;

	return (api-&amp;gt;callback_set != NULL);
}

static int uart_init(void)
{
	int err;
	int pos;
	struct uart_data_t *rx;
	struct uart_data_t *tx;

	if (!device_is_ready(uart)) {
		return -ENODEV;
	}

	if (IS_ENABLED(CONFIG_USB_DEVICE_STACK)) {
		err = usb_enable(NULL);
		if (err &amp;amp;&amp;amp; (err != -EALREADY)) {
			LOG_ERR(&amp;quot;Failed to enable USB&amp;quot;);
			return err;
		}
	}

	rx = k_malloc(sizeof(*rx));
	if (rx) {
		rx-&amp;gt;len = 0;
	} else {
		return -ENOMEM;
	}

	k_work_init_delayable(&amp;amp;uart_work, uart_work_handler);


	if (IS_ENABLED(CONFIG_UART_ASYNC_ADAPTER) &amp;amp;&amp;amp; !uart_test_async_api(uart)) {
		/* Implement API adapter */
		uart_async_adapter_init(async_adapter, uart);
		uart = async_adapter;
	}

	err = uart_callback_set(uart, uart_cb, NULL);
	if (err) {
		k_free(rx);
		LOG_ERR(&amp;quot;Cannot initialize UART callback&amp;quot;);
		return err;
	}

	if (IS_ENABLED(CONFIG_UART_LINE_CTRL)) {
		LOG_INF(&amp;quot;Wait for DTR&amp;quot;);
		while (true) {
			uint32_t dtr = 0;

			uart_line_ctrl_get(uart, UART_LINE_CTRL_DTR, &amp;amp;dtr);
			if (dtr) {
				break;
			}
			/* Give CPU resources to low priority threads. */
			k_sleep(K_MSEC(100));
		}
		LOG_INF(&amp;quot;DTR set&amp;quot;);
		err = uart_line_ctrl_set(uart, UART_LINE_CTRL_DCD, 1);
		if (err) {
			LOG_WRN(&amp;quot;Failed to set DCD, ret code %d&amp;quot;, err);
		}
		err = uart_line_ctrl_set(uart, UART_LINE_CTRL_DSR, 1);
		if (err) {
			LOG_WRN(&amp;quot;Failed to set DSR, ret code %d&amp;quot;, err);
		}
	}

	tx = k_malloc(sizeof(*tx));

	if (tx) {
		pos = snprintf(tx-&amp;gt;data, sizeof(tx-&amp;gt;data),
			       &amp;quot;Starting Nordic UART service example\r\n&amp;quot;);

		if ((pos &amp;lt; 0) || (pos &amp;gt;= sizeof(tx-&amp;gt;data))) {
			k_free(rx);
			k_free(tx);
			LOG_ERR(&amp;quot;snprintf returned %d&amp;quot;, pos);
			return -ENOMEM;
		}

		tx-&amp;gt;len = pos;
	} else {
		k_free(rx);
		return -ENOMEM;
	}

	err = uart_tx(uart, tx-&amp;gt;data, tx-&amp;gt;len, SYS_FOREVER_MS);
	if (err) {
		k_free(rx);
		k_free(tx);
		LOG_ERR(&amp;quot;Cannot display welcome message (err: %d)&amp;quot;, err);
		return err;
	}

	err = uart_rx_enable(uart, rx-&amp;gt;data, sizeof(rx-&amp;gt;data), UART_WAIT_FOR_RX);
	if (err) {
		LOG_ERR(&amp;quot;Cannot enable uart reception (err: %d)&amp;quot;, err);
		/* Free the rx buffer only because the tx buffer will be handled in the callback */
		k_free(rx);
	}

	return err;
}

static void connected(struct bt_conn *conn, uint8_t err)
{
	char addr[BT_ADDR_LE_STR_LEN];

	if (err) {
		LOG_ERR(&amp;quot;Connection failed (err %u)&amp;quot;, err);
		return;
	}

	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
	LOG_INF(&amp;quot;Connected %s&amp;quot;, addr);

	current_conn = bt_conn_ref(conn);

	dk_set_led_on(CON_STATUS_LED);
}

static void disconnected(struct bt_conn *conn, uint8_t reason)
{
	char addr[BT_ADDR_LE_STR_LEN];

	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

	LOG_INF(&amp;quot;Disconnected: %s (reason %u)&amp;quot;, addr, reason);

	if (auth_conn) {
		bt_conn_unref(auth_conn);
		auth_conn = NULL;
	}

	if (current_conn) {
		bt_conn_unref(current_conn);
		current_conn = NULL;
		dk_set_led_off(CON_STATUS_LED);
	}
}

#ifdef CONFIG_BT_NUS_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) {
		LOG_INF(&amp;quot;Security changed: %s level %u&amp;quot;, addr, level);
	} else {
		LOG_WRN(&amp;quot;Security failed: %s level %u err %d&amp;quot;, addr,
			level, err);
	}
}
#endif

BT_CONN_CB_DEFINE(conn_callbacks) = {
	.connected    = connected,
	.disconnected = disconnected,
#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
	.security_changed = security_changed,
#endif
};

#if defined(CONFIG_BT_NUS_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));

	LOG_INF(&amp;quot;Passkey for %s: %06u&amp;quot;, addr, passkey);
}

static void auth_passkey_confirm(struct bt_conn *conn, unsigned int passkey)
{
	char addr[BT_ADDR_LE_STR_LEN];

	auth_conn = bt_conn_ref(conn);

	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

	LOG_INF(&amp;quot;Passkey for %s: %06u&amp;quot;, addr, passkey);
	LOG_INF(&amp;quot;Press Button 1 to confirm, Button 2 to reject.&amp;quot;);
}


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));

	LOG_INF(&amp;quot;Pairing cancelled: %s&amp;quot;, 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));

	LOG_INF(&amp;quot;Pairing completed: %s, bonded: %d&amp;quot;, 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));

	LOG_INF(&amp;quot;Pairing failed conn: %s, reason %d&amp;quot;, addr, reason);
}


static struct bt_conn_auth_cb conn_auth_callbacks = {
	.passkey_display = auth_passkey_display,
	.passkey_confirm = auth_passkey_confirm,
	.cancel = auth_cancel,
};

static struct bt_conn_auth_info_cb conn_auth_info_callbacks = {
	.pairing_complete = pairing_complete,
	.pairing_failed = pairing_failed
};
#else
static struct bt_conn_auth_cb conn_auth_callbacks;
static struct bt_conn_auth_info_cb conn_auth_info_callbacks;
#endif

static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data,
			  uint16_t len)
{
	int err;
	char addr[BT_ADDR_LE_STR_LEN] = {0};

	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, ARRAY_SIZE(addr));

	LOG_INF(&amp;quot;Received data from: %s&amp;quot;, addr);

	for (uint16_t pos = 0; pos != len;) {
		struct uart_data_t *tx = k_malloc(sizeof(*tx));

		if (!tx) {
			LOG_WRN(&amp;quot;Not able to allocate UART send data buffer&amp;quot;);
			return;
		}

		/* Keep the last byte of TX buffer for potential LF char. */
		size_t tx_data_size = sizeof(tx-&amp;gt;data) - 1;

		if ((len - pos) &amp;gt; tx_data_size) {
			tx-&amp;gt;len = tx_data_size;
		} else {
			tx-&amp;gt;len = (len - pos);
		}

		memcpy(tx-&amp;gt;data, &amp;amp;data[pos], tx-&amp;gt;len);

		pos += tx-&amp;gt;len;

		/* Append the LF character when the CR character triggered
		 * transmission from the peer.
		 */
		if ((pos == len) &amp;amp;&amp;amp; (data[len - 1] == &amp;#39;\r&amp;#39;)) {
			tx-&amp;gt;data[tx-&amp;gt;len] = &amp;#39;\n&amp;#39;;
			tx-&amp;gt;len++;
		}

		err = uart_tx(uart, tx-&amp;gt;data, tx-&amp;gt;len, SYS_FOREVER_MS);
		if (err) {
			k_fifo_put(&amp;amp;fifo_uart_tx_data, tx);
		}
	}
}

static struct bt_nus_cb nus_cb = {
	.received = bt_receive_cb,
};

void error(void)
{
	dk_set_leds_state(DK_ALL_LEDS_MSK, DK_NO_LEDS_MSK);

	while (true) {
		/* Spin for ever */
		k_sleep(K_MSEC(1000));
	}
}

#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
static void num_comp_reply(bool accept)
{
	if (accept) {
		bt_conn_auth_passkey_confirm(auth_conn);
		LOG_INF(&amp;quot;Numeric Match, conn %p&amp;quot;, (void *)auth_conn);
	} else {
		bt_conn_auth_cancel(auth_conn);
		LOG_INF(&amp;quot;Numeric Reject, conn %p&amp;quot;, (void *)auth_conn);
	}

	bt_conn_unref(auth_conn);
	auth_conn = NULL;
}

void button_changed(uint32_t button_state, uint32_t has_changed)
{
	uint32_t buttons = button_state &amp;amp; has_changed;

	if (auth_conn) {
		if (buttons &amp;amp; KEY_PASSKEY_ACCEPT) {
			num_comp_reply(true);
		}

		if (buttons &amp;amp; KEY_PASSKEY_REJECT) {
			num_comp_reply(false);
		}
	}
}
#endif /* CONFIG_BT_NUS_SECURITY_ENABLED */

static void configure_gpio(void)
{
	int err;

#ifdef CONFIG_BT_NUS_SECURITY_ENABLED
	err = dk_buttons_init(button_changed);
	if (err) {
		LOG_ERR(&amp;quot;Cannot init buttons (err: %d)&amp;quot;, err);
	}
#endif /* CONFIG_BT_NUS_SECURITY_ENABLED */

	err = dk_leds_init();
	if (err) {
		LOG_ERR(&amp;quot;Cannot init LEDs (err: %d)&amp;quot;, err);
	}
}

int main(void)
{
	int blink_status = 0;
	int err = 0;

	configure_gpio();

	err = uart_init();
	if (err) {
		error();
	}

	if (IS_ENABLED(CONFIG_BT_NUS_SECURITY_ENABLED)) {
		err = bt_conn_auth_cb_register(&amp;amp;conn_auth_callbacks);
		if (err) {
			printk(&amp;quot;Failed to register authorization callbacks.\n&amp;quot;);
			return 0;
		}

		err = bt_conn_auth_info_cb_register(&amp;amp;conn_auth_info_callbacks);
		if (err) {
			printk(&amp;quot;Failed to register authorization info callbacks.\n&amp;quot;);
			return 0;
		}
	}

	err = bt_enable(NULL);
	if (err) {
		error();
	}

	LOG_INF(&amp;quot;Bluetooth initialized&amp;quot;);

	k_sem_give(&amp;amp;ble_init_ok);

	if (IS_ENABLED(CONFIG_SETTINGS)) {
		settings_load();
	}

	err = bt_nus_init(&amp;amp;nus_cb);
	if (err) {
		LOG_ERR(&amp;quot;Failed to initialize UART service (err: %d)&amp;quot;, err);
		return 0;
	}

	err = bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), sd,
			      ARRAY_SIZE(sd));
	if (err) {
		LOG_ERR(&amp;quot;Advertising failed to start (err %d)&amp;quot;, err);
		return 0;
	}

	//add sensor code below
	const struct device *const dev = DEVICE_DT_GET_ONE(st_lsm6dso);
	if (!device_is_ready(dev)) {
		printk(&amp;quot;%s: device not ready.\n&amp;quot;, dev-&amp;gt;name);
		return 0;
	}

	#ifdef CONFIG_LSM6DSO_TRIGGER
		printf(&amp;quot;Testing LSM6DSO sensor in trigger mode.\n\n&amp;quot;);
		test_trigger_mode(dev);
	#else
		printf(&amp;quot;Testing LSM6DSO sensor in polling mode.\n\n&amp;quot;);
		test_polling_mode(dev);
	#endif
		return 0;
	//add sensor code above

	for (;;) {
		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
	}
}

void ble_write_thread(void)
{
	/* Don&amp;#39;t go any further until BLE is initialized */
	k_sem_take(&amp;amp;ble_init_ok, K_FOREVER);
	struct uart_data_t nus_data = {
		.len = 0,
	};

	for (;;) {
		/* Wait indefinitely for data to be sent over bluetooth */
		struct uart_data_t *buf = k_fifo_get(&amp;amp;fifo_uart_rx_data,
						     K_FOREVER);

		int plen = MIN(sizeof(nus_data.data) - nus_data.len, buf-&amp;gt;len);
		int loc = 0;

		while (plen &amp;gt; 0) {
			memcpy(&amp;amp;nus_data.data[nus_data.len], &amp;amp;buf-&amp;gt;data[loc], plen);
			nus_data.len += plen;
			loc += plen;

			if (nus_data.len &amp;gt;= sizeof(nus_data.data) ||
			   (nus_data.data[nus_data.len - 1] == &amp;#39;\n&amp;#39;) ||
			   (nus_data.data[nus_data.len - 1] == &amp;#39;\r&amp;#39;)) {
				if (bt_nus_send(NULL, nus_data.data, nus_data.len)) {
					LOG_WRN(&amp;quot;Failed to send data over BLE connection&amp;quot;);
				}
				nus_data.len = 0;
			}

			plen = MIN(sizeof(nus_data.data), buf-&amp;gt;len - loc);
		}

		k_free(buf);
	}
}

K_THREAD_DEFINE(ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL,
		NULL, PRIORITY, 0, 0);
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/510771?ContentTypeID=1</link><pubDate>Fri, 15 Nov 2024 19:24:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:501ba3c7-2e69-44c1-99bd-38d31ce1888c</guid><dc:creator>connorshannon</dc:creator><description>&lt;p&gt;Okay, I&amp;#39;ll spend some time playing around with the code and trying to combine the peripheral uart sample with the lsm6dso sample. I&amp;#39;m sure I&amp;#39;ll have some issues, so if I get stuck I&amp;#39;ll share my code here again. If I happen to get it working, I&amp;#39;ll also try to share my code in case anyone needs to do something similar.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/510616?ContentTypeID=1</link><pubDate>Fri, 15 Nov 2024 08:08:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d7a7f323-4c5c-4aa5-900d-72243d71a4c9</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;I see. It is good to know you gout the&amp;nbsp;LSM6DSO. I would argue that the simplest way to transmit arbitrary data over BLE is using the Nordic UART Service (NUS), as demonstrated in the &lt;a href="https://docs.nordicsemi.com/bundle/ncs-latest/page/nrf/samples/bluetooth/peripheral_uart/README.html"&gt;Bluetooth Peripheral UART sample&lt;/a&gt;. In principle you can just remove the code related to the actual UART, and send data you get from elsewhere instead (like the&amp;nbsp;LSM6DSO). However, in some cases using a more&amp;nbsp;specific service (standard or custom) may be more suitable in some cases, but that is up to you to decide. If you just want somethign that works early on, I would start with NUS.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/510574?ContentTypeID=1</link><pubDate>Thu, 14 Nov 2024 21:43:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:93910c50-061b-4238-99d8-b42737d96793</guid><dc:creator>connorshannon</dc:creator><description>&lt;p&gt;Thank you for the response. I haven&amp;#39;t made any progress yet, as I only recently got the LSM6DSO working. In fact, the thread you linked is actually mine. My next step is to transmit data from the LSM6DSO over bluetooth, which is what I was hoping to get figured out in this thread. To give you more information, I will eventually be manufacturing a custom circuit board using the NRF52832 chip that I will connect to the sensor and use to transmit data over bluetooth.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/510530?ContentTypeID=1</link><pubDate>Thu, 14 Nov 2024 14:49:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f5f8e24-b829-4309-add3-096fbeb422de</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Øyvind is out of office. Did you make any progress on this? Regarding starting point, it depends on what you need. An alternative would be to start with the Bluetooht peripheral_uart sample that you have seen before. Using the&amp;nbsp;LSM6DSO is discussed in&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/116042/using-lsm6dso-sensor-on-nrf52832/509011"&gt; this thread&lt;/a&gt;, which you may find usefull.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/509339?ContentTypeID=1</link><pubDate>Thu, 07 Nov 2024 02:23:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b2dad713-59b6-47cd-9977-0cac268af600</guid><dc:creator>connorshannon</dc:creator><description>&lt;p&gt;Actually as I spent some time reviewing I found the code used in lesson 4, exercise 2 of the BLE fundamentals course. This code seems like it might be a better place to start than the other two codes, so I&amp;#39;m planning on looking into that a little bit. Do you have any feedback on which of these samples to start from?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/*
 * Copyright (c) 2023 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/gap.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/uuid.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/conn.h&amp;gt;
#include &amp;lt;dk_buttons_and_leds.h&amp;gt;
#include &amp;quot;my_lbs.h&amp;quot;

static struct bt_le_adv_param *adv_param = BT_LE_ADV_PARAM(
	(BT_LE_ADV_OPT_CONNECTABLE |
	 BT_LE_ADV_OPT_USE_IDENTITY), /* Connectable advertising and use identity address */
	800, /* Min Advertising Interval 500ms (800*0.625ms) */
	801, /* Max Advertising Interval 500.625ms (801*0.625ms) */
	NULL); /* Set to NULL for undirected advertising */

LOG_MODULE_REGISTER(Lesson4_Exercise2, LOG_LEVEL_INF);

#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 USER_LED DK_LED3
#define USER_BUTTON DK_BTN1_MSK

#define STACKSIZE 1024
#define PRIORITY 7

#define RUN_LED_BLINK_INTERVAL 1000
/* STEP 17 - Define the interval at which you want to send data at */
#define NOTIFY_INTERVAL 500
static bool app_button_state;
/* STEP 15 - Define the data you want to stream over Bluetooth LE */
static uint32_t app_sensor_value = 100;

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),
};

/* STEP 16 - Define a function to simulate the data */
static void simulate_data(void)
{
	app_sensor_value++;
	if (app_sensor_value == 200) {
		app_sensor_value = 100;
	}
}
static void app_led_cb(bool led_state)
{
	dk_set_led(USER_LED, led_state);
}

static bool app_button_cb(void)
{
	return app_button_state;
}

/* STEP 18.1 - Define the thread function  */
void send_data_thread(void)
{
	while (1) {
		/* Simulate data */
		simulate_data();
		/* Send notification, the function sends notifications only if a client is subscribed */
		my_lbs_send_sensor_notify(app_sensor_value);

		k_sleep(K_MSEC(NOTIFY_INTERVAL));
	}
}

static struct my_lbs_cb app_callbacks = {
	.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 &amp;amp; USER_BUTTON) {
		uint32_t user_button_state = button_state &amp;amp; USER_BUTTON;
		/* STEP 6 - Send indication on a button press */
		my_lbs_send_button_state_indicate(user_button_state);
		app_button_state = user_button_state ? true : false;
	}
}
static void on_connected(struct bt_conn *conn, uint8_t err)
{
	if (err) {
		printk(&amp;quot;Connection failed (err %u)\n&amp;quot;, err);
		return;
	}

	printk(&amp;quot;Connected\n&amp;quot;);

	dk_set_led_on(CON_STATUS_LED);
}

static void on_disconnected(struct bt_conn *conn, uint8_t reason)
{
	printk(&amp;quot;Disconnected (reason %u)\n&amp;quot;, reason);

	dk_set_led_off(CON_STATUS_LED);
}

struct bt_conn_cb connection_callbacks = {
	.connected = on_connected,
	.disconnected = on_disconnected,
};

static int init_button(void)
{
	int err;

	err = dk_buttons_init(button_changed);
	if (err) {
		printk(&amp;quot;Cannot init buttons (err: %d)\n&amp;quot;, err);
	}

	return err;
}

int main(void)
{
	int blink_status = 0;
	int err;

	LOG_INF(&amp;quot;Starting Lesson 4 - Exercise 2 \n&amp;quot;);

	err = dk_leds_init();
	if (err) {
		LOG_ERR(&amp;quot;LEDs init failed (err %d)\n&amp;quot;, err);
		return -1;
	}

	err = init_button();
	if (err) {
		printk(&amp;quot;Button init failed (err %d)\n&amp;quot;, err);
		return -1;
	}

	err = bt_enable(NULL);
	if (err) {
		LOG_ERR(&amp;quot;Bluetooth init failed (err %d)\n&amp;quot;, err);
		return -1;
	}
	bt_conn_cb_register(&amp;amp;connection_callbacks);

	err = my_lbs_init(&amp;amp;app_callbacks);
	if (err) {
		printk(&amp;quot;Failed to init LBS (err:%d)\n&amp;quot;, err);
		return -1;
	}
	LOG_INF(&amp;quot;Bluetooth initialized\n&amp;quot;);
	err = bt_le_adv_start(adv_param, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
	if (err) {
		LOG_ERR(&amp;quot;Advertising failed to start (err %d)\n&amp;quot;, err);
		return -1;
	}

	LOG_INF(&amp;quot;Advertising successfully started\n&amp;quot;);
	for (;;) {
		dk_set_led(RUN_STATUS_LED, (++blink_status) % 2);
		k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
	}
}
/* STEP 18.2 - Define and initialize a thread to send data periodically */
K_THREAD_DEFINE(send_data_thread_id, STACKSIZE, send_data_thread, NULL, NULL, NULL, PRIORITY, 0, 0);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/509308?ContentTypeID=1</link><pubDate>Wed, 06 Nov 2024 16:37:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d33a1123-0c19-467f-943f-7e1d1d06bda0</guid><dc:creator>connorshannon</dc:creator><description>&lt;p&gt;Hello&amp;nbsp;&lt;span&gt;&amp;Oslash;yvind,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you for the quick response. I actually had already&amp;nbsp;gone through all of those. I&amp;#39;ll spend some more time reviewing, but I didn&amp;#39;t feel ready to fully solve this problem based on what I learned.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Sending data from LSM6DSO over bluetooth using nRF52832</title><link>https://devzone.nordicsemi.com/thread/509016?ContentTypeID=1</link><pubDate>Mon, 04 Nov 2024 20:24:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ec796e6e-1ab5-4643-9f44-495435c6073e</guid><dc:creator>&amp;#216;yvind</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
[quote user=""]Also, I&amp;#39;m very new to nordic devices and software, so my apologies my questions are ignorant. Please let me know if there&amp;#39;s any more information I can share, and thank you for the help![/quote]
&lt;p&gt;As you are new to our devices, we &lt;a href="https://academy.nordicsemi.com/"&gt;recommend starting with our Developer Academy&lt;/a&gt;. From here you can start with the &lt;a href="https://academy.nordicsemi.com/courses/nrf-connect-sdk-fundamentals/"&gt;fundamentals of nRF Connect SDK&lt;/a&gt;, then move on to either&amp;nbsp;&lt;a href="https://academy.nordicsemi.com/courses/nrf-connect-sdk-intermediate"&gt;nRF Connect SDK intermediate&lt;/a&gt;&amp;nbsp;or the &lt;a href="https://academy.nordicsemi.com/courses/bluetooth-low-energy-fundamentals/"&gt;Bluetooth Low Energy fundamentals course&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;This will help getting to know the most important aspects of your development and how to add custom sensors and such.&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;br /&gt;Øyvind&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>