gatttool or python interface to nRF8001?

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
nouyang2
 
Posts: 3
Joined: Mon Jun 30, 2014 7:33 pm

gatttool or python interface to nRF8001?

Post by nouyang2 »

Hello,
I am wondering if anyone has been able to talk from their computer to the arduino+nRF8001 breakout.
So far I have gotten to:

Code: Select all

nrw@nrw-PC:~$ sudo gatttool -b DC:05:4F:BC:F6:4A  -I -t random
[   ][DC:05:4F:BC:F6:4A][LE]> connect
[CON][DC:05:4F:BC:F6:4A][LE]> char-desc
[CON][DC:05:4F:BC:F6:4A][LE]> 
handle: 0x0001, uuid: 2800
handle: 0x0002, uuid: 2803
handle: 0x0003, uuid: 2a00
handle: 0x0004, uuid: 2803
handle: 0x0005, uuid: 2a01
[CON][DC:05:4F:BC:F6:4A][LE]> char-write-req 0x0003 0x0067
[CON][DC:05:4F:BC:F6:4A][LE]> Characteristic value was written successfully

[CON][DC:05:4F:BC:F6:4A][LE]> char-write-cmd 0x0003 0x0067
and I am using a modified callBack_echo for the Arduino code.
https://github.com/adafruit/Adafruit_nR ... ckEcho.ino

On the Serial Monitor, I can see

Code: Select all

Advertising started
Connected!
I expect that 0x0067 will transmit "g" but nothing happens on the Arduino (no serial monitor "byte received", no rgbLED color change). If I connect using Android 4.3 and the nRF UART application, then I can send and receive characters fine. Is there something else I need to do on the gatttool side to talk to the UART Arduino sketch?

The ultimate goal is to be able to broadcast data from a computer with a CSR4.0 dongle to several nRF8001 breakouts. I was able to do so using two CSR4.0 dongles on the computer*, but an Arduino is not a raspberry pi and a nRF8001 (slave-mode only) is not a CSR4.0 dongle. sad...
*http://www.orangenarwhals.com/?p=887

Any help appreciated.


System specs: atmega328p on a custom board programmed as a nano328 over usbasp, pins used:
#define ADAFRUITBLE_REQ A1
#define ADAFRUITBLE_RDY 2
#define ADAFRUITBLE_RST A0, data back from atmega via FTDI cable, ubuntu 13.10.

User avatar
Franklin97355
 
Posts: 24182
Joined: Mon Apr 21, 2008 2:33 pm

Re: gatttool or python interface to nRF8001?

Post by Franklin97355 »

I would check with the author of gatttool.

User avatar
ktownsend
 
Posts: 1447
Joined: Thu Nov 05, 2009 2:18 am

Re: gatttool or python interface to nRF8001?

Post by ktownsend »

I've had a lot of frustrations with GATTTool as well as the other related tools in Bluez where BLE is concerned (hcitool, etc.). The documentation is pretty sparse, and it's tough to make much sense of it where newer things like BLE are concerned (there's more info out there on Classic Bluetooth). :(

If you're able to get anything working with Bluez and GATTTool and the related command line tools for BLE, though, please feel free to post the details up here and we'll be happy to mention it on the blog so other people can benefit from it!

The best I could do in my limited attempts was connecting, but I didn't find the magic formula to send/receive data via GATT transactions after the connection was established. Admittedly, I didn't try as hard as I perhaps should have, but since all of the in house development was done using the tools from Nordic, Bluez got neglected early on.

Any submissions here are definitely welcome, but I'm afraid I don't have any good news to report saying that I was able to get this working with BLE myself beyond the initial connection.

nouyang2
 
Posts: 3
Joined: Mon Jun 30, 2014 7:33 pm

Re: gatttool or python interface to nRF8001?

Post by nouyang2 »

WHOA I totally got it working :D

Here are the steps I took. Overview
1) updated from the bluez that is in ubuntu 14.04 package manager,

Code: Select all

nrw@nrw-PC:~$ dpkg --status bluez | grep '^Version:'
Version: 4.101-0ubuntu13
to version 5.20

2) Using that, figured out the correct characteristic I should be writing to

I'm not sure how interlinked 1 and 2 are, presumably I didn't need to update bluez, I was just writing to the wrong characteristic ID yesterday.

phewwwww large order of nrf8001 breakouts not going to waste. hopefully.

In detail, with major help from http://www.jaredwolff.com/blog/get-star ... ow-energy/,

Code: Select all

sudo apt-get remove bluez
sudo apt-get remove bluez-cups
sudo apt-get remove bluez-hcidump
~$  wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.20.tar.xz 
(I checked online to see what the latest version was at http://www.kernel.org/pub/linux/bluetooth/)

Code: Select all

~$ tar xvf bluez-5.18.tar.xz
~/bluez-5.20$  ./configure --enable-library --disable-systemd
$ make -j8 && sudo make install
$ sudo cp attrib/gatttool /usr/local/bin/

Code: Select all

nrw@nrw-PC:~$ sudo hcitool lescan
LE Scan ...
E9:D9:7A:EB:BC:35 
E9:D9:7A:EB:BC:35 (unknown)

IN another terminal (not sure if the sec-level=high is needed)

Code: Select all

nrw@nrw-PC:~$ sudo gatttool -b E9:D9:7A:EB:BC:35 -I -t random --sec-level=high 
[E9:D9:7A:EB:BC:35][LE]> connect
Attempting to connect to E9:D9:7A:EB:BC:35
Connection successful
[E9:D9:7A:EB:BC:35][LE]> char-desc
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0002, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x0008, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0009, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000a, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000b, uuid: 6e400002-b5a3-f393-e0a9-e50e24dcca9e
handle: 0x000c, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000d, uuid: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
handle: 0x000e, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x000f, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0010, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0011, uuid: 00002a27-0000-1000-8000-00805f9b34fb
Looks like we want 0x00b, the TX service according to https://learn.adafruit.com/getting-star ... pp-support

Code: Select all

[E9:D9:7A:EB:BC:35][LE]> char-write-cmd 0x00b 0x0072
On the Serial Monitor:

Code: Select all

Received 3 bytes: r [ 0x0 0x0 0x72 ]
	Writing out to BTLE: 0x0 0x0 0x72
Will write a blog post soon... Hurray!! Now to automate this with python.

User avatar
ktownsend
 
Posts: 1447
Joined: Thu Nov 05, 2009 2:18 am

Re: gatttool or python interface to nRF8001?

Post by ktownsend »

Thanks for the heads up. I'll test this out myself on my end. Ping us at [email protected] with a link to this thread when you have a blog post up and I'll be happy to get it posted here as well!

nouyang2
 
Posts: 3
Joined: Mon Jun 30, 2014 7:33 pm

Re: gatttool or python interface to nRF8001?

Post by nouyang2 »

Hi!
I wrote up how to script access in python (as opposed to entering commands one by one manually, now you can write a program to send values and therefore do things like build interactive robot apps in processing)

http://www.orangenarwhals.com/?p=911
The code for it all is here: https://gist.github.com/nouyang/2a6a733d8facd23115a4

Seems like the adafruit blog is busy, but putting this out there in case it's useful to others.
Happy hacking!

User avatar
2h4u
 
Posts: 2
Joined: Mon Apr 06, 2015 2:59 am

Re: gatttool or python interface to nRF8001?

Post by 2h4u »

I figured out how to "read" from the nRF8001, hope this helps someone:

(I used the the same setup as nouyang2, ubuntu 14.04 with bluez-5.20)


First we need to set the notificationflag,
this is done with the command "char-write-req e 0100"

On the console of your computer:

Code: Select all

sudo gatttool -b C0:85:BB:47:91:38 -I -t random
[C0:85:BB:47:91:38][LE]> connect
Attempting to connect to C0:85:BB:47:91:38
Connection successful
[C0:85:BB:47:91:38][LE]> char-write-req e 0100
Characteristic value was written successfully
Now lets try to write something from the nRF8001 to our computer:
Arduino serial monitor:

Code: Select all

Adafruit Bluefruit Low Energy nRF8001 Print echo demo
* Advertising started
* Connected!
* Sending -> "test"
	Writing out to BTLE: 0x74 0x65 0x73 0x74

On the console of your computer:

Code: Select all

Notification handle = 0x000d value: 74 65 73 74 
Got it! :D


Thanks to nouyang2 for providing all the information!

User avatar
2h4u
 
Posts: 2
Joined: Mon Apr 06, 2015 2:59 am

Re: gatttool or python interface to nRF8001?

Post by 2h4u »

I figured out how to send/receive to/from the nRF8001 with python.
There is a python api called bluepy: https://github.com/IanHarvey/bluepy


So its quit easy to interact with the NRF8001, here's a little test program:

Code: Select all

from bluepy.btle import *

class NotificationHandler(DefaultDelegate):                                               
    def handleNotification(self, cHandle, data):                                               
        print('got response', data)     

conn = Peripheral("C0:85:BB:47:91:38", "random")
conn.setDelegate(NotificationHandler())    

try:
	# Turn on notificiations                                                                   
 	conn.writeCharacteristic(0x0e, '\x01\x00', False)  

 	# Send Message
	conn.writeCharacteristic(0x0b, 'test', False)  

	# wait for response
	conn.waitForNotifications(1)        
except Exception, e:
	print e
finally:
	conn.disconnect

Some notes about the setup:
Raspberry with Raspian 3.18.7 and a BLE tongle
Python v2.7.3
Bluepy v0.9.0
nRF8001
Arduino Leonardo with the callBackEcho programm from adafruit for testing responses

I hope this helps somebody, cause its not that easy to find some information about the nRF8001 and howto use it with other system then iOS or android.

Source: http://newscentral.exsees.com/item/56c3 ... 6d4665e8e3

User avatar
adafruit_support_mike
 
Posts: 67903
Joined: Thu Feb 11, 2010 2:51 pm

Re: gatttool or python interface to nRF8001?

Post by adafruit_support_mike »

Nice1 Thanks for posting!

User avatar
chenglin
 
Posts: 3
Joined: Fri Nov 13, 2015 10:18 pm

Re: gatttool or python interface to nRF8001?

Post by chenglin »

Hi all,

I am following nouyang2 and 2h4u. I want to read data from my adafruit NRF8001. Instead of using a laptop to receive data, I have a Intel Edison board which runs a Linux Yocto on it. I have installed the bluez-5.24 successfully. But when I want to write or read something from the NRF8001, it said "Command Failed: Disconnected".

Code: Select all

[D2:AD:9F:B7:BB:60][LE]> connect
Attempting to connect to D2:AD:9F:B7:BB:60
Connection successful
[D2:AD:9F:B7:BB:60][LE]> char-write-req e 0100
Command Failed: Disconnected
[D2:AD:9F:B7:BB:60][LE]> connect
Attempting to connect to D2:AD:9F:B7:BB:60
Error: connect: Device or resource busy (16)
[D2:AD:9F:B7:BB:60][LE]> char-write-cmd 0x00b 0x0072
Command Failed: Disconnected
[D2:AD:9F:B7:BB:60][LE]> quit
root@SuperSensor:~# gatttool -b D2:AD:9F:B7:BB:60 -I -t random
[D2:AD:9F:B7:BB:60][LE]> connect
Attempting to connect to D2:AD:9F:B7:BB:60
Error: connect: Device or resource busy (16)
[D2:AD:9F:B7:BB:60][LE]> connect
Attempting to connect to D2:AD:9F:B7:BB:60
Connection successful
[D2:AD:9F:B7:BB:60][LE]> char-desc
Command Failed: Disconnected
[D2:AD:9F:B7:BB:60][LE]>

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”