Apr-03-2018, 09:01 AM
import RPi.GPIO as GPIO import time from datetime import datetime from twilio.rest import Client GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(7,GPIO.IN) #LDR (Light Dependent Resistor) GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #top switch GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # bottom switch GPIO.setup(15,GPIO.OUT) # Door Open GPIO.setup(13,GPIO.OUT) # Door Close def send_sms(sms_text="Hello from Python!"): twilio_client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz") client.messages.create(to="+19732644152", from_="+12023351278", body=sms_text) def operate_door(channel): if channel == 7: if not GPIO.input(7) and not GPIO.input(11): # it's day and door is not open print ("Day. Open door") print ("Start motor. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f'))) GPIO.output(15, True) while True: if GPIO.input(11): GPIO.output(15,False) print ("STOP. Door open. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f'))) break #send_sms("sms_text='Door opened'") elif GPIO.input(7) and not GPIO.input(12): # it's night and door is not closed print ("Night. Close door") print ("Start motor. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f'))) GPIO.output(13, True) while True: if GPIO.input(12): GPIO.output(13,False) print ("STOP. Door closed. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f'))) break #send_sms("sms_text='Door opened'") # add events detection, using separate thread GPIO.add_event_detect(7, GPIO.BOTH, callback=operate_door, bouncetime=200) delay = 1 # 1 sec. delay print('Start monitoring') while True: try: #time.sleep(delay) # you may change this or replace it with just pass pass #uncomment this if you decide to remove time.sleep above except KeyboardInterrupt: print('Stop monitoring and quit')For the testing I also commented out the time.sleep line in order to avoid the 1 s delays while testing
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs