Trouble with bluetooth BLE connection between Pi3 and Blueduino

Hi everyone,

I have a processing sketch that gather some data every frames. Then I would need to send those data to a blueduino (basically an arduino with an integrated BLE module) that can then use them to light some LEDs.

The problem I have is that I can’t manage to find a way to send the data to the blueduino. At first I just tried to pair my device with command lines (which was actually quite hard to do…) and then used the serial communication example to send the data: it is not working.

I tried many things to pair the device and sadly, I can’t even remember what I tried since sometimes I was blindly following some tutorials without quite understanding them… I just remember some keywords like gatttool, bluez, bluetoothctl, systemctl, hciconfig, uart… But it is all a bit messy in my head on what they are, how they interract together, if some are not compatible with others…

Now what I’m trying to do is to pair my device inside the processing sketch. But for that I guess I would need a library. I found this one: https://github.com/nicolocarpignoli/jgatttool/blob/master/Example/main/GatewayMain.java. I successfully paired the device with gatttool using command lines that’s why I chose this one. But now I have no idea how to use it inside a processing sketch. And maybe it is not the way to go at all.

So if someone can put me on the right track I would be so, so happy :slight_smile:

1 Like

@jb4x My suggestion would be to figure out pairing first in the terminal, and then replicate that in your code using Processing’s shell() function.

1 Like

Just for the record, what arduino shield are you using?

Kf

Thanks @gohai, I’ll look into it and let you know!

@kfrajer, I’m not using a shield since it’s an arduino replica with built-in bluetooth. Now if you are speaking about the bluetooth module itself it’s a CC2540 BLE module. You can find all the references in the link I have posted previously.

Hi everyone,

I finally got some news! And pretty good ones :slight_smile:
I mange to pair the blueduino and send some data.

Here’s a step by step tutorial:

  • Start with fresh raspberry install (the processing Pi image).
  • Upgrade everything
~ $ sudo apt-get update
~ $ sudo apt-get upgrade -y
  • Check if the built-in bluetooth was up and running (the built-in bluetooth adaptater is called hci0)
~ $ hciconfig

You should get 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 it is not UP RUNNING then simply run this command:

~ $ sudo hciconfig hci0 up
  • Scan to find the device (hit ctrl+c to stop the scan):
~ $ sudo hcitool lescan

Stop when you find the device you want to pair. You now have the MAC adress.
Mine was 50:65:83:A0:BE:81

  • Pair the device with your own MAC adress
~ $ gatttool -I -b 50:65:83:A0:BE:81
[50:65:83:A0:BE:81][LE]> connect

If everything goes fine you get this:

Attempting to connect to 50:65:83:A0:BE:81
connection successful
  • Open a serial monitor for the bueduino and you can send ASCII characters like this:
[50:65:83:A0:BE:81][LE]> char-write-req 0x0029 aabbccdd...

aa, bb, cc, dd, etc. are your ASCII code

I still need some helps now though because I don’t know how to use that inside a processing sketch :crazy_face:

@gohai, you suggested using processing shell function but I can’t find any reference of that, do you have some links I can check out please?

Thanks again!

EDIT:
Ok I found this link: https://www.mkyong.com/java/how-to-execute-shell-command-from-java/ and tried to use some of this code and I think it will work for me. I’ll keep you informed!

3 Likes

@jb4x Thanks for sharing.

Try shell()

Hi @gohai,

I think the processing shell() function can’t work because after the first command line: gatttool -b 50:65:83:A0:BE:81 -I, this line appears: [50:65:83:A0:BE:81][LE]> and this is the line who expect connect.

In fact the only way I managed to send data was by using non interactive mode:

Runtime.getRuntime().exec("gatttool -b 50:65:83:A0:BE:81 --char-write-req -a 0x0029 -n 01

The problem with that method is that It always reconnect to the device so instead of just reading my value I also read OK+CONN and bonded.

To avoid that, I need to use interactive mode to be able to connect to the device once and for all.

Now I read that pexpect would work here so I looked for a java implementation of it and found expectJ.

I tried the 2 following piece of code but nothing works:

ExpectJ exp = new ExpectJ(5);
Spawn shell = exp.spawn("/bin/bash");
shell.send("gatttool -b 50:65:83:A0:BE:81 -I");
shell.send("connect");
ExpectJ exp = new ExpectJ(5);
Spawn gatt = exp.spawn("gatttool -b 50:65:83:A0:BE:81 -I");
gatt.send("connect");

I got no error, it just does not connect…

Any idea on what I coud do or try?

Thanks

@jb4x shell does not work with interactive programs. Use Java Process.exec() for that (you can set an InputStream and OutputStream to push characters in and out the running program at any time.

Thanks @gohai, I’ll try that!

But since I’m a complete beginner with bluetooth I’m learning everything the hard way :crazy_face: and I just figured out that bluetooth BLE doesn’t fit my need since I want to do some sort of streaming…

So I ordered a bluetooth 2.0 module and will continue with that. (I’ll still try to connect the module with your tip in case it works and people are looking for some answers here).

Also before using bluetooth I’ll try to see if everything is working correctly with a USB connection. But I need to wait for a new arduino uno because I destroyed the one I had by plugging a cable just next where it should have been :confused:

@jb4x Too bad your arduino is broken. I wonder if it would be useful to have an arduino simulator aka. some code that simulates an arduino device. Would that work in your case? I am not sure if they exist… I could try to put one together but it really depends on what kind of data you are getting from it.

Kf

Hi @kfrajer,

This question is actually a part of this project: A universal ambilight

To sum up, I’m getting my TV feed through a camera set on a RPi using processing. Based on that feed, I compute the color of my LEDs.

Now I need to send those LED colors (229 colors) to an arduino plug into my LED strip to light them up!

I want to do that using bluetooth but since I have trouble with it I first want to proof test everything with a simple USB connection between the RPi and the arduino.

I still have the blueduino and an arduino micro but I don’t have a long enough USB cable to connect them to the RPi…

Since I want to proof test the system, I’m not sure an arduino simulator would work in my case because I need the output to actually light up the LEDs.

Also I should receive my new arduino and a bluetooth 2.0 arduino module by Friday so I’ll have the whole weekend to try it out :slight_smile:

Thanks anyway!

Hi, @jb4x!

I’ve built many wireless devices using RGB LEDs before and I have lots of experience with WebSockets / MQTT technology which is used in many smart lighting solutions.

I wanted to bring your attention to less frustrating tech stack than Bluetooth.

What you’re trying to do seems like a perfect use case for MQTT which can be sent over WiFi… Have you seen or heard of ESP8266 chips? They can run Arduino code and have built in WiFi capability for less than $5 a pop. If I was building anything with colorful LEDs, I would try Neopixels, ESP8266 running Arduino code, MQTT or WebSockets and use MQTT in Processing to send the data over.

3 Likes

Thanks for your feedback msurguy! I’ll definitely have a look at that.

I’ll still try the bluetooth solution since I bought everything that I need but if I really can’t manage or for future project I’m keeping that solution in a corner of my head :slight_smile:

Out of curiosity you have some examples of those devices you’ve built?

Hi everyone,

I think I was overthinking it and didn’t see the obvious…

The non interactive mode should actually work. The reason I got the OK+CONN and bonded message is because I previously set the auth mode to 3: auth and bond as described here. So setting it back to 0: no auth and passcode needed, default mode should solve the problem, and thus, no need to run the interactive mode.

Sadly, I can’t try this solution because I can’t run AT command anymore and I have no idea why. I might get crazy :crazy_face: :crazy_face: :crazy_face:

Sure you can see some! Not all of the published ones are using WebSockets / MQTT right now but all of my LED products use ESP8266 and sometimes Raspberry Pi to send the MQTT packets:

I also did hundreds of experiments with Raspberry Pi, Arduino, ESP8266 and LEDs so if you want to ask some specific questions - I’ll be happy to share with you everything I know.

1 Like

You have done some really cool project! I really liked the rolling light post and the neosegment project =)

I won’t hesitate to ask then! :stuck_out_tongue_closed_eyes: