You are on page 1of 24

Azure & Raspberry Pi

Please Follow my Youtube Account for video tutorials.


http://www.youtube.com/c/FaisalMehmoodAvan
Also follow me on my LinkedIn Account
https://kr.linkedin.com/in/faisalavan

Contact Information:
Phone: +82-10-6876-6805
Email: faisal.avan@Hotmail.com
Table of Contents
S.No Topic

1. Create an IoT hub

2. Register a device for Raspberry Pi in your IoT hub

3. Set up Raspberry Pi

4. Run a sample application on Pi to send sensor data to your IoT hub

5. Configure the sample application

6. Monitor device-to-cloud messages

7. Save IoT hub messages to your Azure blob storage


Create an IoT hub (1/4)
1. Sign in to the Azure
portal.
2. Select New in the Left
Pane of the Microsoft
Azure Dashboard
3. Select Internet of Things
in the Azure Marketplace
Section
4. Select IoT Hub in
Featured Section
Select New > Internet of
Things > IoT Hub.
Create an IoT hub (2/4)
5. In the IoT hub pane, enter the following
information for your IoT hub:
• Name: Create a name for your IoT hub. If the name
you enter is valid, a green check mark appears.
• Pricing and scale tier: For this tutorial, select the F1
- Free tier or Pay-As-You-Go. For more information,
see the Pricing and scale tier
(https://docs.microsoft.com/en-us/azure/azure-resource-
manager/resource-group-portal )
• Resource group: Create a resource group to host
the IoT hub or use an existing one. For more
information, see Use resource groups to manage
your Azure resources (https://docs.microsoft.com/en-
us/azure/azure-resource-manager/resource-group-portal )
• Location: Select the closest location to you.
• Pin to dashboard: Check this option for easy access
to your IoT hub from the dashboard.
6. Click Create. Your IoT hub might take a few
minutes to create. You can monitor the progress in
the Notifications pane.
Create an IoT hub (3/4)
Now that you have created
an IoT hub, locate the Note Down the Hostname

important information that


you use to connect devices
and applications to your IoT
hub.
7. After your IoT hub is
created, click it on the
dashboard. Make a note of
the Hostname, and then
click Shared access policies.
Create an IoT hub (4/4)
8. In the Shared access policies pane, click the iothubowner policy, and then copy
and make a note of the Connection string of your IoT hub. For more information,
see Control access to IoT Hub. (https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-
devguide-security )

Also note Shared Access Key

Connection String will be used to


connect Raspberry Pi with Azure
Register a device in the IoT hub for your device (1/2)
1. In the Azure Portal (https://portal.azure.com/#dashboard/private/4687ca07-ebf6-458a-aba0-4f85374b500f ), open your IoT hub.
2. Click IoT Devices.
3. In the Device Explorer pane, click Add to add a device to your IoT hub. Then do the following:
Device ID: Enter the ID of the new device. Device IDs are case sensitive.
Device Id is used to connect raspberry pi tot IoT hub.
Authentication Type: Select Symmetric Key. Auto Generate Keys: Select this check box.
Connect device to IoT Hub: Click Enable.

4. Click Save
Register a device in the IoT hub for your device (2/2)
5. After the device is created, open the device in the Device Explorer pane.
6. Make a note of the primary key of the connection string.
Note:
Device Id will be used to connect raspberry pi to IoT hub. We will need Connection
String and Shared Access Key.
Set up Raspberry Pi
• Connect the sensor to Pi, use the breadboard and jumper wires to
connect an LED and a BME280 to Pi as follows.
• BME280 is a sensor for temperature, barometric, pressure, and
humidity. Following table shows connecting hardware
Module PCB Desc GPIO Header Pins
VCC 3.3V P1-01
GND Ground P1-06
SCL 12C SCL P1-05
SDA 12C SDA P1-03
Run a sample application on Pi (1/4)
1. Connect to your Raspberry Pi with IP of the Raspberry Pi
one of the following SSH clients
from your host computer: We
have used PuTTY to access
Raspberry Pi remotely.
2. Make sure SSH is enabled in
Raspberry Pi
3. The default Username is “pi” and
password is “raspberry”
4. To enable SSH in raspberry pi
Follow the following steps
1. Open Command Terminal
2. sudo raspi-config
3. Select Interfacing Option
4. Select P2 SSH
5. Select Enable and Click OK
Run a sample application on Pi (2/4)
• To Find the ip of Raspberry Pi
• Connect the Raspberry pi with the Internet
• Open Command terminal
• Write the following Command
• Sudo ifconfig
• Note down the IP of the raspberry pi to connect via SSH
Run a sample application on Pi (3/4)
2. First check nodejs and npm To check
node .js version run the following
command
sudo nodejs --version
sudo npm --version
If node.js is not install, then install the
latest version of node.js and npm by
running following command.
sudo apt-get install nodejs
sudo apt-get install npm
If Node.js version is lower than 4.x then
run the following command
(node –v; curl -sL http://deb.nodesource.com/setup_4.x |
sudo -E bash; sudo apt-get -y install nodejs)
3. First Install git on raspberry pi, To
Install git run the following command
sudo apt-get install git
Then Clone the sample application.
(git clone https://github.com/Azure-Samples/iot-hub-
node-raspberrypi-client-app)
Run a sample application on Pi (4/4)
4. Install all packages for the
sample.
The installation includes
• Azure IoT device SDK
• BME280 Sensor library
• Wiring Pi library.
cd iot-hub-node-raspberrypi-client-
app
sudo npm install
Configure the sample application (1/4)
5. Open the config file by running the
following commands: We have used Nano
editor to edit config file.
sudo nano config.json
There are two items in this file you can
configure.
1. The first one is interval, which defines the
time interval (in milliseconds) between
messages sent to the cloud.
2. The second one is simulatedData, which is
a Boolean value for whether to use
simulated sensor data or not.
Configure the sample application (2/4)
6. If you don't have the sensor,
set the simulatedData value to
true to make the sample
application create and use
simulated sensor data.
7. Change the Device ID. Device
ID is the ID which we Added in
the Microsoft Azure IoT Hub.
Refer To Slide 7 to know the
Device ID
Now Save by following
Command
Ctrl + 0
And press Enter
Now exit nano editor by
following command
Ctrl + X
Configure the sample application (3/4)
8. Run the sample application by running the following
command:
sudo node index.js '<YOUR AZURE IOT HUB DEVICE CONNECTION STRING>‘
Refer To Slide No. 6 for connection string
sudo node index.js 'HostName=my-hub.azure-
devices.net;DeviceId=raspberry-
pi;SharedAccessKey=OhJ3uXxxK54BDEI9u/jO5iCmG051dOeGBJuOP/r3
ZGI='
Have a “unable to determine hardware version” error.
Configure the sample application (4/4)
9. Solution: Solve the error by
updating the firmware to the latest
version, reverting fixed it using this
command:
sudo rpi-update
52241088c1da59a359110d39c1875cda5
6496764
Then it is able to send data to
Azure IoT Hub:
Monitor device-to-cloud messages using iothub-explorer
To monitor messages that are sent from your device to your IoT hub, follow these steps:
1. Open a console window (bash)
2. Run the following command:
iothub-explorer monitor-events <device-id> --login "<IoTHubConnectionString>“

Note: Refer To Slide 6 for Connection String and SharedAccessKey. Refer to Slide 7 for Device
ID.
iothub-explorer monitor-events raspberry-pi --login "HostName=my-hub.azure-
devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=GZNNngcIvMi2B9mt6XksJ
B99p83QtgfCPwvIYQPbM9A="

Hostname my-hub.azure-devices.net

IoT hub owner Connection HostName=my-hub.azure-


string primary key devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=GZNNngcIvMi2B9mt6XksJB99p83QtgfC
PwvIYQPbM9A=

Device ID raspberry-pi

Device connection string -- HostName=my-hub.azure-devices.net;DeviceId=raspberry-


primary key pi;SharedAccessKey=OhJ3uXxxK54BDEI9u/jO5iCmG051dOeGBJuOP/r3ZGI=
Save IoT hub messages to your Azure blob storage (1/5)
1. Create an Azure storage account. In
the Azure Portal,
click New > Storage > Storage
account > Create.
2. Enter the necessary information for
the storage account and click create.
Name: The name of the storage
account. The name must be globally
unique.
Resource group: Use the same
resource group that your IoT hub
uses.
Pin to dashboard: Select this option
for easy access to your IoT hub from
the dashboard.
Save IoT hub messages to your Azure blob storage (2/5)
3. Add storage as a custom endpoint. Navigate to your IoT hub in the
Azure portal. Click Endpoints > Add. Name the endpoint and select Azure
Storage Container as the endpoint type. Use the picker to select the
storage account you created in the previous section. Create a storage
container and select it, then click OK.
Save IoT hub messages to your Azure blob storage (3/5)
4. Add a route to route data to storage. Click Routes > Add and enter a
name for the route. Select Device Messages as the data source, and select
the storage endpoint you just created as the endpoint in the route. Enter
true as the query string, then click Save.
Save IoT hub messages to your Azure blob storage (4/5)
5. Verify your message in your storage container.
• Run the sample application on your device to send messages to your IoT
hub.
• To Run the sample application, Refer To Slide 17
• Choose the storage you created on dashboard, click Blobs, choose you Blob
service, open the folders, you can see the files saved in cloud. You are able
to click Download to download the file to local.
Save IoT hub messages to your Azure blob storage (5/5)
The downloaded message file:
Contact Information

• Please Follow my Youtube Account for video tutorials.


• http://www.youtube.com/c/FaisalMehmoodAvan
• Also follow me on my LinkedIn Account
• https://kr.linkedin.com/in/faisalavan

• Contact Information:
• Phone: +82-10-6876-6805
• Email: faisal.avan@Hotmail.com

You might also like