Ik ben wat aan het prusten met mijn Raspberry Pi en een Temp sensor. Ik volgde deze handleiding om de temperatuur uit te lezen en op te slaan in een Google spreadsheet: http://www.danielhansen.net/2013/03/ras ... using.html
Bij die persoon werkt zijn setup met 2 temp sensors, ik heb er maar eentje. Heb dus de code wat aangepast maar krijg toch telkens een foutmelding.
Dit is mijn huidige code
Code: Selecteer alles
#!/usr/bin/python3
import os, glob, time, gspread, sys, datetime
#Google account details
email = '[email protected]'
password = 'mijn.wachtwoord'
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
device_folder = glob.glob('/sys/bus/w1/devices/28-0000057bdd14')
device_file = [device_folder[0] + '/w1_slave']
def read_temp_raw(): #a function that grabs the raw temperature data from the sensor
f_1 = open(device_file[0], 'r')
lines_1 = f_1.readlines()
f_1.close()
return lines_1
def read_temp(): #a function that checks that the connection was good and strips out the temperature
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
temp = float(lines[1][equals_pos[0]+2:])/1000
return temp
while True: #infinite loop
temp = read_temp() #get the temp
values = [datetime.datetime.now(), temp[0]]
worksheet.append_row(values) #write to the spreadsheet
time.sleep(600) #wait 10 minutes
Code: Selecteer alles
Traceback (most recent call last):
File "./temp.py", line 45, in <module>
temp = read_temp() #get the temp
File "./temp.py", line 41, in read_temp
temp = float(lines[1][equals_pos[0]+2:])/1000
TypeError: 'int' object is not subscriptable
Code: Selecteer alles
temp = float(lines[1][equals_pos[0]+2:])/1000