IoT-Terrarium

by thebigpotatoe in Circuits > Sensors

9370 Views, 63 Favorites, 0 Comments

IoT-Terrarium

IMG_20191130_204003.jpg
IMG_20191123_162405.jpg
Webpage.JPG

My girlfriend is obsessed with house plants, and a little while ago mentioned she wanted to build a terrarium. Keen on doing the best job she googled how to's and best practices of how to create and look after one of these. Turns out there is a million blog posts and no one straight answer, and it all seems to come down to the look and feel of how individual terrariums are growing. Since I am a man of science and I like data to know if something is actually working, I wanted to put my knowledge of IoT and electronics to good use and create an IoT Terrarium monitor.

The plan was to build a sensor based system that could monitor temperature, humidity, and soil moisture from a simple yet elegant web page. This would allow us to monitor the terrarium's health so we always knew that it was in the best condition. Since I also love LED's (I mean who doesn't), I also wanted to add a neopixel that would turn the terrarium into the perfect mood or night light as well!

After planning the build I knew I wanted to share this so that others could make their own. So to allow everyone to be able to reproduce this project, I have only used easy to source materials which can be purchased in most brick and mortar stores or easily through sites such as Adafruit and Amazon. So if you are interested in building your own Iot-Terrarium on a Sunday afternoon read on!

Supplies

For the most part you should be able to purchase similar items as myself. But I encourage you to diversify and go bigger and better, so some of the items listed below you may want to adapt to your specific build. I will also list some alternative materials and methods throughout this inscrutable for those who don't have access to everything. So, to start there are a few tools you will need in order to follow along, these are;

  • Drill & Bits - Used for drilling through the lid of the terrarium container to mount your sensors, lights and controllers.
  • Hot Glue Gun - Used for gluing the sensors to the lid of the terrarium. You may choose to use a different mounting method like superglue or nuts and bolts.
  • Soldering Iron (Optional) - I decided to make a dedicated PCB for this project so that the connections were the best possible. You can also use a bread board and jumper wires and achieve the same result.
  • About 4 hours - This project from start to finish in building took me about 4 hours or so to complete. This will depend on how you decide to build you version

Below is a list of materials for the electronics for sensing and controlling the terrarium. You do not have to use all of the sensors, nor do you have to use the same sensors for your terrarium, but for the supplied code these materials will work out of the box. A little heads up, I do use amazon associate links for this, so thank you for the support if you decide to purchase anything from those links.

  • An ESP8266 - Used for controlling the neopixel, reading the data from the sensors, and showing you the web page. You may also choose to use the Adafruit HUZZAH
  • Adafruit Flora RGB NeoPixel (or from Adafruit) - These are awesome little neopixels in a great form factor. They have all of the other necessary passive components on them as well for easy control.
  • DHT11 Temperature Humidity Sensor (or from Adafruit) - A basic temperature and humidity sensor. You may also use the DHT22 or DHT21 for this as well.
  • Soil Moisture Sensor (or from Adafruit) - These come in two flavours. I used a resistive type, but i recommend the capacitive type like the one from Adafruit. More on these later.
  • A 5V (1A) Power Supply - You will need a 5V power supply for this project. This needs to be at least 1A in power, so you could also use a standard USB wall socket as well.
  • A prototype PCB - Used to connect everything together in a robust manor. Can also use a breadboard and some jumper wires too.
  • Some mounting bolts - Used to mount your PCB to the lid of your jar. You could also use hot glue as well.
  • PCB Headers - To mount the NodeMCU to the PCB.
  • Wire - Any variety of wire to connect the PCB and sensors together.

For your actual terrarium, there are limitless options that you have. I highly recommend heading to your nearest garden center for all of your supplies as well as advice. There you can also ask for help on the best combination of materials to build a Terrarium for the plants you use. For myself, my local garden center had all the necessary materials in convenient little bags. These were;

  • A Glass Jar - Usually found in your home store. This can be of any shape or size you desire, but should have a lid that will allow you to drill through and attach electronics.
  • Plants - The most important part. Choose wisely and make sure to match all the materials in the build to suit your plant. I used a little help from here.
  • Soils, Sands, Pebbles, Charcoal, and Moss - These are the basic building blocks of a terrarium and are usually easy to find in a hardware store with a gardening section or your local nursery

Also check out a great number of terrarium builds right here on Instructables too!

Making Your Terrarium

IMG_20191123_151917.jpg
IMG_20191123_145626.jpg
IMG_20191123_145814.jpg
IMG_20191123_145925.jpg
IMG_20191123_150047.jpg
IMG_20191123_150725.jpg
IMG_20191123_151229.jpg

To start, we need to actually build a terrarium before being able to hook it up to the internet! There is no right or wrong way to compile a terrarium, but there are however best practices which I will try to outline.

The first and most important is that you are aiming to mimic to environment that your chosen plants thrive in. Typically a terrarium uses more tropical moisture loving plants, but many people still use things like succulents in an open topped container. I chose a more tropical plant for this build so that I could have a sealed lid which I will use to mount the electronics to.

The next best practice is the order of how the ingredients of a terrarium are put together. For the best results you will need to layer them correctly so that water can drain and filter through the system and cycle back through. Watch out for is getting over zealous with plants and materials. Scope out your jar, plants, and materials before putting them altogether, otherwise everything may not fit.

Following along with the photos for this step, the below instructions are how you can layer your terrarium for the best result;

  1. Place some pebbles in the bottom of the jar. This is for drainage and leaves a place for water to collect.
  2. Next place a layer of moss, this is a filter to stop soil from falling through the cracks of the pebbles and eventually ruining the effect the pebbles give. This can also be achieved with a wire mesh as well
  3. Then add your charcoal on top. This charcoal acts as a water filter
  4. On top of the charcoal you can now add soil. At this stage you will want to check how full your jar is getting as you can empty it all out and start again here easier than later
  5. (Optional) You can add other materials such as sand for a layering effect as well. I added a very fine layer of sand for an aesthetic affect, then layered the rest of my soil.
  6. Next, make a hole in the middle then de-pot your plants and place them delicately in the center.
  7. If you can reach, pat the soil a around your plants to embed them firmly into the soil.
  8. Finish by adding a few decorative pebbles on top and a little more moss which will come to life with a little moisture.

Now that was super easy to pot up a terrarium or two on a Sunday afternoon! But don't take my word for gospel, make sure to have a look at how others have build theirs.

Making It Smart

IMG_20191129_170059.jpg
IMG_20191129_165809.jpg

Time to make your terrarium stand out from others. Time to make it smart. To do this, we need to know what we want to measure and why. I am no expert in gardening, so this is a first for me, but I understand sensor and micro controllers very well, so applying my knowledge in one will hopefully bridge the gap to the other.

After some googling to figure out which metrics would be the best, I went shopping to find suitable sensors to work with. I ended up choosing 3 things to measure. These were temperature, humidity, and soil moisture. These three metrics will give a generic overview of the health of our terrarium and help let us know if it is healthy or requires looking after.

To measure the temperature and humidity, I chose the DHT11. These are readily available from many sources such as Adafruit and other electronics stores. They are also fully supported in the Arduino environment along with other sensors of the same family such as the DHT22 and DHT21. The code at the end of this Instructable supports any version, so you can pick any version to suit your budget and availability.

Soil moisture sensors come in two flavours; resistive and capacitive. For this project I ended up with a resistive sensor as that was what was available to me at the time, but a capacitive sensor would offer the same outcome.

The resistive sensors work by applying a voltage to two pins in the soil and measuring the voltage drop. If the soil is moist there will be less voltage drop and therefore a larger value read by the ADC of the micro-controller. The beauty of these is there simplicity and cost, which is why I ended up using this version.

Capacitive sensors work by sending a signal to one of two pins on soil like the resistive version, the difference is it looks for a delay in when the voltage arrives at the next pin. This happens very quickly, but all of the smarts are usually taken care of on board the sensor. The output like the resistive versions is usually also analog allowing it to be connected to the analog pin of the micro-controller.

Now, the idea behind these sensors is not to give an absolute value on everything as their measurement techniques and physical properties depend on too many variables of your terrarium. The way to look at the data from these sensors, especially the soil moisture, is relative since they are not really calibrated. So to help take the guessing game out of when to water or care for your garden, you will need to look at how your terrarium is going for a little bit and mentally match that with your sensor data.

Making the PCB

IMG_20191129_175356.jpg
Circuit Diagram.png
IMG_20191129_170517.jpg
IMG_20191129_171432.jpg
IMG_20191129_171718.jpg
IMG_20191129_172410.jpg
IMG_20191129_174925.jpg

For this project, I decided to make my own PCB from prototype board. I chose this so that everything would be connected together more robust than a bread board or through header wires. Having said this, if you buy the right form factor of sensors and controllers, you can defiantly build this on a breadboard if you do not have access to a soldering iron.

Now, your terrarium will most likely use a different jar to mine and therefore will not use the exact PCB I have made, so I wont go into detail about the exact method I used to create it. Instead below are a series of indicative steps you can take to make sure you achieve the same result. In the end all you need to do to make the project work is follow the circuit diagram in the images.

  1. Start by laying the PCB on top of your lid to see how everything will fit. Then mark any cut lines and mounting holes on the PCB. in this step you should also mark where the hole in your lid for wires should be.
  2. Next cut down your board if you are using prototype board. You can do this using a knife and straight edge by scoring along the holes and snapping it.
  3. Then using a drill, form the mounting holes for the screws to go through into your lid. This hole diameter should be larger than your screws. I used a 4mm hole for M3 screws. You could also use hot glue to mount the PCB to the lid as well.
  4. At this stage it is a good idea to also make the mounting holes in your lid as well while there are no components on the PCB. So place your PCB on top of your lid, mark the holes and drill them using a smaller diameter than your mounting bolts. This will allow the bolts to bite into the lid.
  5. Drill the hole for your wires to go through all the way through. I made a 5mm hole for mine which was just the right size. At this stage it is also a good idea to mark and drill the same hole in your lid.
  6. Now you can lay out components on your PCB and start soldering. Start with the headers for the ESP8266.
  7. With the ESP8266 headers in place you now know where the pins line up, so you can now cut up some wires to connect your sensors. When doing this make sure they are longer than you need, as you can trim them down later. These wires should be for all of your power + and -, as well as the data lines. I also colour coded these so I knew which was what.
  8. Next solder all the wires you need for the board according to the circuit diagram and push them through the hole of the PCB ready for mounting to the lid and connecting to your sensors.
  9. Last, you will need to make a connection for your power supply. I added a small connector (not in pictures) for this. But you could also solder it on directly as well.

That's its for the PCB assembly! Its mostly mechanical suggestions as it will be up to you to lay out your PCB to suit your lid. At this stage don't mount the PCB on the lid as we will need to mount the sensor to the underside in the next step.

Making the Lid

IMG_20191129_183944.jpg
IMG_20191129_175641.jpg
IMG_20191129_175712.jpg
IMG_20191129_181502.jpg
IMG_20191129_182120.jpg
IMG_20191129_182531.jpg

Time to mount the sensors and lights to the lid! If you followed the last step, you should have a lid with all the PCB mounting holes and a large hole for the sensor wire to go through. If you do you can now layout the lights and sensors in the way that you would like that. Just like the last step, the method you use will probably be a little different, but here is a list of steps to help you layout your lid

Caution: The data lines of the neopixels have a direction. Pay attention to the input and output of each light by looking for the arrows on the PCB. Make sure that data always goes from output to input.
  1. Start by placing the lights and temperature sensor on the lid to see where you would like to fit them. I suggest to keep the temperature sensor away from the lights as they will give off a little bit of heat. But other than that the layout is entirely up to you.
  2. With everything laid out, you can cut up some wire to hook up the lights together. I did this by cutting a test piece and using it as a guide to cut the rest.
  3. Next I used some blue-tak to hold down the lights and soldered the wires to them using the pads on the sides of the flora boards. Pay attention to the data directions of the lights.
  4. I then removed the blue-tak from the lights and used hot glue to secure them to the lid along with the temperature sensor in the location I was happy with.
  5. Now take you PCB and mount it to the lid where you drilled and tapped holes earlier. Push the wires through the large hole ready to be connected to the sensors.
  6. Then solder each of the wires to the correct sensors following the circuit diagram provided in the previous step.
  7. Since the soil sensor is not mounted to the lid, you will need to make sure that the wires are left long enough for it to be planted in the soil. Once cut down, solder on your soil sensor.

Congrats, you should now have a fully assembled sensor based lid complete with temperature, humidity, and soil moisture sensors. In later steps you will see I added a 3D printed hat out of wood resin to cover the ESP8266 as well. I haven't described how to make this because the final shape and size of your terrarium will probably differ and not everyone has access to a 3D printer. But I want to point it out so serves as an idea on how you may want to finish your project!

Coding the ESP8266 With Arduino

Board setup.png
Add Library.png
Boards Manager.png
GitHub.JPG

With your sensor-fied lid ready to go, its time to put the smarts into it. To do this you will need the Arduino environment with the ESP8266 boards installed. This is nice and easy to get going thanks to the great community behind it.

For this step, I suggest not having the ESP8266 plugged into the PCB so you can debug any issues with uploading and running it first. Once your ESP8266 is working and connected to WiFi for the first time, then I suggest you plug it into the PCB.

Setup the Arduino Environment:

First you will need the Arduino environment which can be downloaded from here for most operating systems. Follow the installation instructions and wait for it to finish. After its done, open it up and we can add the ESP8266 boards by following the great steps over at the official GitHub repository here.

Once added, you will need to select the board type and the flash size for this project to work. In the "tools"->"board" menu you will need to select the "NodeMCU 1.0" module, and in the Flash size options you will need to select "4M(1M SPIFFS)".

Adding the libraries

This is where most people get unstuck when trying to replicate someones project. Libraries are finicky and most projects rely on a specific version to be installed in order to work. While the Arduino environment partially addresses this issue, it is usually the source of compile time issues found by new beginners. This issue is solved by other languages and environments using something called "packaging", but the Arduino environment does not support this... technically.

For people with a brand new install of the Arduino environment, you can skip this, but for others who want to know how to make sure that any project they make with the Arduino environment will work (providing it does out of the box to start with) you can do this. The work around relies on you creating a new folder anywhere you want and directing your "Sketchbook" location in the "file"->"preferences" menu. Right at the top where it says sketchbook location, click browse and navigate to your new folder.

After doing this, you will have no libraries installed here, which allows you to add in any that you would like without the ones you had installed before. This means for a specific project like this one, you can add the libraries that come with my GitHub repository and have no clashes with other ones you may have installed. Perfect! If you would like to go back to your old libraries, all you have to do is change your sketchbook location back to the original one, its that easy.

Now to add the libraries for this project you will need to download the zip file from the GitHub repository and install all the libraries in the included "libraries" folder. These are all stored as .zip files and can be installed using the steps suggested at the Arduino official web page for this.

Change the required Variables

After you have downloaded and installed everything, its time to start compiling and uploading the code to the board. So with that downloaded repository, there should also be a folder called "IoT-Terrarium" with a bunch of .ino files in it. Open the main file called "IoT-Terrarium.ino" and scroll down to the Main Variables part of the sketch near the top.

Here you need to change a couple of key variables to match what you have build. The first things you need to add is your WiFi credentials to the sketch so that the ESP8266 will login to your WiFi so you can access it. These are case sensitive so be careful.

String SSID = "";
String Password = "";

The next is the timezone you are in. This can be a positive or negative number. For example Sydney is +10;

#define UTC_OFFSET +10

After that is the sampling period and amount of data the device should store. The number of samples collected has to be small enough for the micro controller to handle. I have found that anything under 1024 is okay, anything larger is unstable. The collection period is the time between samples in milliseconds.

Multiplying these together gives you how long the data will go back for, the defaults of 288 and 150000 (2.5 minutes) respectively gives a time period of 12 hrs, change these up to suit how far back you would like to see.

#define NUM_SAMPLES 288
#define COLLECTION_PERIOD 150000      

In the previous steps I connected the LED's to pin D1 (pin 5) of the ESP8266. If you have changed this or have added more or less LED's you can change this in the two lines;

#define NUM_LEDS 3                    // The number of LED's you have connected
#define DATA_PIN 5                    // The pin that the LED's data line is on

The last thing you need to change is your DHT11 settings. Just simply change the pin its connected to and the type if you have not used the DHT11;

#define DHT_PIN 4                     // The data pin that you have connected your DHT sensor to
#define DHTTYPE DHT11                 // Uncomment this when using the DHT11
// #define DHTTYPE DHT22              // Uncomment this when using the DHT22
// #define DHTTYPE DHT21              // Uncomment this when using the DHT21</p>

Compile and Upload

After changing everything you need to, you can go ahead and compile the sketch. If all is good it should compile and give no errors at the bottom of the screen. If you get stuck you can comment below and I should be able to help.Go ahead and connect the ESP8266 with a USB cable to your computer and hit upload. Once done it should start up and connect to the WiFi. There is some messages in the serial monitor as well to tell you what it is doing. Android users should take note of the IP address it states as you will need to know it.

That's it! You have successfully uploaded the code. Now to stick the lid onto the terrarium and see what the sensors have to say.

The Final Product

IMG_20191130_204338.jpg
Webpage.JPG

Once all put together, stick the soil sensor into the soil so that the two prongs are covered. Then simply close the lid, connect your power supply and turn on! You can now navigate to the web page of the EPS8266 if you are on the same WiFi network as it. This can be done by going to its IP address, or by using mDNS at; http://IoT-Terrarium.local/ (Currently note supported by Android, sigh)

The website is there to show you all the data you are collecting and to check on the health status of your plants. You can now view all the statistics from all of your sensors, and most importantly turn on the LED's for a unique little night light, awesome!

You can also save the page to your home screen on either iOS or Android so that it will act like an app. Just make sure to be on the same WiFi network as your ESP8266 when you click it.

That's it for this project, if you have any comments or query's leave them in the comments. Thanks for reading and happy making!