Building a wine cellar monitoring system - Part 1 : The hardware and setup
Tags: iot, java, raspberrypi
Published: 2020-07-29 14:09:02

---
title: Building a wine cellar monitoring system - Part 1 : The hardware and setup
published: true
date: 2020-07-10 15:53:27 UTC
tags: iot,java,raspberry,raspberrypi
canonical_url: https://lengrand.fr/building-a-wine-cellar-monitoring-system-part-1-the-hardware/
---

 ![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://images.unsplash.com/photo-1585146702843-45507b83e662?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

Hi there!

As you may know already, I'm French. And as most French people, I care about my wine :). We moved to the Netherlands with 80 bottles in the trunk and I'm getting about the same amount delivered every year. I store all those bottles in my attic, where the temperature is the most stable. But how stable exactly?! That's what we'll figure out in this series of post !

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-7.png)<figcaption>One of my favourite places in the house</figcaption>

<!--kg-card-end: image-->

In this series of posts, we'll see how to build a cellar monitoring system sending data to the #cloud with some #iot, #timeseries and #microservices on a #raspberrypi. Is that enough buzzwords yet?

More seriously though, **my original goal is to be able to check the variations of temperature and hygrometry in my wine cellar over the course of a year, from my phone.** Let's see where that brings us :).

If you don't want to read, you can find **[the complete project (still WIP) on GitHub](https://github.com/jlengrand/cellar)**.

This is how I (roughly) plan to describe the project :

- **[Ep1 : Setup and Communicating with the sensor - This article](https://lengrand.fr/building-a-wine-cellar-monitoring-system-part-1-the-hardware/)**
- Ep 2 : Creating a java library to interact with the system - WIP
- Ep3 : Service implementation with cloud sync - WIP
- Ep4 : Detecting sync issues and missing data - TODO
- Ep5 : Dashboard and results visualization - TODO

# The hardware! 

Before looking at dashboard and the cloud, we want to be able to actually get the values we need. **That's what we'll look at in this post**.

A few requirements :

- It has to be cheap
- I wanna use as much #java as possible. 
- It should also be robust and quite reliable

That's it!

To check temperature and humidity, we'll have to get some electronic component to do the work for us. Looking at my requirements, the cheapest and most common component in use would be the [DHT11](https://components101.com/dht11-temperature-sensor) (or [DHT21](https://hobbycomponents.com/sensors/840-dht21-am2301-temperature-humidity-sensor) if you want more precise values).

The characteristics seem perfect for us :

- 3 pins, works with 3.5V
- 0 to 50 degrees temperature range and 20 to 90% humidity
- +/-1 % and degree accuracy

It looks like this :  

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image.png)<figcaption>A picture of the DHT11</figcaption>

<!--kg-card-end: image-->

**What pleases me the most about those characteristics is that they fit perfectly with the I/O pins of the Raspberry pi, which means we can basically solder the component on the pi and we're good to go!**

Here is a quick look at the pi model 2 I/O pins :

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-1.png)<figcaption>Overview of the raspberry pi I/O pins</figcaption>

<!--kg-card-end: image-->

All we need here is one of the 5V power pins, one of the ground pins, and one of the GPIO (you may choose the one you like :)).

The complete build looks something like this :

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-11.png)<figcaption>System with DHT11 and RaspberryPi connected together</figcaption>

<!--kg-card-end: image-->

Alright, nice! Now we want to be able to read data from the sensor!

## Setting up Sync between the Pi and my dev machine

Because it is my Pi that has the GPIO, I have to run my code there. I have several options available to me for development:

- **Using my Pi as dev machine**. The simplest setup. However, the pi is not very powerful and running a full IDEA on it will be cumbersome. Because I want realistic from day 1 as well, I placed my pi in the attic, which is not the best place to write code :).
- **Using my usual dev machine and transferring the data to the Pi**. Another solution would be to code locally as I do every day, and then transfer the generated artifacts on the pi using SCP or anything equivalent. That's nice as we will save a lot of compilation time. However it requires a lot of manual labor. We will also see later that compiling native code for the Pi architecture should be done on the Raspberry. 
- **Use some IntelliJ magic and combine both options!** We'll use a nice feature of IntelliJ called [automated deployment](https://www.jetbrains.com/help/idea/tutorial-deployment-in-product.html), which will allow us to keep a two-way synced workspace between our two machines! This way we can write Java on our laptop, compile it there if needed and run on the Pi easily. Here is how it looks: 

In Tools -> Deployment -> Choose 'Configuration'

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-2.png)<figcaption>Deployment - Configuration option of IntelliJ</figcaption>

<!--kg-card-end: image-->

Create a new configuration to access your Raspberry. The easiest in my case was a [SFTP](https://www.ssh.com/ssh/sftp/) connection. You'll be asked to create a SSH configuration the first time, as well as to choose a folder where to upload all of your code.

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-3.png)<figcaption>My Raspberry Pi SFTP configuration</figcaption>

<!--kg-card-end: image-->

Don't forget to have a look at your mappings, which lists the relations between local and remote folders. In my case, I created one for my root project and one per maven module that I have in my project. At this point though, the root mapping should be enough for you if you just want to play around.

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-4.png)<figcaption>An overview of my folder mappings</figcaption>

<!--kg-card-end: image-->

Finally, if needed add some excluded paths. I honestly didn't bother given the small size of the project but it might be more useful for larger projects, or if you want to compile for multiple architectures for example.

Once you have done all this, you will basically have a 1 to 1 relation with your raspberry pi in real-time. You can use the Tools -> Deployment option to sync as needed, or even automate two-way sync like I did. You can even browse the local host to see what its current status is.

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-5.png)<figcaption>An overview of all possible options for sync between machines</figcaption>

<!--kg-card-end: image-->

Don't forget to have a look in the options to choose something you are comfortable with. For example, do you want file and folder deletions to be synced as well or not.

Once all of this is done, you will have access to the informative 'File transfer' view of IntelliJ which will give you a real-time overview of the syncs happening between the machines in real-time.

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-6.png)<figcaption>An overview of the File Transfer view of IntelliJ</figcaption>

<!--kg-card-end: image-->

Alright, everything is setup and we're ready to get some Temperature and Humidity from our sensor!!! Let's go

## Pure Java implementation

As a #java guy, my first hunch was to want a full java setup. **[Pi4J](https://pi4j.com/1.2/index.html)** seemed like the perfect tool for the job! A library to interact with the Pi GPIOs from Java. Pi4J relies on the underlying **WiringPi** library, which has to be installed first.

I've setup a first simple test script using Pi4J and [heavily inspired from this stack overflow answer](https://stackoverflow.com/questions/28486159/read-temperature-from-dht11-using-pi4j).

Now there are a couple things I really didn't like about that method:

- It needed several iterations before being able to read a proper value form the sensor. Usually this means several seconds, and reducing those timings lead to errors
- I had to play around with the `GPIO.delayMicroseconds()` values to get results, which made me doubt the code would be reliable in the long run. 
- The code as written now contained some magic which did not make me comfortable.

I decided to look for lower level alternatives, and search for existing libraries.

## Leverage existing libraries and going native

That's it, the decision was made to write some C code, and glue it with my Java at a later stage using the [JNI](https://en.wikipedia.org/wiki/Java_Native_Interface). It also felt appealing simply because I had not touched the JNI for years and could use a quick refresher.

**I would basically create a small .dll (or .so file in that case) that does all the heavy lifting for me.**

_The only thing about using that method is that my C code has to be compiled for the Raspberry Pi architecture (and in my case most likely on the raspberry itself). That's ok though, I'd only have to do that once._

As I mentioned, the good thing about using this method is that we can rely on the shoulders of giants! I got lucky enough to [find this repository](https://github.com/adafruit/Adafruit_Python_DHT) from Adafruit providing a driver for the DHT11 and some Python test code as well. Let's make use of it!

We'll copy all of the code that we need to read the DHT11. I use a Raspberry2B, so I'll allow myself to cut some corners.

The function we want to use in the end is `int pi_2_dht_read(int type, int pin, float* humidity, float* temperature)` in the `pi_2_dht_read.c` file. We see that this file needs:

- Its own header file `pi_2_dht_read.h`
- The generic code to read from the pi2 GPIOs `pi_2_mmio.c` and `pi_2_mmio.h`
- The files that contain utility functions `common_dht_read.c` and `common_dht_read.h`

We'll keep those files from the library (in a folder called adafruit) and add a very simple test file to test it. Here is the content of my `testAdafruit.c` code :

<!--kg-card-begin: html--><script src="https://gist.github.com/jlengrand/7086168573db6932333e03d38dd64fbd.js"></script><!--kg-card-end: html-->

And this is how our project currently looks like :

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-10.png)<figcaption>a list of all files in my current project</figcaption>

<!--kg-card-end: image-->

Almost there! We will need to compile that code to test it.  We can do this in a single line of code (which I put in a script for convenience).

<!--kg-card-begin: markdown-->
```
$ gcc -Wall -I. -Iadafruit/Raspberry_Pi_2 adafruit/common_dht_read.c adafruit/Raspberry_Pi_2/pi_2_mmio.c adafruit/Raspberry_Pi_2/pi_2_dht_read.c testAdafruit.c -o testAdafruit

```
<!--kg-card-end: markdown-->

This line of code creates an executable file called `testAdafruit`. When we fire it, the following happens :

<!--kg-card-begin: image-->

![Building a wine cellar monitoring system - Part 1 : The hardware and setup](https://lengrand.fr/content/images/2020/07/image-9.png)<figcaption>When running the executable, we get temperature and humidity values from the sensor</figcaption>

<!--kg-card-end: image-->

We did it! We can use our sensor and we're getting values from it, quick enough to create an API like interface for it.

We're just at the beginning and our journey, and haven't done any Java just yet. However we have found out how to achieve our original goal of environment monitoring. We created a simple C program that can read temperature data from our raspberry pi remotely. We also cooked up a nice development setup with the help of IntelliJ deployment's feature. In the next article, we'll look into how to produce a library from our C code and create a basic API that we can use to get data from the sensor.

Stay tuned!