IoT, Raspberry Pi, Technical Stuff

Turning on a LED light on a Raspberrry Pi from Bluemix

My last post was about how to start with a Raspberry PI and all that was because I’ll explain you now how to turn on a LED light on a Raspberry Pi from Bluemix. Check my last post in here:

How to start with a Raspberry Pi? (In Spanish)

So, Where to start?

Wiring everything up

First of all, let’s connect and wire everything up into the Raspberry Pi. Below is the diagram for the Raspberry Pi:

raspberrypiDiagram

I used the GPIO port 17 which is pin 11 for my output where I’ll get the LED light turned on. So, How did I wire the LED light up? You will need below items:

  • 1 resistance of 220Ω
  • 1 led light
  • 1 breadboard
  • Jumped wires
  • GPIO to breadboard interface board
  • Ribbon cable

I recommend you to get a starter kit where you will have all these items, such as: Canakit Raspberry Pi 2. So, let’s start:

  1. We need to connect the ‘Canakit GPIO to breadboard interface board’, like this:
    idetoproto1
  2. Now we connect with the ribbon cable, the Raspberry PI to the breadboard by using the adapter (GPIO to breadboard interface board):
    pitoproto
  3. Let’s wire the LED light. Connect the LED light from the shorter side to the ground rail (blue/-) by using the 220Ω resistance (one side of the resistance will go to the ground rail (blue/-) and the other side will go to the shorter side of the LED light).
    ledtoground
  4. Now, let’s connect the larger side of the LED light to the GPIO output port of the raspberry pi. In this case I used the GPIO #17, which is pin #11:
    allwireup1
  5. Finally, let’s connect our raspberry pi to the electric socket. We are ready to test our LED light.

More tuning into our Raspberry Pi (Installing IoT kit)

So, let’s log into the Raspberry Pi through ssh and let’s make some updates and installations:

  1. First, let’s update our OS by running:
    sudo apt-get update
    sudo apt-get upgrade
    You will need to say “Y” to the update to proceed.
  2. I installed a helper library for using GPIO ports:
    git clone git://git.drogon.net/wiringPi
    cd wiringPi
    ./build
  3. And to install the IoT Kit into the RaspberryPi, I based my process on this procedure: Install IoT Gateway Kit (This will install IoT service, node-RED and Informix into the PI)
  4. Get sure that your iot service is running with: “sudo service iot status” or start it with “sudo service iot start”
  5. After installing the IoT Kit and running our iot service, we need to get the “Device ID” which we will need to register the device into the IoTF. Run below command to get it:
    “sudo service iot getdeviceid”
    #example output:
    The device id is b848eba3b390

Now, let’s go to Bluemix.

The application on Bluemix

Ok, we have everything wire up and ready to test our LED light  turning on and off, but, How are we going to do that? In this case I’ll use a little program running in Node-RED and hosted into a Bluemix application. Let’s start to create it.

  1. Log into Bluemix, by using below url: Bluemix
  2. Go to “Catalog”, and under Boilerplates select the “Internet of things Foundation Starter” boilerplate.
  3. At the right side, fill in the field of “Name” with the name you want for your application and click on “Create”. It will take a few minutes to be created but once created you will get the “Route” (url address) of your new application, which will be a Node-RED instance.
  4. Let’s now bind the IoTF service to our new application, which will be the service that will allow us to register our Raspberry Pi and the one that will allow the communication between the device and Bluemix. Go to your application overview (left side menu) and click over “Add a service or Api”
  5. This will prompt you to the catalog of services. Go to the internet of things section (which is at the bottom of the page) and select “Internet of Things Foundation” service.
  6. You will get some information like service name, the application that the service is bound to and the selected plan, in this case “Free”, do not change anything and click on “Create”. Note: When you create a IoTF service a linkage between it and Bluemix was created in a form of an API-key.
  7. You will be asked to restage the application, click at “Restage” and wait for a moment. We have now a Bluemix application with the IoTF service added and running.

So, let’s start registering devices.

The Internet of Things Foundation

As I told you before, the IoTF gateway is the one that will allow the communication between our device (Raspberry Pi) and our Bluemix Application, but first, we need to register our device. Let’s see how…

  1. Let’s get back to our application overview (left side menu) and let’s click in our new created “Internet of things foundation Service”.
  2. On the section “Connect your devices”, click on “Launch dashboard”. You are now on your IoTF organization dashboard where you can register your devices. An organization is created for every IoTF service you create.
    Note: To check the API-key created to link your Bluemix application and the IoTF go to: Access tab -> Bluemix Apps

Creating a device type

  1. Go to the “Device” tab
  2. Click on “Add Device”
  3. If you haven’t created a device type before, then we need to do that first. So, click on “Create device type”
  4. Enter a name. Example: RaspberryPi2. This is the only important field to fill in so in the next pages just click “Next” until to click the “Create” option.

Once we have created a device type then we can register one.

Registering a device

  1. Choose the type of device to register, in this case, “RaspberryPi2” (the one we just created) and click “Next”.
  2. Enter de “Device ID”. You got this one in previous steps and if you didn’t you can get it by running “sudo service iot getdeviceid” on your pi’s terminal.
  3. Click “Next” to all incoming steps and finally click “Add”
  4. Take account of the information details you got at the last step of your new registered device, it’s important. Example:
    details

One the device is registered we can start to work into Node-RED creating the flows that will turn on the LED light.

Creating Node-RED flows

We will have two instances of Node-RED running, one into the Bluemix application and another one running into the raspberry pi, and they will communicate to each other through the IoTF gateway where we registered our device. This is basically the communication flow:

commflow1

And all the communication is done by two nodes designed for the IoTF, one for input and another for output:

nodesiot

Bluemix Node-RED instance

To enter to your Bluemix Node-RED instance, go to the overview of your application and click at the url located at the side of “Routes”, which is your application’s url. This will take you to the Node-RED dashboard where we can start building our flow. I built up the following flow:

flow1

Follow these steps to build above flow:

  1. From the “Input” section, drag to the dashboard two nodes of “inject”.
  2. Double click on the nodes and on “Payload” field select “String” and below that set “1” for one node and “0” for the other one.
  3. From the “Output” section, drag to the dashboard one node of “degub”.
  4. Make double click on the node and on the “Output” field select “complete msg object” and click “Ok”
  5. From the “Output” section, drag to the dashboard one node of “ibmiot”.
  6. Double click on the “ibmiot” node and fill in as below:
    1. Authentication: “API Key”
    2. API Key: “Add new ibmiot”
      1. Click on the pencil
      2. Set a name for it like: “IoTF”
      3. For API Key and API Token go to your bluemix application overview dashboard
      4. Click on “Show credentials” on your Internet on Things Foundation Service
      5. Get the “apikey” and “apitoken” information.
      6. Get back to the ibmiot node on Node-RED and set this information there.
    3. Output Type: “Device event”
    4. Device Type: In this case, “RaspberryPi2”, but you have to set this up exactly as you set it up when you created your device type.
    5. Device ID: Your device ID, something like: b848eba3b390
    6. Event Type: “on”
    7. Format: “json”
    8. Data: “1”
    9. Click “Ok”
  7. Wire everything up like the image and finally click on deploy.

We have everything ready from the side of Bluemix and the IoTF, now let’s see the side of the Raspberry Pi.

Raspberry Pi Node-RED instance

To access the Node-RED instance running in our PI let’s go to: “http://raspberrypi-IP-address:1880/”. Example: http://192.167.14.2:1880/#

There is one major difference between the Node-RED instance running on Bluemix and the one running on the Raspberry Pi and that is that the one running on the PI has three nodes specially designed for the use of the Raspberry Pi GPIO ports, these nodes are the ones that will help us to send data to the Pi for a specific GPIO port.

gpioports

Ok, let’s create below flow to turn our LED light up:

flow2

Follow these steps to built above flow:

  1. From the input section, drag to the dashboard, one node of “ibmiot”.
  2. Double click on the “ibmiot” node and fill in as below:
    1. Authentication: “API Key”
    2. API Key: “Add new ibmiot”
      1. Click on the pencil
      2. Set a name for it like: “IoTF”
      3. For API Key and API Token go to your bluemix application overview dashboard
      4. Click on “Show credentials” on your Internet on Things Foundation Service
      5. Get the “apikey” and “apitoken” information.
      6. Get back to the ibmiot node on Node-RED and set this information there.
    3. Input Type: “Device event”
    4. Device Type: In this case, “RaspberryPi2”, but you have to set this up exactly as you set it up when you created your device type.
    5. Device ID: Your device ID, something like: b848eba3b390
    6. Event: “on”
    7. Format: “json”
    8. Click “Ok”
  3. From the “Output” section, drag to the dashboard a node of “debug”.
  4. Double click on the node and on the “output” field select “complete msg object” and click “Ok”.
  5. From the “Raspberry_Pi” section, drag to the dashboard a node of “rpi-gpio out”.
  6. Double click on the node and on the “GPIO Pin” field select the port where you will send data, in this case, “11-GPIO0-BMC17”, which means: “pin=11 / port=17”, and click “Ok”.
  7. Wire everything up like the image above and finally click on “Deploy”

We have now everything set up and ready to test. Go back to your Bluemix Node-RED instance and start testing your application by clicking on the “1” and “0” nodes, this will send a digital 1 and digital 0 to turn on and off the LED light. You will see the information sent through the IoTF gateway on the debug section of the Bluemix Node-RED dashboard:

 debugflow1

This information will be received through the IoTF gateway to the Raspberry Pi Node-RED instance and you will see something like this on the debug section:

 debugflow2

Of course you will also get to see your LED light going on and off as long as you keep clicking on the “1” and “0” nodes at your Bluemix Node-RED instance.

ledturnedon
That’s it, as easy as that, the new IBM Platform as a Service called Bluemix rocks¡

Please feel free to add any feedback. Thanks for reading.

Briana S.

One Commnet on “Turning on a LED light on a Raspberrry Pi from Bluemix

Leave a Reply