Introduction: MiControl - Aquarium Monitoring
I study NMCT at Howest (Kortrijk Belgium). As part of the finals, we had to make a project. This project had to meet certain requirements:
- Use flask (a python microframework)
- Save data in a MySQL database
- Show data from the database on a website
- Read values from sensors
- Create a decent electrical circuit
- Build a case of some sort (to mount the hardware)
- Everything has to be controlled by a Raspberry Pi
I chose to create a Raspberry Pi controlled aquarium monitoring system. Using the principle of computer water cooling and a peltier element. I can change the temperature of the water according to the set temperature. Bear in mind, that you can use different peltier elements to achieve different temperature changes. The one i'm using is rated for 60W, so it's best to use this one with aquariums of 20 liters or less.
Step 1: Components
- 1 x Raspberry Pi 3
- 1 x T-cobbler (standard in RPI starter sets)
- 1 x Aluminum water cooling block
- 4 x DS18B20 waterproof temperature sensors
- 1 x IRF530N mosfet
- 1 x Peltier element TEC-12706
- 1 x 12V 30A 360W switching power supply
- 1 x 12V DC water pump
- 2 x G1/4" fittings
- 1 x Tubing (the ones used in PC watercooling)
- 1 x Cooler Master Hyper 212 Evo
- 1 x H-Bridge 10A
- 2 x Mosfet heatsinks
- 1 x Heat insulation foam
- 1 x Breadboard
- 1 x Adhesive thermal conductive tape
- 2 x L293D driver IC's
- 1 x Diode
- 2 x stripboards
- 2 x 120mm PC case fans
- 1 x Nylon PCB standoff set
- 1 x IEC320 C14 power plug
- 1 x MDF board (to create a case)
- Set of M3 bolts
- Long and short screws
- 4 x wheels
- 2 x locks for the doors
I really suggest to solder everything onto a stripboard. For both safety and practicality later down the line. For the cooler, you can choose another model too. Just be sure you'll be able to mount it easily. You theoretically only need 1 heatsink for the mosfets, because you'll only be using 1 channel. If you wanna reuse it, or add another peltier element, you should put heatsinks on both sides. The heat insulation foam, is not a must. It's only there so there will be less temperature losses on the water block.
In my final version, you'll see a small PSU board that i made. You don't really need this to create the project, but I mounted my PCB's already and I only realized later that I didn't put pins on it for the fans.
Step 2: Electrical Circuit and Schematic
- Basic explanation:
This schematic, is the bare minimum that you need to make it work. The H-bridge is not included on the schematic, because Fritzing doesn't have that component in it's libraries. The 3 outputs of the RPI that you need, are all connected to the inputs of the L293D. This way, you can easily supply higher currents than the RPI can deliver.
Later down the line when i'll show the code (or you download the code), you'll see that i used GPIO pins 17, 27, 22 and 4.
- Pin 17: This pin will control the direction of the current that flows through the peltier element; heating or cooling.
- Pin 27: This pin will output a PWM signal. Due to limitations of the H-bridge, the maximum accepted duty cycle is 99%. When the signal is set to 99%, the element will heat or cool. If it's set to 0%, it enters a 'brake' mode, which means that the element will not heat or cool.
- Pin 22: This pin will control the pump.
- Pin 4: This pin is the One-Wire bus. So all the sensors communicate over this pin.
I used DS18B20 One-Wire temperature sensors. Be sure to buy the ones that are waterproof.
Wiring the sensors is really easy:
- VDD -> 3.3V of the RPI.
- DQ -> pin 4 of the RPI.
- GND -> ground of the RPI.
- Between the VDD and DQ you put a 4.7kΩ resistor.
For the mosfet, you can choose whatever mosfet you like. I chose the IRF530N, because I had some lying around here.
Wiring the mosfet:
- A 1kΩ resistor between the input of the RPI and the Gate from the mosfet.
- The Drain should be connected to the GND from the pump.
- A diode should be placed over the pump, as shown in the schematic.
- The Source of the mosfet should be connected to the GND.
You can choose whatever water pump you'd like. Just be sure it's one that is placed outside of the aquarium. And not one that has to be submerged. I chose the SC600, and bought it from Aliexpress.
Wiring the pump:
- Red wire -> 12V of the external PSU.
- Black wire -> Drain of the mosfet.
I chose this driver, because I had some lying around here and I really like them. Simply because they're very easy to drive components that need a higher supply voltage or a higher current than the output pins can deliver. I have included a picture of the pin layout of this driver.
In my final version i have used 2 of these drivers. The project can be made with 1 too, but to drive the mosfet it's best to have around 10V on the gate, so i took another driver and connected it's supply voltage to the 12V of the PSU. The pin connections are exactly the same as the first driver, the only different thing is that the +Vmotor (Vcc2) pin is connected to 12V instead of 5V.
- Connect 5V to the 'Enable 1','+V(Vcc1)' and the 'Enable 2'.
- Now for the 'Vcc2', you have to connect it to the 5V, but if you're using a second driver, the 'Vcc2' of the second driver should be connected to the 12V.
- All the 4 GND pins, should be connected together. Be sure to connect the GND of the RPI to the GND of the PSU!
- Every input pin, should have a 3.3kΩ resistor to the GND. So that when you don't connect it to an input pin, the driver will take it as a logical low.
- The outputs of the driver, should be connected to the inputs of the H-bridge. If you're using 2 drivers, the output on that one should go to the Gate of the mosfet.
- The inputs have to be connected to the GPIO pins. In my soldered version, pin 17 and 27 are connected to input 1 and 2 respectively. Pin 22 is connected to an input on the second driver.
For the PSU, you'll need two more things. An old AC power cord that you can cut in half, and a normal PSU power cord (the ones used for a desktop). The power plug that was in my components list should best be soldered before using it, because the net voltage doesn't feel that good when you touch the pins...
I have included pictures of the PSU and the power plug, with a description of each connection.
I chose this H-bridge, because it's safer this way. I tried making my own H-bridge, but I'm not that experienced with them. Since I'm working with currents that are quite a bit higher than the 20mA from the outputs, I chose for the safest option. In the pictures i have included the pin layout that come directly from the page of the supplier.
Low voltage side:
- Output of the driver IC, which has pin 17 as input, has to be connected to the DIR1 pin.
- Output of the driver IC, which has pin 27 as input, has to be connected to the PWM1 pin.
- GND should be connected to the ground of the RPI.
- +5V should be connected to the 5V of the RPI.
High voltage side:
- GND should be connected to the GND of the PSU.
- POWER should be connected to the 12V of the PSU.
- MOTOR1 (left) should be connected to the red wire of the peltier element.
- MOTOR1 (right) should be connected to the black wire of the peltier element.
You can change the black and red wire, but bear in mind that the code will have to be changed then as well.
Step 3: Materials for the Case
I really enjoy to create things with my own 2 hands. Yes, i could save a lot of time by just 3D printing parts, or by laser cutting the entire case. But then there wouldn't be any room for improvisations during the build. Which is basically my entire build...
When i started creating it, i just knew that i wanted it to be a box, with 2 compartments. The bottom should house the water block, peltier element, heatsink and pump. While the upper part, should house the PSU and other electrical components. So that when a leak occurs, there won't be any water leaking in the PSU and cause a short circuit.
So just as i improvised a lot while creating it, you can do the same. If you want it to be smaller, then you can do that just by moving certain component's or mount them to the side panels... It's all up to you!
I'll go over the basics of what i did. I did not take a lot of pictures of this process, but i'll try to explain it as good as possible.
One thing you'll need for your own ease, is a powerful drill and hole saws. I included a picture of what a hole saw looks like.
- Hinges for the front and back door.
- 2 locks for the doors.
- Long and short screws. In the picture you can see which screws I used.
- MDF boards. Cut on the right dimensions or the dimensions of your choice.
- Wheels for under the case
Before i go over the dimensions, i wanna make clear that any dimensions i mention, are in mm!!! So even when i don't put a unit after it, it's in mm!!!
Dimensions of the MDF boards:
- The thickness of each board is 15.
- The base and top boards are both: 250 x 400.
- The side panels are both: 400 x 450.
- The front and back doors are both: 215 x 415. When I cut them I forgot to take a little bit off from both sides. You'll see that the doors aren't really straight, because I had to manually shorten it using sandpaper.
- The middle board, where your electronics will be mounted on, is: 220 x 370.
In the next steps i'll be going over the process of making the case.
Step 4: Making the Case 1/13
So now i'll be explaining how to make a basic case for this project.
- Take the base plate.
- Draw a line on each side, with a width of 15 (this is the thickness of the plates)
- Now, you need to draw a line on each of the long sides. That are both 7.5 from the sides. (basically in the middle)
- After this is done, you'll have to draw small lines on the middle line you just drew. What you'll have to do, is measure the distance between each of the screws you want to put in, i made a little mistake of not making them symmetrical. Be sure to make both sides symmetrical! (i made a little mistake here in my picture. i also drilled holes on the side of the back panel. Because originally, i was going to only have 1 door)
- When the lines are drawn, you'll want to take something with a sharp point. So that you can make a small hole on the location you want to drill a hole.
- Now you need to take a drill and drill a holes on all the marked locations. (a wood drill of 2.5 or 3 is recommended)
- when all the small holes are drilled, you'll want to take a bigger drill (best one of 6) and drill a bigger hole on top of the small ones. This step is to make sure, that when you put screws in, that they're hidden in the wood.
- Now repeat this process for the top panel.
Step 5: Making the Case 2/13
- Now that you have the base and top plate. You need to adopt the locations of the holes of the base plate, on the sides of the side panels.
- Now that you got these locations, you'll need to drill a hole in the width of the side panels (the middle is 7.5 form the side), so that you can screw the base/top panel onto the side panel. (this will only be done later down the line)
- Now do the same for the other side of the panel.
- And now repeat these steps for the other side panel.
Step 6: Making the Case 3/13
- take one of the side panels, and look at which side you'll want the tubing to go through.
- Now you'll need to use a hole saw, to drill a hole of 30 on the position where you want the tubing.
- Repeat this for the other hole for the outlet tube.
- Now drill a hole of 30, more to the top of the side panel. That's where your temperature sensors will go through. just make sure it's above half of the side panel.
- Now take the other side panel, and drill 2 holes of 30 on the location you want to put the power plug. (2 holes of 30 that overlap each other. You can use a vile to make it rectangular.
- Right next to the power plug, you have to drill a hole of 12. To put the cable of the RPI through.
Step 7: Making the Case 4/13
- Now take the base plate again.
- On the location you want the water block, you draw a square of 40 x 40 (dimensions of the water block).
- Now that you have the location of the block, you'll need to grab the mounting bracket of the CPU cooler.
- Place it, so that the middle of the bracket is in the middle of the block. Then use a pencil to draw the locations of the holes.
- Now take some leftover wood, and saw it so that it's 20 over each side of the location of the bracket screws. Take over the locations of the screws from the base plate. and drill a hole of 6 through the wooden block. (don't drill into the base plate!!!)
- In the middle of the block, 10 from each hole you just drilled. Drill another hole of 3, but now it should be a little bit into the base plate.
- Now screw the blocks in place.
- Repeat these steps for the wooden block on the other side of the bracket.
- Now you can put nylon standoffs in the large holes. You will have to use a bit of force to get them in though. (the height will depend on the cooler you use)
Step 8: Making the Case 5/13
- Now grab the cooling block,2 pads of thermal conductive tape, the heat insulation foam (HIF) and a wooden block of 40 x 40. (if you bought heat insulation foam specifically for peltier elements, you'll need to cut it in pieces to fit the water block)
- Now put the HIF onto the water block. Normally it has one sticky side to it. So put that side against the block.
- when all the HIF is attached, put a pad of thermal conductive tape on the bottom of the water block(on top of the HIF).and also a pad on the bottom of the wooden block. (see the pictures for clarification)
- Now you can put the block on the location you drew on the base plate.
Step 9: Making the Case 6/13
- Now you can mount the water pump to the base plate.
- It's best to align the input of the pump with the input hole you drilled in the side panel.
- Position the pump where you want it. Then draw the location of it on the base plate.
- With a drill of 3, drill the holes through the base plate.
- Use bolts (M3 x 25) to attach the pump to the base plate.
Note: I don't have a picture off the pump before i mounted the whole case. sorry for that!
Step 10: Making the Case 7/13
- If you want to mount wheels on the bottom of the case, now is the time.
- Position them on top of the base plate (15 from each side, otherwise the screws will be under the side panels and you'll be screwed if you ever want to loosen them)
- Now drill a hole of 3 through the base plate. (same as the pump actually) But now you'll need to drill a larger hole of 6 again. so that the bolts will be in the wood. (be sure not to drill through completely with the drill of 6!)
- Repeat these steps for the remaining 3 wheels.
Step 11: Making the Case 8/13
- Now that the base plate is ready, we can mount angle brackets to the side panels.
- To do this, measure how high you want your middle plate to be. (be sure that the heatsink can still fit under the middle plate!!!)
- Draw the hole locations of the brackets on the side panel.
- Now drill a hole of 2.5 on those locations. (don't drill too deep)
- Use the short screws to mount the brackets to the side panels now.
Step 12: Making the Case 9/13
- Now you can mount the side panels to the base plate and top plate.
- To do this, use the long screws now. And screw them in the holes you drilled in the beginning. Don't rush it, or the wood will start to tear a little bit. (i personally experienced that)
Step 13: Making the Case 10/13
- Now you take the middle plate, and position every component like you want. I'll include a picture of my layout as a reference.
- When they're positioned, draw the locations of them on the plate.
- Now drill a hole of 3 through the plate. And on the bottom of the plate, drill with 6 so that the bolts will be hidden in the wood. (don't drill through with 6!!! )
- Before you screw everything onto the plate, drill a hole of 30 in the middle of the plate to route cables through. Also between the standoffs from the H-bridge. So that the cables for the peltier element can go through.
- When all the holes are drilled, you can use bolts to screw everything in place. It's best to use the Nylon standoffs on every PCB you use. This is the easiest way of mounting your hardware to the plate.
Step 14: Making the Case 11/13
- Now take both of the doors.
- And draw where you want the holes for the fans to come. It's best to do it in the middle of the compartment. Let's say the bottom compartment has a height of 200, it's best to drill a hole of 100 or 120 (depending on what you have available) on a height of 100 and a half of the width of the door. (again, i made a little mistake and it was more to the left. But it still works)
- After this, put the fans in the middle of the cutout. And draw the location of the mounting holes on the door.
- Drill a hole of 6 into the mounting locations (be sure to only drill 10 deep!!!)
- When all these are drilled, insert nylon standoffs into them. And use M3 bolts to mount the fan to the door.
Note: I only mounted fans on the front door. You can add fans to the back door as well. I'm planning on mounting another fan on the upper compartment, for better cooling of the heatsinks.
Step 15: Making the Case 12/13
- Now take the hinges. (i used 3 hinges for each door)
- Draw the holes on the wood where you want the hinges to come.
- Now drill the holes with 3 and 2.5. The drill of 3 is used in the side panel, because there you'll need the long screws. And in the door, you'll need the drill of 2.5 and the short screws. (Don't drill through the door!!!)
- After the hinges are in place and screwed on, take the lock and position it where you want the lock to come.
- Drill holes using a 2.5 drill. You can use the short screws for the lock.
Step 16: Making the Case 13/13
- Before you put in the middle plate, mount the cooler and the peltier element on the water block. (apply thermal paste between the peltier element and the water block, also between the cooler and the peltier element!!!)
- Now that everything is in place, put in the middle plate on top of the brackets.
- When it's in place, you can start wiring everything together according to the schematics.
- You can mount the power plug in the side panel now. (use the short screws, and if one hole is too close to the edge, use some super glue for that one)
Note: I just finished putting in the fan in the back, and a lock on the back door.
Step 17: Raspberry Pi 3 Setup
To be able to use a Raspberry Pi 3, you'll need to have a micro SD card. Firstly, you'll have to install an OS on it. To do this, simply insert the SD card into your computer and put the OS on it. To put the OS on the SD card, i recommend using ' Win32DiskImager'. The OS itself is called 'Raspbian-jessie-with-pixel'.
When all this is done, you have to open the windows explorer. Then go to the SD card, and search for the file 'cmdline.txt'. When you have opened this file, add the following code to the file 'ip=169.254.10.1'. I included a picture of what the file should look like. When this is done, you can remove the SD card from your computer and insert it into the RPI. Using a LAN-cable, you can connect to the RPI through putty. Simply connect to the address that you put in the cmdline.txt file.
To log into the RPI, you'll need to use:
Step 18: Install MySQL on the Raspberry Pi
To install MySQL on your Raspberry Pi, simply follow these steps:
- sudo apt-get update && sudo apt-get upgrade
- sudo apt-get install mysql-server --fix-missing
- sudo apt-get install mysql-client
- mysql -uroot -hlocalhost -p
Now you'll be asked to enter the password that you set during the install of mysql. after you've entered the password, you'll see a mysql> on the left. this means that you're good to go.
Now in the >mysql enter:
- CREATE DATABASE database_input;
- CREATE USER 'MiControl'@'localhost' IDENTIFIED BY 'password'; replace password with a password of your choice!
- GRANT ALL PRIVILEGES ON database_input.* TO 'MiControl'@'localhost';
- FLUSH PRIVILEGES;
Now try to log in using the newly created user:
- exit mysql by using CTRL+C
- In the normal RPI cli, enter mysql -uMiControl -hlocalhost database_input-p
- Now you'll be asked for the password. Then it's all good to go.
Step 19: Import the Database File
I have included my own database file, so you don't have to create your own tables.
But firstly you'll need to put this file on your RPI. To do this, you'll need to install FilleZilla.
- After installation, open FileZilla.
- In the top left corner, go to File and then to Site Manager.
- Now click on New Site.
- Enter the IP address of your RPI, if it's still set to 169.254.10.1 enter that one in the host field (be sure you're connected with the LAN-cable)
- Set the protocol to SFTP.
- Set logon Type to ask for password.
- Enter pi as user and click on connect.
- You'll be asked for a password, this should still be raspberry.
- In the bottom right corner, you'll see your folders on your RPI.
- Now navigate to /home/pi/Desktop
- When you're there, you place the database file there.
You can close FileZilla now. And go back to your putty session.
- In the cli, enter MySQL -u MiControl -p -h localhost database_input < /home/pi/Desktop/database_input.sql
- Now the database is ready to go.
Step 20: Install Flask and Get the Code
Before we dive into the code, we'll need to install flask on the RPI.
To do this, simply enter ' sudo apt-get install python3-flask ' in the cli from the RPI.
Now for the code, download the code from GitHub (i'll also include a .zip file with the code, should the Github link be gone for some reason, so bear in mind that this might not be the most up to date version of the code). There will be 2 folders, you have to download both. Once you've downloaded them, you have to open the the DbClass.py file(in both folders). And change the user credentials to match your credentials that you've entered in the MySQL setup. But if you don't want to do this. The default user and password is set to 'root'. This is not a very safe method, but it's the easiest one.
As the last step, we'll have to put the two folders on the RPI. To do this open FileZilla again.
- Navigate to /home/pi
- now create a new directory called Project WITH A CAPITAL P(this is necessary, if you don't want to change things in the python code)
- In this folder, you have to place both folders that you downloaded from GitHub.
Note: The reason you have to do it like this, is because in my Flask.py, i say that he has to execute /home/pi/Project/Main_program.py . So if you want to use another location, you'll have to change the python code.
- Sometimes, you'll see extreme high or extreme low temperature spikes. These should be ignored. Because of limitations in python, i have to run timers in threads. sometimes these overlap. which causes a conflict in the database. But once this happens, the next reading should be correct again.
Step 21: The Web Interface
Now for the web interface, it's not so hard to use, there are 3 different pages on the website.
On the home page, you can see 2 small graphs:
- Average temperature of all the sensors
- The status of the peltier element
These can both be viewed on their respective pages.
On the temperature page, you'll see:
- The big version of the average temperature graph
- A text box and a set button (simply enter the desired temperature in the text box and press the button)
- 4 small graphs, which all represent 1 of the 4 temperature sensors.
On the Heating/Cooling page, you'll see:
- the big version of the heating/cooling graph
- a slider button to set the control to manual or to automatic.
- When the control is set to manual, you'll see two extra sliders.
- A slider to control the power status of the element (on or off)
- A slider to control the heating/cooling status of the element (heating or cooling)
Note: Due to my inexperience in web development, you have to press the confirm button if you want the changes to take effect. So if you want to set it to manual, you have to set the slider to manual and press the confirm button.
Step 22: Suggestions
If you're somewhat experienced in programming, web development or databases, and you have any suggestions to change the code, interface or database design. By all means, let me know and i'll see what i can do about it.
Thank you for taking the time to read my guide, and i hope i was able to help you!
We have a be nice policy.
Please be positive and constructive.