Light and Temp logging to Google Doc`s

This is a quick one which pulls in two off previous tutorials the photocell one comes from Adafruit and the Temp is mine..

Photcell and DS18B20 (temp logging and upload to google doc`s).

I combined the code so both the light values and temp readings were both being uploaded to the spread sheet. In order for the upload to google doc`s to work please follow the instructions on the DS18B20 tutorial.

If you want to code to run unattended just put a & at the end and it will create and give you a process ID. One note – you must have the spreadsheet open on a machine , it will not log the data otherwise.


#!/usr/bin/python3

import os, glob, time, gspread, sys, datetime , RPi.GPIO as GPIO

DEBUG = 1
GPIO.setmode(GPIO.BCM)

#Google account details
email = 'your gmail address'
password = 'your gmail password'
spreadsheet = 'the name of your spread sheet' #the name of the spreadsheet already created

#attempt to log in to your google account
try:
    gc = gspread.login(email,password)
except:
    print('fail')
    sys.exit()

#open the spreadsheet
worksheet = gc.open(spreadsheet).sheet1

#initiate the temperature sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

#set up the location of the sensor in the system
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
print ("Code is running")

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c

def RCtime (RCpin):
        reading = 0
        GPIO.setup(RCpin, GPIO.OUT)
        GPIO.output(RCpin, GPIO.LOW)
        time.sleep(0.1)

        GPIO.setup(RCpin, GPIO.IN)
        # This takes about 1 millisecond per loop cycle
        while (GPIO.input(RCpin) == GPIO.LOW):
                reading += 1
        return reading

# Return CPU temperature as a character string
def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    return res

while True: #infinite loop
    print ("loop is running")
    tempin = read_temp() #get the temp sensor
    temppi = getCPUtemperature() #get the pi temp
    light = RCtime(18) #get light value
    values = [datetime.datetime.now(), tempin , temppi , light]
    worksheet.append_row(values) #write to the spreadsheet
    #print(values)
    time.sleep(300) #wait 5 minutes
  

Temp recording and upload live to Google Doc`s

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.

Ingredients 

1. DS18B20 digital temperature sensor

2. 4.7k Resistor

3. Couple of jumper cables

4. Google Account

breadboard-ic

 

Follow the diagram to wire up the sensor and resistor. To test the sensors is working follow this guide from Adafruit`s site.

 

 

Pre heat

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

wget https://pypi.python.org/packages/source/g/gspread/gspread-0.0.15.tar.gz
tar -zxvf gspread-0.0.15.tar.gz
cd gspread
sudo python setup.py install

Once this is done here is the code to make it all work.

#!/usr/bin/python3
#
import os, glob, time, gspread, sys, datetime # all normal imports and the gspread module
#
#Google account details
email = 'foobar@gmail.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
try:
gc = gspread.login(email,password)
except:
print('fail')
sys.exit()
#
#open the spreadsheet
worksheet = gc.open(spreadsheet).sheet1
#
#initiate the temperature sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
#
#set up the location of the sensor in the system
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
#
#
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
#
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c
#
# Return CPU temperature as a character string
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return res
#
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
print(values)
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.

gdriveputty

 

 

 

 

 

References.

Adafruits Tutorial on DS18B20 

Daniel Hansen Tutorial on DS18B20