The Raspberry Pi 5 is a unbelievable little computing machine. Nevertheless, irrespective of how highly effective the Raspberry Pi 5’s CPU could also be, the Raspberry Pi 5 could be a ineffective lump of silicon and FR4 and not using a technique of interfacing its computational prowess with the true world. Whereas the present Raspberry Pi 5 hype is targeted on its greater pace and array of latest high-speed digital interfaces, there isn’t a comparative highlight on the Raspberry Pi 5’s Bluetooth capabilities. So, hearth up your Raspberry Pi 5 and seize an inexpensive Android cellphone. I’m going to point out you how one can remotely take command of your Raspberry Pi 5’s general-purpose enter/output (GPIO) utilizing that Android cellphone and Bluetooth low vitality.
Set up the lgpio C API
The set up of the lgpio C API permits the programmatical manipulation of the Raspberry Pi 5’s GPIO subsystem (GPIO pins, SPI, I2C, PWM, and many others.). When you intend to make use of a separate private pc to cross compile your Raspberry Pi 5 lgpio-based functions, you will want to repeat the lgpio libraries put in on the Raspberry Pi 5 to a listing on the non-public pc. Set up and use of the lgpio C API is detailed within the lgpio documentation, which may be discovered together with the lgpio obtain bundle on GitHub.
Obtain the btferret/btlib Bluetooth Interface
The btferret/btlib Bluetooth interface is a C bundle that targets the Raspberry Pi household. The btferret/btlib Bluetooth interface file bundle isn’t depending on bluez and is programmed on the HCI (host controller interface) degree. The btferret/btlib Bluetooth interface file bundle consists of btferret.c, btlib.c, btlib.h, and gadgets.txt. A Raspberry Pi 5 btlib utility can act as a Traditional or BLE server or consumer. The btferret.c code is a menu-driven instance utility that runs out of the field on a Raspberry Pi 5 and demonstrates the right way to make the most of the assorted capabilities contained throughout the btlib library. The detailed directions for compiling and operating the btferret code on a Raspberry Pi 5 may be discovered within the btferret/btlib Bluetooth interface documentation, which additionally features a detailed description of the HCI Bluetooth interface. The btferret/btlib Bluetooth interface code and documentation are additionally accessible from GitHub.
Add lgpio API Calls to the btferret Utility
The btlib library capabilities that make up the btferret.c utility code may be extracted individually and modified to create a custom-made Raspberry Pi 5 Bluetooth utility. To maintain issues easy, we is not going to make any adjustments to the unique btferret.c supply code and use it as is. As a substitute, we’ll insert the mandatory lgpio API calls into the unique btferret.c supply code to allow entry to the Raspberry Pi 5 GPIO pins. Our objective is to make use of the marginally modified out-of-the-box btferret code to obtain a command from the Android cellphone over a BLE hyperlink to blink an LED connected to the Raspberry Pi 5, which might be performing as a BLE server.
Earlier than we start work on the btferret.c supply code, we have to outline the gadgets, BLE providers, and traits that might be related to the Raspberry Pi 5 BLE server. The gadgets, providers, and traits are specified by the gadgets.txt file depicted in Determine 1. A full clarification of the gadgets.txt contents may be discovered within the btferret/btlib Bluetooth interface documentation. Our Raspberry Pi 5 BLE server system title is pi5a and has a Bluetooth tackle of D8:3A:DD:98:40:C0. The BLE 18xx providers are predefined Bluetooth providers and are outlined within the Bluetooth specification. The BLE service that begins with 6b5ed4bd is a customized service that we created. Our customized BLE service is related to a customized attribute referred to as gpioCntl. The gpioCntl attribute accommodates 4 bytes of information and may be learn and written by the BLE server and the Android cellphone consumer.
Determine 1: Contents of the gadgets.txt file. FRED EADY
Let’s start our btferret.c modification by including the embody file lgpio.h to the btferret.c supply code as proven in Determine 2. The LED we need to blink is connected to the Raspberry Pi 5’s GPIO 17. As soon as once more, referencing Determine 2, you’ll be able to see that there are two traces of code that configure GPIO 17 as an output and units the GPIO pin’s preliminary logical state.
Determine 2: Configure GPIO 17 utilizing lgpio API calls. FRED EADY
To blink the LED, we should use the consumer BLE utility operating on the Android cellphone to write down to the customized Bluetooth attribute gpioCntl we outlined in gadgets.txt. The BLE server code now we have added that handles the attribute knowledge and blinks the LED is proven in Determine 3. As you’ll be able to see, the LED blink code will solely be executed when the command from the Android cellphone units the primary byte of the gpioCntl knowledge array to 0x55.
Determine 3: Blink the LED utilizing lgpio API calls. FRED EADY
Connecting to the Raspberry Pi 5 BLE Server
Utilizing a BLE check software comparable to LightBlue is the quickest and best solution to set up a connection to the Raspberry Pi 5 BLE server utility. Our btferret Raspberry Pi 5 utility is saved within the /dwelling/fred/pi5_apps listing and is known as bt1. The steps needed to start out the Raspberry Pi 5 BLE server are proven in Determine 4.
Determine 4: Activate the BLE server and course of consumer instructions. FRED EADY
The LightBlue Android app will scan for our promoting Raspberry Pi 5 BLE server and current us the choice to attach as proven in Determine 5. Tapping on the pi5a CONNECT button will reveal the entire BLE server service and attribute knowledge in addition to server system data. A faucet on our customized attribute will take us to a LightBlue panel that can permit us to learn and write to the customized attribute. The results of writing 0x55 to the BLE server may be seen in Determine 4.
Determine 5: LightBlue connection and command sequence. FRED EADY
We are going to need to code a customized Android app after our Raspberry Pi 5 BLE server utility is accomplished. I’ve written a really fundamental customized Android BLE consumer app to enhance our Raspberry Pi 5 BLE server utilizing B4A (Fundamental 4 Android). As B4A is a free software, my app can be utilized as a template to code your personal customized Android consumer app. You’ll be able to get hold of my B4A Android BLE consumer mission from the EDTP Electronics FTP server.