# Build a temperature and humidity sensor for your greenhouse
### By Daniel Brännvall (db222td)
## Introduction
This tutorial will show you a way to build a temperature and humidity tracker (and optionally alarm) for your greenhouse using a pycom device and some bits and pieces.
Building and setting up the device shouldn't take very long, perhaps an hour or two if you need to install software on your computer and update firmwares and stuff.
## Objective
I wanted to build a device for monitoring the temperature and humidity in my greenhouse, and to warn me if it gets too hot/cold/dry. It should give me some insight in how the temperature and humidity changes based on time and weather and, hopefully, help me get a feel for what I need to do before going away for a couple of days based on the weather forecast.
## Materials
This is the list of things I used for this project. The links under the pictures lead to places you can get them from with prices (no, I don't get paid when you use them, I'm just being helpful here).
1. **Pycom LoPy4 Development Board**
A chip with lots of inputs and outputs, and it has support for WiFi, LoRa, Sigfox and Bluetooth. Programmable in Micropython. Quite the overkill for this project, probably, but... it works!

[pycom.io](https://pycom.io/product/lopy4/)
2. **Pycom Expansion Board**
An expansion board usable with LoPy4. Handles power (from USB or a battery), battery charging, serial communications through the USB and a bunch of other stuff.

[pycom.io](https://pycom.io/product/expansion-board-3-0/)
3. **Micro-USB to USB cable**
You probably have one of these lying around. Used for powering the device and communicating with it.

[teknikdelar.se](https://www.teknikdelar.se/mobiltillbehor/sony/xperia-ovriga-modeller/kablar-laddare/deltaco-micro-usb-kabel-3-m-svart/)
4. **DHT11 Temperature and Humidity Sensor**
A sensor for temperature (0-50C) and humidity (20-90%).

[electrokit.com](https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/)
5. **Breadboard**
Not strictly necessary, but it makes connecting everything a lot easier.

[electrokit.com](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/)
5. **Cables**
Cables for connecting sensors and stuff to the expansion board.

[electrokit.com](https://www.electrokit.com/produkt/kopplingstrad-byglar-for-kopplingsdack-mjuka-65st/)
6. **Passive Piezoelectric Buzzer Module (optional)**
If you want the device to beep when it's getting to hot for your plants, you need something like this.

[electrokit.com](https://www.electrokit.com/produkt/piezohogtalare-passiv/)
7. **Push button (optional)**
If the beeping is bugging you, one of these can be useful. This one gives a high signal when the button isn't pushed, so it most likely uses more power than necessary, but it is what I had.

[electrokit.com](https://www.electrokit.com/produkt/tryckknapp-momentan/)
## Computer setup
Before starting, it's probably a good idea to update the firmwares of your expansion board and LoPy. Descriptions of how to do that can be found at pycom.io, more specifically [here for the expansion board](https://docs.pycom.io/pytrackpysense/installation/firmware/), and [here for the LoPy4](https://docs.pycom.io/gettingstarted/installation/firmwaretool/).
Since I'm on a Mac, I installed the DFU-util via brew, then failed miserably at updating the firmware of the expansion board. It seems like there's some problems with getting it to work on the current latest version of macOS, but I was told that it probably wouldn't be a problem to skip that step, so I did. I still recommend trying to do it, though.
After (not) updating the expansion board, I updated the LoPy4 itself using the "Pycom Firmware Update" application, and made sure to use pybytes ([like this](https://docs.pycom.io/pybytes/connect/quick/)).
The easiest way to code for your LoPy4 is to connect it to your computer with a USB cable (which powers it) and use the Pymakr plugin in the editor of your choice (as long as your choice is either [Atom](https://atom.io/) or [Visual Studio Code](https://code.visualstudio.com/)). I chose to use Visual Studio Code, since I've used it before and already had it installed, but Atom seems pretty popular too. Whichever you choose, you'll need to [install the plugin](https://docs.pycom.io/pymakr/installation/), and when that is done, you should be able to access your device from your editor.
## Putting everything together
The parts should be connected together something like the following (disconnect the power before starting to connect stuff):

The yellow lines are signal cables, the blue ones are ground and the red ones are +3.3V. Yellow cables go to the S leg on the modules, blue to the - leg and the red ones to the remaining leg on the modules that need it. Make sure to check the markings on the modules, they might be different than the ones in the diagram (mine are).
If something has been connected in the wrong way, the LoPy4 or the module might start getting hot. That's a sign you should probably disconnect the USB cable/battery pack really quickly and check the connections before trying again.
## Platform
The original plan was to make it possible to use one or more devices for gathering sensor readings and let them report measurements outside specified ranges as warnings or alarms to another device that would make noise, but due to a lack of time, I decided to keep everything on one device. I'm also not in range of any open LoRa gateways, so I'm using WiFi and report the measurements to pybytes (because it was really simple to set up).
## The code
The code is a bit long to add here, but it is available on [GitHub](https://github.com/danbrn/greenhouse). I'm using [JurassicPork's DHT_Pycom library](https://github.com/JurassicPork/DHT_PyCom/tree/pulses_get) to access the DHT11 sensor ([src/lib/dth.py](https://github.com/danbrn/greenhouse/blob/master/src/lib/dth.py)).
The Buzzer class controls the buzzer, Button controls the button, you can add limits for temperature and humidity instantiating objects of the Limits class for each, LocalAlarm actually uses the button and buzzer, and Greenhouse ties it all together. [main.py](https://github.com/danbrn/greenhouse/blob/master/src/main.py) contains code for starting the device with everything enabled, but I'll add some examples here too.
To just measure and report the sensor data to pybytes, you can use something like this:
```python
Greenhouse('P23', 20, 12).run()
```
which instantiates a Greenhouse object that accesses the sensor on pin 23, does a measurement (approximately) every 20 seconds and reports the average values to pybytes twelve times an hour.
The RGB led on the LoPy4 is used to show the status of the latest measurement, green if it's okay and yellow or red if a value is above or below the allowed values. To make the LED show yellow if the humidity is 40% or lower or 60% or higher:
```python
Greenhouse('P23', 20, 12, humidity_limit=Limits(warn_low=40, warn_high=60))
```
And to turn everything on, you can do (if everything is connected to the specified pins):
```python
Greenhouse('P23', 20, 12,
alarm_handler=LocalAlarm(buzzer=Buzzer('P11'),
button=Button('P10')),
temperature_limits=Limits(warn_low=20, alarm_low=16,
warn_high=28, alarm_high=32),
humidity_limits=Limits(warn_low=40, alarm_low=30,
warn_high=60, alarm_high=70)).run()
```
The button is used to acknowledge an alarm, i.e. turn off the buzzer and keep it off until all measurements are okay, then turn it on again the next time there's an alarm.
## Transmitting the data
As mentioned earlier, I'm not within LoRa range, but WiFi works for me, so I'm using that. Since I didn't implement the separation of sensor devices and alarm device, I don't really need any advanced communication so pybytes does enough, and it lets you show graphs of the values you send to it. How often the data is reported to pybytes by the device is configurable, 1-30 times per hour.
## Presenting the data
When the device is active, it reports data to pybytes. If you log in to your pybytes account at pybytes.pycom.io, you get access to the signals from your device and can set up graphs and stuff ([tutorial](https://docs.pycom.io/pybytes/dashboard/)). The unknown signal 11 reports the temperature and 21 the humidity. If your device reports data twelve times an hour, you'll need to set the pull size to 288 to show 24 hours worth of data. The data is stored for one month, but if you want to visualize that much, it's probably better not to report data quite that often.
A dashboard showing the data for a couple of hours might look like this:

## Finalizing the design
This prototype works fairly well (the software could probably use some more testing, though), but it doesn't exactly look ready for use.

It would obviously need some kind of case, and it would really benefit from the multi-device support, even if it would be possible to connect the temperature/humidity sensor to the actual device with loooong cables. Also, this version pretty much requires being plugged in, it probably uses quite a lot of power (which isn't really a problem for me, but it would be nice to be able to use batteries), so... I should probably get a LoRa gateway all of my own and a better button for the next version. And make the device(s) sleep more.