domingo, janeiro 11, 2015

Simple Weather Monitor

Well, a year ago, I started to thinking about how to monitoring the weather. Using the simple way, precise like as I need, and possible to access from anywhere using internet. The proposed is measure the temperature (celsius), humidity (%) and atmospheric pressure (ATM).
So for monitoring the environment, I choosed a Arduino Nano board and two others sensors, DHT-22 for temperature and humidity, and Bosch BMP085 for pressure and temperature again.
The integration and how to manipulate the sensors can be seen bellow:
DHT-22 and Arduino using DHT Library


BMP085 and Arduino using Adafruit Library


As the BMP085 read the pressure using Pascal unit, we need to convert this value to ATM, and decrease the precision, but for these propose is enough. You can choose the best precision as you need.
After the sensors were integrated with Arduino, the next step is send the arduino information to a host computer, and store in a database. I have a dedicated server (oldest laptop), running Ubuntu Linux in my home, so the easy way to integrate will be using the Arduino USB/Serial port, to get/send information, using a pre defined simple protocol. A return string can be like as bellow:

TempBMP085:32.65C_TempDHT22:33.30C_Humidity:46.50%_Press:91857.00_ATM:0.91

To do this, was developed a perl script to connect via serial port with the Arduino board, send a command (Ex: letter 'g' from get), and get the string values read from sensors. After obtained the string, a parser structure "cut" the specific values and populate a RRD Database on my host computer.

root@laptop:~/ ./get_data.pl
Connected !
Command Sent !
String received !
Parsing String...
The measure values are:
TEMPBMP: 32.65
TEMPDHT22: 33.30
Umidade: 46.50
Pressao: 91857.00
Pressao ATM: 0.91

When you have the exact values (float type) that you need, storaged in a perl variable, simple running a external command (rrd tool) to populate a database using this value, like bellow:

system("rrdtool update /root/LogTemp/rrd_db/temperature_bmp085.rrd N:$temp_BMP");

If you want, you can generate a rrdtool graphs from data storaged in database, using command:

######################################################
##  BMP085
######################################################
#hourly
#create a png named temp_hourly.png
rrdtool graph /var/www/pages/temp_bmp085_hourly.png --start -1h \--title "Temperature " \--vertical-label "Temperature C" \--alt-autoscale \--alt-y-grid \DEF:tempBMP085=/root/LogTemp/rrd_db/temperature_bmp085.rrd:temperatureBMP085:AVERAGE \LINE2:tempBMP08500FF00:"BMP085" \GPRINT:tempBMP085:MIN:"Min\: %4.2lf" \GPRINT:tempBMP085:MAX:"Max\: %4.2lf" \GPRINT:tempBMP085:AVERAGE:"Avg\: %4.2lf" \GPRINT:tempBMP085:LAST:"Current\: %4.2lf  C"

and get a result like this

or if you prefer, use a perl script to generate a dynamic html page with javascript, and you can do your dashboard. Bellow you can see a example using Google Developer API Gauges Chart.


So, when you have a graph (png file) or a dynamic page (html+js), just use the Apache server on Ubuntu Server to provide the information on the internet.