Raspberry Pi temperature logging using DS18B20 and Google Docs

The Raspberry Pi does not, unfortunately, have an ADC (Analog-to-Digital converter) and this excludes most off the shelf temperature sensors as they return an analog signal. Options for temperature sensing on the Pi are to use a separate ADC between the sensor and the Pi or use a digital temperature sensor. The DS18B20 digital temperature sensor is available from Adafruit, and comes in standard, waterproof, and high temperature versions. Using some of the code from Adafruit's tutorials, I adapted my sensors to take temperature readings, then push these to a Google Docs spreadsheet and publish a graph (see below).


The hardware requirements are very basic. I used a Adafruit T-Cobbler, breadboard and jumpers, a standard DS18B20 and a waterproof DS18B20 and a 4.7K resistor. It is quite simple to adapt the setup for only one sensor, instead of two. Obviously you will also need a RasPi with internet connectivity.

The sensors are fairly unique in that they can be wired in parallel, and as many sensors as desired can be added to the system. The sensors communicate using a single wire serial interface and Rasbian will identify the sensors as connected serial devices. However, the sensor must be wired to pin4 on the GPIO as this is the only pin which (currently) allows communication using the one wire serial protocol.

The wiring diagram is -

And in reality -

To check that the sensor it working, open a terminal then enter the following.

You should see an output of two lines of text. The first line will say YES or NO at the end. If YES, then the measured temperature will be at the end of the second line, in 1/000 degrees C. If a NO appears on the end of the first line, the sensor is still working, it just failed to communicate with the Pi correctly. Reenter cat w1_slave and it should give a YES.

Setting up a Google Docs Spreadsheet

Before the code can be run a speadsheet must be created for the code to write to. The process is trivial - Go to Google Docs and create a new Spreadsheet. Then rename the spreadsheet to something like Temperature_Log. Now delete all the rows in the spreadsheet except the first and name the rows as shown.

Python Code

Before the code will work, a python library is required which lets Python talk to the Google spreadsheet. The library is called gspread. To get the library enter the following at the terminal -

Next, create a python file with the code and save as Temp.py -

You will need to enter your actual gmail address and password into the code. If you have two factor authentication on your gmail account, you will need to get an application specific password and enter that as your password instead. Now you should be able to run your program and watch it update the spreadsheet.

Automatically run on startup as a background process

Now, if your anything like me, you will want the program to automatically start when the Pi boots up, and run in the background. Getting the program to do this takes a few more steps.

Turn off power management

The RasPi is automatically set to go to sleep after one hour of inactivity, which means that your program will stop logging. To stop the sleep mode you need to edit the file - /etc/kbd/config. Change  BLANK_TIME= to BLANK_TIME=0 and POWERDOWN_TIME= to POWERDOWN_TIME=0 then save and reboot. 

Make your python script executable

Now you need to tell Linux that the python script you created is an executable program. Enter the following at a terminal -

The second line copies the program to the /usr/local/bin/ directory and this lets you execute your script from any directory by simply typing Temp.py at the terminal.

Run at startup

I found the instructions for making a file execute on startup on this post - Stuff about Code

The process is quite simple, you need to create a script file which calls your program. Open a terminal and type - sudo nano /etc/init.d/Run_Temp

This will open a black text file in the the nano text editor. Enter the following, then save and exit -

A point of interest here, when the script calls the python program - line 20: /urs/local/bin/Temp.py & the "&" at the end of the line tells Linux to run the script as a background process. Without the "&" the terminal would not return to the user for input. To see the program running in the background at any time, run - ps axg at a terminal to see a list af all running processes.  

Now make the script executable - sudo chmod +x  /etc/init.d/Run_Temp

Now test that everything is working - sudo /etc/init.d/Run_Temp start
and stop it again - sudo /etc/init.d/Run_Temp stop

and finally register the program to start at boot - sudo update-rc.d Run_Temp defaults

and done. Now you can reboot the Pi and watch your python script start. It appears as a line item during the boot process. If it fails to execute properly, you will get the message "fail" prior to the login promt.