Introduction: Automatic Water Dispenser for Pets (Raspberry Pi)
Hi, in this instructable for school I'm going to show you how to create your own water dispenser for pets.
Please keep in mind that this is my very first instructable so it might not be perfect.
Step 1: Tools and Materials
- 1x Raspberry Pi 3
- 1x 8GB SD card
- 1x Ethernet cable
- 1x Breadboard
- 2x HC-SR04 Ultra sonic sensor
- 1x 3.5 ~ 12V DC Water pump
- Electronic wires
- 3x 1K resistor
- 2x 2K resistor
- 1x 4.7K resistor
- 1x Potentiometer 10K
- 1x BC517 Darlington transistor
- 1x 1N4007 Diode
- 1x 16x2 LCD
- 1x Tubing for the water pump (if yours doesn't come with tubing)
- 1x DS18B20 Temperature Sensor
- 1x 9V Battery
- 1x Drink bowl for pet
- Some kind of reservoir for your water, I used a bucket
- 250x30 cm wood for the case (doesn't have to be wood, personal preference)
- Nails or screws (preference)
- Jigsaw (no, not the guy from Saw)
- Screwdriver or hammer (if you want to use screws or nails)
- Bodkin (optional)
Step 2: Electric Circuit
I can't really help you re-creating the circuit but here are some tips for building it.
VERY IMPORTANT: Make sure all wires going to the pi are connected with the right GPIO pin, otherwise your program won't work.
Step 3: Building the Case
First of all, your case doesn't have to be exactly like mine, you can build whatever you want but you want to make sure this rule applies to your build:
The ultra sonic sensors have to be on top of your reservoir and water bowl facing the water.
As you can see for my reservoir cover, there are 2 holes in it that I cut with a hot knife so I could get the water loop out of it and the ultra sonic sensor in it.
That's it, other than this rule you can really just build what you prefer.
(Photo nr.1) After I cut the cover for my reservoir I drilled a big hole in the front plate (around the middle) so I could later on pass the water loop and drilled a small one for the cables of the hc-sr04 in front, then I started mounting the walls on the base plate
(Photo nr.2) Then I drilled a hole in my middle plate where the electronics are so I could let the wires pass through the hole and mounted it in my case.
(Photo nr.3) In my roof plate I cut a hole with a jigsaw so I could mount the LCD on it and mounted it on top of the rest.
(Photo nr.4) Last thing that had to be done was making sure the front sensor was in a good position so I created a "roof" for my sensor so it could be right on top of the water bowl.
Step 4: Setting Up Your Pi
- Raspbian lite (OS for your pi)
- Win32 disk imager (to install the OS on the SD card)
- Putty (to access to the pi)
- Pycharm (for the code)
Configuring the OS:
When you open Win32 Disk Imager you need to select your img file you have just downloaded.
Then under "Device" you select the drive of the SD card.
To continue click "Write", the software will now put the OS on your SD card.
Tip: The "Write" button can later on be used to create a backup.
Now we need to navigate to our SD card, in my case the "E:/" drive.
Open the cmdline.txt with your text editor of choice and add "ip=169.254.10.1" to your file (without quotes) so it looks like the photo above.
To get a safe conncetion with our pi we need to enable SSH, we can do this by putting an empty file(without extension) named "ssh" on our SD card.
Safely remove the sd card and put in the pi and connect the ethernet cable between your pc and pi.
Now you can plug in the power cord for the pi and start Putty. Put "169.254.10.1" in the Host name form in putty, choose SSH as connection type and press "open".
We can now start working on our pi in the terminal that just opened. The default username is "pi" and the default password is "raspberry".
Here you can find a list of common linux commands to help you navigate through the OS.
Go to /etc/dhcpcd.conf and check if these are the same as the screenshot above.
Next we want to make sure our pi is up to date, we can install updates with this command:
sudo apt-get update && sudo apt-get upgrade
If we want to utilise the temperature sensor we'll have to enable 1-wire protocol on our pi.
We can do this by using this command:
Next go to "5 Interfacing options > P7 1-Wire > Yes"
Reboot your pi with this command after you enabled 1-wire:
Step 5: Installing MySQL
We need MySQL to store data from our water dispenser so we can later on use it on our website. Use this command to install it on your pi:
sudo apt-get install mysql-server && sudo apt-get install mysql-client
Now that MySQL is installed we can access the MySQL console by using this command: mysql -uroot -p
We'll create 2 MySQL users, 1 for localhost and 1 for remote connections:
CREATE USER 'namehere'@'localhost' IDENTIFIED BY 'passwordhere'; GRANT ALL PRIVILEGES ON *.* TO 'namehere'@'localhost' WITH GRANT OPTION; CREATE USER 'namehere'@'%' IDENTIFIED BY 'passwordhere'; GRANT ALL PRIVILEGES ON *.* TO 'namehere'@'%' WITH GRANT OPTION;
Tip: Replace namehere and passwordhere with your own username/password but keep the quotes around them.
Creating a database:
CREATE DATABASE WateroMatic;
Congrats, the hardest work on the pi is now over.
Step 6: Pycharm - Part 1
Download the code we'll use in pycharm here
As you can see there are 2 folders on my github page: app and website.
"App" is the code we'll first use to make the water flow and "website" is the code we'll use second to run the website.
Open up Pycharm and select "open project" and select the app folder.
In Pycharm go to (File > Settings > Project: app > Project interpreter) and you'll see a window similar to the screenshot above.
First we need to select our Python interpreter that's located on our pi.
Click the settings icon (upper right) and select "Add remote" and fill in the fields like the screenshot.
We now have a remote Python3 interpreter to work with. This also means we can start adding packages to work with. Install the following packages with the green add button on the right:
Next we'll also want to upload our code to our pi using (Tools > Deployment > Configuration)
Add a new configuration, choose SFTP as type and fill in the fields like the screenshot above.
Don't forget the 2nd tab "Mappings" -> see screenshot too.
Now we'll have to adjust the code a bit to make it work with your own project.
Open DbClass.py and change "thibault" to your username (username from MySQL), and do this for passwd too.
Now comes the tricky part: getting the right distances for your ultrasonic sensors.
Open up Distances.py and run the program to measure the distances (metric) for an empty and full reservoir + a nearly empty bowl and fill in these distances in RUN.py where:
- "meting_vol" is reservoir full
- "meting_leeg" is reservoir empty
- "volume_vol" is your reservoir's maximum capacity in liter
- down the bottom you have
if lees_waterbak() >= 26.9:
change "26.9" with the distance for your nearly empty water bowl.
The water volume needed is determined by the pumping time in seconds, so my bowl needs 8 seconds to get filled from a nearly empty bowl as you can see in this line:
waterpomp_aan() time.sleep(8) waterpomp_uit()
Now that our code is good we can create our database structure by opening the database window:
(View > Tool windows > Database)
In the database window click the "add" button and select MySQL, then just fill in the General and SSH/SSL tab as shown in the screenshot.
Click on our added database and click the open console icon and copy this code in the MySQL console and execute it:
CREATE TABLE Data( ID INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, Datum DATETIME, Temperatuur FLOAT, Reservoir_liter DOUBLE, Verbruik_liter DOUBLE ); CREATE TABLE Gebruikers ( ID INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, user VARCHAR(50), password VARCHAR(50) NOT NULL );
The only thing we have to do now is to provide a login and password for our website. We can do this by right clicking on "Gebruikers" and selecting "Table Editor". Now add 1 in the ID column, a username in the user column and a pass in the password column.
You can now run RUN.py but make sure you have your water bowl in place.
If everything is good now you can upload the code to your pi by right clicking the app folder and click upload to 169.254.10.1
Step 7: Pycharm - Part 2
Our app and database is now ready and now we can start looking at our website.
Open "website" in pycharm and open DbClass and fill in your credentials, just like we did with the app.
At the bottom of the website.py you can see a line
This means that the website will be hosted at that ip address, this needs to be the address of your pi. So if you have configured your pi to connect with your Wifi you'll have to replace it with the appropriate IP address.
Now we're gonna let our code autostart when our pi boots so it can run on its own. There are multiple ways to do this but I'm choosing for the rc.local method
Open /etc/rc.local with your favorite text editor and put this code above the "exit 0" line
sleep 15 python3 /home/pi/files/app/RUN.py & python3 /home/pi/files/website/website.py &
Finally when we want to see our website, you can browse to the IP address that was specified at the bottom of the website.py file (default is 169.254.10.1:5000).
This is the end of my first instructable, if something's not clear, you can comment or message me.