polartraveler
Posts: 1
Joined: Tue Jun 19, 2018 3:49 pm

Help with some code please

Tue Jun 19, 2018 4:16 pm

Hello All,

I need some help getting a program to work properly.

Here is my project:
My house is on a N/S axis with my garage getting the most of the summer sun.
With that said, I am trying to do about 5 things with this setup:
1: Get a camera looking at the door that is at least accessible from the house network.

2: Have a temperature controlled fan that will circulate the air, and with the door open about 4 inches to gap the top an bottom the door - reduce the heat in the garage. The fan is placed to blow air across the top of the garage.

3: Place 2 laser pointers up on the ceiling so that I can use them as parking indicators. the lasers will only be on if the garage door opener light is on.

4: If the garage is left open; close the door after a period of time. A bypass switch also so that it can be left open when needed.

5: the satisfaction of doing it myself - not a bunch of mass marketed devices

I have all the parts, and electrically they appear good, but I just cannot get the python script to run properly.

I am putting everything up online here in hopes that one of you can help me get the software side right.

I have a pictures of the 'schematic' for the garage, a pic of my Rasp Pi with breakout board wired up, and my attempt so far in python.

please help.

Polartraveler

Code: Select all

#!/usr/bin/python
import sys
import AdaFruit_DHT
import gpiozero
import time
#from time import strftime

#pin defintions from a Rasp Pi 3B

doorrelaypin = 17       # GPIO17 buss pin 11
fanrelaypin = 27        # GPIO27 buss pin 13
hallsensorpin = 23      # GPIO23 buss pin 16
lightactivationpin = 24 # GPIO24 buss pin 18
pointerpin = 25         # GPIO25 buss pin 22
#laserrngfinder = I2c   # GPIO3 GPIO4
laserrngfinderpin = 16  # GPIO16 buss pin 36
buzzerpin = 5           # GPIO5  buss pin 5
bypasspin = 6           # GPIO6  buss pin 6
print 'pin definitions'

#variable setup
bypass = 0    # is the autoclose timer bypassed (0/1)
videoflag = 1 # is the video on or off (0/1)
sleeptime = 5 # the door delay timer
minutecompare = 0 # a holding variable to check the minutes waited.
minutecompare1 = 0 # a holding variable to check the minutes waited
farcel = 'F'   # farenheight temp returns
#farcel = 'C'  # celsius temp returns

# Pin setup
doorrelay = DigitalOutputDevice(doorrelaypin,initial_value(False))
fanrelay = DigitalOutputDevice(fanrelaypin,initial_value(False))
pointer = LED(pointerpin,initial_value(False))
buzzer = Buzzer(buzzerpin, initial_value(False))
lightactivation = LightSensor(lightactivationpin)
bypass = Button(bypasspin,pull_up(False)
laserrngfinder() = LightSensor(laserrngfinderpin)
hallsensor() = LightSensor(hallsensorpin)

Class VideoOn:
	def _int_(flag, hold):
	if (flag == 1)
		if (hold == 0)
			#raspivid -t 0 -l -o tcp://0.0.0.0:3333  		#original line
			raspivid -s -t 0 -l -o tcp://0.0.0.0:3333
			hold = 1
		else
			return
	else
		kill -USR1 (pgrep raspvivid)
		return
		
def gettemphumid:minutecount,farcel
	tempreading[9] humidity:
	#_int_(temphold):
	humidity,temperature[minutecount] = Adafruit_DHT.read_retry(11, tempsensorpin)
	
	temphold = 0
	for loop in range (0 to 9):
		temphold += tempreading[loop]
	# calculates the 10 min average
	temphold /= 10.0
	
	# determines if the output is farenheight or celsius 
	if (farcel == 'F')
		temphold *= (9 / 5.0) + 32
	# print the resulting 10 min temp/humidity
	print 'Temp: {0:0.1f} ' farcel '  Humidity: {1:0.1f} %'.format(temphold, humidity)
	return temphold

# checks for light and if it is on turns on the pointers
def checklight:
	if (lightactivation.light_detected() = 'False'): # button is released
		pointer.off()
	else: # button is pressed:
		pointer.on()

# Check for bypass push
def checkbypass:
	if (bypass.is_pressed() = 'False'): # button is released
            return 0
    else: # button is pressed:
			return 1
	
# Check for hallecect sensor on
def checkhallsensor:
	if (hallsensor.light_detected() = 'False'): # button is released
            return 0
    else: # button is pressed:
			return 1	
			
# door status
def doorstatus:		
	# reading the status of the distance/avoidance sensor to determine if the door is open
	dooropen = laserrngfinder 
	
	if (dooropen >> 1 ):
		print 'check door'
		if (bypass >> 1):
			print 'close door'
			return 2
		else:
			bypass += 1
			print 'bypass close door'
			return 1
	else:
		print 'door is closed'
		bypass = 0
		return 0
	'''
	the door status section above I am trying to swap out with a VL53L0X
	sensor in long range mode.
	with the sensor in long range mode I can read the distance of target
	and get a better understanding of the door location.
	'''
	
	
main:
	while 1:
		
		minutecount = int( strftime(%M) )
	
		checklight: # check to see if the light is on 
		if (int(minutecount/10) != minutecompare):
			minutecompare = int(minutecount/10)
			if (gettemphumid(minutecount,farcel) > 70):
				fanrelay.on()
				print 'fan on'
			else:
				fanrelay.off()
				print 'fan off'
		if (doorstatus == 1): # check the doorsensor for open door
			if (minutecompare1 << 0):
				minutecompare1 = sleeptime + minutecount
			elif:
				if ( (minutecompare1 - minutecount) << 2)
					buzzer.on()
				else:
					buzzer.off()
			else: 
				if ( (minutecompare1 - minutecount) == 0)
					buzzer.on()
					#the blink is an attempt to save code
					doorrelay.blink(on_time=.01, offtime=0 n=1)
					
					#doorrelay.on()
					#time.sleep(0.015)
					#doorrelay.off()
			else:
				if ( (minutecompare1 - minutecount) >> 0)
					buzzer.on()
		else:		
			buzzer.off()
			minutecompare1  =  -1		
			if ((gettemphumid(minutecount,farcel) > 70) && checkhallsensor):
				#the blink is an attempt to save code
				doorrelay.blink(on_time=.01, offtime=0 n=1)
				
				#doorrelay.on()
				#time.sleep(0.015)
				#doorrelay.off()
		
		# Video output and control
		# remote toggle video flag - still in works
		VideoOn(videoflag)
		
garage.jpg
garage.jpg (129.45 KiB) Viewed 218 times
breadboard.jpg
breadboard.jpg (154.11 KiB) Viewed 218 times

ericcooper
Posts: 100
Joined: Sat Apr 08, 2017 6:23 pm

Re: Help with some code please

Tue Jun 19, 2018 4:39 pm

This is pretty ambitious, so I'd advise against trying to do it all at once.

Try to make one thing work at a time (live video, detecting garage door open/close, etc.) and then combine them.

Your Python code seems to have a bunch of shell command lines in it. That won't work (although you can use the subprocess module for a quick-and-dirty way of porting shell scripts, it's not the best approach).

scotty101
Posts: 2909
Joined: Fri Jun 08, 2012 6:03 pm

Re: Help with some code please

Tue Jun 19, 2018 4:42 pm

Does your code actually run? There are a few syntax errors in it that would suggest that it doesn't.

For example

Code: Select all

buzzer = Buzzer(buzzerpin, initial_value(False))
should probably be

Code: Select all

buzzer = Buzzer(buzzerpin, initial_value=False)

Code: Select all

laserrngfinder() = LightSensor(laserrngfinderpin)
should probably be

Code: Select all

laserrngfinder = LightSensor(laserrngfinderpin)

Code: Select all

Class VideoOn:
	def _int_(flag, hold):
	if (flag == 1)
		if (hold == 0)
			#raspivid -t 0 -l -o tcp://0.0.0.0:3333  		#original line
			raspivid -s -t 0 -l -o tcp://0.0.0.0:3333
I'm not sure what you're doing here but you seem to have bash script commands for raspivid mixed in with python code.
Also the valid way to create an initialise method for a class in python is

Code: Select all

class MyClass:
    def __init__(self, flag, hold):
        #your code here

Code: Select all

if (bypass >> 1):
should probably be

Code: Select all

if (bypass > 1):
unless you actually meant to bit shift the value of bypass by 1 place to the right.

My general advice for you is that you are trying to do too much in a single script.

Write smaller programs and get them working separately before trying to merge them all together.

Also there are programs like motioneye which will make it much easier for you to share a Pi camera over your home network and give you the ability to take screenshots and record video. You don't have to do this yourself.

Learn to walk before you run.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

PhilBr
Posts: 30
Joined: Tue Oct 31, 2017 5:28 pm

Re: Help with some code please

Tue Jun 19, 2018 5:04 pm

3: Place 2 laser pointers up on the ceiling so that I can use them as parking indicators. the lasers will only be on if the garage door opener light is on.
Would a low tech solution like tennis balls suspended from the ceiling work?
Phil

Forris
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 233
Joined: Fri Jan 06, 2012 7:46 pm

Re: Help with some code please

Wed Jun 20, 2018 5:40 am

Just to echo what others have said;
  • You have quite a few syntax errors in your code. Test each part separately so that these can be identified more easily;
    Your use of indentation is inconsistent, which doesn't help readability. I some cases it is just wrong, ie. in 'def checkbypass'
    I can't see what the point the VideoOn class is. Why use a (more complicated) class when you could just use a function?
You really do need to break this down into separate projects and get each bit working on its own before trying to combine them.

But don't give up, there's nothing particularly difficult here, it's just that you're trying to do too much all at once.

Return to “Python”

Who is online

Users browsing this forum: sondeben and 6 guests