domenica 9 giugno 2013

How to make a wifi datalogger for temperature and humidity.


In this project I use two Arduino boards: the first one (Arduino UNO) is used for measuring temperature and humidity, the second one (Arduino MEGA 2560) for wifi connection
to my home lan. The two boards communicate each other through serial connection.

The Arduino UNO board has a DHT11 sensor (but you can use a more accurate DHT22 sensor) with a 10 kΩ resistor, a RTC module (from Adafruit: http://www.adafruit.com/products/264) and
a SD card module (from Adafruit: http://www.adafruit.com/products/254). This is a complete datalogger for temperature and humidity, infact it saves these two values with the time every 10 minutes (you can set this frequency in the sketch as you like, even if I suggest not less than 10 minutes because the file becomes too big) in a CSV file stored into the SD card. The sketch creates automatically the path for the log files using the path /templog/yy/mm/dd. You can read the logs putting the SD card into your pc's card reader.
The sketch provides some APIs for serial communication. The commands are:
1) sf& (size file):  the syntax is sf&yy/mm/dd?
   If you type this command on the serial monitor, the board will show the size of the file /templog/yy/mm/dd
2) rf& (read file): the syntax is rf&yy/mm/dd&startbyte&stopbyte?
   If you type this command on the serial monitor, the board will read the file /templog/yy/mm/dd FROM "startbyte" TO "stopbyte" and will send it through the serial port.
   So, if you want read the whole file, you must put startbyte = 0 and stopbyte = size of the file (in bytes). This syntax allows to split the file in several chunks, if it  is too big.

For example: the log for April 24th 2013 is in the file /templog/13/04/24, suppose its size is 2184 bytes. First you want to know the size of the file so you must type the command sf&13/04/24? on the serial monitor. The board will reply printing the value 2184. Then you can read the whole file typing the command rf&13/04/24&0&2184?


So: 
1 - connect the Arduino UNO board as shown in the figure below 
2 - create the "templog" folder in the SD card 
3 - insert the SD card in its module
4 - download the sketch test_comunicazione_seriale_UNO.ino from   https://github.com/rickit69/techrm/tree/master/test_comunicazione_seriale_UNO
5 - download the dht library from https://github.com/adafruit/DHT-sensor-library
6 - download the rtc library from https://github.com/adafruit/RTClib
7 - load the sketch

The board will start to save temperature, humidity, hour, minutes every 10 minutes. Use the serial monitor as explained above, to interrogate the board.



Wiring scheme




You can use the second board (Arduino MEGA 2560 with Weburban wifi module) to download the log files wireless:
1 - wire the board following the instructions in the article http://techrm.blogspot.it/2012/12/how-to-interface-arduino-mega-2560-to.html  and download the modified wireless     library from the same article
2 - download the sketch test_comunicazione_seriale_MEGA.ino from   https://github.com/rickit69/techrm/tree/master/test_comunicazione_seriale_MEGA

The sketch communicates serially to the UNO board through the Serial3 port. So wire the two boards following the scheme below:

MEGA                   UNO
pin 14         to         pin 0
pin 15         to         pin 1
GND          to         GND


Now open a shell and go in the folder where you downloaded the python script. These are the parameters for the script:

[techrm@notebook-dell test_comunicazione_seriale_python]$ python test.py -h
usage: test.py [-h] [-c COMMAND] [-y YEAR] [-m MONTH] [-d DAY] [-v]

optional arguments:
  -h, --help            show this help message and exit
  -c COMMAND, --command COMMAND
                        the command
  -y YEAR, --year YEAR  year (yy)
  -m MONTH, --month MONTH
                        month (mm)
  -d DAY, --day DAY     day (dd)
  -v                    verbose


In particular, -c is the parameter for the command. In the script the only command is "rf" (read file). For example, we want to download the log file for May 06th 2013 , so type
in the shell the command line   python test.py -c rf -y 13 -m 05 -d 06 
If you want to redirect the output to a file, type the command line   python test.py -c rf -y 13 -m 05 -d 06 > 13_05_06.txt




Finally, I wish to thanks my friend Mauro Pala for his suggestions about C language.