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 project for you.
This project will help track your feline friend's health by measuring its weight every time it sets foot on the litter tray, and monitors its urination patterns. The equipment can be connected to a companion smartphone app that displays the relevant data in an easy-to-understand graph format, so that cat parents can quickly spot symptoms of unusual weight loss.
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.
- 1 x Raspberry Pi (We used a Raspberry Pi 3 Model B for this demo)
- 1 x Breadboard
- 2 x Female to male wires
- 1 x 3D printer [Optional] It was used for printing the case where the electronics are encloused.
- 1 x PLA filament [Optional] of any color you want.
- 1 x Solder iron and wire.
- 1 x Screwdriver.
- 8 x M2x6mm Bolts.
- HX711 module This works as a load cell amplifier.
- 4 x 50kg load cell (x4). They are used to measure the weight. Four of them were used for 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.
- Node js should be installed on your Pi - Check out this article for tips
- NPM should be installed on your Pi
- Python should also be installed on your box.
Sign up for MongoDB Atlas. We will be using MongoDB Atlas to save all of our IoT Sensor data.
- More information about getting started with MongoDB Atlas can be found here: https://docs.atlas.mongodb.com/getting-started/
Download and unpack IoT Kitty Litter Box. Or alternatively checkout from source:
git clone https://github.com/JoeKarlsson/iot-kitty-litter-box
cd iot-kitty-litter-box
Next, inside the project, you need to install the project's various NPM dependencies:
npm install
Next, inside the project, you need to install the project's various Python dependencies:
npm run setup
Rename src/example_config.json
to src/config.json
and add your MongoDB Atlas URI connection string.
You should now be ready to spin up a development build of your new project:
npm start
Navigate to http://localhost:3000.
The methods below describe ways to automatically start you IoT Kitty Litter Box on boot, and even ways to keep it running in case of a failure.
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks. In this case we will use it to keep a shell script running.
Install PM2 using NPM:
sudo npm install -g pm2
To make sure PM2 can do it's job when (re)booting your operating system, it needs to be started on boot. Luckily, PM2 has a handy helper for this.
pm2 startup
PM2 will now show you a command you need to execute.
To use PM2 in combination with IoT Kitty Litter Box, we need to make a simple shell script. Preferable, we put this script outside the IoT Kitty Litter Box folder to make sure it won't give us any issues if we want to upgrade the mirror.
cd ~
nano iot-kitty-litter-box.sh
Add the following lines:
cd ~/iot-kitty-litter-box
DISPLAY=:0 npm start
Save and close, using the commands CTRL-O
and CTRL-X
.
Now make sure the shell script is executable by performing the following command:
chmod +x iot-kitty-litter-box.sh
You are now ready to the IoT Kitty Litter Box using this script using PM2.
Simply start your mirror with the following command:
pm2 start iot-kitty-litter-box.sh
You box should now boot up and appear on your screen after a few seconds.
To make sure the IoT Kitty Litter Box restarts after rebooting, you need to save the current state of all scripts running via PM2. To do this, execute the following command
pm2 save
And that's all there is! You IoT Kitty Litter Box should now reboot after start, and restart after any failure.
With your IoT Kitty Litter Box running via PM2, you have some handy tools at hand:
pm2 restart iot-kitty-litter-box
pm2 stop iot-kitty-litter-box
pm2 logs iot-kitty-litter-box
pm2 show iot-kitty-litter-box
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Joe Karlsson |