Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing a parking sensor with rasberry pi and python this is the code :
Python
import RPi.GPIO as GPIO
import time
#from picamera import PiCamera
from time import sleep
from gpiozero import MotionSensor
import smtplib

sender = '*****@gmail.com'
reciever = '*****@gmail.com'

def BlueLED (): #Blue LED Function 

    GPIO.output(27, GPIO.HIGH)
    time.sleep(3)
    GPIO.output(27, GPIO.LOW)


def RedLED (): #Red LED Function

    GPIO.output(22,GPIO.HIGH)
    time.sleep(3)
    GPIO.output(22, GPIO.LOW)

def Buzzer (): #Buzzer Function 

    GPIO.output(17, GPIO. HIGH)
    time.sleep(3)
    GPIO.output(17, GPIO.LOW)


def email(sender,reciever,msg):
    try :
        server = smtplib.SMTP('smtp.gmail.com',587)
        server.ehlo()
        server.starttls()
        server.login(sender,'******')
        server.sendmail(sender,reciever,msg)
        server.close()

        print('Email sent!')

    except :
        print('Error')
    
try :

    GPIO.setmode(GPIO.BCM)
    #camera = PiCamera()
    pir = MotionSensor(4)
    GPIO.setwarnings(False)
    
    
    GPIO.setup(27, GPIO.OUT) #blueLED
    GPIO.setup(22, GPIO.OUT) #redLED
    GPIO.setup(17, GPIO.OUT) #buzzer
    GPIO.setup(18, GPIO.OUT) #tempsensor

    GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP) #entry button

    count = 0

    while True :

        if (pir.motion_detected):
            print('Motion Detected')

            #Calling the buzzer function 
            #Buzzer()

            #The content that is going to be sent via email 

                       
            msg = """Subject : Car Park 

            (Picture) """

            email(sender,reciever,msg)

          
    

            print('\nPlease press the button for the gate to open')

         

            while True :

                if(GPIO.input(21) == False):
                    if (count < 5):
                        BlueLED()
                        print('\nThere are ',(5-count), ' parking spaces empty ')

                    else :
                        RedLED()
                        print('\nSorry but the parking is full')
                        
                    count = count + 1

                   

except Exception as ex :
    print('Error occured',ex)


My problem is that the first while loop is not working, i.e if the motion sensor is triggered nothing happens yet you can repress the button and the count is increased. I'm guessing there is an easy solution to this but non seem to come to mind. Would love your help, thanks

What I have tried:

Tried to the button first and it work but was not that logical correct in my opinion
Posted
Updated 15-May-17 3:53am
v3
Comments
Richard MacCutchan 13-May-17 12:36pm    
What does "not working" mean? We cannot see your screen so have no idea what happens here, or what button you are pressing.
[no name] 13-May-17 13:05pm    
I don't know python, but for me it Looks like you should break the inner Loop after RedLED(). Only a guess :)
[no name] 14-May-17 4:22am    
h

1 solution

As already noted by 0x01AA:
The inner while loop is never left once a motion has been detected.

To be more precise:
Your outer loop is working and looping until a motion is detected. Then you have another infinite while loop which prevents returning to the outer loop.

So you have to remove the inner while loop statement or leave the loop using a break[^] statement.

You should avoid using infinite loops without break conditions. When using such, you can only use a single one within an application because it can be only left by killing the application.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900