An Introduction To IoT; Or How I Built an IoT Kitty Litter Box Using JavaScript


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.


Hello, welcome to an introduction to IoT, parentheses, Internet of Toilets. This is going to be a talk about how I built a Internet-connected kitty litter box using JavaScript and Node. Let's just jump into it. First of all, my name is Joe Carlson. You probably don't care about me at all, that's fine. But I'm a software engineer and developer advocate, and I work for a company called MongoDB. You may have heard of it before. I think a pretty cool database product, if I do say so myself. If you are wanting to hang out with me ever again in the future, please do so. Best place to do that is on Twitter. But if you're on TikTok too, you should for sure check that out and make funny videos about programming over there. If you want to follow any of the links, source code, recording of this video, slides, code samples, all that, you can find that on the link there,, or if you scan that QR code in upper right-hand corner of the video, that'll also take you there. Lastly, and this is very important, but anything I say in this talk reflects my own views and not the views of my employer. Cool. I love my job. I don't want to get fired. What are we going to talk about today? First of all, we're going to just do a quick introduction to IoT. What is it and why do I think it's so cool? Then I'm going to talk about why you should be considering JavaScript and Node for your next IoT project. Then we're going to talk about my favorite part here, which is my litter box. How I built it, we'll show you some code and how it works. Lastly, we're going to make some broad generalizations about the future of the Internet of things. This will be fun to watch in five years together. We can see how wrong I am about everything. But hey, whatever, predicting is fun, and I'm going to try my best at it. What the heck is IoT? Well, I'm sure you already know, but it's basically anything that connects up to the Internet. It's just a thing with a chip on it that has Wi-Fi or Ethernet or whatever. It just can connect up to the Internet. But I want to talk about my particular interest in IoT. I think that us as engineers, we are particularly well-suited for exploring tech and the Internet as a medium of art. I think the Internet is the defining thing that has shaped who we are as a generation, how we behave, and I think that there's so much room to explore that area. I love doing that through art and tech. In particular, even more niche, I'm obsessed with stupid shit. I love putting chips in things that they do not belong in. I think it's fun exploring that fine line between just genius and complete stupidity. In fact, I love it so much, I ran a hackathon called the Stupid Shit and Terrible Ideas Hackathon, which we just made stupid stuff. Someone made a camera that only takes photos when you shake it, or a six-foot wooden fidget cube, or a laptop made out of cardboard. Fun stuff. I love stupid shit. IoT, great. I think we all know what it is. I just wanted to talk about why I love it. But why should you be considering JavaScript and Node for your next IoT project? Well, did you know that 58 percent of IoT developers self-identify as Node developers? This is purely my opinion, but I think JavaScript is a great choice for new developers. A lot of people are learning it when they're coming out of boot camps. It was my first programming language. It's just a good tool for people who are learning how to like or want to do more visual stuff. Not to say that C or C++ are bad programming languages, they definitely still have a place in programming Internet-connected devices that is not going away anytime soon. They are faster and smaller than JavaScript is right now, and that's probably not going to change anytime soon. They're just harder to develop for sometimes. It's harder for me anyways. If you're learning hardware, just take one thing at a time, just learn the hardware component. They're also easy to update over a network. Traditionally, with embedded devices, how to update the firmware on them is you have to actually take that device, plug it in, and reflash that code on there. JavaScript is a little easier though. All you have to do is run Git pull and npm install and you're done, you're all updated. The Internet already speaks JavaScript. We're already speaking HTTP over the web. We're already sending these JavaScript files to run the Internet. Makes sense to just do it for IoT devices as well. You may not know this, but there's already tons of existing tools, libraries, and datasets already available for you, including Cylon.js and Johnny5, which you check out both of them, they're great. JavaScript is event-driven in nature. If you think about it, IoT devices are also event-driven. You have some sensor out in the field that's waiting for some event to happen, and then once it's triggered, you want some event or callback in JavaScript, if you want that code to run. I do work for MongoDB and I do need to talk a little bit about data considerations if you're building IoT projects. I want to talk broadly about what to look for when you're building out or you're looking at databases to save IoT data. First of all, you want to make sure you're looking at databases that are able to ingest data quickly. IoT projects are unique in that they are write-driven applications. Most applications that are web-focused are read-heavy. I write one tweet to a database and it could be potentially read by millions of people. None of mine are, but if I was that famous, maybe it could be. But you want to make sure you're looking at databases with different needs. Being able to ingest or write data concurrently and massively is extremely important for an IoT database. We mentioned earlier that a lot of IoT projects are event-driven. We're waiting for some asynchronous event to happen out in the field, and we want to make sure we have a database that matches that event-driven nature of our IoT projects. Flexible schemas are uniquely well-suited for IoT projects. For example, on my project, I added a new sensor and I was able to update my time series data to include that new sensor data. Time series data is unique in that it gets stale really quickly, and you're typically using it to visualize in some dashboard. Maybe we're using it for historical uses as well. As we're adding and upgrading over time, we can start adding and adjusting our schema flexibly at scale. I do want to note here, it's a common misconception that MongoDB is a schemeless database. You can actually validate schemas on a database level with MongoDB. You don't need an ORM like Mongoose. You want to make sure you have a database that handles time series data well. You can make a time series data with most RDBMSs. Cool, not a problem. But it does get messy really quickly. I'm going to show you my IoT time series data I used for my project and why I think it's superior to other models. I may be biased here since I work for MongoDB, but MongoDB is a great choice because it does all these things and more. All you have to know is that if you're looking at using a MongoDB or IoT project in the future, you should definitely be considering MongoDB. Sales pitch is over. Let's get back to the good stuff. IoT key litter box. Let's jump in. This is actually what I ended up building. It's a little mid-century box I bought. I know it's extra. You don't need to get something fancy like this. You could get a $20 plastic thing on Amazon or whatever, as long as it's got an enclosure and can use cold litter. That's all you need. I thought this was a dumb idea, but I recently found out this robot kitty litter box exists. Wired gave it an eight out of 10, and more importantly, they're selling it for 500 bucks. I'm not trying to monetize this, but if you wanted to, this might be your chance. How does this thing work? I have a load sensors underneath the box. They're basically like an Internet connected bathroom scale. What it does is, well, let's get to the other part first. We have an enclosure on the box and there's a switch on it to determines when it's open or closed. When we open that box, we're going to something I call maintenance mode. We're either removing waste or adding litter to the box. Point is though, the base weight of that box changes every time it goes into maintenance mode. Once I've cleaned it up, done my work on it, close it back up again, it leaves maintenance mode, waits for it to chill out a little bit, then we reinitialize the base weight of that box. After the base weight of that box has been determined, what we do is we wait for a cat-sized object to enter that box. Now, I don't know if that's an opossum, a raccoon, or a large rat, doesn't really matter. We're just waiting for something that's around 5-15 pounds to enter that box. Once a cat has been detected by the load cells, we wait a little bit for cat to settle. Then we take a measurement of that cat's weight. I can passively measure the cat's weight over time, and we record a bathroom event having occurred. It does its business, and we fire that event off into a MongoDB database in the Cloud. Cool. Cat's done its business. What we do is wait for things to cool off a little bit. I wait five minutes, and we reinitialize a new base weight of the box. We wait for a new event, either that's a maintenance event or a new cat entering the box event. These are all these asynchronous events that we're just waiting for to constantly occur. That's it. It's pretty simple. Let's jump into some code though. Let's build a little code, guys. If you want to follow along with any of the source code, again, all the code is available at link or if you scan that QR code with your phone. The first step for any IoT project, if this is your first IoT project, the first step you need to do is make an LED. LED blank. It basically just makes sure that your circuits are set up, your board's fine, you're able to communicate it and make it do what you want. Just like programming, iterate on that and build. You can see that actually have the load cells staged up next door or next to it over there. I'm just making the LED go on and off. That's it. How do we do that? With Johnny-Five, it's pretty easy. Johnny-Five works with a lot of different boards. We need to import the Raspberry Pi package, and we reinitialize a brand new Johnny-Five board and tell it, hey, your input output you're going to be dealing with today is in fact a Raspberry Pi board. Then we wait for that board to be ready to start listening, and then we just initialize a brand new LED on the 13th GPIO pin. I don't know what that is. I got to Google it every single time I hook it up and then it just has a built-in function, says blank. But that's it. What is that? That's like, I don't know, less than 10 lines of code to get our hello world working for our Johnny-Five. It's pretty simple. How do we make an LED into a toilet? Good question. We're going to do the same thing. We're going to bring in that Raspberry Pi board. Instead of bringing in LED, we're going to be using a magnetic switch to determine if that box is in maintenance mode or not. Then just depending on the state of whether it's open or closed, we can change the state of the box. Let's see it in practice. I'm going to show you the board or me doing it. You can see the little switch there, that little white thing. That's the magnetic switch, there's one on each side. As I open and close that box, you can see that those events being read in real-time by my Raspberry Pi and determining what state the box is in. Pretty simple. The load cells. This part's actually a little more complicated. I had to get a little creative with how I solved this problem. I actually ended up using, because I couldn't find a library with Johnny-Five that interface directly with the load cells I bought. Go figure. But I did in fact find a Python library that could do it. I ended up using spawn child process, which is just baked into Node core, to spin off a child thread that runs a Python script and sends the data from the Python script up to the parent process. Then I can do whatever I want with it, my Node program. Pretty simple. It's Node and JavaScript, but I got a little creative on it too. Every 10th of a second, it's just reading the base with a box and it sends it up. I convert it into a float, update the average, and then I just keep waiting for that cat size event to happen. Let's take a look at it. Underneath that piece of plywood, I have the load cells at each corner. It's hard because I couldn't get too far enough away, but I'm pressing on the board. You'll see here in just a moment, those presses are being registered in real-time on my Node application. I'm able to register and see in real-time what's happening or the weight of the box at all times. I promised you we'd talk about my IoT time series data here, and I'm still going to follow through with that promise. This is what it looks like. Every single 24 hours, I create a brand new document in my MongoDB database. In there, I have some metadata about me and the cat in case I wanted to scale this up to multiple boxes and cats and owners, and I have an events array. In my events array, every single time a cat goes in the box, or I clean the box out, I add a brand new document or sub-document into that events array. I designed it this way because of how we use it. I'm reading this data to display on a dashboard, and I want to show all the events per day of what's happening. I designed my IoT schema to be easily read for my reporting that I'm doing on this, which is like the number one rule of MongoDB schema design. Just design your schema based on the needs of your application. How are you going to be using that data? This was how I'm going to be using it. You could do this once a week, once a year, but for a litter box like this, it makes sense just to do it once a day. Cool. Box assembly. Put the whole box together. You can see the pie mounted on the inside there. I got some wires and I got a little wire to plug it in, and that's it. It's pretty simple. It doesn't look elegant when you open the box up, but it looks pretty good when it's all closed up. There's my cat. I had to bribe him some treats to get this photo. Almost done here, I promise. The future of IoT, this makes some predictions here. First of all, I think, of course, devices are going to get smaller and more powerful in the future. We're already seeing this. They're going to be embedded in even more things as we move forward. We're actually already seeing this, but JavaScript is being compiled down to more IoT and embeddable sizes. They're more stripped down and they have a smaller garbage collector to be more energy and memory efficient, so they are more effective on smaller devices. We're already seeing this happen. We're going to see better hardware support. Like today, I told you I had some sensor issues with those load cells. I was able to find a Python library, but in the future, those will get fixed, I think. Of course, batteries are and will continue to be the bottleneck. As devices get more powerful, they also generate more heat, which also consumes more power, unfortunately. Especially embedded devices that aren't plugged in 24-7 like my box, energy is and continue to be the issue as we see these in more places. If I've inspired you all today to want to become an IoT or JavaScript or MongoDB master, how do you do it? Well, listening to me talk about this is one thing. The other thing I would recommend doing is just to get out there and do it. Pick out a project, doesn't have to be cool, make some stupid shit at home, just make something fun, make an LED blank. You can get a Raspberry Pi, most computer stores for about 25-35 bucks. You get a little sensor IoT thing, a starter pack on Amazon for like 20 bucks too. Just play around. I think don't worry about having to monetize it or making something that's professional or cool. I think it's fun just to explore and see what you get, and try to share it if you can. I'd love seeing that stuff. I'd love to see what you work on. I'm in the chat if anyone has any questions. Otherwise, you can definitely hit me up on Twitter at JoeCarlson1. I'd love to connect with you anyways, even if you don't have questions. All the links for the stock video slides, code samples, everything available at the link there or if you scan that QR code. Thank you so much, everyone. You've been incredible. I've had a blast here. Bye. Now, let's discuss the poll. The question is, what's your experience with IoT? Not judgments, just curious. None said 50 percent of them says none. Just interested, I'm potentially getting into someday. A little with 34 percent. A little I've made a blank blank one time. Sixteen percent responded, I have a couple of projects under my belt. Zero percent said, I do IoT work full-time. Nobody does it full-time, but most of them have. That's about what I expected. This is a gentle introduction to Internet toilets. I hope too, even if you've been a master, maybe you still learn something new, but this is a great talk for newbies, so that's great. Awesome. Let's go to the Q&A. We have, tell me about some other cool IoT projects you've been working on. Great question. I didn't invent this project, but I recently made it. The Magic Mirror project, it's basically putting a monitor behind a two-way mirror and then hooking it where it has a very high up and it runs a little electron web app. That's super fun. That's honestly one of the most useful things I've ever made. It's so fun. I also made those nano leafs, those cool fancy triangles that light up you can put on the wall. They're also very expensive, but I made my own Internet-connected nano leafs with a 3D printer and some LEDs, and a little web app so you could control them. It was so fun. It was such a fun project. Then have I made anything else? No, those are the big ones, and my Internet-connected litter box. I think they're just fun. I did make a digital graffiti board a couple of weeks ago. The idea was it's like an art piece or I could have it in the back when I'm giving a talk like this, and people could join in and draw on it in their phones and interact with it in real life. Wow, all of that sounds amazing and super fun. Thanks, Liz. I'm biased, but I totally agree with you. They are really fun and amazing. I would definitely play with those. That's super fun. The next question is, any small experiments that could be easy to dive into IoT with JavaScript? Yeah. I talked about this a little bit in the talk, but my recommendation if you're new to IoT programming, get a Raspberry Pi. It's like a Unix-based environment. So you're used to programming on a Mac or on Linux, it's going to, same deal, and you're going to have a full node environment on it, and just play around with it. The easiest thing to do is just make an LED to blink, turn on and off. I don't even know. One of my first projects I ever made was a web app that could translate text into Morse code. So just blink Morse code for whatever message you're doing. Yeah, I don't know. I mean, that was such an easy circuit, but trying to make a fun web project out of it was a blast. Yeah. It's not running anymore. Yeah. I think everybody that starts with IoT, they always start with a Raspberry Pi. It's like most common and easiest. Yeah. Oh, totally. I've gotten into the more advanced embedded chips and smaller things and whatever, like cool. But if you're just starting out, why make your life hard? Just do what you know. I agree. The next question is, in your opinion, what are some other practical or everyday IoT projects that you've come across? Oh, well, I mean, anything that has a chip in it. Personally, there's a ton of amazing cool Pi projects out there that are IoT projects. My Pi hole is one of my coolest things I currently use. I didn't make Pi hole. It's a network-wide ad blocker. It works in the DNS, which is super cool. I made a NAS or network-attached storage with Raspberry Pis, which is cool. Little art projects. The point is that you can make practical things for your home. You can make dumb art projects. You can make useless stuff. It depends on whatever you want to do. Just give it a Google, like Raspberry Pi beginner projects. You're going to find a bunch of cool stuff to make. That's a great advice. Thank you, Joe. It seems that we don't have any more questions, but please join Joe in his speaking rooms on Spatial Chat. The link to join is on the timeline. Thank you, Joe. See you later. Thank you. Bye. Bye.
23 min
10 Jun, 2021

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Workshops on related topic