Bluetooth communication between Raspberry PI and Arduino

This post is part II of a III part series of posts related to Arduino and Raspberry PI Wireless Bluetooth communications.

  • Part I
  • Part III (coming soon)

In another post, I’ve shown how to communicate with Arduino through Bluetooth. Arduino is a very nice platform for electronics, but sometimes, we need a full computer for more expensive workload.

Here, I’ll show how to share information between the Arduino and the Raspberry PI. Imagine that you have the Arduino taking measurements and send them to the Raspberry PI to work on the data – a web server for example.

I’m going to explain with two examples:

  • One – a very simple example – where the Raspberry PI is sending the information to the Arduino.
  • Two – the Raspberry PI is receiving information from the Arduino.

For both examples, the configurations in the Part I (except the Android Phone part) are going to be necessary. I’m going to assume you have the Arduino ready (running with the Bluetooth sensor blinking)

First, installing the necessary packages and configurations

Raspberry PI

A few months ago, I wrote an article showing how to connect a Bluetooth Speaker to the Arduino.

There, I’ve used an Edimax EB-DGC2 USB bluetooth dongle. Today, I’m going to use the Conceptronic CBT2NANO (v2.0).

Install the necessary Bluetooth applications

Add your user to the Bluetooth group (logoff and login again for the changes to take effect)

Plug in the Bluetooth USB dongle and start the Bluetooth service

Initialize the Bluetooth device

Scan for devices

After you find your device, pair with it. (This is the Bluetooth sensor plugged in to the Arduino)

The PIN Code is 1234

We can now trust the device, so that every time the PI boots, it will be automatically connected

The result will be 0 for not trusted and 1 for trusted

If it’s 0, let’s make it trusted

And check again

The result should now be 1

Now that we have the Bluetooth paired with the Arduino, lets start communicating

Bluetooth support in Python is done using sockets – so, for those already familiar with sockets, will not have a problem.

You can find more information in Kevin’s Doran blog.

Raspberry PI sending information to Arduino

Raspberry PI code

Arduino Code (from Part I)

Here’s a small video showing the commands sent from the Raspberry PI to the Arduino

Raspberry PI receiving the information

To use Raspberry PI as the sender, we need some data to be sent from the Arduino.  Were going to try with a DHT11 sensor (Temperature and humidity).

First, get the DHT11 library from github and install it in the Arduino library directory.

To the connections of the Arduino, add the DHT11

BluetoothDHT11

Raspberry PI receiving Temperature and Humidity

I add a problem with the code, because Python only was getting a char at a time:

i.e: 35% humidity would display 3 in a line and 5 in another line.

After a bit of research (found an answer from Stack Overflow by Tim), the solution was  to add a terminator to the string in the Arduino code and only print the receiving data in Python after the terminator was received.

Arduino code

Upload the code to the Arduino and start the Python code in the Raspberry PI (There’s no need for sudo)

Humidity and Temperature returned by the Arduino
Humidity and Temperature returned by the Arduino

Next part (part III) – displaying the data in a graphical interface without X using the frame buffer.

Some references

Communicating with RFCOMM

 

10 Replies to “Bluetooth communication between Raspberry PI and Arduino

  1. hi ,
    on cmd ~$ sudo apt-get install bluetooth bluez-utils bluez-alsa
    getting the response as : E: package ‘bluez-utils’ has no installation candidate
    E: package ‘bluez-alsa’ has no installation candidate
    ~$ sudo /etc/init.d/bluetooth start
    [ok] starting bluetooth (via systemctl): bluetooth.service.
    ~$ pi@raspberrypi ~ $ bluez-simple-agent hci0 xx:xx:xx:xx:xx:xx
    bluez-simple-agent:command not found

    please help me to solve this problem!!!

    1. Hi Rahul. What Linux version are you using ? I’m using Raspbian, but an older version – as of September 2015. Probably now the bluetooth package has another name. Try searching for it – apt-cache search bluetooth and see what the output is. Try searching for pi-bluetooth or blueman…

      1. I have finished till importing the values from arduino to rpi but i want to upload the values to a server through rpi and later compare it with weather reports so that it can estimate it for the next 24 hours (the estimation of next 24 hours must be done by rpi).
        Regards

  2. I have finished till importing the values from arduino to rpi but i want to upload the values to a server through rpi and later compare it with weather reports so that it can estimate it for the next 24 hours (the estimation of next 24 hours must be done by rpi).
    Regards
    Roop

    1. That is the easiest part.
      You can use IOT platforms like carriots, thinger.io or thingspeak for that ! the Internet is abundant with examples for the Raspberry and those platforms.

  3. Im confused because I have a rpi2 with a bluetooth module, hm10, connected to its gpio pins. The way I use it is via Serial0 and I send it AT commands like ser.write(“AT”) from a python script. I can make this connect to a bluetooth relay board with an hm10 in it as well and send it commands like ser.write(“e”) and such. What is this method of bluetooth communication?

    1. Hello !

      That and this is serial communication. AT is an AT command. The difference from your connection to mine is that you’ve connected a Bluetooth module to RPi GPIO and I’ve used a USB Dongle and have a few extra steps to do, because I have to pair it with the bluez Bluetooth stack (you bypass this connecting directly to the GPIOs) . After all that, its a another (serial) device that you can talk to using AT commands

Leave a Reply to feiticeir0 Cancel reply

Your email address will not be published. Required fields are marked *