Azure IoT Hubs with Raspberry Pi and Node.js - Azure Bootcamp - 27-04-19
1. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
http://bit.ly/nottsiotvscodeVisual Studio Code
http://bit.ly/nottsiotputtyAn SSH Client
Any!A Web Browser
http://bit.ly/nottsiotvsessentialsAn Azure Subscription
Prerequisites:
Microsoft Azure IoT Hubs with Raspberry Pi and Node.js
http://bit.ly/nottsiotiotdeviceexplorer
Azure IoT Device
Explorer Twin
2. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Microsoft Azure IoT Hubs
with Raspberry Pi and
Node.js
PETE GALLAGHER
PJGCREATIONS.CO.UK
27/04/2019
3. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
About Me
Pete Gallagher
@pete_codes / @PJGCreations
www.PJGCreations.co.uk
Pete@PJGCreations.co.uk
IT Consultant
Decades of Desktop, Web & Embedded Software experience
Meetup Organiser
STEM Ambassador … Gadget Addict
Father of two inquisitive girls
4. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
What we’re going to be doing…
Basic understanding of IoT
The Hardware we’re using
The Software we’re using
GPIO and a description of the Circuit
Connecting to the Raspberry pi
5. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
What we’re going to be doing…
Introduction to Azure’s IoT Offering
Message Routing and Azure Logic Apps
Q&A
Closing…
6. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
What is IoT?
7. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
The Hardware we’re using…
Raspberry Pi B+
Upwards with
Node.js 6.0+
Some
Components
A BreadBoard Some Wires
Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2018
8. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
The Software we’re using…
Visual Studio
Code
An SSH Client A Web Browser An Azure
Subscription
> _
9. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
http://bit.ly/nottsiotvscodeVisual Studio Code
http://bit.ly/nottsiotputtyAn SSH Client
Any!A Web Browser
http://bit.ly/nottsiotvsessentialsAn Azure Subscription
Prerequisites:
Microsoft Azure IoT Hubs with Raspberry Pi and Node.js
http://bit.ly/nottsiotiotdeviceexplorer
Azure IoT Device
Explorer Twin
10. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
GPIO and the Circuit
LET’S GET HANDS ON
11. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Raspberry Pi GPIO
12. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Raspberry Pi GPIO
13. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Our Circuit and Parts
Part Qty
Breadboard 1
Red LED 1
Green LED 1
220Ω Resistor 2
Push Button 1
BME280 1
Jumper Wires 11
14. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 1
The Raspberry Pi
This is the heart of our circuit
and will be controlling how all of
the electronics work as well as
running our code.
15. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 2
Grab a Breadboard
This is what we’ll be using to
connect all of our components
together
16. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Insert the Red LED with it’s
shortest leg to the outside of the
breadboard, and it’s longest leg
to the hole next to it.
Don’t worry about making it
look the same!
LED’s have a polarity and only
work one way round.
Step 3
Add the Red LED
17. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 4
Connect LED Ground
Add a Purple Wire from the Red
LED short Leg to Pin 9 (Ground /
0v) .
This is one side of the circuit to
light the Red LED.
18. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Connect a resistor between the
Red LED Long Leg hole and the
adjacent hole.
This limits the current to the
LED, preventing the Pi Pin from
burning out.
Step 5
Add a 220Ω Resistor
19. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 6
Connect Red LED
Connect an Orange Wire to the
hole on the empty side of the
Red LED Resistor to Pin 10
(Wiring Pi Pin 16)
This is the control to turn our
Red LED On and Off
20. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 7
Add Green LED
Insert the Green LED with it’s
shortest leg to the outside of the
breadboard
It’s the same column as the Red
LED
Insert it’s longest leg into the
hole next to the Red LED
Resistor.
21. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 8
Add a 220Ω Resistor
Connect a resistor between the
Green LED Long Leg hole and
the adjacent hole.
This limits the current to the
LED, preventing the Pi Pin from
burning out.
22. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 9
Connect Green LED
Connect a Grey Wire to the hole
on the empty side of the Green
LED Resistor to Pin 11 (Wiring Pi
Pin 0)
This is the Control to turn our
Green LED On and Off
23. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 10
Add a Button
Connect the Button between the
two sides of the Breadboard
It will only fit in one orientation!
Don’t force it!
24. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 11
Add Button Control
Connect a White Wire from the
Right Hand Button Leg Hole to
Pin 26 (Wiring Pi Pin 11)
This is one side of our Button
Circuit, and how we’re going to
sense if the Button has been
pressed.
25. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 12
Add Button Ground
Connect a Black wire from the
Left Hand Button Leg Hole to
Pin 25 (Ground / 0v)
This is the other side of our
Button Circuit.
26. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 13
Add the BME280
This is our Temperature /
Humidity / Pressure Sensor
Insert the BME with the body
overlapping the gap, and the
pins in the bottom half of the
breadboard.
27. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 14
Add BME280 Power
Connect a Red Wire from the left
most BME280 Pin Hole to Pin 1
(3v)
This supplies the power for the
Sensor.
28. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 15
Add BME280 Ground
Connect a Brown Wire from the
Pin Hole to the right of the Red
Wire to Pin 6 (Ground / 0V)
This is our 0v and part of the
power to the BME280
29. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 16
Add BME280 Clock
Connect a Yellow Wire from the
Pin Hole to the right of the
Brown Wire to Pin 23 (Wiring Pin
14 / SCLK)
This is the clock for the SPI
(Serial Peripheral Interface)
30. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 17
Add BME280 Serial In
Connect a Grey Wire from the
Pin Hole to the right of the
Yellow Wire to Pin 19 (Wiring Pi
Pin 12 / MOSI)
This is the Serial Data from the
BME280 to the Raspberry Pi
31. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 18
Add BME Chip Enable
Connect a Green Wire from the
Pin Hole to the right of the Grey
Wire to Pin 24 (Wiring Pi Pin 10 /
CE0)
This allows the Raspberry Pi to
Enable the BME280 in case there
are multiple SPI devices
connected
32. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Step 19
Add BME280 Serial Out
Connect a Blue Wire from the far
right Pin Hole to the right of the
Yellow Wire to Pin 21 (Wiring Pi
Pin 13 / MISO)
This is the Serial Data from the
Raspberry Pi to the BME280
33. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Let’s Get Started…
34. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Instructions:
35. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Instructions:
Git Version:
http://bit.ly/pjg-azurebootcamp2019
Word Doc Version:
http://bit.ly/pjg-azurebootcamp2019-docx
36. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Connecting to the Raspberry Pi
LETS GET TOGETHER
37. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Flashing Our LED’s
TIME FOR SOME CODING!
38. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Using the Config File
WE DON’T LIKE NUMBERS
39. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Using the Config File
If you want to cheat…
git checkout 03_use_config
40. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Adding a Button
FLASHING IN A LOOP IS BORING
41. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Check that the circuit is working…
FLASH THOSE LIGHTS!
42. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Adding a Button
If you want to cheat…
git checkout 04_add_button
43. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
What is an Azure IoT Hub
HUH?
44. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
What is an Azure IoT Hub?
Provides Device to Cloud and Cloud to Device Messaging using a variety
of protocols including AMQP, MQTT, HTTP etc
Based on Event Hubs… But allows for 2 way communications.
Allows for 1 million simultaneous connections!
Stores a Registry of each IoT Device
Provides “Device Twin” functionality for Desired and Actual Properties
Device libraries available in a host of languages including .NET, JavaScript,
Node.js etc
Sits on the Edge of the Cloud between the IoT Devices and the full Azure
Stack
Connects to Service Bus, Event Hubs, Blob Storage, Ai etc etc
45. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
R
E
S
O
U
R
C
E
S
F
I
E
L
D
IoT and Azure
I
O
T
H
U
BIoT Hub
AZURE
46. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
What does it cost?
Edition Price Per Unit
(Per Month)
Max Number of
Devices
Total Number of
(Per Day Per Unit)
Message
Meter Size
Free Free 500 8,000 0.5KB
S1 £18.63 Unlimited 400,000 4KB
S2 £186.38 Unlimited 6,000,000 4KB
S3 £1,863.273 Unlimited 300,000,000 4KB
47. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
A Real World Example…
48. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Creating and Connecting to an Azure
IoT Hub
LET’S GET CONNECTED
49. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Configuring and Connecting to an
Azure IoT Hub
You’ll need…
Azure IoT Device Explorer Twin
http://bit.ly/nottsiotdevexplorer
50. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Configuring and Connecting to an
Azure IoT Hub
Follow along…
git checkout 05_connect_to_iot_hub
If a file is Read Only use…
cd..
sudo chmod 777 NottsDevIoTHub -R
cd NottsDevIoTHub
51. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Configuring and Connecting to an
Azure IoT Hub
Create a file called security.json containing;
{ "SharedAccessKey": "" }
52. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Using a VS Code extension in place of
the Device Explorer (or Node!)
ALL TOGETHER NOW…
53. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Sending a Message to the IoT Hub
HELLO FROM THE OTHER SIDE
54. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Sending a Message to the IoT Hub
Follow along…
git checkout 06_send_iot_hub_msg
If a file is Read Only use…
cd..
sudo chmod 777 NottsDevIoTHub -R
cd NottsDevIoTHub
55. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Sending a Message to the IoT Hub…
Using a Button
WE’RE IN CONTROL
56. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Sending a Message to the IoT Hub…
Using a Button
Follow along…
git checkout 07_send_msg_on_button
If a file is Read Only use…
cd..
sudo chmod 777 NottsDevIoTHub -R
cd NottsDevIoTHub
57. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Receive Cloud to Device Messages
ARE YOU LISTENING TO ME?
58. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Receive Cloud to Device Messages
Follow along…
git checkout 08_receive_msg
If a file is Read Only use…
cd..
sudo chmod 777 NottsDevIoTHub -R
cd NottsDevIoTHub
59. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Invoke a Method on the Device
DO WHAT I TELL YOU TO DO!
60. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Invoke a Method on the Device
Follow along…
git checkout 09_invoke_method
If a file is Read Only use…
cd..
sudo chmod 777 NottsDevIoTHub -R
cd NottsDevIoTHub
61. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Add in the Temperature Sensor
IS IT HOT IN HERE?
62. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Add in the Temperature Sensor
Follow along…
git checkout 10_add_BME
If a file is Read Only use…
cd..
sudo chmod 777 NottsDevIoTHub -R
cd NottsDevIoTHub
63. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Routing Messages & the Service Bus
SAT NAV ENGAGED
THIS WAY
64. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
R
E
S
O
U
R
C
E
S
F
I
E
L
D
IoT and Azure
IoT plays nicely with Azure…
I
O
T
H
U
B
IoT Hub
Routes
Service bus
Queue
Topics
Relay
65. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Add in the Temperature Sensor
Follow along…
git checkout 11_add_routes
If a file is Read Only use…
cd..
sudo chmod 777 NottsDevIoTHub -R
cd NottsDevIoTHub
66. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Logic Apps
SPOCK WOULD BE HAPPY
67. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
R
E
S
O
U
R
C
E
S
F
I
E
L
D
IoT and Azure
IoT plays REALLY nicely with Azure…
I
O
T
H
U
B
IoT Hub
Routes
Service bus
Queue
Topics
Relay
68. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
What are Logic Apps?
Provides a Graphical Interface to Hook together a bunch of Apps, Services,
Workflows and functions.
No code required!
REALLY cheap - £0.000019 per execution!
Can hook together an almost unlimited combination of items to perform
just about any Workflow
For example
Twitter Timeline Sentiment Analysis - Populate Power Bi Dashboard to help
improve customer service
Order Approval – Route order types to the correct people
Monitor feedback forms and email the correct people
73. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
More Tools
IoT Extensions for VS Code: http://bit.ly/nottsiottoolkit
Device Explorer (Windows): http://bit.ly/nottsiotdevexplorer
74. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Further Viewing
The Connected Cow: http://bit.ly/nottsiotconnectedcow
http://bit.ly/nottsiotconnectedcowfd
MVA Introduction to IoT: http://bit.ly/nottsiotmvaiot
Azure Friday Azure IoT Hub: http://bit.ly/nottsiotazurefriday
Azure IoT Deep Dive: http://bit.ly/nottsiotazuredeepdive
75. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
Questions?
76. Raspberry Pi + Azure IoT Hubs + Node.js – Pete Gallagher – PJG Creations - 2019
What could possibly go
wrong?
Hinweis der Redaktion
We all know what the “Internet” in IoT is…
The Things of course are things like…
Fourth Industrial Revolution
General Purpose Input Output.
Pi’s way of connecting to the Physical World.
Power connections.
GPIO Pins.
Pin 1 is the pin closest to the Display Connector at the Top.
Pin 40 is the pin at the diagonal opposite.
Other collections of pins for Serial Communications
Wiring Pi Numbering in Grey.
Matches the Code we’ll be using
Hold up the parts
Sharing our Laptop’s Wifi Connection, so we can connect to our Pi and the Pi can communicate out to the cloud.
I’ll be using a PC… However, if you’re on a Mac…
I’ll be using PutTy… However, if you’re on a Mac, you can use Terminal;
Finder -> Go -> Utilities -> Find Terminal
ssh iothub1
Field… Or “The Edge”! (Not the guitarist from U2)
Field = “Things”
Air Conditioning, Logistics Fleets, Cows
IoT Hub on Edge of Cloud
Provides an interface between the field and Azure
Messages In (Device to Cloud), shipped off to Azure Resources
Messages Out (Cloud to Device) to “Things”
If “Thing” isn’t capable, could have a Field Gateway to do comms
Remote Bluetooth sensors controlled from pc…
Internet = Azure = “Processing”
Only 1 Free IoT Hub per subscription.
Price Per Unit is price per 8,000, or price per 400,000, or price per 6,000,000, or price per 300,000,000.
Every Message charged at 4Kb.
Be Careful! Message Meter Size is total size of message per unit… Bigger sizes use more messages in multiples of Message Meter Size.
Can send a just over a million messages a day for a whole month for just over £100.
Worked Example:
A device ingests the 1 KB device-to-cloud message size data per minute to the IoT Hub. The solution backend invokes a method (with 512 bytes of payload) on the device every ten minutes to trigger a specific action. The device responds to the method with a result of 200 bytes.
The device consumes 1 message * 60 minutes * 24 hours = 1440 messages per day for the device-to-cloud messages, and 2 requests plus responses * 6 times per hour * 24 hours = 288 messages for the methods, for a total of 1728 messages per day.
Tower Block – Air Con in Every Room
Record Data from 1 device, 1 floor, several floors etc etc
Possible to interrogate 1 unit
Update 1 unit, or a floor or whole building.
Using Azure Portal to create an IoT Hub.
Make sure you have your Azure Subscription up and running
Other ways to create an IoT Hub are;
Using an ARM (Azure Resource Manager) Templates
Using node and the cli
Using the Visual Studio Extension – IoT Hub Toolkit
Field = “Things”
Air Conditioning, Logistics Fleets, Cows
Edge = “Routing”
Queue = one to one
Topic = One to Many (Pub Sub)
Relay = Secure connection from on prem to Internet
Internet = Azure = “Processing”
Field = “Things”
Air Conditioning, Logistics Fleets, Cows
Edge = “Routing”
Internet = Azure = “Processing”