Kassian's IoT Hour
Presented by: Kassian Rosner Wren
Originally aired on October 20, 2021 @ 6:00 AM - 7:00 AM EDT
Let's dig into how you can use Serverless to supercharge your IoT projects! We'll walk through setting up a remote sensor and LED with Cloudflare Workers, Workers KV, and a Raspberry Pi.
English
Tutorials
Internet of Things
Transcript (Beta)
Good afternoon everyone. I am Kassian and welcome to the IoT Hour. So what we've been working on for the past few weeks is we have a parent Pi, which is just the Pi 4.
We have two children. We have one that has lights and we have one that has a temperature sensor.
And what we were doing was we were getting the temperature data to be put into RedisView.
Now we had some trouble with routing and I traced it back to the router plugin.
So we are going to change this to be a happy server, HAPI. We're going to start refactoring that today and hopefully get through it and have our routing back.
So let's start by looking at the code. So here we've got our server and it's got a bunch of stuff woven into it, mostly Redis and Redis PubSub and things like that.
So what we're going to do is we're going to change this up to be a HAPI server now.
We're going to use this get starting tutorial.
So okay, so we need to first, I'm going to and we need to get pull rebates to make sure there's nothing else sitting on the server waiting for us.
Okay.
Now we've got a lot cleaned up.
We're going to go, we're actually going to install HAPI.
Okay.
So good, that's done. I already have this tuned into our remote server. So what we're going to do is we're going to get rid of the router.
And now this will break and that's okay because we'll do this.
And we can get rid of this and we can get rid of this.
So we're going to go back to it's And then here at the bottom, we put the egg.
I guess we now have all that taken care of, so what we're going to do next, oh, we need to add this.
Make it match the rest of our project.
All right. Now we've got all that, what we're going to do is start working on the routes.
So we're going to add routes, so it goes in the init, I guess we're going to take this route, and we're going to convert it, so change the server route, and it's going to become Okay, so we don't have to set the headers in this version in HAPI, we just return.
So that means we will need to take our PubSub and move them up here.
Move that close by.
We're kind of cleaning up our code as we go, too. Okay, so now we know all these routes in here, so what we'll do is comment them out for now.
So, see if our server runs this way. Okay.
Okay.
Oh, yes, that would be where to turn.
Path is required.
Oh, it's path not route.
Now we can go check.
Okay, so the new route is working.
HAPI is working, so now let's convert one of our other routes into HAPI, that way we can get some more information.
So let's view sensordata.type.
Okay, so it's in parentheses, and then it's request.programs.user.
So we're going to create an object here, we're going to say path.
Type method.
Get handler. Okay.
So we don't need this. We're just going to return, for now, type type.
So that's to make sure that the routing is working, it doesn't actually do anything yet.
Okay, and then we're going to go back to our browser.
This route still works, and then I'm going to say...
Sensordata.type.
Okay, it's not finding it, and it's giving us a 404 message.
So it's not found.
Oh, right.
Okay. Okay. Okay. Okay. Okay. Internal server.
Okay, let's figure out what that is. Request is not defined. Oh, it got to the route, though.
That's good. Oh, that should be right. Yay!
All right, routing works. Fantastic. So now we've got a working router, and we can get all this stuff set up so we can start figuring out how to move forward.
So, yeah.
Let's do it. So now let's change this to actually get the data out of the...
So what we're going to do is we're going to say zone.
Okay. Okay.
Okay. Okay. Okay.
Okay. Let's see if this works.
It doesn't like the way PubSub.
Now we're going to go over to our browser.
We're going to say lab. Browser.
Oh, goodness, it's got a lot of data coming out, but why is there an internal server in it?
Does not return a value, a promise, or throw an error.
Okay. So it didn't like that it doesn't do that, but it does have temperature data.
How do I return from inside a callback?
Or does this count as a promise? Let's find out.
True.
Okay. That's interesting. Okay. Okay. On my other screen.
Okay.
Okay. Okay.
Okay.
So possibly do it like this.
Let's see if this works.
Oops. Let's see if this works.
Let's see if this works.
Let's see if this works.
Let's see if this works. Okay.
Let's see if this works.
So that's not the reply. Okay. What we're going to have to do is create an async await method chain.
So we're going to say...
And then we're going to say...
await async... That equals results.
That equals results. That equals results.
That's not going to work. I'm going to try it anyway. I'm going to try it anyway.
I'm going to try it anyway.
I'm going to try it anyway.
Okay.
So it does need to be a promise or... Oh, I typed a callback. So what we're going to do...
Okay.
Okay.
Okay.
So this block will set up everything here.
Okay. Okay. Okay. Okay. Okay.
Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay. Okay.
Down here, what we need is...
This should actually be...
This should actually be...
This should actually be...
This should actually be...
Okay. Okay.
Okay.
I think we've got it now.
Okay. Okay. Okay. Okay.
All right. Let's see if this works. I don't know what to do. I don't know what to do.
Okay.
Okay.
Okay. Something went really wrong that time. Okay.
Okay.
Okay.
Let me comment this out to see if this is a problem. Okay, so definitely that is a problem.
Okay. Okay.
Okay.
Okay.
Okay.
Okay.
Okay. Okay. There we go.
So I need to pass the context. So let's try this.
Ah, fantastic.
All right, we're getting the data back. Fantastic. All right, so we're making progress.
Let's just send back the last 25 responses.
Let's see if that works.
Yeah, that's much better.
It's the last one. Okay, cool, cool, cool, cool, cool, cool.
Let's see about getting the child with the temperature sensor. There we go.
So it's sending data to the parent. Okay, so now when I go over to my browser, it should be one from 1012.
Oh, I see.
I see. So that's the top. So we don't need 25. We need zero. Here.
Let's go up to parent. And again, check on the browser. There we go.
1006 in the top tier of 1012. Awesome. Okay, so now we've got the newest 25 pieces of data being sent to us.
Fantastic. So next, what we'll do is we will And start working on some of the other routes.
But first we want to do Now we push this up to GitHub because I just want to make sure that this is a good safe point because it's working.
So we don't need this route anymore, because we're posting data via PubSub.
And then we can get rid of chat like this right now.
Okay, so now we're going to do is we're going to look at serving static files and getting a dashboard to work.
And we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is will not code is we're going to do is we're going to do is we're going to do is we're going to do what is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to do is we're going to need a plugin called nerds founded up we're going to need a plugin called nerds.
Silence. Silence.
Silence.
Silence. Files around here.
Silence. Silence. Silence.
Silence. Silence. Silence. Silence. Silence. Silence. Silence. Silence.
Silence. Silence. Silence. Silence. Silence. Silence. Silence. Silence.
Silence. Silence. Silence. Silence. Silence. Silence. Moving on, we'll go in here.
Moving on, we'll go in here.
Here. Here. Silence.
Silence.
Silence.
Silence.
Okay.
Okay.
Okay.
Okay. Okay.
Okay. Okay.
Okay.
Okay.
Okay. Okay. Okay. Okay.
Okay.
Okay.
Okay. Okay. Okay. Okay.
Okay.
Okay.
Okay. Okay.
Okay. Okay. Okay.
Okay. Okay.
Okay.
Okay. Okay.
Okay. Okay.
Okay. Okay.
Okay.
Okay.
Okay, so I'll go back to... Okay, so this still works.
This still works.
Awesome. Awesome. Awesome. Okay. Okay. Okay.
Okay. Okay.
Okay, so it's in the build folder.
So what we're going to do is go over here. And instead of public, we're going to say build.
Okay, so I'm going to try running this.
And we'll go to our browser.
And fantastic.
There we go. Okay. So we've got our IoT-level dashboard working here. Okay.
Okay.
Okay, so we've got a little bit under eight minutes left.
What to do now.
Okay. Because now we have the server running.
We can retrieve all sorts of data.
We can get the HTML. I'm going to get the last 25 data pieces.
We can also do... There we are.
We can do...
Community.
Well, we'll get them. Let's see. Oh. Oh.
I don't have it published in relative immunity. Okay. Okay.
Okay. Okay. Okay.
Okay. Okay. Okay.
Okay. Okay. Okay. Okay.
Okay.
Okay.
Okay.
So now... Because that way... Pushed immediately. Fantastic. Now we're going to check our server here.
For the humidity. Fantastic.
So we now have the relative humidity being pushed as well. Cool, cool, cool.
So... Next time what we're going to do is start putting this information into the actual dashboard.
We're also going to... We can actually do this now. Okay. So...
Okay.
So there is L trim. And so what we're going to add to our push parent... Whenever data gets pushed, we'll also trim the list.
Okay.
Okay.
Okay.
We're going to go beyond 100. So we will... Once we get information from...
We'll wait for that. And then it'll be just about time to wrap it up.
So... Waiting on more information. Oh, right. Okay. That won't console.log it out anymore.
I'll wrap this up for today. Well, we're just about out of time.
Thank you so much for watching. Next week we will start getting working on the actual dashboard.
And I hope you enjoy the rest of your day.
And... And the rest of our TV programming.
And I will see you next week. Have a good one.