August 16th, 2024

Building a GATT Server on Pi Pico W

The guide instructs on building a Bluetooth GATT server on the Raspberry Pi Pico W, covering Bluetooth architecture, GATT server-client interactions, and creating a GATT database for effective communication.

Read original articleLink Icon
Building a GATT Server on Pi Pico W

This webpage serves as a guide for building a Bluetooth GATT server on the Raspberry Pi Pico W, primarily aimed at students in ECE 4760 at Cornell University and hobbyists interested in Bluetooth integration. It emphasizes the implementation of a GATT server while providing a brief overview of the Bluetooth architecture, particularly focusing on the BTstack library used for Bluetooth communication. The document outlines the structure of the Bluetooth stack, detailing the roles of various layers including the physical layer, link layer, and host-controller interface. It explains how the GATT server interacts with GATT clients, highlighting the asymmetry in their roles where the server responds to client requests without initiating actions. The guide also covers the creation of a GATT database, which organizes data into attributes, allowing clients to query the server effectively. Key components discussed include building an attribute database, adding services and characteristics, and configuring the server for communication. The document aims to provide a foundational understanding of GATT server implementation, preparing readers for further exploration of GATT clients.

- The guide is intended for students and hobbyists interested in Bluetooth GATT server implementation on the Raspberry Pi Pico W.

- It provides an overview of the Bluetooth stack and the roles of different layers in communication.

- The GATT server responds to client requests, emphasizing the asymmetrical relationship between clients and servers.

- The document details the creation of a GATT database and the organization of data into attributes.

- It serves as a prerequisite for understanding GATT client development.

Link Icon 6 comments
By @evanjrowley - 8 months
I'm trying to understand what GATT is.

From what I've found in the links below, it looks Bluetooth 5.4 spec contains an Attribute Protocol (ATT) that allows for sharing of custom attributes between bluetooth devices. Attributes have a type identified by UUID, a server-specific 16-bit handle, a higher-level handle group, a value, and then permissions. Permissions specify read/write capability and requirements for encryption, authentication, and authorization. Generic Attribute Profile (GATT) is a service discovery/management framework built around ATT.

While wondering about what applications this is intended for, I found an example in Figure 2.2 the GATT spec (2nd link) showing a computer communicating with a sensor over bluetooth, so presumably, this is ATT and GATT are meant to improve IoT networks utilizing bluetooth. Appendixes A and B show examples of the protocol containing data for battery status, temperature sensors, and glucose monitors.

Is this the current state of the art for this type of information over Bluetooth, or is there something else that is currently being used?

I'd like to see a Pi Pico W bluetooth host for my custom split keyboards, and it obtaining from the the battery status to display on a small LCD/OLED display. The Nordic nRF52-based keyboards I have are already capable of reporting that type of information over bluetooth, so is that an example of GATT in the wild? Is GATT what's currently enabling my Apple Magic Trackpad and Pixel Buds Pro to report their battery status over bluetooth, or will GATT be the next generation of that capability?

Found in the Bluetooth 5.4 spec:

Part G. Generic Attribute Profile (GATT): https://www.bluetooth.com/wp-content/uploads/Files/Specifica...

Part F. Attribute Protocol (ATT): https://www.bluetooth.com/wp-content/uploads/Files/Specifica...

By @MuffinFlavored - 8 months
By @briandw - 8 months
GATT is great if you want to make yet another heart rate monitor. I was building a lock that worked over BTLE and I spent most of my time trying to get around the problems with GATT. It's been 8 years now but I believe that that there was a bunch of extra overhead that bloated the packets and the packets were too small. Resulted in too much back and forth etc. I really just wanted a socket. iOS eventually allowed you to interact directly with the Logical Link Control and Adaptation Protocol aka L2CAP. Seems like every time someone try to design a generic structure that does one job but is also extendable, it's a mess. Just give me the byte stream and move on. L2CAP doesn't seem to be accessible on the pico yet, https://github.com/micropython/micropython-lib/issues/803
By @trescenzi - 8 months
It’s not a full generic GATT server but I implemented a server for temperature sensors with MicroPython if anyone is curious what that looks like. I have it running on two picos to collect data across my house and then I push it to a MQTT server. Honestly would love feedback too because it’s pretty gross and error prone.

https://git.tcrez.dev/tcrez/micropython/src/branch/main/temp...

By @clumsysmurf - 8 months
When I last look at the description of the Pi Pico W from adafruit

https://www.adafruit.com/product/5544

it says:

"Bluetooth Low Energy - note this isn't supported in software yet, its just a hardware capability."

Yet this blog discusses BTStack so it must work at some level ... Can anyone clarify: if you want to use BLE on this device, is BTStack the only option or is this outdated info?