Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

MongoDB Developer
JavaScript
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
JavaScriptchevron-right

An Introduction to IoT (Internet of Toilets)

Joe Karlsson11 min read • Published Feb 07, 2022 • Updated Sep 23, 2022
RaspberryPiJavaScript
Facebook Icontwitter iconlinkedin icon
Rate this code example
star-empty
star-empty
star-empty
star-empty
star-empty
social-githubView Code
My favorite things in life are cats, computers, and crappy ideas, so I decided to combine all three and explore what was possible with JavaScript by creating a brand new Internet of Things (IoT) device for my feline friend at home. If you're reading this, you have probably heard about how hot internet-connected devices are, and you are probably interested in learning how to get into IoT development as a JavaScript developer. In this post, we will explore why you should consider JavaScript for your next IoT project, talk about IoT data best practices, and we will explore my latest creation, the IoT Kitty Litter Box.
Stop Motion gif of the completed IoT Kitty Litter
Box
Stop Motion gif of the completed IoT Kitty Litter Box

IoT And JS(?!?!)

Okay, so why on earth should you use JavaScript on an IoT project? You might have thought JavaScript was just for webpages. Well, it turns out that JavaScript is famously eating the world and it is now, in fact, running on lots of new and exciting devices, including most internet-enabled IoT chips! Did you know that 58% of developers that identified as IoT developers use Node.js?

The Internet

Already

Speaks JavaScript

That's a lot of IoT developers already using Node.js. Many of these developers use Node because the internet already speaks JavaScript. It's natural to continue building internet-connected devices using the de facto standard of the internet. Why reinvent the wheel?

Easy to Update

Another reason IoT developers use Node is it's ease in updating your code base. With other programming languages commonly used for IoT projects (C or C++), if you want to update the code, you need to physically connect to the device, and reflash the device with the most up-to-date code. However, with an IoT device running Node, all you have to do is remotely run git pull and npm install. Now that's much easier.

Node is Event-Based

One of the major innovations of Node is the event loop. The event loop enables servers running Node to handle events from the outside world (i.e. requests from clients) very quickly. Node is able to handle these events extremely efficiently and at scale.
Now, consider how an IoT device in the wild is built to run. In this thought experiment, let's imagine that we are designing an IoT device for a farm that will be collecting moisture sensor data from a cornfield. Our device will be equipped with a moisture sensor that will send a signal once the moisture level in the soil has dropped below a certain level. This means that our IoT device will be responding to a moisture event (sounds a lot like an event loop ;P). Nearly all IoT use cases are built around events like this. The fact that Node's event-based architecture nearly identically matches the event-based nature of IoT devices is a perfect fit. Having an event-based IoT architecture means that your device can save precious power when it does not need to respond to an event from the outside world.

Mature IoT Community

Lastly, it's important to note that there is a mature community of IoT developers actively working on IoT libraries for Node.js. My favorites are Johnny-Five and CylonJS. Let's take a look at the "Hello World" on IoT devices: making an LED bulb blink. Here's what it looks like when I first got my IoT "Hello World" code working.
IoT Hello
World
IoT Hello World
Just be careful that your cat doesn't try to eat your project while you are getting your Hello World app up and running.
My cat eating the IoT Hello World
project
My cat eating the IoT Hello World project

IoT (AKA: Internet of Toilets) Kitty Litter Box

This leads me to my personal IoT project, the IoT Kitty Litter Box. For this project, I opted to use Johnny-Five. So, what the heck is this IoT Kitty Litter Box, and why would anyone want to build such a thing? Well, the goal of building an internet-connected litter box was to:
  • Help track my feline friend's health by passively measuring my cat's weight every time he sets foot in the litter tray.
  • Monitor my cat's bathroom patterns over time. It will make it easy to track any changes in bathroom behavior.
  • Explore IoT projects and have some fun!
Also, personally, I like the thought of building something that teeters right on the border of being completely ridiculous and kinda genius. Frankly, I'm shocked that no one has really made a consumer product like this! Here it is in all of its completed glory.
Stop Motion gif of the completed IoT Kitty Litter
Box
Stop Motion gif of the completed IoT Kitty Litter Box

Materials and Tools

  • 1 x Raspberry Pi - I used a Raspberry Pi 3 Model B for this demo, but any model will do.
  • 1 x Breadboard
  • 2 x Female to male wires
  • 1 x 3D printer [Optional] - The 3D printer was used for printing the case where the electronics are enclosed.
  • 1 x PLA filament [Optional] - Any color will work.
  • 1 x Solder iron and solder wire
  • 8 x M2x6 mm bolts
  • 1 x HX711 module - This module is required as a load cell amplifier and it converts the analog load cell signal to a digital signal so the Raspberry Pi can read the incoming data.
  • 4 x 50 kg load cell (x4) - They are used to measure the weight. In this project, four load cells are used and can measure a maximum weight of 200kg.
  • 1 x Magnetic door sensor - Used to detect that the litter box is opened.
  • 1 x Micro USB cable
  • 1 x Cat litter box

How Does the IoT Kitty Litter Box Work?

So how does this IoT Kitty Litter Box work? Let's take a look at the events that I needed to handle:
  • When the lid of the box is removed, the box enters "maintenance mode." When in maintenance mode, I can remove waste or refresh the litter.
  • When the lid of the box is put back on, it leaves maintenance mode, waits one minute for the litter to settle, then it recalibrates a new base weight after being cleaned.
  • The box then waits for a cat-sized object to be added to the weight of the box. When this event occurs, we wait 15 seconds for the cat to settle and the box records the weight of the cat and records it in a MongoDB database.
  • When the cat leaves the box, we reset the base weight of the box, and the box waits for another bathroom or maintenance event to occur.
You can also check out this handy animation that walks through the various events that we must handle.
Animation of how the box
works
Animation of how the box works

How to Write Code That Interacts With the Outside World

For this project, I opted to work with a Raspberry Pi 3 Model B+ since it runs a full Linux distro and it's easy to get Node running on it. The Raspberry Pi is larger than other internet-enabled chips on the market, but its ease of use makes it ideal for first-timers looking to dip into IoT projects. The other reason I picked the Raspberry Pi is the large array of GPIO pins. These pins allow you to do three things.
  1. Power an external sensor or chip.
  2. Read input from a sensor (i.e. read data from a light or moisture sensor).
  3. Send data from the Raspberry Pi to the outside world (i.e. turning a light on and off).
Animation of how the box
works
Animation of how the box works
I wired up the IoT Kitty Litter Box using the schema below. I want to note that I am not an electrical engineer and creating this involved lots of Googling, failing, and at least two blown circuit boards. It's okay to make mistakes, especially when you are first starting out.

Schematics

IoT Kitty Litter Box Schema
Design
IoT Kitty Litter Box Schema Design
We will be using these GPIO pins in order to communicate with our sensors out in the "real world."

Let's Dig Into the Code

I want to start with the most simple component on the box, the magnetic switch that is triggered when the lid is removed, so we know when the box goes into "maintenance mode." If you want to follow along, you can check out the complete source code here.

Magnetic Switch

1const { RaspiIO } = require('raspi-io');
2const five = require('johnny-five');
3
4// Initialize a new Raspberry Pi Board
5const board = new five.Board({
6 io: new RaspiIO(),
7});
8
9// Wait for the board to initialize then start reading in input from sensors
10board.on('ready', () => {
11 // Initialize a new switch on the 16th GPIO Input pin
12 const spdt = new five.Switch('GPIO16');
13
14 // Wait for the open event to get triggered by the sensor
15 spdt.on('open', () => {
16 enterMaintenceMode();
17 });
18
19 // Recalibrate the box once the sensor has closed
20 // Once the box has been cleaned, the box prepares for a new event
21 spdt.on('close', () => {
22 console.log('close');
23 // When the box has been closed again
24 // wait 1 min for the box to settle
25 // and recalibrate a new base weight
26 setTimeout(() => {
27 scale.calibrate();
28 }, 60000);
29 });
30});
31
32board.on('fail', error => {
33 handleError(error);
34});
You can see the event and asynchronous nature of IoT plays really nicely with Node's callback structure. Here's a demo of the magnetic switch component in action.
Switch Demo
Switch Demo

Load Cells

Okay, now let's talk about my favorite component, the load cells. The load cells work basically like any bathroom scale you may have at home. The load cells are responsible for converting the pressure placed on them into a digital weight measurement I can read on the Raspberry Pi. I start by taking the base weight of the litter box. Then, I wait for the weight of something that is approximately cat-sized to be added to the base weight of the box and take the cat's weight. Once the cat leaves the box, I then recalibrate the base weight of the box. I also recalibrate the base weight after every time the lid is taken off in order to account for events like the box being cleaned or having more litter added to the box.
In regards to the code for reading data from the load cells, things were kind of tricky. This is because the load cells are not directly compatible with Johnny-Five. I was, however, able to find a Python library that can interact with the HX711 load cells.
1#! /usr/bin/python2
2
3import time
4import sys
5import RPi.GPIO as GPIO
6from hx711 import HX711
7# Infintely run a loop that checks the weight every 1/10 of a second
8while True:
9 try:
10 # Prints the weight - and send it to the parent Node process
11 val = hx.get_weight()
12 print(val)
13
14 # Read the weight every 1/10 of a second
15 time.sleep(0.1)
16
17 except (KeyboardInterrupt, SystemExit):
18 cleanAndExit()
In order to use this code, I had to make use of Node's Spawn Child Process API. The child process API is responsible for spinning up the Python process on a separate thread. Here's what that looks like.
1const spawn = require('child_process').spawn;
2
3class Scale {
4 constructor(client) {
5 // Spin up the child process when the Scale is initialized
6 this.process = spawn('python', ['./hx711py/scale.py'], {
7 detached: true,
8 });
9 }
10
11 getWeight() {
12 // Takes stdout data from Python child script which executed
13 // with arguments and send this data to res object
14 this.process.stdout.on('data', data => {
15 // The data is returned from the Python process as a string
16 // We need to parse it to a float
17 this.currWeight = parseFloat(data);
18
19 // If a cat is present - do something
20 if (this.isCatPresent() {
21 this.handleCatInBoxEvent();
22 }
23 });
24
25 this.process.stderr.on('data', err => {
26 handleError(String(err));
27 });
28
29 this.process.on('close', (code, signal) => {
30 console.log(
31 `child process exited with code ${code} and signal ${signal}`
32 );
33 });
34 }
35 [...]
36}
37
38module.exports = Scale;
This was the first time I have played around with the Spawn Child Process API from Node. Personally, I was really impressed by how easy it was to use and troubleshoot. It's not the most elegant solution, but it totally works for my project and it uses some cool features of Node. Let's take a look at what the load cells look like in action. In the video below, you can see how pressure placed on the load cells is registered as a weight measurement from the Raspberry Pi.
Load Cell
Demo
Load Cell Demo

How to Handle IoT Data

Okay, so as a software engineer at MongoDB, I would be remiss if I didn't talk about what to do with all of the data from this IoT device. For my IoT Litter Box, I am saving all of the data in a fully managed database service on MongoDB Atlas. Here's how I connected the litter box to the MongoDB Atlas database.
1const MongoClient = require('mongodb').MongoClient;
2const uri = 'YOUR MONGODB URI HERE'
3const client = new MongoClient(uri, { useNewUrlParser: true });
4client.connect(err => {
5 const collection = client.db('IoT').collection('toilets');
6 // perform actions on the collection object
7 client.close();
8});

IoT Data Best Practices

There are a lot of places to store your IoT data these days, so I want to talk about what you should look for when you are evaluating data platforms.

High Database Throughput

First thing when selecting a database for your IoT project, you need to ensure that you database is able to handle a massive amount of concurrent writes. Most IoT architectures are write-heavy, meaning that you are writing more data to your database then reading from it. Let's say that I decide to start mass manufacturing and selling my IoT Kitty Litter Boxes. Once I deploy a couple of thousand boxes in the wild, my database could potentially have a massive amount of concurrent writes if all of the cats go to the bathroom at the same time! That's going to be a lot of incoming data my database will need to handle!

Flexible Data Schema

You should also consider a database that is able to handle a flexible schema. This is because it is common to either add or upgrade sensors on an IoT device. For example, on my litter box, I was able to easily update my schema to add the switch data when I decided to start tracking how often the box gets cleaned.

Your Database Should Easily Time Series Data

Lastly, you will want to select a database that natively handles time series data. Consider how your data will be used. For most IoT projects, the data will be collected, analyzed, and visualized on a graph or chart over time. For my IoT Litter Box, my database schema looks like the following.
1{
2 "_id": { "$oid": "dskfjlk2j92121293901233" },
3 "timestamp_day": { "$date": { "$numberLong": "1573854434214" } },
4 "type": "cat_in_box",
5 "cat": { "name": "BMO", "weight": "200" },
6 "owner": "Joe Karlsson",
7 "events": [
8 {
9 "timestamp_event": { "$date": { "$numberLong": "1573854435016" } },
10 "weight": { "$numberDouble": "15.593333333" }
11 },
12 {
13 "timestamp_event": { "$date": { "$numberLong": "1573854435824" } },
14 "weight": { "$numberDouble": "15.132222222" }
15 },
16 {
17 "timestamp_event": { "$date": { "$numberLong": "1573854436632"} },
18 "type": "maintenance"
19 }
20 ]
21}

Summary

Alright, let's wrap this party up. In this post, we talked about why you should consider using Node for your next IoT project: It's easy to update over a network, the internet already speaks JavaScript, there are tons of existing libraries/plugins/APIs (including CylonJS and Johnny-Five), and JavaScript is great at handling event-driven apps. We looked at a real-life Node-based IoT project, my IoT Kitty Litter Box. Then, we dug into the code base for the IoT Kitty Litter Box. We also discussed what to look for when selecting a database for IoT projects: It should be able to concurrently write data quickly, have a flexible schema, and be able to handle time series data.
Final IoT Kitty Litter Box Assembly
process
Final IoT Kitty Litter Box Assembly process
What's next? Well, if I have inspired you to get started on your own IoT project, I say, "Go for it!" Pick out a project, even if it's "crappy," and build it. Google as you go, and make mistakes. I think it's the best way to learn. I hereby give you permission to make stupid stuff just for you, something to help you learn and grow as a human being and a software engineer.
When you're ready to build your own IoT device, check out MongoDB Atlas, MongoDB's fully managed database-as-a-service. Atlas is the easiest way to get started with MongoDB and has a generous, forever-free tier.
Check out the following resources for more information:

Facebook Icontwitter iconlinkedin icon
Rate this code example
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

Using AWS Rekognition to Analyse and Tag Uploaded Images


Sep 11, 2024 | 1 min read
Tutorial

Create a Data Pipeline for MongoDB Change Stream Using Pub/Sub BigQuery Subscription


Apr 02, 2024 | 5 min read
Tutorial

Getting Started With Deno 2.0 & MongoDB


Oct 22, 2024 | 13 min read
Tutorial

Building a Scalable Media Management Back End: Integrating Node.js, Azure Blob Storage, and MongoDB


Nov 05, 2024 | 10 min read
Technologies Used
Languages
Technologies
Table of Contents