2

Bluetooth Low Energy: Read-Write to Sensor Network from Raspberry Pi

In this tutorial, I am using a RedBearLab Bluetooth Low Energy (BLE) shield connected to Arduino UNO to demonstrate the BLE communication from Raspberry Pi.

  • Install hard-float Raspberryian OS using this link.
  • Install Bluez using step 1, 2, 3 of this link.
  • I used CSR 4.0 dongle in the Raspberry Pi for BLE connection.
  • Make sure you have gatttool in the path using
    $which gatttool
    If it is not in the path, copy it from bluez/bluez-5.11/attrib
    $cp bluez/bluez-5.11/attrib/gatttool /usr/local/bin/
  • Check if the Bluetooth dongle is up and running
    ~/bluez/bluez-5.11 $ tools/hciconfig
    If not (you will see DOWN written in the result), use below command to get it up
    ~/bluez/bluez-5.11 $ tools/hciconfig hci0 up
  • Now scan for BLE devices. The command below might continue scanning forever, so type CTRL+C when you have the device address
    $ sudo hcitool lescan
    Assume you get the BLE device address xx:xx:xx:xx:xx:xx
  • Now connect with the device using gatttool
    $ sudo gatttool -b xx:xx:xx:xx:xx:xx -t random –interactive
    You might not need to use -t random. Also if you get device busy error, remove the dongle and reconnect it.  You will see an interactive prompt. Type connect like below
    [xx:xx:xx:xx:xx:xx][LE]> connect
    Attempting to connect to xx:xx:xx:xx:xx:xx
    Connection successful

Once it is connected, we want to write to a LED to turn in ON/OFF, and read the sensor data. We are using FSR as the sensor in this example. We will use gatttool to do the read & write.

Before we begin, we need to understand how this works fundamentally. You can go ahead and read this book or any other good tutorial that I am not aware of. But basically, you need to have some code on your Arduino that sets up a profile in the nRF8001 component on your RedBearLab shield that defines “pipes” which is the link between a characteristic exposed by the radio and a function from which you can read data or to which you can send data.

I am using SimpleControl sketch from RedBearLab as the code that runs on Arduino.  To do read-write, we need to know which address to write and what value to write.

Find the Address to Read & Write: 
I look at RedBearLab’s Android source code to figure out the TX, RX address.  In the RBLGattAttribute.java, you will see

public static String BLE_SHIELD_TX = “713d0003-503e-4c75-ba94-3148f18d941e”;
public static String BLE_SHIELD_RX = “713d0002-503e-4c75-ba94-3148f18d941e”;

Now, if you type below in the gatttool interactive prompt, you can figure our the address (handle) of those TX, RX

[xx:xx:xx:xx:xx:xx][LE]> char-desc
handle: 0x000b, uuid: 713d0003-503e-4c75-ba94-3148f18d941e
handle: 0x000c, uuid: 2803
handle: 0x000d, uuid: 713d0002-503e-4c75-ba94-3148f18d941e
handle: 0x000e, uuid: 2902

So you basically see that the handle of TX, RX are 0x000b and 0x000d respectively.

How to Turn ON/OFF the LED:
From the SimpleControl.java, you can see that you need to write 010100 to the TX handle to turn the LED ON and 010000 to turn it OFF.

digitalOutBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
             byte buf[] = new byte[] { (byte) 0×01, (byte) 0×00, (byte) 0×00 };
             if (isChecked == true)
                  buf[1] = 0×01;
             else
                  buf[1] = 0×00;
           …………………………..
   }
 });

Now type below to turn the LED ON
[xx:xx:xx:xx:xx:xx][LE]>char-write-cmd 0x000b 010100

To Read Analog Data: 
Similarly from the SimpleControl.java, you can see that you need to write A00100 to the handle 0x000b to enable the analog reading capability through the RedBearLab shield. You also need to enable listening to the 0x000e handle. The 0x000e is the handle of the Client Characteristic Configuration (CCC) uuid 2902. You can find this 0x000e handle by typing char-read-uuid 2902 in the gatttool interactive prompt.

[xx:xx:xx:xx:xx:xx][LE]>char-write-cmd 0x000b A00100
[xx:xx:xx:xx:xx:xx][LE]>char-write-req 0x000e 0100 –listen

You will see sensor values in coming the the RX handle (0x000d)
Notification handle = 0x000d value: 0b 00 c0
Notification handle = 0x000d value: 0b 00 d0
Notification handle = 0x000d value: 0b 00 e4
Notification handle = 0x000d value: 0b 00 f0

……………….

2

How to Setup an openHAB Server at Raspberry Pi

The open Home Automation Bus (openHAB) project is an open source integration platform for home automation. I was working on setting up the openHAB server at Rasp Pi. The setup process seemed to be scattered around. So I decided to note down the steps below.

The setup below works on Raspberry Pi Rev-B board with hard-float Raspberryian OS.

  • To install hard-float Raspberryian OS, use this link.
  • JVM >=1.6 is needed. Use this link if not already present.
  • Download openhab-runtime. Unzip it in /opt/openhab
  • Download openhab-demo-configuration. Unzip and put the contents of this folder to /opt/openhab/configurations. Make sure you do not remove other files that were already existing in the /opt/openhab/configurations directory (openhab_default.cfg etc.)
  • Go to /opt/openhab. Run start.sh
  • Your openHAB server should be up and running now.
  • Test it in the browser http://localhost:8080/openhab.app?sitemap=demo
  • Or if you are doing SSH to Rasp-Pi like me, replace localhost with the IP-address of the Pi.
2

Using a Tilt Sensor

I was experimenting with tilt sensor this morning. The AdaFruit tutorial wasn’t clear enough and the Arduino tutorial seems incomplete. So I decided to publish one here.

I used below steps to build the circuit:

1. Connect one end of the tilt sensor to the +5V. Connect the other end to Arduino digital pin#5
2. Connect a 10K resistor in series with the digital pin#5. Connect the other end of the resistor to the GND.
3. Connect the negative leg (short) of the LED to the GND. Connect the positive leg (long) to Arduino digital pin#3
4. Connect a 330 ohm resistor in series with the digital pin#3. Connect the other end of the resistor to the GND.

tilt_sensor_bb

You can use the sketch to test the tilt sensor. If everything goes fine, you should see the LED glow when the sensor is tilted. You can also check the print message in the Arduino serial monitor.

0

Web controlled Robot

We developed a web controlled robot. Using pusher, developed a desktop client that tells the robot to move forward, backward, turn left and right. We used Cherokey 4WD kit to build the robot. An Arduino Uno (thanks to the Make Robot Hacks Session), a Wifi/Wifly shield were connected to the kit that receives the control signals from the web. Based on the control signals, the motor controller is instructed to move. Here is a video.

0

The Perfect Pour

This weekend, I joined Silicon Chef Hardware Hackathon. Our team (VeronicaStephanieJudithYan & Me) created a smart beer cozy! An Arduino based project that can detect how well you are pouring and prevents you from spilling the beer. We used a 3-axis Accelerometer to detect the tilt angel. Messages are displayed in the LCD screen to signal you whether you are pouring too fast or too slow. And guess what? We won an Arduino project book as the prize! The code is in github.

IMG_1437

 

IMG_1471