Monday, 18 January 2016

IBM Bluemix : IOT Tutorial

It’s been quite some time now that I have been playing with IBM Bluemix platform and applying it in various IOT projects.You can make your application on this platform and bind it to some services.The services can not only be used by the run time applications on bluemix but also can be accessed by programs running on remote machines.This tutorial will be on how to set up IBM Bluemix for using it's IOTF service.The client that I'll be using is a python client.

This particular implementation publishes the CPU utilisation(in percentage) of my computer to the IoTF cloud. Find the source code on my GitHub.

Pre-requisites:

1.You need to register on IBM Bluemix.

2.You should have Linux machine with paho-mqtt package installed for python.

3.Basics of MQTT.

4.Knowledge of Python programming.

So let’s get started!

Assuming that you have registered and are logged into IBM Bluemix,you will see a page something like this :
Few names that are important here : ORGANISATION , REGION, SPACE.

ORGANISATION – It is like your unique Identity on Bluemix.

REGION – There are two regions(US and UK).By default,my region was US.

SPACE – It is like a directory where your work is saved.All the apps and services are added to your space.I have named my space as “dev”.You can give any name.

After creating the space you can add apps or services to your account.I will add a service called Internet of Things which can be found in the CATALOG tab.This service gives you cloud platform to publish or subscribe the data.The data is published or subscribed using MQTT protocol.I will not elaborate on MQTT.As for now, you should know that it is a machine to machine light weight protocol for implementing Internet of Things.
Name your IoT service and Leave it unbound.Then hit create.
You will be asked to add a device.Now device is a “thing” which you want to get connected to the cloud.This “thing” will be called as device hereafter.Any device which intends to publish or subscribe the data needs to be registered first.We do it by adding this device.
Name your device.Device name will be referred to as device type.You can add description of your device.This is not necessary though.Hit next and you will be asked to add device ID.Now this can be any unique ID.Usually we give MAC address of the device.Any unique sequence of characters will do.Remember that “:” i.e, colons are not valid so you need to remove these from your MAC address.






Leave the authentication token as blank as it will be automatically generated.Hit next and you will see a page containing all the important credentials required to authenticate your device.You need to put this information in your code that runs on your machine.
That’s it.Now add this newly created device.

You will see your device added in the list.The status of the device initially will be something like this.



Now,I will run the python code on my machine and let’s see what happens.


You can see the data on the Bluemix as well.Click on the device name and refresh the prompted page.You will see the data being published by the machine/device.
IBM Bluemix provides various other interesting services and supports many runtime languages to build applications.Do explore them and keep learning!

If you found this blog post helpful please like and subscribe for more such articles.

Friday, 1 January 2016

Galileo-Link : Connect THINGS to Social Network

Hello people!The following project is a basic implementation of what can later give rise to a dedicated social network for sensors. Have you ever wondered how would it be if all your inanimate things can talk on social network like Facebook, twitter? Green-house updating it’s health on it’s timeline? This project is an effort to give social life to the “things”.

I have implemented the project using Intel Galileo board as the sensor gateway, you can use any other board running Linux OS with python installed. I tried making my home talk. That is, posting sensors data installed on Intel Galileo kept in my room to the Facebook account by name “Galileo Link” wall. So all we need is access to Facebook Graph API and a python script.

To access the Graph API, we need to create a Facebook App on facebook developers page. Now follow these steps:
1. Click Add a New App -> Choose platform WWW -> Choose a new name for your app -> Click Create New Facebook App ID -> Create a New App ID -> Choose Category -> Click Create App ID again.

2.Go back to Apps Dashboard -> Select the new app -> Settings -> Basic -> Enter Contact Email.

3.Go to Status & Review -> Do you want to make this app and all its live features available to the general public? -> Toggle the button to Yes -> Make App Public? -> Yes. This will enable others to see posts by your app in their timelines – otherwise, only you will see the wall posts by the app.

4.Now – you should see a green dot next to app’s name, and the text This app is public and available to all users.

5.Make a note of the App ID and App Secret (Click Show next to it; you will be asked to re-enter your Facebook password). Now that the app has been created, the next step is to get Facebook OAuth Token.

Steps to get Facebook OAuth Token :

1.Go to Graph API Explorer-> In the Application drop down -> Select the app created -> Click Get Access Token -> In Permissions popup go to Extended Permissions tab -> Select manage_pages, andpublish_actions These permissions will allow your app to publish posts acting as the page -> Click Get Access Token -> You will see a message saying “{App} would like to post publicly to Facebook for you. Who do you want to share these posts with?” -> I chose Public for maximum visibility – as I wanted to post to a public page.

2.Make a note of the short-lived token shown in Graph API Explorer.

3.Facebook has deprecated offline access, the next best thing is long-lived token which expires in 60 days. We will convert the short-lived access token noted above to a long-lived token. For that, fill in the values in the URL below and open it in a browser:


Replace the values in {}.

4.You should see access_token={...}&expires={...}. This new access_token is the long-lived token we will use in our Python script.

5.long-lived token will also expire eventually, be prepared to perform this Step 3 again before that happens!

6.We will use Facebook Python SDK to access Facebook’s Graph API. You can install it using pip: pip install facebook-sdk.

NOTE : Some might get AttributeError: ‘module’ object has no attribute ‘GraphAPI’ . This maybe because of bad installation or corrupt packages. Other facebook packages may have been installed and it might be a possibility that the python script is importing from wrong path.
You might want to go to those directories where Facebook instances are installed. To know the directory. create a python script :

import facebook
print facebook.__path__

The path to the facebook will be displayed.Go to the directory delete facebook packages and then re-install it again.Problem solved!

Now the next important thing is the page ID which can be found in About tab of the Facebook account (usually it is a big number). Finally the code on Intel Galileo will post to Facebook wall of the Account.
Follow this link to my github and find the python script for this project.
Here is the link to my Galileo-Link!
Do like,follow and share this blog post if you found it useful.