Skill Level: Intermediate

Knowledge of the installation and management of Raspberry PI. Some experience with IBM Cloud and Git. A good skill in C/Python programming if changes to the programs provided are needed

It is a POC to show the capabilities of the IoT Platform Starter in IBM Cloud to manage data gathered by a Raspberry PI from a connected BT device.I will reuse and expand part of the paper “Bring IoT home: Connect your car to your home” by Josef Reisinger


  • Raspberry PI (RPI) 3 model B with SD card and Sense HAT accessory
  • IBM Cloud (formerly Bluemix) account
  • A wi-fi access point to connect to internet ( I used an iPad Air 2)
  • Laptop to connect to the Raspberry using ssh (in alternative, USB keyboard and HDMI video)
  • A device supporting Bluetooth networking ( I used a phone Nokia 222)


  1. POC overview

    1. Car approaching home causes the lights to switch on

    2. Car departing from home causes the lights to switch off



    POC Architecture:




    POC flow:

    • A Phone is paired with the Raspberry device over Bluetooth
      • You can use a different Bluetooth device as long as it can be paired with the Raspberry (ie,perhaps some car navigation system)
    • Raspberry is at home and is able to connect to the phone and to check the strength of the signal coming from the phone
    1. When the phone (on board the car owner) approaches home, the signal becomes stronger
    2. Raspberry is programmed to send the value of the signal power to a MQTT Broker (the IBM IOT Platform in IBM Cloud) on a topic using encryption
    3. The data is made available to a Node-Red application which decides what to do. If the signal strength is above a threshold (meaning the phone is close to home), the application :
    4. sends back the Raspberry the command :
    5. to switch on the leds of the SenseHAT accessory
    • When the power of the signal becomes weaker, the Node-Red app tells the SenseHAT to switch off
    • In the reality, to avoid damage to the light-appliance, a sequence of on and off commands is suggested to be separated by a given delay of time
    • A web dashboard requests the status of the connection which is showed in a page using a gauge


    Setup – major steps:

    • Raspberry PI (RPI) installation (OS + packages)
    • Compilation of the source programs which run on the RPI
    • Boilerplate IBM IOT Platform starter provisioning in IBM Cloud
    • Installation of a Node-RED flow
    • Installation of the html/js files which make up a dashboard based on Node.js
  2. Raspberry setup: Raspbian installation

    • Insert the SD card on the laptop and install the OS image downloaded from www.raspberrypi.org/downloads/
    • I installed the image of Raspbian GNU/Linux 9 (stretch). Do not use Noobs.
    • Do not forget to create an empty file named ssh in the root directory to enable ssh
    • To configure wi-fi, edit the file /etc/wpa_supplicant/wpa_supplicant.conf , adding the following stanza:

        ssid="Your SSID"

        psk="Your SSID Password"

    • Insert the SD card into the RPI an power it.
    • Change the default password raspberrypi for user pi
  3. Raspberry setup: packages installation

    • Connect to the RPI from the laptop
      • RPI and laptop need to be attached to the same wifi network and you should discover the IP address of the RPI (using the info coming from your access point or using an IP scanner in your laptop)
    • Because RPI will talk with the IOT Platform using the MQTT light protocol, you need to install the following packages (and dependencies) with your favourite tool (I used apt-get install):
      • MQTT client and SDK
        • mosquitto-clients
        • libmosquitto-dev
        • libbluetooth-dev
    • Update python 2.7 since we need it
    • Confirm that sense-hat packages are installed as well as the python API for sense.


  4. Raspberry setup: accessing Git to download the source programs

    • All the sources need by the POC are available in the following Git repository:


    • The RPI folder contains all the source programs  and makefile to compile and put on the Raspberry
    • The Node-RED folder has the only one flow to be imported in Node-RED
    • The public folder contains all the files which make up the web dashboard
  5. Raspberry setup: application installation

    • Working from the home dir of user pi, create the directory projects/car@home and move into it
    • Copy the files that you will find under the RPI folder in the Git repo:

    blue.c   Manages the Bluetooth network and get the strength of the signal coming from a paired devices

    btclient.c  Main program. Publish the data from the BT device to the IBM IOT Platform (MQTT Broker) and get commands from it (switch on/off the leds of the RPI)

    btclient.cfg  Custom parameters (see later)

    btclient.d  Directory which contains the «actuator» scripts (written in Python 2.7) to execute the commands (to toggle the leds of the Sense HAT)

    cert  Directory where to place the certificate of the IOT-Platform. See later

    tprintf.c  Utility module to print log messages to the screen

    config.c  Read in the parameters from btclient.cfg

    config.h  Define default values for many properties


  6. Raspberry setup: application customization

    The file btclient.cfg accepts the following custom parameters. Many match values coming from the registration of the device to the IOT Platform.

    deviceid=a unique string which identifies the device to IBM Cloud (it matches the one registered to IBM Cloud – see later)

    btmac=XX:XX:XX:XX:XX:XX mac-address of the paired BT device (the phone, see later how to discover it)

    subscription=iot-2/cmd/switch/fmt/+ (topics where the cmds are sent to)

    host=<Org-Name>.messaging.internetofthings.ibmcloud.com (IBM Cloud, you will generate the <Org-Name> later)

    port=8883 fixed port number to support encryption. Otherwise, use 1883.

    password=<the Authentication Token from the registration>


    typeid=<DeviceType> (in my case I called it carproximity)

    organisation==<Org-Name> from the registration to IBM Cloud (see later)


  7. Raspberry setup: python scripts installation

    • The ”actuator” scripts allow the Raspberry to receive and execute commands (switch on and off the leds of the sense-hat accessory).
    • They are in directory btclient.d and are:

    run-command.py    Called by btclient.c, reads the env proprerties PAYLOAD and   TOPIC and execute the command

    switch-off.py          Switch off the led of the sensor hat

    text-scroll.py          Make the word “welcome home” to scroll on the led of the   sensor hat


  8. IBM Watson IOT Starter: create an instance

    • Login to IBM Cloud with your account (or sign up if you don’t have any)
    • Go to the Catalog and create an instance of the IBM IOT Platform starter setting a name:  I called it «IOT-Platform-CarHome» and choose to host it in one of the available datacenters (I chose Germany).
    • Three components are provisioned:
      1. A Node.JS runtime which will run the Node-RED app and will host the static files of the web dashboard
      2. An instance of the IBM Watson IOT Platform
      3. A Cloudant NoSQL database which will store the flows of Node-RED and optionally the Historical Data Storage of the IOT Platform
  9. IBM Watson IOT: customization steps summary

    1. Register the RPI to the IOT Platform generating the Device Credentials (Authentication Token)
    2. Enable the encryption of the comunication from the RPI
    3. Generate the API Authentication Token to be used later by the Node-Red app to access the API of the IOT Platform
    4. Pair the Bluetooth device (my phone in this case) to the RPI
    5. Import the Node-RED app to process the Bluetooth device data
    6. Create the web dashboard by enabling a Delivery Pipeline service
      1. add the html/js files to Node.js
      2. update the Node-RED flow adding the endpoint to read the signal strength sent by the device and to set a new threshold to switch on/off the lights
    7. Finally, run the POC

     You will now perform the above steps.

  10. 1. IBM Watson IOT: registration of the RPI

    • To launch the IOT Platform dashboard, from ghe IBM Cloud console:
      • Choose the <name>-iotf-service and press the “Launch” button, where <name> is the name (“IOT-Platform-CarHome” in my case) you gave to your instance of Watson IOT Platform
      • select the «Devices» entry from the left sidebar
    • First select from the top menubar the Device Types entry to group the devices
      • In my case I named it «carproximity» to group the devices which alert home when they approach
      • You can skip the “Device Information” section
    • Then select «Browse» from the top menubar and add the device, filling the following  fields:
      • Device ID  <the unique string that you put in file btclient.cfg>
      • Device Type  <the Device Type previously created, «carproximity» in my case>
    • You may also add some metadata information like the device model (Raspberry PI….)
    • Leave the Authentication Token empty in order to be automatically generated
    • Press Done
    • Please, take note of the Authentication Token generated, because it cannot be recovered afterwards
    • The organization will be set automatically and will be part of the hostname to connect to the IOT Platform
  11. 2. IBM Watson IOT: enable the encryption

    • We will enable the encryption of the comunication from the RPI to IBM CLoud
    • From the IOT Dashboard, select «Security» on the left
    • Edit the «Connection Security» item and set (or verify) the «Security Level» of  the «Default Rule» policy to:
      • TLS with Token Authentication
    • You may need to download the IOT Platform certificate (messaging.pem) from the IOT Platform documentation here and move it under the ~/projects/car@home/cert directory on the RPI
    • The C programs in the RPI are already instructed to use the correct Mosquitto APIs (MQTT client) in order to enable TLS
  12. 3. IBM Watson IOT: Generate the API Authentication Token

    • Select from the left sidebar of the IOT Dashboard, the «Apps» entry
    • Then press the «Generate API Key» button, set the role to “Standard Application”,  and take note of the generated:

    API Key

    Authentication Token

    • Because you need aftewards and are not shown anymore
    • This credentials will be used by the Node-RED app to access the IOT Platform
  13. 4. IBM Watson IOT: pair the Bluetooth device to RPI

    • The MAC address will be used by the app running on the RPI to connect to the BT device
    • Open a SSH session from your Laptop to the Raspberry (use Putty)
    • Login as pi and use the password you changed before or, if you didn’t, the password is raspberrypi
    • You need to know the name which the BT device uses to publish itself over Bluetooth
    • To find the MAC address and pair the BT device to the RPI, run the following sequence of commands from the ssh session on RPI:
      • # bluetoothctl
      • # power on
      • # scan on     and wait until your device name (your phone) pops up and take note of the MAC address to insert into the file btclient.cfg
      • # agent on
      • # pair <MAC address you’ve just written down>     probably you need to give a confirmation on your phone
    • It is preferable to do the above operations in a zone far from other BT devices
    • Here follows an example of the output from the scan on command:
    • [bluetooth]# scan on

      Discovery started

      [CHG] Controller B8:27:EB:84:YY:YY Discovering: yes

      [CHG] Device B4:E1:C4:XX:XX:XX RSSI: -64

      [CHG] Device B4:E1:C4:XX:XX:XX RSSI: -86         <– the mac address

      [CHG] Device B4:E1:C4:XX:XX:XX RSSI: -73

      [bluetooth]# exit 


      After pairing, you can verify giving the following command:

      pi@raspberrypi:~ $ bluetoothctl

      [NEW] Controller B8:27:EB:84:YY:YY raspberrypi [default] [NEW] Device B4:E1:C4:XX:XX:XX MyNokia222      <— the mac address with the name of the BT device


  14. 5. IBM Watson IOT: import the Node-RED app

    • The Node-RED app will gather the data coming from the device (our RPI)
    • It will elaborate the data to decide which command (if any) to send back to the RPI
    • If the strength of the BT signal received by the RPI from the phone will go above a preset threshold, the app will order the Sense Hat of the RPI to switch on the leds.
    • Conversely, when the signal strength falls below the threshold, the command is to switch off the leds


    • To create the Node-RED app, from the «IOT-Platform-CarHome» page of the IBM Cloud console select “Visit App URL” and follow the steps to secure the editor
    • A page with a flow appears: select all and delete it
    • Login using the credentials that you’ve just defined
    • Import the Flow1.json (it is a file in the Git repo under folder node-RED), by opening it with an editor and copying the whole content into the clipboard
    • In the Node-RED open the menu on the top-right corner and select Import –> Clipboard and paste the content of the Clipboard and then press Import
    • A new tab “Flow 1” is created (you can delete the previous Tab from the Menu by selecting  Flows –> Delete)
    • The Node-RED flow appears as follows:


    • You need to insert your API Key and the API Auth Token to send back the comand to the RPI through the IOT Platform
      • Double-click on the box “Appliance cmd”
      • Set ”API-key” as Authentication
      • Edit the ”API Key” field and insert
      • The name of the “API Key” and the “API Token” you generated previously in Step #12



  15. 6. IBM Watson IOT: the web dashboard

    • The end user has available a simple management console which shows the «proximity» of the car to home and the state of the lights (on/off)
    • The console allows also the user to change the threshold above which the lights are switched on
    • To create the web dashboard, from the «IOT-Platform-CarHome» page of the IBM Cloud console, scroll down a bit and enable the «Continuos Delivery» service
    • Keep everything with the default values
    • Press Create
    • It will automatically build a tool chain with different tools:
      • Issue tracker
      • Private github repo where to place/update the code
      • Eclipse Orion Web IDE which is a browser-based development environment where you can develop for the web.
      • Delivery Pipeline which is configured to pull the code from the github repo, to build and deploy it as soon as a new commit of the code is performed


    • Populate the Git in the toolchain with the files of the web dashboard (you should have already downloaded from my Git), by clicking in the Git box
    • The «IOT-Platform-CarHome» web dashboard is comprised of the following files which need to be uploaded under the public folder of the Git:
      • Logo.png  A placeholder for a logo
      • Lampadina.png  Icon of the bulb switched on
      • LampadinaOff.png  Icon of the bulb switched off
      • dash.html  Main html file
      • dash.js  Application logic written in javascript
    • dash.js has a loop to periodically (every 5 sec) Get the /status of the signal strength from the Node-RED application using an Ajax call and display it into the dashboard in the form of a gauge
    • Besides that, a form allows to set a new value for the proximity threshold


    Activation of the Toolchain:

    • To see the toolchain running, press into the box of the “Delivery Pipeline”.
    • The Build and Deploy process should start
    • Once completed the deploy stage, you will find the base URL to access the web app.
      • To launch thei dashboard just append the file /dash.html to the url showed



    The web dashboard:



  16. 7. Run the POC

    At this point you shoud have all pieces iof the POC n place and configured. Follow the steps below to run the POC.

    • Restart the app in the IBM Cloud and also reboot the Raspberry PI
    • Login to the IBM Cloud console and go to the Node-RED app by clicking in “Visit App URL”
    • Login to Node-RED
    • Click on the square of the “App init” component to initialize the flow




    • Login to the RPI from your laptop using putty
    • Go to directory ~/projects/car@home
    • Run the command ./btclient
    • A lot of messages start to appear. If your phone is close enought to the RPI you will see the following message:
      • Running command ‘ switch on ’
    • And the leds of the Sense-Hat should switch on
    • Access the web dashboard to see the gauge changing values while you move the phone far or close to the RPI



2 comments on"Connected car – Exploiting Raspberry to sense the proximity at home of your car"

  1. Christian27 June 16, 2018

    Well, after hours my RPI demo works.

    cert/message.pem is required. I don’t understand why? I use a new RPI Zero Stretch.
    I didn’t understand the different between device API and IoT API. Node-Red seems to talk via Application API Key..
    I need help for bluetooth from this page https://www.sigmdel.ca/michel/ha/rpi/bluetooth_01_en.html

    Compile of btclient works without -liw and sudo apt-get install libbluetooth-dev.
    I switched to displayotron hat because it’s cheaper and available. Thank you for keeping it in python.
    Why there are so many different languages: python, C, bash, JavaScript, node.js. Is this what you want to show?

    • Hello Christian27.
      I am happy to know that you run successfully the demo.
      Coming to your notes:

      you can find some information about the messaging.pem in the knowledge center of the IBM Watson IoT Platform: https://console.bluemix.net/docs/services/IoT/reference/security/connect_devices_apps_gw.html#client_port_security

      Actually Node-RED needs the Application API Key you generated in step #12.
      I updated the paper in order to accomplish this task just after the import of the Node-RED flow in step #14

      I updated the makefile and the package list in step #3 to include libbluetooth-dev.
      Apart from that package which is required, I didn’t have specific problems with the Blootooth connection of my Raspberry PI, but it is useful to know that in the case there is the guide that you mentioned that can help.

      There are many languages used across the POC, but I assume that most are well known. I reused from a previous work part of the programs that run in the RPI and that’s why they are in C

      Thank you

Join The Discussion