Conf42 JavaScript 2020 - Online

- premiere 5PM GMT

An Introduction To IoT (Internet of Toilets 🚽); Or How I Built an IoT Kitty Litter Box Using JavaScript

Video size:

Abstract

My favorite things in life are cats 🐈, computers 🖥 and crappy ideas 💩, so I decided to combine all three and make an IoT (Internet of Things) litter box using a Raspberry Pi and JavaScript! If you have ever wanted to get build your own IoT project, but didn’t know how to start, then this is the talk for you.

Together, we will go through how I setup my IoT Litter Box from start to finish. Including how to setup Node.js on a Raspberry Pi and how to connect sensors to a Raspberry Pi and how to read the sensor inputs with Node.js.

Summary

  • This is a gentle introduction to the Internet of things or like how to get started with programming with hardware. If you want to follow along with any of the resources, include the slides, recording of this video, any resources, and the code I use in this deck. If I say anything controversial today, just know that those opinions are my own and not my employer.
  • Joecarlson: Why you should be considering JavaScript and node for your next IoT project. He says his favorite part of IoT is the stupid stuff. There is a new wave of art coming about from Internet connected devices, he says. And then he makes wild speculations about the future of IoT.
  • 58% of IoT developers self identify as node JS developers. A lot of developers are coming out of boot camps and colleges are learning node and JavaScript. It's a great language to learn. You can build IoT devices, servers and full service web pages on top of that.
  • With JavaScript all I have to do is do a git pull and NPM install and boom, it's done. Internet's already speaking JavaScript, this is my opinion. There's actually a vibrant ecosystem of JavaScript tools, libraries and APIs available to make hardware projects.
  • At its core is an event driven app programming language. JavaScript and Nodejs core structure directly translates how we develop applications with hardware and sensors. If it's your first project, just do what you know, just go out there and try it.
  • People are spending up to $500 on Internet connected smart kitty litter boxes. It takes a passive measurement of my cat's weight and a timestamp of when that bathroom event happens. But it turns out this actually might Iot be a bad idea.
  • We're requiring our Johnny five package. Johnny five works a bunch of different chips and cards and hardware. All we're saying is there's an Led on the 13th pin. Make it blank easy. That's all it took.
  • Using the spongehold. Process nodejs function. We have a web application that shows you a real time stats of my cat's bathroom behavior over time. We're also using an IoT time series data. Using that spawn child process and mixing languages to get this actually work.
  • We're using to see smaller and more powerful hardware devices. I think this is going to mean that we're going to see additional compute power on the edge. The batteries are and will continue to be the bottleneck.
  • The best way to learn how to do something is to program and actually get your hands dirty. Also, if you are planning on using a project, you should totally check out MongoDB. Best way to get a hold of me is on Twitter, so I'd love to see you there and resources.

Transcript

This transcript was autogenerated. To make changes, submit a PR.
Hello, my name is Joe Carlson and you are here for an introduction to IoT Internet of Toy. This is supposed to be a gentle introduction to the Internet of things or like how to get started with programming with hardware, with JavaScript and Nodejs. What quick introduction? We don't have a lot of time here today, but I thought, hey, my name is Joe, I work for a company called MongodB and I'm a developer advocate and software engineer. If you want to hang out with me, I'm going to be posting at the end, but you can find me on Twitter. It's the best way to get a hold of me. And actually this may be the first time you see this, but I'm also on TikTok. Who knew? If you want to follow along with any of the resources, include the slides, recording of this video, any resources, and the code I use in this deck. You can totally check that out at that QR code in the upper right hand corner, or at that bitly link. And most importantly, if I say anything controversial today, just know that those opinions are my own and not my employer. Please don't get me fired. And let's jump in here. So what are we going to talk about today? First thing, I just want to introduce IoT, like what is it? How do we use it? Then I want to talk about why you should be considering JavaScript and node for your next IoT project. I'm also going to be talking about a practical project I recently built. It's my Internet connected smart kitty litter box. What? And then lastly, I love making wild speculations about the future. So we're going to be talking about my predictions for the future of IoT. So stick around if you want to check that out. You can play this back in five years from now to see how embarrassingly wrong I was about all these two, which is fun. So what the heck is IoT? What is this thing? What is this Iot thing? Well, basically it's just anything. It's basically anything you put a chip in. Anything you put a chip in. It's just the Internet, but we put physical hardware and stuff on it. You've probably seen a bunch of it. Fitbit smartwatches, rings, google nests and homes, whatever, right? But my favorite part about IoT, it's the stupid shit. I love the stupid parts about IoT. I love the weird little dumb project. I love straddling that line between something that's kind of like totally ridiculous and dumb and totally smart. I think that there is actually kind of a new wave of art coming about from Internet connected devices, and I think us as developers and programmers are uniquely positioned to explore that space. A lot of artists don't know how to actually do programming, but we do. I think that that's fascinating. I think that's absolutely fascinating, and I love exploring that line. I have a bunch of projects, you can check those out on my website, joecarlson.com, if you're curious. We don't have time to get into those today though. Okay, so why should you be considering Javascript for your next IoT project? Let's dig into it together. So did you know that actually 58% of IoT developers self identify as node JS developers? What? It's huge. That's a quorum. I think that's what that word means, but that's a lot of people, right? I would also argue this is total opinion, but JavaScript makes a great choice for new developers. A lot of developers are coming out of boot camps and colleges are learning node and JavaScript. It's a great language to learn. I know there's Python and Java heads all over the place too, but traditionally embedded software has typically been programmed with a lower level programming language like C or C plus plus or now with rust. And these are really difficult to develop for because you have to worry. But memory management JavaScript has a garbage collector built into the language, so we don't have to worry about that, and it makes it a great choice for new developers to pick up. So if you want to learn hardware, JavaScript is a great place to start. And you can build IoT devices, servers and full service web pages on top of that. Let's talk about updating. So traditionally with an embedded device, you have to actually secure the actual device, plug it in, and reflash that with a new code on it to get it updated IoT. So with JavaScript, right, with JavaScript all I have to do is do a git pull and NPM install and boom, it's done. If it's connected to the Internet already, if you have a git client installed on there, which with a raspberry PI is totally doable, it's mega easy to do updates. Internet's already speaking JavaScript, this is my opinion. Right, but why not just use the language you're already using for speaking on the web and transfer to this new frontier of devices? I think that's kind of exciting. And did you know there's actually a vibrant ecosystem of JavaScript tools, libraries and APIs available to make hardware projects? The two big ones on the market today are Cylon Js and Johnny five. For my Internet connected smart kitty litter box, I'm actually using Johnny five. We're digging into that hello world together as well. I wanted you to think about a typical use case for a IoT device. Typically what you have is some sort of Internet connected chip or hardware that has a sensors out in the real world. And when that sensor detects some sort of event, something happens. For example, maybe you have a luminescent sensors that determines how bright something is. And when it reaches some sort of level of luminescence, then you want to have something happen, like a light bulb turn on or off or something like that. But if you think about it, it's an event driven app node. At its core is an event driven app programming language, right? We use callback functions and promises, and now async await, which is just async tactical trigger for promises, right? But for handling asynchronous events that are happening even on the front end, right? We have some sort of click event triggers, a callback function to do some sort of thing. JavaScript and Nodejs core structure directly translates how we develop applications with hardware and sensors. It's a perfect marriage, honestly. Okay, that's a bunch of great reasons why I personally think that Javascript is a great language for using it. And there are other considerations too, like memory management, other things which you might want to consider other languages for. But if it's your first project, just do what you know, just go out there and try it. Okay, my favorite part, let's talk about my Internet connect litter box. I love jumping into some practical projects while doing these presentations, but what does things thing actually look like? Here's actually the real finished product. I've got a little mid century fancy little box, and you can see a little cable coming out of the back of that thing, but it's actually pretty hidden in there, right? Like I said, I'm obsessed with the stupid ideas, but I did some homework after I built this, and it turns out this actually might Iot be a bad idea, right? People actually are spending up to $500 on decently well reviewed Internet connected smart kitty litter boxes. Mine's not quite as fancy as this, but Iot could be with a little bit more effort, right? I don't know, I think that there's something uniquely special about that. Okay, so what does my box actually do? So how does this work? There's a switch on the door, and when that lid is lifted off it goes into maintenance mode. Maintenance mode basically means I'm either cleaning or adding additional litter to the box. This basically means that the box weight is going to be changing when this lid is open. So once I put that lid back on, what I do is I have basically a really fancy Internet connected bathroom scale. And what IoT does is it reinitializes the base weight once that lid has been back on the box. Then we do the waiting game and we wait for some sort of cat size object to enter the box. I'm not smart enough to determine if it's like an opossum or raccoon or whatever, a small human, I don't know. But we're waiting for a small cats sized object to enter that box. We're waiting for that weight to be placed on the actual litter pad to detect it. So let's assume we have an actual cat and it's detected in the box. We have that weight gain on it. It does its business, and it does two things. It takes a passive measurement of my cat's weight and a timestamp of when that bathroom event happens. So I can passively keep track of my cat's weight over time, and I can keep track of the number of times he's using the restroom to see if there is drastic changes over time with these health indicators. Right. So once the cat has evacuated the box, done as biz, what we do is we reset with a brand new base weight of the box, and then we just wait for a new event, whether it is a maintenance event or a cat or cat size object entering the box. Again. Oh, let's check out some code. Nice. All right, again, if you want to check out all the code, bitly link or that QR code in the upper right hand corner. So, hello world, it's our first thing we always do with a Internet connected device or IoT device. An IoT equivalent of a hello world is just making that dang led blink. And this is actually my real pie, making a real led blink with a real Johnny five code base just blinking on and off. That's all we do. You can see my sensors and stuff all the side getting ready to go, but we just have that initial making sure everything's set up correctly. But what does that actually look like with the code? So we're requiring our Johnny five package. Johnny five works a bunch of different chips and cards and hardware. So we're actually telling Johnny five, hey, you're going to be interacting with a raspberry PI today. I said, okay, cool. So we want to initiate instantiate, a brand new instance that board of a raspberry PI board. So we can start working with that board in memory. And we have a callback function, an event driven right. And when that board has been ready to start receiving events, we instantiate a brand new led. And on the P 113, I have no idea what that is, but I have to google every single time. Literally, I'm messing with a PI. But it's what GPIO pin or the general purpose input output pins? Those basically allow us to interact with the real world on a pie. But we're saying on the 13th pin, on this pie, what I want you to do is just blink on and off. Johnny five is a built in functions for leds that can do a bunch of different stuff. But all we're saying is there's an Led on the 13th pin. Make it blank easy. Right? That's it. That's it. That's how you get an Led to blank with Johnny five on a pie. That's all it took. Super easy. Okay, let's get to some fun stuff, huh? We gotta go through this again. All right, let's make a toilet. So we got the hello world. Our board's initialized. We good, right? We good. So what do we do? We got to wait for our board to be ready. But all I'm doing now, instead of an LEd, I'm using that switch, right? That magnetic switch on the door that determines whether it's in maintenance mode. And when that event is either open or closed, I just change the state of the application whether it's in maintenance mode or not. So let's actually see things in practice. Sorry, this is a terrible video, but it's all I got. I'm so sorry. So you're going to see the little white thing on the board there. That's the magnetic switch I got screwed into the side there. And I'm opening and closing the door. And on my laptop, you can see that those open and closed events are being triggered by Johnny five. Woohoo. Right? Okay, here's the fun part. I love this. I learned a lot. It's going to have to go fast, so we only got 10 minutes left. I'm so sorry, y'all. But load cells again, this is a fancy Internet connected bathroom scales type things that measures the cat's weight. I actually couldn't find a node package that could interact with the load cells I bought. Whoops. But I did find a python package. And I've messed around with a bunch of stuff for a long time, but I actually got to play with the spongehold. Process nodejs function. It's a core function, right? It allows you to spin off on a separate thread, other stuff that's not even node. Right? I'm spinning off a python script from the main event loop on a separate thread to run this constantly. And what it's doing is this python script. It just reads the weight from the load cells every 10th of a second, and you're sending that out as standard input on the python script, and it is received by the process that spawned it on node as data. And I just parse the int or parse the float, and we update the weight for that thing every 10th of a second, constantly reading it. You can do error enclosed states, whatever, not a big deal, right? But the cool part is using that spawn child process and mixing languages to get this actually work. It's a little bit of a hack to admit, but it's super cool. And I love that. I got to explain things for real this time. Okay, so let's see what this looks like in practice. You can see the actual piece of plywood has the load cells underneath of it. That's why we put the box on. You can see here it's reading numbers. And I'm pressing on there and I'll show you. Just a second. Press. Right, press. And you can see that those weight measurements are being registered on my laptop every single time I'm pressing on that board. Not the greatest for you. I'm so sorry, but those load cells are being received by our node application. So now we have the maintenance mode, that switch, and we have that load cells registering weight from the box. Awesome. What are we going to do with all this data? What I'm doing is actually, I'm going to show you quickly an IoT time series data. I'm saving this, obviously in a MongoDB database because it works super well for IoT time series projects. And I'm saving it in a time series project because typically if you think about this, most IoT projects are being saved and reported on some sort of dashboard, so you can see real time results of whatever's happening. I'm doing the same thing. We have a web application that shows you a real time stats of my cat's bathroom behavior over time. I design the schema based on how I'm going to be reading that data, and the time series makes a lot of sense. So what does it actually look like with a MongoDB document? I'm glad you asked. We have a bunch of metadata about what's going on, but I make a brand new document in MongoDB every single day. And what I'm doing is I have an array of events, right, that events thing there at the bottom. And I'm keeping track of a timestamp and a weight and the type of event that's occurring, whether it's a maintenance event, a bathroom event. And I want to keep track of, passively keep track of my weights, my cat's weight, and the time that he went to the bathroom. And this can go as much as possible, right? We're going to probably only have a couple events per day. We're not going to have infinite amount, but the something's probably seriously wrong. But this is a perfect use case for how I'm reading that data. The other thing that works really well with this too, is MongoDB has a flexible schema. So I added that additional door sensor to keep track of the maintenance weight. I just updated that time series schema and it was super easy. I didn't even care about the old data. From now on, if I added additional sensors in the future, I would just update the time series data. It takes literally seconds, right? I don't have to worry about the old data. I can if I want to. I could update everything else if I wanted to, but I don't have to. And for a time series day like this, it makes so much sense that I don't have to do that. Okay, let's see the whole thing put together. Iot. That raspberry got that sliding doors back on and plug it in and you're done, right? I think it hides away pretty good. It's pretty easy to see, and it looks great. You could use a cheaper box if you wanted, but like, hey, I'm extra and I wanted something fancy for my cat, so sue me. There he is at the end. I added bribe matrix to sit on there. There he is at the end. I added bribe matrix to sit on there at the end of the photo shoot. Okay, time for some wild speculation. We're using to see smaller and more powerful hardware devices. I think this is going to mean that we're going to see additional compute power on the edge, which basically means right now what's happening. A lot of our devices are sending the raw data back to a central server in the cloud somewhere to actually process that data. I think we're going to actually see more of that data processing happening on the edge in the actual IoT devices in the field as they get more powerful. It's already happening. I did it with this project, we're going to see a continued increase in that happening. I think we're going to see Javascript more optimized for hardware devices, right? Smaller, lighter, smaller footprints, easier to run. I think there's incentive because there's just such a massive developer community devoted to JavaScript that it's going to continue to get more optimized for these specialty use cases. We'll see additional hardware support. I already ran into this issue with the load cells not working the way I expected them to. I think we're going to see additional support growing in the future as more and more developers are using node and JavaScript to interact with hardware. And lastly, this is true for just hardware in general. The batteries are and will continue to be the bottleneck. It will not be compute power. As the devices get more and more powerful, we're going to see continued reliance. It's going to get hotter and hotter and do more stuff, and it's going to require more energy to do that. We're seeing but their cell phones too, right? Like they're getting more and more powerful, but battery life is not getting better. There's optimizations of software to make them last longer, but it's still enormously difficult. And bottlenecks continue to be on the batteries. All right, we're almost done here, but let's say hypothetically, I've inspired you to want to try but some IoT and some hardware for your next project. How do you go about doing that? I might have a weird brain, but how I do it, I just do it. Get out there, pick a dumb project, just go buy a raspberry PI flex $35 and just try making an led blink. Try making a plant moisture sensor. Make something Google. There's a ton of raspberry PI projects out there, but try to make one. And every time you get stuck in something, just google IoT and try to figure it out. I think the best way to learn how to do something is to program and actually get your hands dirty. Also, if you are planning on using a project, you should totally check out MongoDB. It's so optimized for IoT types of data, including time series data and schemas. If you have any questions about things, stuff or want to chat about it, Twitter is the best way to get a hold of me and I'd love to talk about it with you and try to figure out your situation. But I just want to say thank you so much for having me here. I so much appreciate being a part of this community. It's incredible and I'm so honored to be invited to be here. It's incredible. And I'm so honored to be invited to be here. You again. If you want any of the resources I chatted about, including slides, this video code, whatever, you can check it out at that. And please, please, best way to get a hold of me is on Twitter, so I'd love to see you there and resources. But thank you so much, everyone. You're the best. I had so much fun. Hopefully to see you soon.
...

Joe Karlsson

Developer Advocate @ MongoDB

Joe Karlsson's LinkedIn account Joe Karlsson's twitter account



Awesome tech events for

Priority access to all content

Video hallway track

Community chat

Exclusive promotions and giveaways