Bluetooth low energy raspberry pi

Bluetooth LE on the Raspberry Pi

The Raspberry Pi 3 has built-in support for Bluetooth Low Energy (BLE). BLE is the technology behind many fitness trackers and smartwatches, and allows small, low-power devices to transmit and receive information from a central computer or smartphone.

This tutorial gets you started with Bluetooth Low Energy using a Texas Instruments SensorTag — these are small, robust, relatively cheap and do interesting things out of the box. You can buy one directly from TI, or in the UK they are available from RS and Farnell.

Pre-requisites

Compatible Hardware

This page was written assuming the following hardware:

  • Raspberry Pi 3 Model B
  • Raspbian Jessie (April 2017), although later versions will probably work.
  • TI CC2650 SensorTag, firmware 1.30 (May 2016) or later

We’ll also assume you are familiar with the basics of typing Bash commands using the command line.

Older models of the Pi should work with an external USB adapter. The author has used these two successfully:

Basic installation checks

The Pi 3’s built-in Bluetooth adapter is called hci0. You can check it is operating correctly with the command:

This should show something like this:

hci0: Type: BR/EDR Bus: UART BD Address: B8:27:EB:23:E2:A4 ACL MTU: 1021:8 SCO MTU: 64:1 UP RUNNING RX bytes:1987 acl:0 sco:0 events:91 errors:0 TX bytes:1647 acl:0 sco:0 commands:57 errors:0

If for some reason it is shown as DOWN you can re-enable it with:

Using the SensorTag

Scanning using hcitool

SensorTag with outer case removed

If you’ve not already done so, take a look at TI’s Getting Started Guide which explains how to set up the SensorTag.

To wake it up, press the button on the left-hand side (see illustration) and the green LED should flash, once per second.

On the Pi, enter the command:

This will start a scan for any Bluetooth LE devices in range which are currently ‘advertising’. You should shortly see something like this:

pi@raspberrypi:~ $ sudo hcitool lescan LE Scan . B0:B4:48:ED:44:C3 (unknown) B0:B4:48:ED:44:C3 CC2650 SensorTag

Type Control-C to stop lescan. The 12 hexadecimal digits (B0:B4:48:ED:44:C3 in the example) are your tag’s MAC address. You will need to know this when making Bluetooth LE connections to it.

Connecting with gatttool

The gatttool utility can make connections to Bluetooth LE devices. To try this, make sure the SensorTag’s LED is flashing then run:

gatttool -I -b MAC-address 

where MAC-address is the value reported by hcitool (see above). gatttool will give you a prompt, ending [LE]>. Type connect to make a connection to the SensorTag. This is shown below:

pi@raspberrypi:~ $ gatttool -I -b B0:B4:48:ED:44:C3 [B0:B4:48:ED:44:C3][LE]> connect Attempting to connect to B0:B4:48:ED:44:C3 Connection successful [B0:B4:48:ED:44:C3][LE]>

You can then type a number of commands. The primary command lists the available ‘Services’, which are groups containing ‘Characteristics’ — these are data items which can be read or written to the device. So, to read the device name you could use the char-read-uuid command, giving it the ID of the Bluetooth Device Name characteristic:

[B0:B4:48:ED:44:C3][LE]> char-read-uuid 00002a00-0000-1000-8000-00805f9b34fb handle: 0x0003 value: 53 65 6e 73 6f 72 54 61 67 20 32 2e 30

(These are the ASCII values for SensorTag 2.0.

Читайте также:  Выбрать кодек bluetooth наушников windows 10

The Sensortag User Guide at http://processors.wiki.ti.com/index.php/CC2650_SensorTag_User’s_Guide lists all the available services and characteristics. For example, to turn on the optical sensor, we need to discover the ‘handle’ for the configuration characteristic (with ID f000aa72-0451-4000-b000-000000000000), then write an 0x01 byte to it:

[B0:B4:48:ED:44:C3][LE]> characteristics 1 ffff f000aa72-0451-4000-b000-000000000000 handle: 0x0046, char properties: 0x0a, char value handle: 0x0047, uuid: f000aa72-0451-4000-b000-000000000000 [B0:B4:48:ED:44:C3][LE]> char-write-req 47 01 Characteristic value was written successfully

In the example above the handle value was 0x47 (it may change with firmware versions). One the sensor is enabled, the light level can then be read from characteristic f000aa71-0451-4000-b000-000000000000:

[B0:B4:48:BF:C9:83][LE]> char-read-uuid f000aa71-0451-4000-b000-000000000000 handle: 0x0044 value: bc 0a

In this example the light level bytes are bc 0a, and will change with the amount of light falling on the CC2650 device.

Using Bluetooth LE with Python

Installing bluepy

Using gatttool is very laborious for doing any useful work, so it’s a good idea to use a programming language. The bluepy package is one way to use Bluetooth LE commands from Python — it can be installed from https://pypi.python.org/pypi using the pip command. If you don’t already have pip, on the Pi or other Debian systems it is easily installed with:

sudo apt-get install python-pip

Before installing bluepy you will also need some support libraries:

sudo apt-get install libglib2.0-dev

If successful, it will show a message such as this:

Installing blescan script to /usr/local/bin Installing sensortag script to /usr/local/bin Successfully installed bluepy Cleaning up.

bluepy command-line programs

The blescan program performs a similar function to «hcitool lescan» but produces more information. You will need root privileges (using e.g. sudo) to run it. Typical output looks like this:

pi@raspberrypi:~ $ sudo blescan Scanning for devices. Device (new): b0:b4:48:ed:44:c3 (public), -68 dBm Flags: Incomplete 16b Services: Complete Local Name: ‘CC2650 SensorTag’ Tx Power: 0x12: Manufacturer:

Читайте также:  Kia rio x line подключить блютуз

You can alter its behaviour with various command-line options; blescan -h gives help text.

The sensortag program connects to various versions of SensorTag, and reads their sensors. sensortag -h gives a full list of options; a typical run looks like this:

pi@raspberrypi:~ $ sensortag -T -B B0:B4:48:ED:44:C3 Connecting to B0:B4:48:ED:44:C3 ('Temp: ', (26.84375, 21.3125)) ('Barometer: ', (27.15, 1017.46)) ('Temp: ', (26.875, 21.5625)) ('Barometer: ', (27.15, 1017.48)) .

Use Ctrl-C to stop the program.

Simple bluepy programming

The following simple example shows how to connect to a device and display its services:

from bluepy import btle print "Connecting. " dev = btle.Peripheral("B0:B4:48:BF:C9:83") print "Services. " for svc in dev.services: print str(svc)

For a Sensortag, ensure the green LED is flashing before trying to connect. To connect to the SensorTag’s «light level» service, and list the characteristics, you could add this:

lightSensor = btle.UUID("f000aa70-0451-4000-b000-000000000000") lightService = dev.getServiceByUUID(lightSensor) for ch in lightService.getCharacteristics(): print str(ch)

To initialize and read from the light sensor you might first add:

import time import binascii

to the top of the program, then add the following lines:

uuidConfig = btle.UUID("f000aa72-0451-4000-b000-000000000000") lightSensorConfig = lightService.getCharacteristics(uuidConfig)[0] # Enable the sensor lightSensorConfig.write(bytes("\x01")) time.sleep(1.0) # Allow sensor to stabilise uuidValue = btle.UUID("f000aa71-0451-4000-b000-000000000000") lightSensorValue = lightService.getCharacteristics(uuidValue)[0] # Read the sensor val = lightSensorValue.read() print "Light sensor raw value", binascii.b2a_hex(val)

Once you have set lightSensorConfig and lightSensorValue (these are Bluepy Characteristic objects), you can simply use their write() and read() methods repeatedly. (There is no need to call getCharacteristics() each time).

Using Bluetooth LE with Go (Golang)

Gatt is a Go package, which provides developers to create BLE applications for Linux and OS X.

Developers install Go language on the host machine, and cross-compile the applications for RPi.

The package accesses HCI devices directly via HCI sockets provided by BlueZ core (kernel space), so it doesn’t require the BlueZ userland package.

To test the example programs (sample GATT server and clients):

Cross-compile the server example for an ARMv6 target device.

GOARCH=arm GOARM=6 GOOS=linux go build examples/server.go cp server

Start the server on the target device

Cross-compile the client example (discoverer) for an ARMv6 target device.

GOARCH=arm GOARM=6 GOOS=linux go build examples/discoverer.go cp discoverer

Run the discoverer to scan surrounding peripheral devices.

Источник

Quickstart — Raspberry Pi RP2040 with BLE and CircuitPython

You will be redirected back to this guide once you sign in, and can then subscribe to this guide.

micropython___circuitpython_Bluetooth_Logo.png

The nRF52840 uses Bluetooth Low Energy, or BLE. BLE is a wireless communication protocol used by many devices, including mobile devices. You’ll be able to communicate with your nRF52840 board using your mobile phone!

Читайте также:  Где ноутбуке включить блютуз

There’s a few terms and concepts commonly used in BLE with which you may want to familiarise yourself. This will help you understand what your code is doing when you’re using CircuitPython and BLE.

The major concepts can be broken down into two categories: connection set up and communication. The first deals with setting up connections between devices, such as between your mobile phone and the nRF52840 board. The second deals with communication between the devices once they are connected.

  • Central — The host computer. This is often a mobile device such as a phone or tablet, or it could be a desktop or laptop.
  • Peripheral — The connected device. Examples of peripherals are: heart rate monitor, smart watch, or fitness tracker. The CircuitPython code we have so far is designed to make the Adafruit nRF52840 devices work as peripherals.
  • Advertising — Information sent by the peripheral during connection set up. When a device advertises, it is transmitting the name of the device and describing its capabilities. The central looks for an advertising peripheral to connect to, and uses that information to determine what the peripheral is capable of.
  • Service — A function the peripheral provides. The peripheral advertises its services. A really common service that we use is the UART service, which acts like a hardware UART and is a way of bidirectionally sending information to and from devices.
  • Packet — Data transmitted by a device. BLE devices and host computers transmit and receive data in small bursts called packets.

To use these terms in the context of connecting to your Adafruit nRF52840:

  • You run CircuitPython code that makes your board act as a peripheral by advertising its name and the services it’s capable of.
  • You start up Adafruit’s Bluefruit LE Connect app on an Android or iOS device in central mode, that device becomes the central, and begins listening for the peripheral.
  • You set up the connection between the nRF52840 peripheral and the Bluefruit LE Connect app, and the app discovers the details about the services that the peripheral is capable of.
  • Once this connection is made, you can use CircuitPython code to read packets sent from the Bluefruit LE Connect app to your nRF52840 board. For example, you can receive data describing screen button presses or RGB color values.

Now that you have a general idea of basic BLE terms and concepts, it’s time to install the Bluefruit LE Connect application, and run some CircuitPython demos!

This guide was first published on Mar 26, 2021. It was last updated on Mar 31, 2021.

This page (Bluetooth Low Energy Basics) was last updated on Jan 31, 2019.

Источник

Оцените статью
Adblock
detector