Page 1 of 1

Python with dallas temp and photocell sensors

Posted: Wed Sep 10, 2014 7:18 am
by pasivnyprijem
Hi Im readin values from 2 dallas temp sensors and one photocell and writing this data to a mysql database. The issue is when I run the script after a while it crashes with the error:

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)

Re: Python with dallas temp and photocell sensors

Posted: Wed Sep 10, 2014 5:20 pm
by davef21370
I think the culprit lies here...

Code: Select all

def read_temp():
   lines = read_temp_raw()
   while lines[0].strip()[-3:] != 'YES':
      time.sleep(0.2)
      lines = read_temp_raw
...if read_temp_raw doesn't return anything lines[0] won't exist and therefore won't have a getitem function, hence the error.
Try putting "lines = read_temp_raw()" inside try/except to catch the error.

Dave.

Re: Python with dallas temp and photocell sensors

Posted: Wed Sep 10, 2014 6:39 pm
by billio
Is it not something to do with
lines =read_temp_raw
this is assigning the function read_temp_raw to lines, not a list of text lines returned by read_temp_raw.
The error message is telling you that lines contains a function, not a list .