If you follow my tutorials, you know that I live with some cats and I love doing projects involving four things: 3D printing, internet of things, robots and, of course, cats! You can find other of my projects like this scale saves weight measurements in the cloud or this hanging cat bed made with PVC pipes.
When I was shooting the video on the IoT scale, I realized that it would even be used to track the weight of my cats. They usually come up on the wooden surface regularly (especially if I'm trying to weigh myself in an attempt to troll me and make I look heavier), but they don't have not have the patience to stand on the device for long enough to complete the measurements. It made me think of ways to weigth them regularly without having to worry about having to put them on some specific surface and hold them there for a while. And that's where the idea of using their litter boxes as "bait" came up.
They usually stay inside the litter box long enough to make the necessary measurements, and they use it several times a day without the need for human intervention, making the box the ideal surface for the installation of a similar device used om my IoT scale. In addition, their weights are considerably different (one has about 3 kg and the other one 5 kg), so that it would be possible to distinguish between the measurements which cat was on the scale.
But I finally realized that it would be possible to use the device to extract a lot of new information! By measuring the weight of the box it would be possible to monitor the amount of sand available inside (as a way of indicating that it is time to fill with more sand or change all the contents), the number of times the cats have used the box per day, when the box was last cleaned, etc.
I rolled up my sleeves and set out to design the project described here: a cat litter box with WiFi, using an ESP32, able to measure the weight of my cats (using weight sensors and HX711 amplifiers), monitor the general cleaning conditions of the cat litter box and even store all the data in the cloud (using the Thingspeak platform)! The device is even able to generate notifications by email, Twitter, etc.
Surely it may seem like a shitty idea (in this case, cat poo), but that can be used by the reader to learn some new skill that can be used in other projects:
- How to program an ESP32 using the Arduino IDE;
- How to read digital inputs using an ESP32;
- How to use load sensor and an HX711 for weight measurement;
- How to send ESP32 data to the cloud using Thingspeak platform;
- How to design and print a 3D case using Fusion360.
Did you like this project? Please consider supporting my future projects with a small bitcoin donation! : D
Deposit Address: 1FiWFYSjRaL7sLdr5wr6h86QkMA6pQxkXJn
Don't forget to vote for this project on Instructables Pets Contest! I'll really appreciate your vote! :D
Step 1: Tools and Materials
The following tools and materials were used in this project:
Tools and materials:
- 3D printer. It was used for printing the case where the electronics are encloused.
- PLA filament of any color you want.
- Solder iron and wire. Some of the components (ESP32 Firebeetle and HX711, for instance) doesn't come with soldered terminals. I needed to solder some wires or pins in order to connect those devices.
- Shrinking tube. l also had to solder the wires of each load cell. A piece of shrinking tube might be used for a better isolation of the conductors.
- Screwdriver. The structure is mounted using some screws. A set of screwdrivers was used.
- M2x6mm Bolts. They were used for mounting the electronics inside the case.
- FireBeetle ESP32 dev board. It's really easy to use and program using Arduino IDE. It has built-in Wi-Fi module, so you can use it in a variaty of projects. You can also use other ESP8266 based bords (link / link / link) if you wish.
- HX711 module. This works as a load cell amplifier. Four strain gauge load cells are connected to this module, and it communicates on a serial communication with the microcontroller.
- 50kg load cell (x4);. They are used to measure the weight. Four of them were used for a maximum weight of 200kg.
- Magnetic door sensor. It consists in a magnectic switch, which was used to detect that the litter box is opened.
- 6 female-female jumper wires;
The links described above are only a suggestion of where you can find the items used in this tutorial (and support my future hacks). Feel free to search for them elsewhere and buy at your favourite store.
Step 2: Setup FireBeetle ESP32 on Arduino IDE
For this project I used Arduino IDE for programming my ESP32 module. It's the easiest way if you've already used an Arduino before, and you won't need to learn a new programming language, like Python or Lua for instance.
If you've never done this before, first you'll have to add ESP32 board support to the Arduino software.
1. Download and install Arduino IDE latest version
You can find the latest version for Windows, Linux or MAC OSX on Arduino's website: https://www.arduino.cc/en/main/software
Download it for free, install it on your computer and launch it.
Arduino IDE already comes with support to a lot of different boards: Arduino Nano, Mine, Uno, Mega, Yún, etc. Unfortunatly ESP32 isn't by default among those suported development boards. So in order to upload your codes to a ESP32 base board, you'll have to add its properties to Arduino's software first.
- Navigate to File > Preferences;
- Add the following URL to Additional Boards Manager textbox (the one on the bottom of the Preferences window):
- If the text box wasn't blank, it means had already add other boards before on Arduino IDE before. Add a comma at the end of the previous URL and the one above.
- Hit "Ok" button and close the Preferences Window.
- Navigate for Tools > Board > Boards Manager for adding your ESP32 board.
Now your Arduino IDE will be ready to work with ESP32 FireBeetle board.
Plug the microUSB cable on the ESP32 and on your computer. It will automatically install Windows communication driver. If it fails to install the driver, download the following file and manually install the driver for the newly connected device.
If you wish to use an alternative ESP32 based board, add the appropriate board.
3. Adding the libraries
The following libraries will be used for our Arduino code. Download the following libraries:
Navigate to Sketch-> Include Library -> Manage Libraries... on your Arduino IDE and add the libraries above.
Now that your dev environment is ready, let's move on to the next step!
Step 3: Wiring Up - HX711, Load Cells, Reed Switch and ESP32
This project used the following electronic components:
- ESP32 Firebeetle development board: This board has an Expressif microcontroller and is the component responsible for reading variables, handling and sending data to the cloud using a WiFi connection. It is in this device that the project code runs.
- Load Sensors: component responsible for measuring the force, converting the pressure applied on the device into an electrical signal.
- Amplifier HX711: circuit responsible for the amplification of the electric signal generated by the charge cells and digital analogue conversion of sina. The signal generated by the load cells has a very restricted amplitude (signal in mV). This circuit amplifies this signal and performs the conversion of the analog signal to digital, delivering the converted signal to the microcontroller.
- Door sensor switch: consists of a switch that is normally-closed in the absence of a magnetic field. When applied a magnetic field in its proximity, the switch closes an internal contact, allowing the conduction of electric current. In this project, a switch of this type was used to identify whether or not the top of the litter box was opened. A magnet has been attached to the top cover, while the switch is attached to the controlled and fixed on the bottom of the box. When that cover is closed, the magnet and key will be aligned and close together, keeping the switch closed. When the two parts are separated, the magnet will move away from the switch and the circuit will open.
Each device was connected according to the schematics.
HX711 - input:
- Upper left load cell signal (red wire) => HX711 E- pin
- Lower left load cell signal (red wire) => HX711 A+ pin
- Upper right load cell signal (red wire) => HX711 A- pin
- Lower right load cell signal (red wire) => HX711 E+ pin
- HX711 Vcc pin => ESP32 3.3V pin
- HX711 GND pin => ESP32 GND pin
- HX711 SCK pin => ESP32 GPIO 2 (pin D9)
- HX711 DT pin => ESP32 GPIO 5 (pin D8)
- Reed switch (terminal 1) => ESP32 GND pin
- Reed switch (terminal 2) => ESP32 GPIO 25 (pin D2)
When everything is wired up, plug a USB cabe on the ESP32 and get ready for uploading the code.
Step 4: Thingspeak Setup
There are several cloud data storage services for use with the internet of things. They allow communication between your microcontroller and the network, allowing you to send and receive all kinds of data. You can see some of the projects I've developed using Adafruit.io, for example:
Thingspeak is another such service. It is really easy to use, allowed monitoring of various information.
1. Create your account: first you must create your account at https://thingspeak.com/.
2. Create your channel: Once your account is created, you can create a channel where your data will be stored. Click on "New channel" and enter the name of your channel In my case I created a channel called "IoT Litter Box".
3. Set the fields of your channel: each channel can have up to 8 fields. Each field is equivalent to a variable that will be stored on your channel. In my project, 6 channels were used:
4. Write down your writing key: click the "Keys" option and copy your write key. You will need this information later in your Arduino code.
And it's ready! Your channel will be set up to start receiving data from your controller. You can see the data beginning to arrive in the Public view tab (if your channel is configured as public) or Private view. You can create specific views for public and private modes.
Step 5: Arduino Code
The code idea is as follows:
1 - The controller must be initially calibrated to know the empty box weight and adjust the calibration factor. The information on how to calibrate is presented at the end of this tutorial. This information will be stored in the controller memory and is not expected to change over time. This value will be used in the future to allow the calculation of the volume of sand and individual weight of the cats.
2 - Whenever the top of the box is opened, the controller understands that the box has gone into maintenance (cleaning) and should stop monitoring the box weight measurement. In that period, someone should be tossing sand (adding or removing sand, poop, etc.), and there should be fluctuations in box weight.
3 - As soon as the box is closed, the controller should wait some time for stabilization and re-check the weight. The first measurement performed should be stored in the controller memory as the weight of the box + sand. This value will be subtracted from future measurements to allow the calculation of the weight of the cats discounted from the weight of the box and its sand content.
4 - By subtracting the measured weight (in the absence of a cat inside the box) from the empty box weight (stored in the controller during its calibration), it is possible to determine the volume of sand in the box. This value is expected to decrease over time whenever the litter box is cleaned. You can set alarm limits to alert when the sand level in the bin is too low, indicating the need for general cleaning and sand replacement. The sand volume in the box will be sent to the cloud and you can generate alerts.
5 - Whenever the controller senses a significant increase in the weight of the box with the top cover closed, you will understand that some cat must have come in to make your needs. After stabilizing the measurement (understanding that the cat has stopped moving to make its needs), the controller will store the measured average weight. The value will be compared with expected maximum and minimum values for each of the cats (assuming more than one cat should use the box) in order to determine which cat has entered the box. The information on the weight of the cat and the count of times the cat used the box on the day will be updated and sent to the cloud.
6 - Periodically the controller check the time from a server. When detecting the beginning of a new day the counter of times that each cat used the box will be reset.
The following statements should be updated according to your case:
- myChannelNumber = Thingspeak channel number
- apiKey = ThingSpeak API key
- ssid = ssid of WiFi network
- pass = WiFi network password
- weightbox = weight indicated for the empty box
- cat1MinWeight = minimum weight of cat # 1
- cat1MaxWeight = maximum weight of cat # 1
- cat2MinWeight = minimum weight of cat # 2
Click Here Download iot-litter-box-thingspeak-v0
Step 6: 3D Modeling
3D printing was used to make some of the parts in this project. Of course the litter box itself was bought in a normal pet shop. However, some custom parts had to be designed and printed to allow the electronics to be added to the litter box.
All parts were designed using Fusion360 CAD software.
The following parts were designed:
- Supports for the load sensors: the load sensors are embedded inside four supports on the box feet. Such brackets allow the installation / removal of the sensors.
- Case: the case protects the electronic components from physical contact. The case is designed to be installed in the box using double-sided adhesive tape, thus allowing the case to be subsequently removed (for cleaning the case, for example).
- Case cover: allows frontal closure of the case, protecting electronic components.
Step 7: 3D Printing
You can download all the stl files on the following websites:
I printed the whole structure in PLA, usign two different colors. The whole print took me around 5h, using 0.2 mm resolution and 10% infill. No supports needed. This is a experimental prototype. Notice that it was designed for a given ESP32 dev board model (the ESP32 Firebeetle).
If you don't have a 3D printer, here are some things you can do:
- Ask a friend to print it for you;
- Find a hacker/maker space nearby. The parts used in this model can be printed quickly. Some hacker/maker spaces will only charge your for the materials used;
- Improvise! You can try to assemble a structure without 3D printed parts.
Step 8: Assembling the Litter Box
Once the parts are printed, it's time to assemble!
1 - Solder the terminals of the HX711 module to the ESP32 using some jumpers according to the schematic previously presented;
2 - Solder a pin bar (male type) on the HX711 for later connection of the load cells;
3 - Install the ESP32 and HX711 in the case using 6 M2x6mm screws;
4 - Thread the reed switch wires through the bottom hole of the case. Solder the reed switch terminals according to the schematic above;
5 - Fit the load cells inside the supports printed in 3D;
6 - Apply double-sided tape to the back of the case and glue to the side of the litter box;
7 - Glue the load cells to the bottom of the litter box;
8 - Solder the wires of the load cells according to the schematic presented above. Solder a female connector on the red wire of each cell to allow its connection to the pin bar on the HX711;
9 - Arrange the wires. Use insulation tape to stick them to the box;
10 - Collect the load cells to the HX711 module according to the schematic previously presented;
11 - Install the front cover. Use three M2 x 6mm screws to secure the cover;
12 - Stick the reed switch on the box (bottom) and the magnet on the box cover (top). Both should be aligned;
13 - Plug the micro USB connector to power the circuit.
Step 9: Calibration
The calibration of the balance consists of the determination of two values: the zero and the calibration factor.
For calibration of the scale, the following steps must be followed:
1 - Download the calibration code in the ESP32 and open the Serial Monitor;
2 - Every second the controller will read the weight in the load cells and print on the screen. Put a known weight on the box (a clean sandbag, for example) and check the value indicated on the screen. The value indicated on the serial port should match the weight of the reference weight.
3 - If the value is not as expected, type + or - in the Serial Monitor (followed by enter) to increase or decrease the calibration factor of the scale until the indicated weight coincides with its reference weight.
4 - Remove weight and press 0 followed by enter. The controller will measure the new value from zero, considering the current calibration factor.
5 - Put the reference weight again and check the indicated value. If the value is not nearly as expected, repeat steps 3 through 5 until the desired result is achieved.
Note the values of the calibration factor and the empty box weight. These values should be updated in the final Arduino code.
Step 10: Testing
Once the project is complete, open the lid, put sand in the box and power the circuit. The controller will connect to the internet and begin to run the code.
Now just wait for some of the cats to show up to see if everything is working properly.
You can track the data daily on the Thingspeak website.
Step 11: Bonus Round #1 - Daily E-mail Reports
Once the data is stored on the Thingspeak server, you can use this data to generate alerts, send emails, tweets, etc.
In this step I'll show you how you can generate a daily email report with a summary of the information collected. To do this, I'll use the integration between Thingspeak and IFTTT.
The following steps should be followed:
1) Create an account in IFTTT (or log in to the account if you already have one).
2) Create a new applet. Select "My Applets" and hit the "New Applet" button.
3) Select as "input" (+ This) the Webhooks service.
4) Complete the trigger configuration by clicking on "Receive a web request box". Create an event name (in my case I used "LitterBox", and create the trigger.
6) Choose the output (+ That). Select the "Email" option.
7) Select the "Send me an email box" option and configure the format of the message. In my case, I used only the "Event name", "OccuredAt" and "Value1" fields in the email body. Create the action and end the applet.
8) Select My Applets> Services and search for Webhooks. Click the documentation button. Copy your key (you will need this information later).
9) You should now create a code in Matlab (MATLAB Analysis) to evaluate the data and send the e-mail request to the IFTTT. In Thingspeak, select Applications> MATLAB Analysis.
10) Select "New" and enter the code below and save MATLAB Analysis.
% Read the channel data from the past Day.
% Send an email via the IFTTT service to report the user about the measurements
% Store the channel ID for the IoT Litter Box.
channelID = XXXXX;
% Enter the trigger url from IFTTT. It will have the form:
iftttURL = 'https://maker.ifttt.com/trigger/LitterBox/with/key/XXXXX';
% Channel Read API Key (if you are using your own moisture channel)
% If your channel is private, then enter the read API Key between the '' below:
readAPIKey = 'XXXXX';
% Read the last day data from ThingSpeak
cat1weightData = thingSpeakRead(channelID,'NumDays',1,'Fields',1,'ReadKey',readAPIKey);
cat2weightData = thingSpeakRead(channelID,'NumDays',1,'Fields',2,'ReadKey',readAPIKey);
cat1usesData = thingSpeakRead(channelID,'NumDays',1,'Fields',5,'ReadKey',readAPIKey);
cat2usesData = thingSpeakRead(channelID,'NumDays',1,'Fields',6,'ReadKey',readAPIKey);
sandData = thingSpeakRead(channelID,'NumDays',1,'Fields',4,'ReadKey',readAPIKey);
% Calculate average daily values
avgcat1 = (max(cat1weightData) + min(cat1weightData))/2;
avgcat2 = (max(cat2weightData) + min(cat2weightData))/2;
avgsand = (max(sandData) + min(sandData))/2;
% Calculate number of uses of the box
usescat1 = max(cat1usesData);
usescat2 = max(cat2usesData);
% Create message
message = "<b>Daily report:</b><br> <b>- Cat #1:</b><br>- Average weight: " + ....
num2str(avgcat1,2) + " kg<br>" + "- Used the litter box " + num2str(usescat1) + ...
" times today<br><br>" + "<b>- Cat #2:</b><br>- Average weight: " + ...
num2str(avgcat1,2) + " kg<br>" + " - Used the litter box " + num2str(usescat2) + ...
" times today<br>" + "<br><b>- Sandbox status:</b><br>- Sand weight: " + ...
num2str(avgsand,2) + " kg<br>" + "- Total uses: " + num2str(usescat1 + usescat2);
% Send message to IFTTT
Notice you'll have to update it with your Thingspeak channel and read key and IFTTT URL key.
11) You should now create a TimeControl that will serve as a trigger to execute the above code. Click Application> Timecontrol> New
12) Choose how often the code should be executed and the name of the analysis and save the new Timecontrol.
13) Ready! The Code should be executed daily at the established time and send a daily report to your email!
Step 12: Bonus Round #2 - Warnings! (coming Soon!)
Step 13: Final Considerations
I put a lot of effort into this project, to make it as clear and educational as possible. If you liked it, please don't forget to 'like' and 'share' it. Your support is very important to keep my job! :D
If you still don't follow my tutorials, take a look at those other projects in which I explain a little bit about internet of things, robotics and 3D printing. I hope you enjoy them too!