Traceback (most recent call last):
File "thermo.py", line 92, in <module>
print insert_data()
File "thermo.py", line 77, in insert_data
outdoor=read_temp_2()
File "thermo.py", line 65, in read_temp_2
while lines[0].strip()[-3:] != 'YES':
TypeError: 'function' object has no attribute '__getitem__'
this error happens after 8-9 reading and the last photocell readin is much lower than the others (maybe its related with the errro)
.
Code: Select all
import os
import glob
import time
import MySQLdb
import time
import RPi.GPIO as GPIO
DEBUG = 1
GPIO.setmode(GPIO.BCM)
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28-0000060852e6')[0]
device_file = device_folder + '/w1_slave'
device_folder_2 = glob.glob(base_dir +'28-000006455868')[0]
device_file_2 = device_folder_2 + '/w1_slave'
db = MySQLdb.connect("localhost", "x", "x", "mysql")
cursor = db.cursor()
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
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp_raw_2():
f_2 = open(device_file_2, 'r')
lines_2 = f_2.readlines()
f_2.close()
return lines_2
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
return temp_c
def read_temp_2():
lines = read_temp_raw_2()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw_2
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
def insert_data():
indor=read_temp()
outdoor=read_temp_2()
sun=RCtime(18)
sql = """INSERT INTO temp_outside (temp,tempinside,sun) VALUES (%s, %s, %s)"""
try:
cursor.execute(sql, (indor, outdoor, sun))
db.commit()
except:
time.sleep(10)
cursor.execute(sql, (indor, outdoor, sun))
db.commit()
return sun
while True:
print insert_data()
time.sleep(10)