Python程序没有产生预期的结果

时间:2018-02-02 17:31:03

标签: python raspberry-pi3

我有一个覆盆子pi 3,没有任何东西连接到它的GPIO引脚(标头引脚)

我添加了有用的注释来帮助理解我的短python程序

我对这个python程序的期望(现在):

由于GPIO引脚没有连接,0应写入alarm.txt

发生了什么:

1正在写入alarm.txt。 (查看下面的截图)

我的Python程序:

from subprocess import call
import RPi.GPIO as GPIO

#Note the BOARD Mode
GPIO.setmode(GPIO.BOARD)

#Alarm Sensors
GPIO.setup(8,GPIO.IN)   #Fire Sensor
GPIO.setup(10,GPIO.IN)  #Motion Sensor
GPIO.setup(12,GPIO.IN)  #Gas Sensor

#Relay
GPIO.setup(37,GPIO.OUT) #Red Alarm Bulb
GPIO.setup(35,GPIO.OUT) #White Normal Bulb

#Keypad Columns
GPIO.setup(22,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)

#Keypad Rows
GPIO.setup(36,GPIO.IN)
GPIO.setup(32,GPIO.IN)
GPIO.setup(26,GPIO.IN)
GPIO.setup(24,GPIO.IN)

#Initializing Keypad Columns
GPIO.output(22,GPIO.LOW)
GPIO.output(18,GPIO.LOW)
GPIO.output(16,GPIO.LOW)

#Initializing Relay
GPIO.output(37,GPIO.LOW)
GPIO.output(35,GPIO.LOW)

#Global variables to update it inside conditional statements
global i
i=0         #Just an incrementer
global chkalrm
chkalrm='0'     #No 'alarm' in beggining
global unlock       
unlock='0'      #Locked in beggining
passorig=[2,0,1,8]  #Original Password
global passcode
passcode=[0,0,0,0]  #To store input password from keypad
global prevpass
prevpass=passcode   #To prevent duplicate fast input when keypad pressed

#Writing '0' to 'unlock' and 'alarm' file
with open('/home/pi/Documents/HAP/unlock.txt','w') as f1:
    f1.write(unlock)
with open('/home/pi/Documents/HAP/alarm.txt','w') as f2:
    f2.write(chkalrm)

#Infinite Loop
while(True):

    #Read 'unlock' file which can be changed by other programs
    with open('/home/pi/Documents/HAP/unlock.txt','r') as f3:
        unlock=f3.read()

    #Choosing different type of alarm(chkalrm) to be written in 'alarm' file
    if(GPIO.input(8)):
        chkalrm='1' #Choose '1' in case of fire sensed
    elif unlock=='0':
        if(GPIO.input(10)):
                chkalrm='2' #Choose '2' in case of motion sensed when locked
    elif(GPIO.input(12)):
        chkalrm='3' #Choose '3' in case of gas sensed
    else:       #Take password input from keypad when no alarm triggered

    #Taking input from 4x3 keypad
        GPIO.output(22,GPIO.HIGH)
        if(GPIO.input(36)):
            passcode[i]=1
            i+=1
        elif(GPIO.input(32)):
            passcode[i]=4
            i+=1
        elif(GPIO.input(26)):
            passcode[i]=7
            i+=1
        GPIO.output(22,GPIO.LOW)

        GPIO.output(18,GPIO.HIGH)
        if(GPIO.input(36)):
            passcode[i]=2
            i+=1
        elif(GPIO.input(32)):
            passcode[i]=5
            i+=1
        elif(GPIO.input(26)):
            passcode[i]=8
            i+=1
        elif(GPIO.input(24)):
            passcode[i]=0
            i+=1
        GPIO.output(18,GPIO.LOW)

        GPIO.output(16,GPIO.HIGH)
        if(GPIO.input(36)):
            passcode[i]=3
            i+=1
        elif(GPIO.input(32)):
            passcode[i]=6
            i+=1
        elif(GPIO.input(26)):
            passcode[i]=9
            i+=1
        GPIO.output(16,GPIO.LOW)

        if prevpass!=passcode:  #Prevent duplicate fast input by delay for 1 sec
            time.sleep(1)

        prevpass=passcode   #Getting ready for next input

    #Out of if-elif-else condition but inside while loop
    GPIO.output(22,GPIO.HIGH)
    if(GPIO.input(24)):     #Stop Alarm
        chkalrm='0'
    GPIO.output(22,GPIO.LOW)

    GPIO.output(16,GPIO.HIGH)
    if(GPIO.input(24)):     #Proceed (after entering password)
        i=0         
        passcode=[0,0,0,0]  
        if passcode==passorig:
            unlock='1'      #Unlock when password matched
    GPIO.output(16,GPIO.LOW)

    if chkalrm != '0':
        GPIO.output(35,GPIO.HIGH)   #Switch ON red bulb when alarm
    else:
        GPIO.output(35,GPIO.LOW)

    if unlock == '1':
        GPIO.output(37,GPIO.HIGH)   #Switch ON White bulb when unlocked
    else:
        GPIO.output(37,GPIO.LOW)

    #Finally writing to 'unlock' and 'alarm' file
    with open('/home/pi/Documents/HAP/unlock.txt','w') as f4:
        f4.write(unlock)
    with open('/home/pi/Documents/HAP/alarm.txt','w') as f5:
        f5.write(chkalrm)

    #Executing alarm based on content of 'alarm' file
    call("/home/pi/Documents/HAP/alarm.sh")

如果你很好奇我的Bash脚本:

#!/bin/bash

#Read first character of alarm file
count=$(head -c 1 ~/Documents/HAP/alarm.txt)

if [ "$count" == "1" ]  #If file content is "1"
then
if ! pgrep -x "aplay" > /dev/null   #If "aplay" service not already running
then                    #If another alarm is not ON already
aplay ~/Music/fire.wav &        #Play Fire Alarm
fi
elif [ "$count" == "2" ]
then
if ! pgrep -x "aplay" > /dev/null
then
aplay ~/Music/intruder.wav &
fi
elif [ "$count" == "3" ]
then
if ! pgrep -x "aplay" > /dev/null
then
aplay ~/Music/gas.wav &
fi
else
pkill aplay #Kill aplay service
fi

在python程序上面运行后,屏幕截图显示了alarm.txt的内容(左下角的注意事项):

enter image description here

1 个答案:

答案 0 :(得分:1)

配置引脚时,在所需输入上配置下拉电阻:

GPIO.setup(<pin>, <mode>, pull_up_down=GPIO.PUD_DOWN)