⬆️ ⬇️

Using GATT in Bluetooth LE on Intel Edison

Intel Edison is able to become the brain of any device from the Internet of Things Army. The brain is able to process information, but in order to receive it, it needs sense organs. For example, how can Edison find out if it’s hot or cold in a room right now? The answer is simple - using a temperature sensor.



From this material, you will learn how to use the Generic Attribute Profile (GATT) profile when organizing Intel Edison to interact with Texas Instruments SensorTag via the Bluetooth Low Energy protocol.



Before we start



In order for you to be able to use this guide in practice, you will need an assembled Intel Edison board, the firmware of which has been updated to the latest version. A connection should be established between the board and the computer, which emulates a serial connection (serial) port, and the device must also have access to the Internet via Wi-Fi. If you need help preparing your board, refer to the Intel Edison Getting Started Guides .



Install gatttool



Texas Instruments SensorTag, like many other Bluetooth devices, supports GATT for communicating with computers, smartphones, tablets. Gatttool is a standard set of software tools that is included with the BlueZ package. It is, however, not installed by default on Intel Edison. In order to fix this, you need to connect to Intel Edison through a terminal emulator, and then download and compile BlueZ using the following commands:

')

root@edison: cd ~ root@edison: wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.24.tar.xz –no-check-certificate root@edison: tar -xf bluez-5.24.tar.xz root@edison: cd bluez-5.24 root@edison: ./configure --disable-systemd –disable-udev root@edison: make root@edison: make install 


In order for gatttool to be run from anywhere, modify the PATH variable:



 root@edison: export PATH=$PATH:~/bluez-5.24/attrib/ 


Scanning the air and detecting BLE-devices using bluetoothctl



1. Turn on Bluetooth on Intel Edison.



 root@edison: rfkill unblock bluetooth 


2. Run the bluetoothctl



 root@edison: bluetoothctl 


3. We register the agent and set it to the default state.



 [bluetooth]# agent KeyboardDisplay [bluetooth]# default-agent [bluetooth]# scan on 


4. If SensorTag cannot be found, click the pairing button with other devices on it. After finding the MAC address of the SensorTag, you can turn off the scan mode and exit the bluetoothctl session.



 [bluetooth]# scan off [bluetooth]# quit 




The dedicated MAC address of the SensorTag found during a search for Bluetooth devices.



Use gatttool to read sensor readings



Now we can use gatttool to read the sensor with SensorTag.



1. Run gatttool online using the MAC address found in the previous step.



 root@edison: gatttool -b 34:B1:F7:D5:15:38 –I 


2. Connect to the device and turn on the temperature sensor by writing 01 to the settings identifier (configure handle) 0x29. After that, read the temperature readings from the identifier 0x25.



 [34:B1:F7:D5:15:38][LE]> connect [34:B1:F7:D5:15:38][LE]> char-write-cmd 0x29 01 [34:B1:F7:D5:15:38][LE]> char-read-hnd 0x25 


The identifier code required to access the temperature sensor is taken from the SensorTag attribute table .





Sensor reads from SensorTag



The temperature sensor readings look like two 16-bit unsigned numbers. In order to convert this data to the usual form, they must be processed using a script that implements the conversion algorithm described in the knowledge base on SensorTag .



Using Python to interpret sensor readings



1. In order to interpret sensor readings, you can use a script written in the Python programming language and using the pexpect module. As an example, take this script and apply it to read temperature readings with SensorTag.



 root@edison: wget https://github.com/msaunby/ble-sensor- pi/archive/master.zip -–no-check-certificate root@edison: unzip master.zip root@edison: cd /ble-sensor-pi-master/sensortag 


2. Open the sensortag_test.py script with vi.



 root@edison: vi ./sensortag_test.py 


3. We make changes to line 62. Namely, this code:



 tool.expect('\[CON\].*>') 


replace it with this:



 tool.expect('Connection successful') 


4. After making the changes, save them and exit by pressing Escape and entering the following command:



 :wq 


Installing pip and necessary Python modules



To run a python script using pexpect, the latter must be installed. The easiest way to do this is with Pip. Pip is not installed on Intel Edison by default, it is not in the official opkg repository. However, Pip can be found in the unofficial Intel Edison repository compiled by Michael Hirsch. The following instructions are based on his guide on using the informal repository.



1. Edit the base-feeds.conf file with vi.



 root@edison: vi /etc/opkg/base-feeds.conf 


2. We introduce the following:



 src/gz all http://repo.opkg.net/edison/repo/all src/gz edison http://repo.opkg.net/edison/repo/edison src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32<source> 3.  Escape  : <source>:wq 


4. Update opkg and install Python.



 root@edison: opkg update root@edison: opkg install python-pip 


Install the Pip setup tools (setup tools):



 root@edison: wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -–no-check-certificate -O - | python> 


Run a python script and read temperature readings



Now everything is ready to run a script that reads the temperature sensor readings. To do this, go to the directory / ble-sensor-pi-master / sensortag and execute the following command:



 root@edison: ./sensortag_test.py 34:B1:F7:D5:15:38 




Reading temperature sensor readings using a python script



Results



Now you know how Intel Edison can receive and process temperature data from an external sensor connected via Bluetooth LE. A similar principle can be used, firstly, in order to connect other Bluetooth devices to Edison, and secondly, to collect readings from other TI SensorTag sensors. Successful experiments!

Source: https://habr.com/ru/post/267763/



All Articles