AVNET's iotconnect.io cloud platform is an online service that you can use to send data to, and then show it on a dashboard. In this blog series I'm learning the Python SDK and integrating BLE devices. In this post: talk to the RSL10 sensor kit via its Bluetooth Low Energy interface. Like the previous blog, the technique in this post can be used on other devices. When you use it outside the context of the Avnet portal, you can learn BLE integration from it too. |
The Bluetooth Low Energy Python interface
The Avnet + On Semi example that I'm reviewing uses the bluepy Python BLE library for Linux.
It is a neat library, well maintained and supported by a group of committers.
The interface file that defines its API is btle.py.
The example imports that API:
from bluepy import btle
Before starting the BLE scan process, the example uses Linux command tools to check what BLE interfaces are available on the device:
It then uses the bluepy functionality to find the RSL10 and gets its data.
Instead of replicating the code, I'm showing examples from bluepy.
Scan for devices and get info:
scanner = Scanner().withDelegate(ScanDelegate()) devices = scanner.scan(10.0) for dev in devices: print "Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi) for (adtype, desc, value) in dev.getScanData(): print " %s = %s" % (desc, value)
output (the RSL10 is 60:c0:bf:28:a3:68)
By default, the RSL10 firmware sends Eddystone TLM data, when in Environment Only mode.
elif("aafe" in value and adtype == 22 and 'mybeacaddress' in globals()): # Eddystone TLM packet (Service Data - 16-bit UUID) if (mybeacaddress == dev.addr): print("Eddystone Battery")
You can see the info in the log:
This is parsed by the sample project:
# Filter out the RSL10 beacon from other beacons raw = value.replace("aafe", "") battery = round(float((int(raw[4:6],16)*256 + int(raw[6:8],16)))/1000, 2) # unit Volts os.system('touch /home/avnet/ok') batteryValue = battery
Let's review the battery:
battery = round(float((int(raw[4:6],16)*256 + int(raw[6:8],16)))/1000, 2) # unit Volt
2.95 Volt.
When you put the RSL10 in Acceleration mode (with the custom firmware that it is running, this is done by pushing user button 1 then shaking the device), it sends more info, 128 bytes:
The example code also looks for that signature, and performs different logic:
elif("203606dce9dcb3b7745559262ee12305" in value and adtype == 33): # ON Semi Advertisement packet (Service Data - 128-bit UUID) - MOTION DATA (Red LED state) print("MotionMode") parsed = motion_parse(value) packet = { # ...
This is how the BLE part is implemented in the example.
The process is continuously controlled by a python thread.
There is another thread that will - independently - send the data to
Custom Firmware on RSL10
The firmware loaded on the RSL10 is custom for this evaluation. It is comparable to the out-of-box, but has support for Over The Air firmware upgrade:
The output captures I've posted in the first paragraph are the Environmental Only Mode (default when starting up, green led) and Acceleration Mode (red led) BLE captures.
Top Comments