2017-10-04 21 views
-1

나는 다른 pir 센서 스크립트를보고 색상 줄 집합을 제어하여이 비트를 함께 던졌습니다. 그것을 아마 백만 가지 방법으로 더 나은 방법이 있지만 여기에 메신저입니다.pir 모션 센서 용 간단한 파이썬 스크립트

문제는이 스크립트가 내 전화 앱에서 말하는 것보다 우선합니다. 휴대 전화의 표시등을 켜고 센서 옆에 움직임이 없으면 1 초 또는 2 초 후에 표시등이 꺼집니다. 이것은 피하고 싶습니다, 나는이 스크립트가 다른 곳의 컨트롤에 의해 오버런 될 수 있기를 바랍니다. 누군가가 나를 도와 줄래?

편집; 30 분 동안 빛이 계속 켜져 있기를 원하기 때문에 time.sleep (60 * 30)이 사용됩니다. 이 어딘가에 더 좋은 해결책이 있을까요?

실행되는 두 개의 스크립트는 조명을 켜거나 끄기 위해 색조 브리지에 알려주는 스크립트입니다. 그 이유가 무엇인지 알 필요가 있으면 게시 해 드리겠습니다.

import RPi.GPIO as GPIO 
import time 
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(15, GPIO.IN) 
while True: 
i=GPIO.input(15) 
caseCommand = getinput() 
if (i==0) and (caseCommand == 0): 
    print "No movement detected - Turning lights off",i 
    exec(open("./LightsOff.py").read(), globals()) 
    time.sleep(1) 
if (i==1) or(caseCommand == 1): 
    print "Movement detected - Turning lights on",i 
    exec(open("./LightsOn.py").read(), globals()) 
    time.sleep(60 * 30) 

편집 : 코드가 위와 같습니다. 그러나 그 기능을 어떻게 쓰는지 확실하지 않습니다. 분명히 위의 내용은 저에게이 함수에 대한 에러를줍니다 ..

답변

0

프로그램이 정확히 무엇을해야하는지 !!!

운동이 있다면 (i = GPIO.input (15)) 빛을 켜고 움직이지 않으면 빛을 끕니다 !!!

if 문에 몇 가지 여분의 사례를 넣어야합니다. 예

:

여기
caseCommand = getinput() # getinput() is a function you should write, it should get the input from your phone or whatever and can return 0 or 1 

# your if statement should change 
if (i==0) and (caseCommand == 0): # both have to be 0 to turn off 
# turn lights off 
if (i==1) or(caseCommand == 1): # any one of them should turn the light on 
# turn light on 

좀더 상세한 예이다

작성에 붙어
import RPi.GPIO as GPIO 
import time 
GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(15, GPIO.IN) 

def getinput(): 
    path = "pathtoyourfile\yourfilename.txt" 
    return_valu= "" 
    with open(path ,"r") as f: 
     for l in f: 
      return_value = l #it will get the last line incase you have more than one line 
    return return_value 


while True: 
    i=GPIO.input(15) 
    caseCommand = getinput() 
    if (i==0) and (caseCommand == 0): 
     print "No movement detected - Turning lights off",i 
     exec(open("./LightsOff.py").read(), globals()) 
     time.sleep(1) 
    if (i==1) or(caseCommand == 1): 
     print "Movement detected - Turning lights on",i 
     exec(open("./LightsOn.py").read(), globals()) 
     time.sleep(60 * 30) 

    # add a time.sleep 2 or 3 second so while loop takes a break and you can write on the file 
    time.sleep(2) 
+0

기능했다. 위의 편집 된 게시물 코드 ... – Kimzr

+0

간단한 예 : 바탕 화면에 텍스트 파일을 만들고, getinput() 함수에서 텍스트를 읽고, 텍스트 파일에 1을 쓰면 점등해야하고, 0은 꺼야합니다 –