Beer Tempature Monitering with Raspberry Pi

BEER MONITORING WITH MY RASPBERRY PI

The secret to brewing great beer at home is making sure that you keep it at the right temperature. This can be tricky when your house doesn’t have a thermostat and you’re not in the house for most of the day. However, by using a cheap and cheerful sensor with a raspberry pi, you can record a log of the temperature and check it over the internet to make sure your beer is brewing nicely.

Hardware

The sensor I used is the DHT11 which, at the time of writing, you can order on eBay for £1.12 delivered. It has a digital interface, so you don’t need to do any calibration or digital conversion as you would with a thermistor. To connect the sensor to the RPi, all you need is a 10k resistor to pull-up the data signal and to make the following connections (see pic).Raspberry Pi Beer Interface

RPi VCC (pin 1) -> DHT11 pin 1

RPi GPIO4 (pin 7) -> DHT11 pin 2

RPi GND (pin 6) -> DHT11 pin 4

 

Interfacing

The DHT11 uses its own serial interface, which can be interrogated using the wiringPi C library. To install and compile the library, use the following commands:

sudo apt-get install git-core build-essential

git clone git://git.drogon.net/wiringPi

cd wiringPi

./build

This blog post details the code necessary to read the sensor data. The code below has been modified to return only the current temperature and to repeat the request if there is an error. Place the following into a file named dht11.c.

#include <wiringPi.h>

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

#define MAX_TIME 85

#define DHT11PIN 7

#define ATTEMPTS 5

int dht11_val[5]={0,0,0,0,0};

int dht11_read_val()

{

uint8_t lststate=HIGH;

uint8_t counter=0;

uint8_t j=0,i;

for(i=0;i<5;i++)

dht11_val[i]=0;

pinMode(DHT11PIN,OUTPUT);

digitalWrite(DHT11PIN,LOW);

delay(18);

digitalWrite(DHT11PIN,HIGH);

delayMicroseconds(40);

pinMode(DHT11PIN,INPUT);

for(i=0;i<MAX_TIME;i++)

{

counter=0;

while(digitalRead(DHT11PIN)==lststate){

counter++;

delayMicroseconds(1);

if(counter==255)

break;

}

lststate=digitalRead(DHT11PIN);

if(counter==255)

break;

// top 3 transistions are ignored

if((i>=4)&&(i%2==0)){

dht11_val[j/8]<<=1;

if(counter>16)

dht11_val[j/8]|=1;

j++;

}

}

// verify checksum and print the verified data

if((j>=40)&&(dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF)))

{

printf("%d.%d,%d.%d\n",dht11_val[0],dht11_val[1],dht11_val[2],dht11_val[3]);

return 1;

}

else

return 0;

}

int main(void)

{

int attempts=ATTEMPTS;

if(wiringPiSetup()==-1)

exit(1);

while(attempts)

{

int success = dht11_read_val();

if (success) {

break;

}

attempts--;

delay(500);

}

return 0;

}

Compile and execute the source code with the following commands:

gcc -o dht11 dht11.c -L/usr/local/lib -lwiringPi

sudo ./dht11

The program should return two numbers – one for relative humidity and the other for temperature.

Logging

The simplest method of creating a log of the temp/humidity is to use a cronjob. The program must be run as root, so use the following command to edit the cron config file:

sudo crontab -e

Add the following line to the end. It will save a timestamp and the temp/humidity every minute to temp.log.

* * * * * echo `date +\%Y\%m\%d\%H\%M\%S`,`/home/pi/wiringPi/dht11` >> /home/pi/temp.log

If that doesn’t seem to be working, the system cron may not be running. The following command will start it, and the line after will make sure it starts every time the RPi turns on:

sudo service cron start

sudo update-rc.d cron defaults

Display

Reading a log file isn’t the easiest way to check how the temp/humidity is changing, but we can use a graphing library to read the log file and plot it. For this, I used DyGraph which is a Javascript library for plotting time-based information. Here is the final .html file I used:

You can set this up on the RPi by installing a web server like Apache, creating a symlink to the log file, downloading dygraph and saving the above html file as /var/www/temp.html.

sudo apt-get install apache2

ln -s /home/pi/temp.log /var/www/temp.log

wget -P /var/www http://dygraphs.com/dygraph-combined.js

Then use your browser to navigate to 192.168.1.2/temp.html, replacing the IP address with that of your RPi. To be able to access the page from the internet, you’ll have to set up NAT on your router.
Beer-Graph


cropped-logo3.png


Leave a Reply