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

sudo apt-get install bluetooth bluez-utils bluez-alsa

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

sudo gpasswd -a pi bluetooth #replace pi with your user

Plug in the Bluetooth USB dongle and start the Bluetooth service

sudo /etc/init.d/bluetooth start
[ ok ] Starting bluetooth: bluetoothd rfcomm.

Initialize the Bluetooth device

sudo hciconfig hci0 up

Scan for devices

hcitool scan
Scanning ...
	98:D3:31:50:0A:CE	BT UART

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

pi@raspberrypi ~ $ bluez-simple-agent hci0 98:D3:31:50:0A:CE

The PIN Code is 1234

RequestPinCode (/org/bluez/16976/hci0/dev_98_D3_31_50_0A_CE)
Enter PIN Code: 1234
Release
New device (/org/bluez/16976/hci0/dev_98_D3_31_50_0A_CE)

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

bluez-test-device trusted 98:D3:31:50:0A:CE

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

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

 bluez-test-device trusted 98:D3:31:50:0A:CE yes

And check again

bluez-test-device trusted 98:D3:31:50:0A:CE

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

import bluetooth

bd_addr = "98:D3:31:50:0A:CE" //the address from the Arduino sensor
port = 1
sock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)
sock.connect((bd_addr,port))

while 1:
        tosend = raw_input()
        if tosend != 'q':
                sock.send(tosend)
        else:
                break

sock.close()

Arduino Code (from Part I)

//This sketch shows how to connect to a bluetooth device
// Arduino -> bluetooth
// Used with Android BlueTerm app

#include <SoftwareSerial.h> //Serial library

/**
 * Arduino connection HC-05 connection: 
 * HC-05  | Arduino
 * TX     | 5
 * RX     | 6
*/
// Here, we exchange them -
SoftwareSerial bt (5,6);  //RX, TX (Switched on the Bluetooth - RX -> TX | TX -> RX)
int LEDPin = 13; //LED PIN on Arduino
int btdata; // the data given from the computer

void setup() {Arduino Code (from Part I)
  bt.begin(9600);
  bt.println ("Bluetooth ON. Press 1 or 0 to blink LED..");
  pinMode (LEDPin, OUTPUT);
}

void loop() {
  if (bt.available()) {
    btdata = bt.read();
    if (btdata == '1') {
      //if 1
      digitalWrite (LEDPin, HIGH);
      bt.println ("LED ON!");
    }
    if (btdata == '0') {
      //if 0
      digitalWrite (LEDPin, LOW);
      bt.println ("LED OFF!");
    }
  }
  delay (100); //prepare for data
}

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

import bluetooth

bd_addr = "98:D3:31:50:0A:CE" //The address from the HC-05 sensor
port = 1
sock = bluetooth.BluetoothSocket (bluetooth.RFCOMM)
sock.connect((bd_addr,port))

data = ""
while 1:
	try:
		data += sock.recv(1024)
		data_end = data.find('\n')
		if data_end != -1:
			rec = data[:data_end]
			print data
			data = data[data_end+1:]
	except KeyboardInterrupt:
		break
sock.close()

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

//This sketch will use bluetooth to send temperature and humidity
// to a Raspberry PI using bluetooth
// Arduino -> bluetooth

#include <SoftwareSerial.h> //Serial library
#include <DHT.h>

#define DHTPIN 2  //Where is DHT connected
#define DHTTYPE DHT11  // Type of sensor used

/**
 * Arduino connection HC-05 connection: 
 * HC-05  | Arduino
 * TX     | 5
 * RX     | 6
*/
// Here, we exchange them -
SoftwareSerial bt (5,6);  //RX, TX (Switched on the Bluetooth - RX -> TX | TX -> RX)
int LEDPin = 13; //LED PIN on Arduino
int btdata; // the data given from the computer

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  bt.begin(9600);

  /* Since we run out of 5v PIN 
  * and don't wanna use a breadboard - the VCC of the DHT11
  * is connected to PIN 8
  * And we just use digitalWrite to put it HIGH
  */
  pinMode(8, OUTPUT); //explained above why PIN 8
  digitalWrite(8,HIGH);
  dht.begin();
}

void loop() {
    float hh = getHumid();
    float tt = getTemp();
    //Serial.println(hh);
    //Serial.println(tt);
    bt.print (String(hh) + "," + String(tt));
    bt.print("\n");
    delay (2000); //prepare for data (2s)
}

float getHumid() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  return (float)dht.readHumidity();
}

float getTemp() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  return (float)dht.readTemperature();
}

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