Kassian's IoT Hour
Presented by: Kassian Rosner Wren
Originally aired on March 7, 2021 @ 10:30 AM - 11:30 AM CST
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.
Original Airdate: August 24, 2020
English
Transcript (Beta)
I am Kassian and welcome to the IoT Hour. So today what we're going to be doing is we're going to be working with Raspberry Pi like so and what I've got installed on it already is a Node.js runtime 14.8.
I also have Redis installed and I'm going to use the PubSub and I have Argo Tunnel installed and so what I'm going to do is I'm going to use the PubSub ability from Redis to get this Raspberry Pi talking and then next week we're going to add more Raspberry Pis.
I had to order some power supplies my Raspberry Pi is being quite finicky as to which power supply it'll use so right now I only have the one running on this power supply and so next week we'll get them to talk to each other but this week we're going to get this one started as the main hub of the Raspberry Pi kind of network that we're going to build so let's get started.
I'll share my screen like I usually do. All right so right now what we're looking at is the Redis documentation and so we're looking at the PubSub for Node.js Redis and so what we're going to do is we're going to create a client that will subscribe to a channel and yeah so we'll subscribe to your channel and then what we'll do is we'll write a worker that when you write an HTTP request will make a message and send an HTTP request to the Raspberry Pi which will make a PubSub and so then what'll happen is other Raspberry Pis will subscribe to the Redis instance on this Raspberry Pi and listen for notifications.
So let me walk you through what I've done to this Pi so far. What I've done so far I set up here.
I've done all this. I've installed Raspbian. I've set up Wi-Fi and SSH.
I've done all this. Gotten started with installing Node, installing NPM.
I installed the Cloudflare daemon so it's running to push .nodebotanist.
You can see here so right now it's just returning a JSON response. What we're going to do is we're going to write a worker that will ping it and it will send a message.
Now the question would be like why would you write a worker when you've got everyone kind of running and the reason for that is I don't want to go back to the origin every time.
I want the worker to handle going back to the origin so I want the basically I want the response to come as quickly as possible to say a customer.
If I was building a distributed system of IoT devices, I would send them to a worker which would be faster for them to get a hold of and get the response back from and then the worker would have the job of going back to the origin and actually making the request.
So that's why we're doing this with Argo Tunnel and workers and not just you know say Argo Tunnel.
So let's get started with writing our Redis request.
So what I'm going to do here is I'm going to actually want to open VS Code and I'm going to use SSH.
This is a host so we're going to say push i iot push .local and we're going to actually put i at oh a hash in there that I shouldn't have.
Close this window. Try that again.
Okay so it's setting up VS Code on the Pi which is kind of nice that way we don't have to worry about installing it ourselves and what we're going to do is edit our code through VS Code from here just because I like the experience a little better.
You can use Vim or VI or whatever you want to use. I'm not going to get in that argument about which is better because they're all good and so yeah I just like VS Code.
Let's install and while it's installing actually what we'll do is we'll set up a new tab here on my local machine.
I'm going to code. I'm going to iot hour. Okay so we got IoT dashboard and it showed temp so what we're going to do is we're going to say wrangler first mpli g.
Remember the domain slash wrangler.
Okay so that's all installed.
Made sure it's up to date and then we're going to say wrangler create and we'll call it push.
Wrangler net.
No not wrangler. Sorry about this.
Just had a blank out there. Wrangler. Ah wrangler generate.
There we go. Lots of weird notification noises from all over the place.
Wrangler generate and we'll say iot push.
Okay so now we got this as well.
Let's see how it's going. Okay cool. So we've got iot push to local.
We're going to go to files. We're going to open a folder and we're going to open our iot push folder here.
Okay I'm going to ask for my password again.
Nope did not. All right. I don't know why this file is called index .html.
It should be index.js. That's very strange.
Rename and we'll go into the pi.
Okay so the server's running again.
Okay so we've got our server here. This is the other reason I wanted to use VS Code.
We can leave our server running. Actually you know what?
It's even better. What we can do is we can close this and then we're going to npm install globally nodemon.
And then this way when we edit something in VS Code and save it, it'll reload the server which would be pretty handy.
Okay I just installed it though.
Well that's strange.
I did one thing way earlier.
Okay there we go.
I just nodemon. I changed the npm install prefix for global so that it would have access so to get around the e-access issue.
So now it'll change every time we edit something here in VS Code.
For instance if I say change this message to hi and I save it.
Here you see it made the changes and when I go into here and I go push in the bot nest refresh.
Yes it's hot. So that's our dev setup right there.
All right so now we've got our dev setup. Let's start messing around with Redis and getting this pub sub setup.
So actually we are gonna have to close this because I want to install my Redis and then nodemon.
Okay so now it's running and we have access to Redis.
Redis equals require. All right I've got Redis and make sure this runs properly.
Yep cool. So right now what we're going to do is we're not going to do any smart routing.
We're just going to say every time it pings send a message saying you know someone connected and then we'll start adding things like you can send data between pies and things like that.
So we'll start out very simply and build up. That way hopefully this makes sense to everyone.
I will also put all of this code in the Cassians IOT hour GitHub repository under node bot nest slash Cassians dash IOT dash hour.
That way you'll be able to access this code after this airs. So well it's airing live so after we're done here.
So now we've got Redis. So let's go into our node Redis and what we've got is const publisher and const subscriber.
So we are going to create a publisher.
I'm sorry const Redis or actually db.
Oh PubSub for now. We don't need any options because everything's in the default right now.
So let's check okay everything's running correctly and then okay.
So we're going to subscribe or not subscribe a channel. Oh we're going to publish.
I see we're going to we're going to be doing one of the publishing. So we're going to publish or not publish a channel a message.
So okay. I see why it does both.
We're actually going to do both as well. We're going to call it temp subscribers so that we can emulate another pi listening.
And save that.
And so on our request listener what we're going to do is um here we're going to PubSub publish and color channel iot push just to keep things consistent.
So we're going to send the message.
Message published back to the user.
Okay what we're going to do now is as soon as the server is running we're going to check it.
It pushed an abundance. This is published. All right sweet. So we are publishing onto a redis instance.
We're not getting any errors or anything like that so that's cool.
Where do all these semicolons keep coming from? They're not mine and no I don't want to have that argument with y'all.
So anyway it makes me think I'm writing rust and then I do rust things when I use semicolons.
So that's my reason for not using semicolons in javascript.
It's not the best but you know it is what it is.
All right so we've got our publisher.publish right. So now what we're going to say is temp subscriber on message.
We're going to open up a function which I believe takes a channel and a message.
we're actually going to do this.
All right cool.
So now we've got temp subscriber on message, channel message and we're going to say temp subscriber dot subscribe to iot push.
Okay now we go here.
The server is running and when I go here and hit refresh what it should do is not just say that but channel iot push message hello.
So now we've got pub sub working on our raspberry pi and we can call it from our gotunnel.
So what we're going to do is create write a worker to call this extension.
So that way again we don't have to go back to the origin for the user.
The user can just get back a response immediately while the pub sub happens in the background and goes back to the origin in the background.
We use an asynchronous call to do so. So let's go into our local folder.
We're going to open up vscode in here. Okay so all this and index.js.
Okay so return response hello worker.
So what we're going to do actually is we're going to say because now this does not allow asynchronous calling.
So when we run this what will happen is it will wait until the call has come back.
So we'll go back to the origin but we can work on this later to make it a little more asynchronous.
So the next thing I need to do is go to my Cloudflare account on another screen.
No that's not what I wanted really.
What I wanted to do is go here. There we go. Now we're going to log in and we're going to get my account id so that I can get all that settled.
Manage workers.
Okay I can't let me copy. I'm going to go to wrangler.toml.
I'm going to put in my account id. Save this and close it.
All right now what we're going to do is worker wrangler preview. Okay now in my other window we'll pull it back into this window.
It opened up a new window.
You can hear me slamming my mouse around. So Cloudflare Workers opened up this tab in Cloudflare Workers.
It says hello worker, which is good because that means hopefully what we'll return when we go back into here is iot push message hello.
So now there's three. So let's see if we get a fourth when we run it again. We did.
So it is getting it from the worker. So we now have a worker that is pushing a PubSub message or well it's pushing an HTTP request to raspberry pi which is then setting up a PubSub message.
And what's really cool about this is we can set this up on multiple devices, not just raspberry pi.
It's basically anything I could take that can subscribe to the Redis PubSub will be able to use this.
So what we're going to do next is kind of flesh out the HTTP server and kind of allow us to send our own messages and we will allow to send JSON and that way we can kind of send like a data back and forth.
For instance, the other raspberry pi I have has the light grid on it.
We could send colors back and forth from the main one and whenever there's a color this raspberry pi with the lights will be listening and will light up that color.
So you can kind of flesh this out a little bit as we go.
So let's start with our server. So our server is very simple.
We don't really have any routing or anything like that. We just kind of have this one setup.
So what we're going to do is we're going to change that. I remember correctly.
That is not what I'm looking for.
Here we are. Okay.
So we're going to add our router. First we're going to close the server for a second.
Okay.
I've got router.
I'm going to put this under HTTP. Here.
Okay. Anyway. So now we've got all this. What we're going to do is we're going to go back here and this is router rec res final handler rec res.
Okay. So we're going to copy this line here.
We're going to take our server.
We're going to kind of oh that's not what I need to do.
So we're going to delete it.
We're going to move it here in a moment. Paste this. Okay.
So oh, right. We've got to establish it. So router in uppercase. And then down here router.
New router or just router?
Wow.
I've been working on this a lot lately. So okay. Okay. Okay.
It says the server's running.
I don't think it'll actually do anything if we go to refresh it.
Because yeah, by gateway. So oh, no monop crash. Final handler is not defined.
Okay. Oh, I see.
We don't need to add a final handler if we don't want to. So we can just close this.
I think the server will be working again.
But I don't think it'll do anything again if we refresh.
By gateway. Argument callback is required. Yeah.
Function handler router. Point 14. It does require a callback. Um. Let's see.
Now I don't get by gateway, but I don't get anything back.
Because it's not actually doing anything with our REC and our RES.
So now what we're going to do is we're going to add router.
Get. Res.
Take this code and copy it.
And then.
Message message published.
And we go back to our server. Challenge. Now we've got it back to where we wanted it at the beginning.
But with a router. So now we can kind of mess around with the router.
And a lot of different messages go different places.
For instance, we'll channel to IoT push. That's where we're at.
And then we'll do is we'll say router.
Get.
And then we'll do is we'll say, um, res. Um. As.
As.
As.
As.
I can type, you know. Okay. So now what we're going to do is we're going to PubSub.
Publish.
We'll say 32 point.
It's about right. Celsius degrees. Right. Sounds like a normal temperature for outside.
So. And then. JSON.
And then.
Right. 200. And.
And. We'll do this. Tactics. Message.
Temperature.
Air.
Data. Now we go over here.
It's running. To our browser. And we'll say push. Botanist slash temp.
JSON.
And we go to our. Oh. I said a different channel, didn't I? Yes, I did. So.
Blue square over. Temp. Subscribe. Data.
Again.
Refresh. Refresh here. Temperature. Descent. Temperature. So. What we've got now is we've got a functioning.
Redis server that can. Take in different routes. And send to different channels.
So you have to subscribe to all these different channels.
So. What we're going to do next. Is we are going to. Flush this out. So we can actually send the data as part of the request.
Also we want these to be post. Requests.
Because we are adding data. To a database. We don't necessarily want these to be get requests.
So let's do that. So first. What we're going to do. I'm going to say.
Router.get. It stays the same. But we're going to change. Here's router.post. So when we go back here now.
When we refresh. It's not going to do anything. Right.
So we're going to go to Cloudflare workers. And testing. We're going to post. Yes.
Push. Botanist.
Test.
It's back.
Hello worker. And we go back here. I have to message me. Yes. That's because.
I didn't add. Temp. Data. Okay. Test. Strange.
Why isn't it running?
Getting more pushes on message.main.
Oh. I think that's because of. I just realized.
I don't think. Fantastic.
So now.
I think. Fantastic. Okay.
So now. We're going to do. Is. We are going to. Collection. Push collection. Create.
Name.
Post. To. Temp. Data. Okay. Push.
So. The request URL. Post request. I'm going to. Make this. Bigger. Let's see.
Yes. We also need to get the worker going.
I knew I forgot something. We'll get the worker going as well.
So. Push. Sent.
To the console.
Okay. So this is working. Awesome. So now what we want to do. Is we want to get the worker.
Posting to temp data. So we'll go. This way. To our worker.
That's right.
All right. And then we will. So. Hmm.
Doesn't seem to work. Okay. So that's not it.
Been a while.
Method posts.
Always different.
Okay.
Hello worker. Oh, that was not right. Okay. Let's check that. We've got another.
We did get another temp push. Let's make sure that was from the right source.
Okay.
Worker. So now we've got our worker making our request. Our post request to. To our account.
So. Let's start fleshing out what a home automation.
Redis database would look like.
So not just a pub sub, but also database so that we can see like. Historical.
Historical temperature data, things like that.
So we're going to watch node Redis and we're going to go from pub sub to.
Other events.
There's a one-on-one mapping the Redis command.
So we'll open up Redis commands. Close this for now.
Close router for now. Using fetch closes public workers. Okay. So we've got a bunch of commands that we can.
This isn't exactly.
We're looking for.
Here. Okay. Okay.
Okay.
Okay.
All right. So we're just going to use. For now we're going to use just like set.
Okay.
So we're going to use HS set. And I'm pretty sure this one's even covered here.
HM set. All right. So let's.
Do first let's allow the user to send in the body of their requests. And.
A temperature that can then be used to.
So we can send them a temperature along with it.
And so body. We're going to send along with the post.
We're going to send a body. And it's going to be JSON stringify.
Jsonify. We create temperature.
That will be a math. For. Random.
Random. 12.
Plus.
13.
No. We'll say. Yeah, that's a 20. So the minimum is 20 and the maximum is 32. So we're going to send a random.
A random temperature every time.
So we're going to save this. That's our worker.
And so we want to set up our server to actually take in this data.
Here. Here. Here.
Here.
Here. Here. I'm not.
For. For. For.
I did. I forgot how to do this. Okay. Should be indirect that body.
Okay. Okay. Okay.
Is that.
I was running. Okay. Here to. Where was. Right. Again. Okay. Right.
Okay.
Yeah. So something's going on with the fetch request. Let's figure out what that is.
Okay.
It's stalling. Let's figure out why that is. Okay. Okay.
Okay.
Okay.
Cool.
Okay. So we're going to have to do is we're going to have to implement this inside this setup.
That.
And then we're going to. This. Here. Here. There's no. Okay. Okay.
Running.
Send. There we go. Kind of. Gateway. All right. Okay. Unexpected end of Jason input.
Oh, right.
Right. Still that gateway.
I've crashed. Okay. That's fine. Okay. Right. Right. We're going to have to.
If. Body. Okay.
Still stalling, which is strange.
Oh, these are query params. These are not. Okay.
That's why. All right. All right. Body. Let's see.
Okay.
I stop it. Or can I. Awesome.
Okay. So it was because we didn't have a body. Okay. Message temp 3.2. Okay. So we want to do is we want to extract.
The temperature. Okay. Okay. Oh, right.
Right. Arse.
Okay.
Save it.
We're still running. Is. This man will send it. He did publish.
Okay. We're not getting anything. It is still publishing, but it's still not going through.
Okay. Which is strange. We just need to put this.
Here. Running.
Okay.
Sense. It should be. Is it console dot logging the body and then not actually pub the subbing.
The temp data. Okay. Oh, it's not actually getting into.
Just to be sure.
Okay. Okay.
Okay. The attempt field and Jason body. Okay. Okay. Okay. That's something at least.
Okay.
Okay.
Okay.
Okay.
I got a message there. It was important, but it seems. As we were.
All right. Add gateway.
Oh.
Okay. It's not restarted yet.
And again.
Okay.
But I have that. Hmm. That's so strange.
The quotes are still there though, which probably shouldn't be. Yeah.
Yeah. So I thought, so that's not it. Oh, Jason.
And then I need to go here.
And refresh the server. Running again. Okay.
Temperature. Okay. So it was all that. Phew. That was a lot. Cool. So we have got.
A Redis instance that is taking in data. And pub subbing it. And. So next week, what we're going to do is we're going to start saving temperature data so that we can have like a series of temperature data.
We can make a grass. Although you've seen my.
Experimentation with graphs. We're going to have some fun with that.
So that's going to be in our, And then our next eventual also be talking between two raspberry pies.
There is a power supply on its way. For next week so that we will have two raspberry pies and then we'll be able to talk to each other.
Via Redis pub sub, which would be pretty rad.
I think. And we will be doing all that. Next week. So for the rest of the time, I think I'm just going to clean up code.
Pardon me and get this ready to set into the.
IOT hour. So. All right, so we've got this all cleaned up.
Okay. It's actually pretty cleaned up.
All right.
Semi-colon sitting around. And then, you know, what else will do. Okay. It's using tabs now.
Yes, it is. Awesome. Oh, no, it's using all spaces. Fantastic.
Okay. It just did it all for me, though. These are two. In. Okay.
It's just about ready to go into Cassian's IOT hour stuff.
So what I'm going to do is I'm going to get that.
Going well, you. So.
So. Okay. Okay. You know what?
That's not, no, that's not the. Goodness. I'm off my game today. Here we go.
Okay. It's a PI code.
And we're going to do here is you're going to create a new directory. For.
IOT. Push. We're running out of time.
So thank you very much, everybody for watching. I hope you really enjoy the rest of clubs or TVs programming for the day.