By: Garden4Dinner

The reason I wanted a Raspberry Pi initially was to log temperature. My small 6ft x 8.5ft garden room has such varying temperatures that my heat mat thermostat can’t keep my seedling warming mat at a stable temperature. In the evenings the temperatures get low and when the sun is shining it is very warm. I needed to get a handle on what the temperature is in the room so that I can stabilize it.

There are many other temperature loggers out there. I was unable to find one within a similar price range that functioned the same way. Additionally, I plan to use the Raspberry Pi for other automation also. The less expensive temperature loggers I found were generally USB dongles, which I worry would measure some of the computer temperature in the reading because of the proximity. I would also need an additional machine to plug the dongle into. I decided on the DHT22 temperature sensor . Another benefit to the DHT22 is that it is a humidity sensor also, which I think will be useful in the future. Plus, did I mention putting it together would be fun? I do have electrical and software engineering experience. If I had no experience at all, I would have purchased something already made.

Putting everything together was surprisingly easy. I started with my Raspberry Pi that I already set up and configured. I will list out my resources and steps to set up the temperature and humidity logging next.

Useful websites:

temperature and humidity logging with raspberry pi My Raspberry Pi and DHT22 Temperature and Humidity Sensor Hooked Up

temperature and humidity logging with raspberry piHow I connected my Raspberry Pi (GPIO pinout picture from http://www.raspberrypi.org). The green line is connected to pin 4 (not pin 14)

After using those four websites to hook everything up and install the default software, it needed to be configured. I started by following the instructions in the readme file included in the software I downloaded. My pinout above shows pin 4 used for GPIO, but it would have been easier if I used pin 23 instead, because it is the default configured pin in the software.

In the examples directory I ran simpletest.py after I edited the file to have the right GPIO pin configuration. I had to comment out the beagleboard pin, uncomment the raspberry pi pin, and change it to the pin I plugged it into which was 4. Then I ran it – and tada – I had temperature and humidity output on the console. To run it:

python simpletest.py

Google Spreadsheet Logging

Currently, I use the google spreadsheet example with some minor edits. It is great because I can bring up the spreadsheet on my phone from anywhere and check the current and past temperature. I do that twice a day lately. To use the google spreadsheet example I followed the instructions in the beginning of the file google_spreadsheet.py

As part of the instructions at the beginning of the file, I changed DHT_PIN to the pin I have hooked up (from 23 to 4). Additionally, I generated credentials using Google’s instructions. I had to create a new project under the google spreadsheet API.

A part that I didn’t see documented was that when I was creating my service account key, it forced me to choose a Service account. I selected Compute Engine default service account and that worked for me.

When I set up the GDOCS_OAUTH_JSON variable I put the entire path to the json file so that when I run it from a directory other than the examples directory, it finds the file. My variable looks like this:

GDOCS_OAUTH_JSON       = '/home/pi/git_repository/Adafruit_Python_DHT/examples/Filename.json'

Where Filename.json is my filename.

Logging Fahrenheit

The default temperature is logged in Celsius and I wanted Fahrenheit. To change add it, I added these two lines:

fahrenheit = 9.0/5.0 * temp + 32
print('Temperature: {0:0.1f} F'.format(fahrenheit))

placed those 2 lines under the following lines:

print('Temperature: {0:0.1f} C'.format(temp))
print('Humidity:    {0:0.1f} %'.format(humidity))

Additionally, I had to change this line:

worksheet.append_row((datetime.datetime.now(), temp, humidity))

To this:

worksheet.append_row((datetime.datetime.now(), temp, humidity, fahrenheit))

My other problem is that it was adding 1000 blank rows before the temperature logging.

After some research, I found the solution is to delete the 1000 blank rows. After I figured out how to do all of this I found this website that looks like it contains all of the steps I went through in more details.

temperature and humidity logging with raspberry piA screen shot of my temperature logger showing Fahrenheit starting at line 7

Execute Script on Boot Up

To start up automatically if I reboot the machine I added this:

(sleep 10;python /home/pi/git_repository/Adafruit_Python_DHT/examples/google_spreadsheet.py)&

To the end of the /etc/rc.local file before the exit 0 command.

Overall Experience

I didn’t time how long this took me, but I was amazed that it took me less than 2 hours. I do have electronic, Linux, and software experience. For a newbie, I would expect for it to take longer learning how to do it all. It is a fun project and I am looking forward to adding additional sensors for gardening in the future.

It is great to be able to check the temperature on my phone easily with Google Spreadsheets at any time of the day and see how much it fluctuates throughout the day!


Materials:
(All of the materials I received as a gift as part of my Raspberry Pi and are listed as $0)

Total cost: $0 (If parts were purchased I would have spent $20.97.)
Total time: less than 2 hours

Do you have any automation in mind for your gardening? Would a temperature and/or humidity logger be useful for your gardening? What would you use it for?

[Image Credit: ©2017 Garden4Dinner]

Advertisements