Connecting Devices – IoT Part 1

 

Getting Started…

I recently did a community event on IoT and thought to share my learning's.

My journey with IoT starts when I started working on Microsoft Azure Stream Analytic Service. It’s interesting how real time reporting brings lot of insights for business decision maker to get real sense of their business and make effective decisions. There are lot of real time services available like storm, MapR, Microsoft Azure Stream Analytic Service etc which help building real time solutions. I worked on Apache storm and when I started working on Azure Stream Analytic Services, one thought came to my mind which is “This is really simple”. I am not doing any comparison here which many people might be interested in. I will probably write another blog on such comparison but for now some information can be found here.  

During my research phase, I read lot about IoT (Internet of Things). So I started getting deeper and deeper and wonder how technology benefit world in real time. How this concept can help us to make a better world. There are lot of work going on in this direction.   

I started looking onto sensors, board, devices etc. so that I can get involved in it. I looked onto various boards available in the market like CubieBoard, Banana Pi, MinnowBoard Max, Intel Galileo, Raspberrypi etc. I bought raspberry pi because it’s cheap Smile and I thought to use it as my entertainment device at home. I setup my raspberry pi device with sensor and send all information to PowerBI through Microsoft Service bus and Azure Stream Analytics. Here are the steps.

When I received my raspberry pi 2 kit, here what I got in the packet.

1) Raspberry Pi 2 Model B with 1GB RAM, QuadCore CPU

2) 16 GB Micro SD Card with NOOBS (New out of box software)

3) Plastic Case

4) 5V 2Amp Power Adapter with Micro USB cable

5) High Quality HDMI Cable

6) 4Pole 3.5mm to 3 RCA Video/Audio Cable

7) Cat5E Network Patch Cable

image

The 16 GB Micro SD Card I received have ‘new out of box software’ includes various operating system like Raspbian, Pidora, OpenELEC, OSMC, RISC OS, Arch Linux. We can choose to install any one the OS provided in Micro SD Card. Though I have Raspbian on the card but I thought to format it, download the bits and install it. Here are the steps…

Setting up device…

1) Put SD Card in a card reader.

image

2) Put SD card reader into the laptop.

clip_image002

3) Format the card.

clip_image002[5]

4) Download Raspbian image on a separate disk.

5) I am using “win32 Disk Imager” to the write image (downloaded in previous step) on the card.

5

6) Put SD card to raspberry pi

6

7) Connect network, Power, Display, Keyboard and Mouse to raspberry pi and switch it on.

7

8) It will start booting up the device and pause incase you want to change anything in configuration. The default keyboard layout is British so you may want to change it. That can be done even after. Click finish.

8

9) Once it finish the configuration, it open the console to run queries. Type “startx” and press enter.

9

 

10) The above command will open Linux UI (below).

10

 

Next I want to test some code to connect to event hub and send events. Let’s explore how that can be done.

Installing Azure SDK for python…

11) Open LX Terminal.

11

 

12) Setting up python setuptools. Type following command sudo easy_install pip

13) Install azure sdk by running following command sudo pip install azure

12

14) Once installation is done. It’ll be good to reboot. Type sudo reboot and press enter.

15) After Reboot, it will prompt for login and password. Type pi as login and raspberry as password. Press enter.

16) T``ype startx and press enter.

16

Setting up Service on Microsoft Azure…

Before we exploring python code to send events to Event hub, we need to setup service bus on Microsoft Azure.

17) Open browser on raspberry pi or on any other device.

18) Goto https://manage.windowsazure.com

19) Login with your account which is associated with Microsoft Azure Subscription.

20) Once you login, Click on Service Bus and click CREATE A NEW SERVICEBUS. You can use existing service bus as well.

19

21) Enter Service Bus Name, Region, Type and Messaging Tier as mentioned below.

 20

22) Once service bus set up, double click on newly created service, click on Configure and copy the Primary Key which is required in next step.

WP_20150714_010

We are not going to create event hub. That will be done through python code. For more information on service bus can be found here.

Sending events from Python code…

23) Open Python IDE

 

WP_20150714_011

 

24) Click File, New Window

WP_20150714_012

25) Copy & Paste the following code

#import random because using one of the functions in code
import random
from azure.servicebus import ServiceBusService

# SharedAccessKeyName from Azure Portal
key_name ='RootManageSharedAccessKey'

# SharedAccessKey from Azure Portal
key_value=''

# Pointing to Microsoft Azure Service Bus
sbs = ServiceBusService('SERVICE_BUS_NAME', shared_access_key_name=key_name, shared_access_key_value=key_value)

# Event Hub Creation
sbs.create_event_hub('hubcreationfromlinux')

# Some randomg values
devicecounts = 5
eventcounts = 1000

for eventcount in range (1, eventcounts) :
deviceId = "dev-" + str(random.randrange (1, devicecounts))
temperature = str(float("{0:.2f}".format(random.uniform (0,50))))
sbs.send_event('hubcreationfromlinux', '{ "DeviceId":"' + deviceId + '", "Temperature":"' + temperature +'"}')
print "DeviceId : " + deviceId + " Temperature : " + temperature

The above code I put up in github here.

26) Click File –> Save

27) Click Run –> Run Module

28) Once code start executing, it will first create a Event hub and then start sending the events.

14

29) Switch to Microsoft Azure portal and click on newly created event hub. Under the dashboard, it will show events are coming up.

 

15

That’s all for now. I hope you like it (though it’s little longer Smile). I will write another blog on same line where I will showcase how to use sensors with raspberry pi and sending events to event hub and then how to present that information on PowerBI. Signing off….Have a great day!

- Eat Healthy…Stay Fit and Keep learning.