Home Wireless
Published in

Home Wireless

Building Bluetooth Services in Zephyr

Part 1: Getting Started

Creating a Bluetooth Service in Zephyr is a wonderfully easy task. I’ve had a high startup cost doing simple things like PWM drivers in Zephyr, but Bluetooth is just great.

Overview

The latest version of my application uses the nrf5x library and C++ code to create a standard procedural application. It has a main() and runs in a single thread. It’s about 25,000 lines of code.

Although it’s very efficient in space and resources the entire application had to be built from the ground up. Using Zephyr gives me lots of useful libraries (like Logging) that I don’t have to build from scratch — and that I don’t have to maintain.

The most important thing, though, is that the nrf5x library from Nordic Semiconductor relies on a Bluetooth library that comes as binary only. There is no source code and it only logs to the Nordic logging subsystem — which is useless. Worst of all, you can’t debug anything when the Bluetooth stack is running because it just hard crashes.

So, although the Nordic nrf5x library and Bluetooth driver seem very robust and reliable they are the world’s worst when it comes to developing. Conversely, the Zephyr Bluetooth library is open source and it has extensive logging that goes to the stock Zephyr logger — so you can watch the Bluetooth logging in real-time.

Segger Debug Terminal Output Showing Bluetooth Diagnostics

One reliable undebuggable core vs one open-source seemingly reliable debug-able threaded operating system. No contest. So, I’ve been moving to Zephyr.

Getting Started with Bluetooth

First, one must correctly set the prj.conf bluetooth defines. Here are mine…

# ----------------------------------
# -- bluetooth stuff
# ----------------------------------
CONFIG_BT=y
CONFIG_BT_DEVICE_NAME="MyBlueDevice"
CONFIG_BT_SMP=y
# --- we're a peripheral
CONFIG_BT_PERIPHERAL=y
# --- connection definitions
CONFIG_BT_MAX_CONN=16
CONFIG_BT_MAX_PAIRED=4
CONFIG_BT_TINYCRYPT_ECC=n
# --- debugging
CONFIG_BT_CTLR_ASSERT_HANDLER=y
CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_DEBUG_CONN=y
CONFIG_BT_DEBUG_ATT=y
CONFIG_BT_DEBUG_GATT=y

It’s very short. Most of the debugging and connection definitions are optional and have reasonable defaults.

Next there’s a simple bluetooth startup routine. This routine mainly sets up the advertising data (which lists all of the services provided by this Bluetooth peripheral) and the advertising parameters (power, iteration cycle, …) and then starts advertising.

Don’t be confused by the length of the file. The many callbacks just print out a diagnostic.

Since gists flow in Medium this ends part 1:

Home automation in the wireless IOT era

Recommended from Medium

Backtesting For Especially FX Markets With Python. Almost The Way It Should Be.

AZ 103 Exam Study guide and Question dumps (Part 1 — Azure Resource Mangement)

People of Golem: meet team R&D

How to use App Designer helper functions as Simulink callback methods.

Windows 10 and Oracle 12C, Not a good match!

Summarized Clean Code Architecture Concept

Some highlights from the Serverless track of AWS Builders Day Manchester

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mark Zachmann

Mark Zachmann

Entrepreneur, software architect, electrical engineer. Ex-academic.

More from Medium

New in February: Annual plan, performance improvements and more

Perl list processing is for hashes, too

Ever wondered how search engines give result of things you search for?

Reduce Your Energy Bills using MongoDB — Part 1 Monitoring Power Usage