Introduction: Easy Husbandry

About: Student MCT at Howest (Belgium)

I am a first year student at Howest MCT, as first-years we had to make a project which combines all the modules from that year. we had the freedom to choose our own project as long as we had enough sensors and actuators, working front and backend normalised database and a mobile first site design.

I chose to combine that with one of my hobby’s, and made a mini smart terrarium called Easy Husbandry.

It is basically a terrarium add on, u can always remove it and still have a normal terrarium. u could also move it between setups if u reconnect the relay.

Its features are:

  • It reads the temperature and blows a breeze if it gets too hot.
  • It reads the natural light from the outside of the terra and in calculation with the time at that moment decides to light up the lamp or not.
  • It reads the humidity and warns the user when it gets to dry or to humid.
  • It has an LCD on the outside of the device that shows the above values.
  • And it is fully configurable and controllable from your phone.

My Github where u can find all the code: GitHub

Supplies

The main component.

  • Raspberry PI- with a 16gb+ sd card

Sensors:

  • DH11 - for humidity (u could also use this for temperature)
  • DS18B20 - for temperature (I use this over the DH11 because it gives u the option to lay the sensor on the bottom and read the temperature closest and most relevant to the animal)
  • LDR light sensor with 10k resistor - for brightness

Actuators:

  • 12 volt computer fan - works well with 9volt battery
  • Normal lamp - (or in my case 2 lamps)
  • 16x8 Lcd - displays sensor values

Extra actuator:

  • Red led with 300 ohm resistor - burns when device is on

More electronics:

  • MCP3008 - change the analog value from the ldr to digital for the pi
  • 5 volt relay - to control the lamps with the pi
  • TIP120 transistor
  • Diode
  • 9volt battery - too power the fan
  • potentiometer - for contrast lcd

Case:

  • whatever fits, I used an old waterfilter and with some tinkering made it work

Tools:

  • soldering iron with solder and heat shrinks
  • drill and screws
  • strong double-sided tape

Finally:

  • lots of pin-cables and a few resistors

List of materials used:

Step 1: Raspberry Pi

The raspberry pi is the base of my project, it reads the sensors runs the actuators, frontend, backend, database and hosts the website.

Image:

install image on sd

- First off, we need something to boot the pi off. U can download the Raspbian image here.

- Now we need to write this on a formatted sd card, I suggest using Win32 disk manager.

setting up sd

- To connect ur computer and pi need to be in the same subnet.

- Go to the boot file of your sd using your computer.

- Open the file cmdline.txt with notepad++

- Add in 'ip=192.168.168.168' after a space.

- Create a file called ssh without extension in the same directory u found the last file.

- Safely insert sd into pi.

connecting to pi

- connect your pi to your computer with a ethernet cable.

- toggle your computer ip to 192.168.0.111 guide windows (same subnet as pi)

- open commandline with cmd and type in ssh pi@192.168.168.168

- type 'yes' if u get a warning and login with the password 'raspberry'

- U are in!! but u should probably change the default username and password because, u know security.

configuring pi

- u can configure your pi as u desire using raspiconfig, more info here.

<a href="https://www.raspberrypi.org/documentation/configuration/raspi-config.md"></a>sudo raspi-config

- when in the config:

- enable SPI and onewire as we need them for our sensors later.

- disable Splash screen and Choose cli for startup instead of desktop

Setting up wifi:

- this will make the application wireless when needed but u can always fallback on its static ip.

- change SSID to your network name and PASSWORD to well its obvious.

sudo wpa_passphrase "SSID" "PASSWORD" >> /etc/wpa_supplicant/wpa_supplicant.conf<br>

- now type the following commands in the respective order.

sudo wpa_cli
interface wlan0
reconfigure 
ip a

- now u should see a ip under wlan0

- u can ssh to that ip when wireless

installing everything:

-type in following commands to install needed software

sudo apt install apache2 -y
sudo apt install mariadb-server mariadb-client -y
mysql_secure_installation

- press enter on first prompt

- set pass for mysql on second prompt

- press y for everything else and it should be secure.

- now we will create a mysql user, be sure to change username and password to your own in the second command

mysql -u root -p 
 grant all on *.* to 'username'@'localhost'
     identified by 'password'; grant grant option on *.* to 'username'@'password'; 
 flush privileges  
 exit

code-editor:

- I used visual studio code as my editor, so i will show u how to set it up.

- open vscode on your computer and install Remote-ssh under extensions

- press F1 and type ssh

- choose Remote-SSH: Add New SSH Host

- type ssh username@192.168.168.168 -A

- choose the option to save files

- press f1

- choose Remote-SSH: Connect To Host

- choose the ip

- now u can code with vscode on your pi.

python on the pi:

- use following commands to configure python on your pi

- u can type these in the terminal from vscode when connected

pip3 install flask-cors
pip3 install flask-socketio
pip3 install mysql-connector-python
pip3 install gevent
pip3 install gevent-websocket

- reboot

sudo reboot

connecting to pi-database with mysql:

- in mysql workbench

- make new connection and choose:

  • Connection Method: Standard TCP/IP over SSHSSH
  • Hostname: ip-pi
  • MySQL Hostname: 127.0.0.1
  • MySQL server Port: 3306
  • Username: mysql

Unless i forget something crucial this should be it :).

Step 2: Prototype

The next step is to make an electrical scheme in which u thoroughly go through each part and lay down the right way to wire the project. This is used as a blueprint when u actually wire the device and as a quick check-up in the event something comes lose, this small process could save your raspberri-pi.

U can find my electric scheme below and also a more visual breadboard scheme.

Make sure u connect the fan and lamp to a battery of 9-12 volt and wall plug, respectively. Both currents 5v and 3.3v from the raspberry-pi are used. Make sure to check the schematics on how to connect these so only the grounds overlap.

Step 3: Database

Before
we start to program the device, we need a place to store and read all the data we are going to use. Above u see a EER diagram of the database I made.

The database is not that complex, but it does what is needed and only that.

The dump under Database-Export here adds every sensor and actuator in its respective tables. To use this simply copy and paste this into MySQL workbench and run it. U can then check the sensor/actuator IDs via MySQL as these will be important when programming the reads and writes to the database. The table settings are used to commit new settings via the web-interface and act on these.

I made my database 3.5nf to avoid redundancy.

Step 4: Backend

Now that everything is setup we can start the fun process. We need to make sure the basic of our device works as in reading sensors and controlling actuators, before we worry about design and frontend.

Lucky for you i already wrote the code for this project, so instead of reinventing the wheel u can use the code from my GitHub here.

If u like to write your own code here are some tips i found usefull, read the datasheets for each component when not using libraries and use classes for each device so u can call upon them in a much more readable and compact main file.

Step 5: Frontend

U got all your sensors working: the lcd displays data, the fan blows and the lamp shines. Now we start the frontend process. This will require some backend too.

This involves JavaScript to handle events and communication with the backend and database, HTML for the general layout and CSS for the design. Again, u can simply use my code, the design is responsive and made mobile first which is recommended.

U could just use the JavaScript part and make a whole new, maybe even better design, that is completely up to you.

I made 3 different html page which all use a different JavaScript file. I used a POST via the API of my site to store the settings in the database with every submit and used Socket.io. to show the sensor data.

Step 6: Case

If you are making this project this part is probably going to be entirely different from mine.

I used an old water filter large enough to fit 2 breadboards and the pi. In your case unless u have the exact same filter laying around unused, you will need to improvise.

I am going to share how I made it, the pictures above were made during this process.

First I removed the filter contents and cleaned it. Then I drilled a hole in the backside for the display and light sensor and the bottom for the fan. Tip: Make sure the lamp has the least amount of effect on the LDR as possible (put the ldr on the backside of the device). I then made a holder for the fan with some metal parts I had laying around, and screwed it to the bottom, this was also a good place to attach the humidity sensor, so I did this with a strong kind of double-sided tape.

After placing in all the sensors and the fan I made some small roll cages for each breadboard, so they don’t tumble inside the case. Attached the battery with a zip tie to this roll cage as seen in the pictures.

After sliding everything in I decided a red led would be perfect to really make it complete. I drilled a hole in top and connected a led so it would always be on the moment the device received power.

Now everything was in place except the raspberry-pi itself. For my own comfort and to be able to program the pi without having to power the complete device, I taped the pi on the lid so I can always just power the raspberry pi with the T-part disconnected.