This project is a mismatch off a couple of project on-line at the moment . I will reference all the sites where I got the information from. In a nutshell this project takes the temperature from a DS18B20 sensor. This is a great piece of kit as it only needs 3 leads attached to it and 2 off those are 3.3v and ground. The resistor is used to pump up the voltage on the output pin to keep the Pi happy. Because the digital sensor is accessed by it serial number it means more sensors can be added and no extra resistors just extra cables. The idea of this project was to explorer putting live data on-line. In fact my 9 year son came up with an idea for this project. Dad he said ” we could put this in nanny`s house and keep an eye on her temp in the house and even link up some LED`s too” . So I can see this becoming a stand alone project. So have a read try it out and let`s get cooking.
1. DS18B20 digital temperature sensor
2. 4.7k Resistor
3. Couple of jumper cables
4. Google Account
Follow the diagram to wire up the sensor and resistor. To test the sensors is working follow this guide from Adafruit`s site.
First you`ll need to create a blank spreadsheet on google drive. Rename it what you want I called mine Temperature_Log in cell A1 type Date/Time , B1 Temp In and C1 Temp Pi . you can call these what ever you like. Then delete all the other rows. If you do not your data will be entered right at the bottom.
Next we need to download a new python module called gspread . code from Daniel Hansen
tar -zxvf gspread-0.0.15.tar.gz
sudo python setup.py install
Once this is done here is the code to make it all work.
import os, glob, time, gspread, sys, datetime # all normal imports and the gspread module
#Google account details
email = 'email@example.com'# your google email
password = 'foobar`s password'# your google password
spreadsheet = 'Temperature_Log' #the name of the spreadsheet already created
#attempt to log in to your google account
gc = gspread.login(email,password)
#open the spreadsheet
worksheet = gc.open(spreadsheet).sheet1
#initiate the temperature sensor
#set up the location of the sensor in the system
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')
device_file = device_folder + '/w1_slave'
f = open(device_file, 'r')
lines = f.readlines()
lines = read_temp_raw()
while lines.strip()[-3:] != 'YES':
lines = read_temp_raw()
equals_pos = lines.find('t=')
if equals_pos != -1:
temp_string = lines[equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
# Return CPU temperature as a character string
res = os.popen('vcgencmd measure_temp').readline()
while True: #infinite loop
tempin = read_temp() #get the temp sensor
temppi = getCPUtemperature() #get the pi temp
values = [datetime.datetime.now(), tempin , temppi]
worksheet.append_row(values) #write to the spreadsheet
time.sleep(600) #wait 10 minutes
Hopefully if all this worked you should see the temp from the sensors , temp of the CPU and time and date in your SSH connection and live on your Google spread sheet.