IanH
Posts: 67
Joined: Mon Dec 19, 2011 5:53 pm

Bluetooth LE and TI SensorTag from Python

Sat Oct 19, 2013 9:05 am

Hi folks,

I've made a beta-quality release of some code which allows you to talk to Bluetooth Low Energy devices from Python. This includes example code to read values from a TI SensorTag (which is, amongst other things, virtually an all-in-one wireless weather station, for about £20).

The GitHub page is at: https://github.com/IanHarvey/bluepy - if you have any trouble getting it to work, please let me know.
https://github.com/IanHarvey

transfinite
Posts: 5
Joined: Thu Apr 11, 2013 7:33 pm

Re: Bluetooth LE and TI SensorTag from Python

Tue Oct 22, 2013 8:11 pm

How do you talk to the SensorTag from the Pi? Is there a particular bluetooth device required?

-transfinite

IanH
Posts: 67
Joined: Mon Dec 19, 2011 5:53 pm

Re: Bluetooth LE and TI SensorTag from Python

Tue Oct 22, 2013 9:35 pm

Good point - you'll need a Bluetooth 4.0 compatible USB adapter. I'm using a Plugable one - http://www.amazon.co.uk/gp/product/B009ZIILLI . This uses a Broadcom BCM20702 chipset and identifies itself on USB as VID/PID 0x0A5C/0x21E8.

I'm using Linux kernel v3.6.11+ dated 13/June/2013; the Bluez code I've borrowed needs at least 3.4.x, so you should be fine on pretty much any current Pi distribution.

Ian
https://github.com/IanHarvey

dvalenzuela
Posts: 1
Joined: Mon Oct 28, 2013 2:05 am

Re: Bluetooth LE and TI SensorTag from Python

Mon Oct 28, 2013 2:07 am

I have installed bluez5.8 and i get compilation errors under gatt_find_info
Below the compiler errors
/home/pi/bluepy/bluepy-helper.c:370: undefined reference to `gatt_find_info'
/tmp/ccGpTdte.o: In function `cmd_char_desc':
/home/pi/bluepy/bluepy-helper.c:618: undefined reference to `gatt_find_info'

IanH2
Posts: 79
Joined: Tue Dec 18, 2012 10:17 am

Re: Bluetooth LE and TI SensorTag from Python

Wed Oct 30, 2013 6:53 am

I'm not sure what you mean by 'installed BlueZ 5.8'. Did you replace the code in the bluez-5.4/ directory? The gatt_find_info() function is in bluez-5.4/attrib/gatt.c, near the end of the file.

Could you post the entire output from 'make', please, so I can see how the compiler is being invoked.

Thanks,
Ian
-----
https://github.com/IanHarvey

mvartani76
Posts: 2
Joined: Mon Nov 11, 2013 4:36 pm

Re: Bluetooth LE and TI SensorTag from Python

Mon Nov 11, 2013 4:41 pm

Hi Ian,

Thanks for creating this code. This is very useful.

I was able to successfully build the helper file and can see the peripheral BTLE device but am having difficulty connecting with the device.

I receive the following error message...

Running /home/pi/bluepy/bluepy/bluepy-helper
{'sec': ['low'], 'state': ['tryconn'], 'rsp': ['stat'], 'dst': ['00:02:5B:00:15:10'], 'mtu': [0]}
Response {'sec': ['low'], 'state': ['disc'], 'rsp': ['stat'], 'mtu': [0]}
{'code': ['connfail'], 'rsp': ['err']}
Traceback (most recent call last):
File "btle.py", line 250, in <module>
conn.connect(sys.argv[1])
File "btle.py", line 142, in connect
while rsp['state'][0] == 'tryconn':
KeyError: 'state'

It appears that it is failing to connect to the peripheral. As I do not have the sensor tag as of yet (on order), I slightly modified the code but was just wondering if you might have any thoughts/suggestions on how to trace down this error.

The peripheral devices that I have tried are: LightBlue app on iPhone5 running Proximity Profile and CSR1010 running the Security Tag profile. I am able to connect to these devices using the CSR demonstrator kit.

Thanks again for any thoughts you might have.
Mike

mvartani76
Posts: 2
Joined: Mon Nov 11, 2013 4:36 pm

Re: Bluetooth LE and TI SensorTag from Python

Mon Nov 11, 2013 5:39 pm

Hi Ian,

Thanks for this code and tutorial. It is very useful.

I was able to successfully compile the C helper function.

As I am still waiting for a TI sensortag, I tried modifying the code slightly to have the bluepy code just connect to a peripheral. The modified code is under main in btle.py as shown below.

Code: Select all

conn = Peripheral("00:02:5B:00:15:10")
conn.connect("00:02:5B:00:15:10")
I receive the following error message when trying to run this modified code...

Running /home/pi/bluepy/bluepy/bluepy-helper
{'sec': ['low'], 'state': ['tryconn'], 'rsp': ['stat'], 'dst': ['00:02:5B:00:15:10'], 'mtu': [0]}
Response {'sec': ['low'], 'state': ['disc'], 'rsp': ['stat'], 'mtu': [0]}
{'code': ['connfail'], 'rsp': ['err']}
Traceback (most recent call last):
File "btle.py", line 250, in <module>
conn.connect(sys.argv[1])
File "btle.py", line 142, in connect
while rsp['state'][0] == 'tryconn':
KeyError: 'state'


It appears to fail to connect although I can see this BDADDR when initiating the following command

Code: Select all

hcitool lescan
I have tried this with a CSR1010 running the security tag profile as well as the LightBlue iPhone5 app running the Proximity profile and receive the same error messages.

I have also tried this code with a Pluggable BT4.0 dongle and an IOGear BT4.0 dongle.

Any comments/thoughts/suggestions would be greatly appreciated...

Thanks,
Mike

klinenator
Posts: 1
Joined: Thu Dec 12, 2013 4:18 am

Re: Bluetooth LE and TI SensorTag from Python

Thu Dec 12, 2013 4:19 am

Do you have a way to get the RSSI?

dhruvkapur91
Posts: 1
Joined: Thu Jan 16, 2014 2:06 pm

Re: Bluetooth LE and TI SensorTag from Python

Thu Jan 16, 2014 5:20 pm

Hey IanH!

Thanks for sharing the code and the effort involved. I was able to compile and run the exe on ras pi. However can you give me a few pointers in how to use the code to use sensor tag as a digital compass...

Thanks :)

IanH
Posts: 67
Joined: Mon Dec 19, 2011 5:53 pm

Re: Bluetooth LE and TI SensorTag from Python

Wed Mar 05, 2014 9:40 pm

mvartani76 wrote: As I am still waiting for a TI sensortag, I tried modifying the code slightly to have the bluepy code just connect to a peripheral. The modified code is under main in btle.py as shown below.

Code: Select all

conn = Peripheral("00:02:5B:00:15:10")
conn.connect("00:02:5B:00:15:10")
I receive the following error message when trying to run this modified code...
Hi - this may be a little late, but does the command

Code: Select all

sudo hcitool lecc 00:02:5B:00:15:10
work (it'll say something like 'Connection handle 64' if it does). If you get "Could not create connection: Input/output error" it's possible the Bluetooth daemon is in a mess and needs to be restarted:

Code: Select all

sudo /etc/init.d/bluetooth restart
Also, it's worth making sure the LE device isn't actually connected to anything else (e.g. your iPhone) at the time, as it won't be able to connect to the Pi if so.
https://github.com/IanHarvey

lostcitizen
Posts: 7
Joined: Sun Mar 02, 2014 2:38 am

Re: Bluetooth LE and TI SensorTag from Python

Tue Mar 11, 2014 8:24 am

Hello,

Sounds pretty cool, can you provide us a getting started guide please?

Thanks!

IanH
Posts: 67
Joined: Mon Dec 19, 2011 5:53 pm

Re: Bluetooth LE and TI SensorTag from Python

Thu Mar 13, 2014 8:40 am

If I get the time, I'll write something up at elinux.org based on a recent/clean install of the OS. In the meantime, the basics are:

- get a Bluetooth 4.0 compatible USB adapter (I've mostly used a Plugable one and a random Chinese one with a CSR chipset)
- (handwaving here) possibly install or update some bluetooth packages
- download bluepy, go to the bluepy/ directory and run 'make'
- If you have a SensorTag, press the button on the side to reset it / wake it up
- run 'sudo hcitool lescan', which will scan for LE devices
- hopefully you'll get a 'SensorTag' device with a MAC address (looks like 11:22:33:AA:BB:CC)
- run 'sudo hcitool lecc <MAC address>' which will make a test connection to the device (insert handwaving about debugging here)
- edit the code under 'if __name__ == "__main__" in bluepy/sensortag.py to replace "BC:6A:29:..." with the MAC address of your SensorTag
- run 'python sensortag.py', which should connect to the device and loop reading the IR temperature sensor, the humidity sensor, and the air pressure sensor.

Hopefully the example code in sensortag.py will give you enough clues to read the sensors as you want.
https://github.com/IanHarvey

lostcitizen
Posts: 7
Joined: Sun Mar 02, 2014 2:38 am

Re: Bluetooth LE and TI SensorTag from Python

Thu Mar 20, 2014 5:34 pm

Thank you! I'll give a try soon

IanH
Posts: 67
Joined: Mon Dec 19, 2011 5:53 pm

Re: Bluetooth LE and TI SensorTag from Python

Mon Apr 14, 2014 8:38 pm

Update: I've just made a new release of code at https://github.com/IanHarvey/bluepy and have written up a general guide to getting it going, at http://www.elinux.org/RPi_Bluetooth_LE.

I'm finding it helpful to be able to test things with the BlueZ command-line tools (as documented on the page), as the Python wrapper seems relatively trouble-free once you can get BlueZ itself working with the device.
https://github.com/IanHarvey

tila
Posts: 1
Joined: Mon Apr 14, 2014 9:50 pm

Re: Bluetooth LE and TI SensorTag from Python

Mon Apr 14, 2014 10:03 pm

Hey Ian

First of all thanks for some great work.

I was wondering if there is a way to check for connectivity to the sensortag, before trying to get data?
and then try to reestablish the connection without blocking.
The reason for this is that I'm using four sensortags and don't want the program to go down if connection to one device is lost. I have tried modifying the btle.py without any luck. Maybe you can point me in the right direction?

Thanks again
- Tim

IanH2
Posts: 79
Joined: Tue Dec 18, 2012 10:17 am

Re: Bluetooth LE and TI SensorTag from Python

Thu Apr 17, 2014 8:05 am

Basically, the only way to do this at the minute is to use a thread for each connection; there's no reason this shouldn't work.

Alternatively, can you split the system into several programs (processes), where each process talks to one sensor, and writes an output file to disk (which is picked up by another program)? I'm doing this in my own 'home monitoring' system. It has the advantage that uncaught exceptions (etc) dealing with one sensor just crash one process, and don't stop the whole system.
-----
https://github.com/IanHarvey

ff2014
Posts: 1
Joined: Fri May 02, 2014 12:41 am

Re: Bluetooth LE and TI SensorTag from Python

Sat May 03, 2014 9:04 am

Hello, lanH:
Thankyou for your nice code.
I would like to get long log data.
Sensor tag has auto power off.
Can I send the command to keep device on via bluepy?

Ti forum says,
osal_pwrmgr_device( PWRMGR_ALWAYS_ON ); // To keep device on
osal_pwrmgr_device( PWRMGR_BATTERY ); // Revert to battery mode

and I would like to control sensor's interval time.
I don't have any idea for this.
Do you have some example?

Thanks

diegogs
Posts: 1
Joined: Wed Dec 05, 2012 4:09 pm

Re: Bluetooth LE and TI SensorTag from Python

Thu Mar 12, 2015 11:55 am

Hi!,

Have you tried the new sensortag?, I've been playing a bit with it, but I'm facing problems with the Movement Processor Unit. This PMU mixes Accelerometer+Gyro+Magnetometer data into the same Data UUID, and I couldn't find any info related to its configuration and data format. Can anybody help me?

Thanks!

Return to “Python”

Who is online

Users browsing this forum: nellapatella and 27 guests