Technology Choices in Monitoring a Greenhouse
Tags: rasberrypi, python, influxdb, sideprojects
Published: 2019-06-04 08:36:47

---
series: Greenhouse Telemetry 
title: Technology Choices in Monitoring a Greenhouse
published: true
description: My journey into monitoring (and perhaps controlling) my greenhouse
tags: #rasberrypi #python #influxdb #sideprojects
cover_image: https://thepracticaldev.s3.amazonaws.com/i/6283verkipnzqayfpyq7.png
---


### Introduction


I've started to put together a telemetry setup for my greenhouse.  I've been thinking about it for a while but ultimately I've left this a bit late.  It's summer, although the constant Scottish rain begs to differ.


I've only recently installed a proper greenhouse and inspired by m'colleague Neil Galls [Pirragor](https://dev.to/neilgall/pirrigator-18hf) Series, I've gotten started.  I'm not doing some fancy Rust cross cross compiling, resulting in installing a new OS like Neil.  I've opted to move my complications somewhere else, by growing chillies instead of tomatoes in various varieties.   Needing many more individual plants and finer grained plant care.   This means monitoring of temperature and humidity is the most important concern with some further thought needed in automating the upkeep of the individual plants of which there are over 20.


Straight off the bat, off the shelf [Rasberry Pi](https://www.raspberrypi.org/) is a good choice for rapid development.  Python is already available [so I got started](https://github.com/rossdrew/gh-telemetry).


### Need Input!


I need humidity and temperature monitored, hopefully at different points in the greenhouse and compared to outside.  Internally I'll be using [AM2302 humidity/temperature sensor](https://www.digikey.co.uk/product-detail/en/sparkfun-electronics/SEN-10167/1568-1793-ND/6163749?utm_adgroup=&mkwid=sLFm8fMw6&pcrid=338141659323&pkw=&pmt=&pdv=c&productid=6163749&slid=&gclid=EAIaIQobChMIgOjG0LHP4gIVZpPtCh2FAgsZEAQYAiABEgLQffD_BwE) and the [Adafruit Python Drivers](https://github.com/adafruit/Adafruit_Python_DHT) for it  which makes it as easy as 

```python
humidity, temp = Adafruit_DHT.read_retry(sensor, pin)
```


And rather than weather-proofing sensors and thinking about wiring it up, I'll just use a weather REST API in the form of [OpenWeatherMap](https://openweathermap.org/) and I get things like cloud cover and barometric pressure for free.


#### Problems?

I've noticed that randomly the AM2302 outputs a humidity of 3303.6 and a temperature of 70~ and the values had to be sanitised but otherwise, all good.


### Those who cannot remember the past are condemned to repeat it!


A grandiose way of saying we need to store the data for comparison.  Originally I stuck it all in a CSV file and tried -using [Pygal](http://pygal.org/en/stable/)- to write my own charts, which was time consuming and ultimately inflexible.

![](https://thepracticaldev.s3.amazonaws.com/i/5cmjki1639z7kxlyg82b.png)


I need better control over the data which means a database.  All of my data is going to be tied to a specific time so a [time-series database](https://en.wikipedia.org/wiki/Time_series_database) is what I'm looking for.  I installed [InfluxDB](https://www.influxdata.com/time-series-technical-paper/?utm_source=google&utm_medium=cpc&utm_campaign=&utm_term=influxdb&utm_content=&gclid=EAIaIQobChMIhYKmj7LP4gIVCZztCh0QngkZEAAYASAAEgKQ2fD_BwE) on my Rasberry Pi, no hassle and with the help of the [influxdb](https://influxdb-python.readthedocs.io/en/latest/include-readme.html) Python library, started to write my data there:

```python
    db_client = InfluxDBClient('localhost', 8086, 'root', 'root', 'TelemetryHistory')
    record_entry = [
        {
            "measurement": "am2302",
            "tags": {
                "update": "whole",
                "device": "am2302",
                "location": "gh1"
            },
            "fields": {
                "temp": float(temp_reading),
                "humidity": float(humidity_reading)
            }
        }
    ]
    write_success = db_client.write_points(record_entry)
```

InfluxDB, bullet point review:
 - quick to install
 - very quick to get started being schemaless
 - very quick to learn
 - perfect for analytic data, with good visualisation with Grafana
 - it has other benefits I wont use (efficient eviction, high throughput)


Then, using [Grafana](https://grafana.com/) on my laptop, I could connect to the database on my Rasperry Pi and view whatever selected, filtered, calculated data I wanted with the ability to draw very flexible, zoomable, pretty graphs.  Granted of my room at this point but pretty nonetheless.

![Grafana](https://thepracticaldev.s3.amazonaws.com/i/j4yp16f3s5822h63q15e.png)


### Next up!


I need to come up with a battery/solar panel configuration that will properly power all of this.