We have here, raspberry pi connected to
a relay and there's a DHT11
temperature humidity sensor.
And what's happening,
is that this IoT
rig actually has data coming
from the DHT sensor being sent to
Azure IOT Hub.
Now, if you're familiar with Azure and
IoT stuff,
you know, the IOT hub is like the entry level for
everything two-way or bi-directional communication
with your IOT device and the Cloud.
So we have this temperature data being
sent through the PI over the IoT Hubs into
the IoT Hubs.
And now what we want to do is we've added
this um high voltage fan here, and caution
a risk of electric shock - this actually is
240 volts so be careful when you're dealing
with that.
What's happening is that we're going
to see how we can use IoT Hub and
some Power Apps to actually trigger this
on and off.
I'm going to share with a couple of
codes on how to do this.
I have a couple of series on my blog
posts and my YouTube channel on how to
do it. But let's get right into the
code. So I'm gonna share bits of code
for you here to understand what's happening.
So right here we have the code that
actually is written out.
This is where,
you know, being a Pro-Dev comes in.
Now, if you're familiar with most of
the Azure
APIs and services,
there are SDKs for different languages,
whether you use your writing node,
you're writing java,
you're writing python.
There are always different services for
you to make use of.
So here, I'm using the node version of
the SDK for IoT Hubs. And this is simply
the code that is running currently on
the Raspberry Pi.
So what we have here is we've set up
a couple of commands which we call directs
and location commands on uh system here
and then two of them actually.
So the first one here,
as you can see is toggle
fan on.
And then this one is toggle LED
So these two commands allow our Raspberry
Pi to actually respond to comments that
are sent by external services or even
through IoT Hub itself.
So I'm going to go ahead and then I'm
going to run this command.
This actually is running locally on
a Raspberry Pi and thanks to the awesomeness
of Visual Studio Code
I'm able to remote into the Raspberry Pi.
You can see from the bottom left,
I'm remote it into the Raspberry Pi
and then running the code in there.
So let's go ahead and run this.
I walk you through what this code basically
does, so you get a good understanding
of that, so I'm running the code now
which is running on the Raspberry Pi
and if you look closely you realize that
it sends temperature and humidity
data to the Azure IoT Hub - it
does that in intervals of about five seconds,
so now I've implemented a technique that
doesn't actually overwhelm the IoT
hub in terms of messages,
it only checks to see if there is a
change in the previous temperature before
sending the data,
you know,
into IoT Hub for processing.
Now, what we want to layer on top of
this is that now this could be an industrial
system which you want to monitor,
data, telemetry coming from it and then
act upon that data.
So here we're gonna mimic this fan.
Now this fan could be an air conditioner,
This fan can be any IoT device that
you want to control.
So at the moment Pi is talking to
Azure IoT Hub and it knows how to,
you know,
talk to this device.
So now let's go ahead into the second
piece of code which we're going to
use to trigger and help manage - before that
if you're not familiar,
you don't necessarily need to go directly
into, let's say, Azure Portal to do that.
There is a new tool which allows
you to do that.
It's the IoT Explorer.
Now what this does is that it allows you
to actually access all of your IoT devices in
your IoT Hub.
So here I think already have a connection string,
so I'm going to go ahead and connect. Now,
once I connect with this -
all along,
what we're doing here is Pro-Dev stuff,
you know,
You build your IoT solution,
you've deployed it, you want to test
it out. This is all just spruced up.
Now, the big question becomes now,
how can I rapidly or quickly interact with
this service without having to go through
a lot of hoops?
Now
that is where the power of Power Platform
comes in today,
I'm going to show you how to use that
with the Power Automate and potentially,
you know, just connect that to a Power App and
then you build a complete dashboard where
you can control your IOT devices from
anywhere you are in the world.
So currently I have my Raspberry Pi,
that's my only device when I connect that.
As you can see, everything looks good,
let's go to the diagnosis and implication. And
to do this.
I mentioned that we have- let's just
bring in our... yeah.
so we have toggle fan
right? We have this open for us.
So this is the method we want to invoke
now. So we put in the method name here
now, what happens is that I have designed
this to take two states,
you know, an on state,
or an off state.
So let's go ahead currently.
So to do this,
I'm gonna flip so you can see the two
things happening.
So when I send an "on" here and this
is this is going through the IoT Explorer.
So when I send this "on" here,
let's do this.
There we go.
And then we call the invoke.
We call invoke.
If you look to your right,
you should see that the fun is running.
Isn't that cool?
So the whole idea is that the DHT detects
the changes in the temperature and
the humidity in the room and then automatically
sends this command for my device to,
you know,
turn on and off.
Ideally, we can also use this to invoke
it now to turn this off.
Quite simple.
I'm just going to send a payload of off.
We invoke this and as you can see the
fan goes off.
Yeah, this is,
this is all cool stuff.
Right? So where does the citizen developer
come in?
This is where we're gonna,
you know,
have some fun.
So I have gone ahead
and created.
We're good. We're still going to leave
the code running on the Pi just so
we can have some fun.
So I've gone ahead to actually create
an Azure Function.
So I created this locally.
And I'm also going to test this locally
just to be sure that,
you know,
we're hitting the correct end point and
doing the right things.
So I'm going to go ahead.
And if you love Azure Functions, like
I do, you know that you can run and test
this locally and have lots of fun.
So I'm going to test this locally.
Now, FYI, I've already deployed this into
Azure Functions in Azure.
So we're gonna use that in in connecting to
Power Automate and Power Apps.
So I just want to show you that this
works. And as a pro developer,
the next thing will be,
"Hey, Sam, we've built the IoT
thing - it is working fine.
What we want to do is we want the ability
for you to create a UI to interface so
we can remotely turn on and off the
fan. Well,
with this done,
this is this is running locally.
So let's open this up,
let's copy this,
bring this here and here-
go ahead and send.
Let's end the state off... on.
The state of on and look at that!
Now let me zoom this in.
Let's zoom this in.
So you can have a good look at this.
So right now,
as you can see this is just a simple thing,
Let's run zoom in.
So we can give you guys,
I can give you guys a much interesting look
here. There we go.
So if you look closely,
all I did was to call this endpoint and
pass a state of on and then the fan
was triggered.
Now, the same thing,
I can go back here and then change the
state to off.
And let me also bring this back in
when we hit this,
look at this,
a state of off.
So now we have this working.
What is the next step?
Now? You're just giving this endpoint as
a Pro-dev
You're like "OK,
Sam, you've been given this endpoint
We want you to build something that allows
you to trigger this.
Alright, That is where our Power Automate
comes in.
So this is going to jump right in into
my portal where I have this actually deployed
a couple of things we're going to
do just to make sure this works.
So let's go here,
let's bring in our portal real quick.
Let's change this,
let's change this quickly so we can
get this going.
And this is this is exciting because it
gives an opportunity to,
you know, combine
my prowess as a pro developer and
then also a citizen developer to create
these things and do them rapidly.
So here we go.
This is what I deployed earlier and
call the Power Fan.
Let's open this up and let's have
some fun with this.
And then if we go all the way,
yep, if you open our functions up just
to show you that,
you know,
it's just,
it's just the same thing,
nothing different from it.
So, let's go back.
Actually,
it's a power -
it's it's it's it's,
Let's see.
Let me open our resources.
I deployed one specifically for this
for our show Powerful Devs
yep, there you go. Actually,
it's - let's use Powerful Dev CH
we have it here and there you go,
Power Fan GH.
Let's open this up.
Okay, so here we are,
we have our function here and let's
copy this because that is how we're gonna test
it. So if we go here just to make sure
that we have our functions all nice and
easy here.
Yes, we have the on fan here.
Okay, so,
yep, it's an read more,
that's fine.
So let's let's go ahead and test this.
So this is the local one which was running
and I'm gonna go ahead and test this,
let's copy this here,
the state and add that to it.
And let's try turning this on.
Okay, now this is a run because now
if you want to connect your IoT Hub
and other devices,
you know,
to the cloud,
you need to ensure that you have a
good access token to do that.
I deliberately didn't do this so I
can show you how to set this up.
So let's go back to functions right here
and I'm gonna go to configurations and
for the sake of
just showing how this is,
I had my set up here to show the
configuration to have the configuration file
that I'll need to run this in an environmental
variable,
so I need to add that so that the function
knows. So I need to create these two
pieces of information because that is
what it will need to be
able to call the function.
So let's do this quickly.
So let's go ahead here,
let's add a new application setting.
I'll put this here.
Let's go back here.
I'm going to copy this,
yep and I'll make sure I'm changing this
right after the show. So we paste this
here. That's okay.
And we need to add a second one.
We need to have the second one,
which is the device name.
So our application settings device name.
And let's put this here. Now, bear in
mind, this is just for demo purposes when
you're doing this in production,
the couple of things we need to take
into consideration.
So yeah, we have our two settings
in there,
let's save this and continue.
So now let's go to our function and
check this out.
So that's go here when they go in
and let's look at our function just
to be sure everything in there is okay.
Yep. Let's check the code and test
to ensure that our function has everything
you need to run.
All right,
so I'm just gonna go ahead and get
the function URL. Yeah,
just the default key.
So copy this,
the clipboard.
Let's bring this here.
All right.
And then I'll just put the state equals
on- fingers crossed and check that out!
We have our fan running.
Isn't that awesome?
But then someone who still say- "Sam-
but what you've just done,
there's nothing interesting about this,"
but at least what I have done now is
exposes an APL this service that controls this
fan through Azure IoT Hub on Azure Functions.
Now, if I send them off,
this goes up now,
this takes us to the next level.
So now I can copy this
URL and I have pre prepared this - first.
let me actually start with
just a very basic Power Automate.
All right.
We can use this where if you call
this part,
this function is actually going to
trigger that for you.
But let's let's just go and make this,
you know,
let's let's let's show you how easy
it is to integrate this into different workflows.
So I'm gonna start with Automate and
then I'm going to create a flow.
Yeah, just very,
you know,
very basic.
Something very basic and I'll show you
how to trigger this and again keeping screens
in touch so we can do this so I can
go ahead and do this -let's do an instant flow,
click on this and yeah,
I would just say the Power Fan Flow.
Yep. Let's keep it this way and then
I'll manually trigger this and let's
go ahead and create.
So very simple as a citizen developer - giving
access to all this Pro-dev stuff.
It's easy for you to come in here and
then create,
you know,
all the information you want.
So all I'm going to do here is the
next step. When this happens,
I just want to make an HTTP, right?
I just want to make an HTTP request.
Right? Quite simple.
So let's just do HTTP request
here and this.
I think we can do a get - that's fine.
And then I put this here. Now the code
that I created allows for either a header
parameter or, you know,
a query string or something in
the in the body.
So that's that's just fine.
So what I can do here is - let's copy
this, I can copy this because this is
actually a URL.
So in the body I'll just go ahead
and do this.
Let's do - first of all,
you know, let's just try this.
Let's just try straight with the
URL and then we'll come back and
you know, make this look a bit pretty.
So let's go ahead and then do on in
this case and it's just a
test. So I mean I'm not going by any
best practices here.
We're just trying to show you how this,
just trying to show you how this works.
So I have this saved.
I'm just gonna go ahead and hit test,
perform this task, and then hit test
and then I wanna make sure everything is
okay and then just run the flow and
if everything went well,
this should go ahead and then trigger this
particular -
uh oh! We have a problem.
Let's see what happened here.
Editing,
locking my - it should have locked in
my inputs.
Oh, not found,
this is interesting.
Let's go back here and check cause of
demo day.
Well let's run this again and be sure
that straight on and be sure we have
our stuff going,
yep, we have it going, awesome,
awesome, awesome.
Okay.