Posts: 22
Threads: 1
Joined: Mar 2018
I am not sure what is going on it seems to work fine, for a while then I have to double tap the photo cell(I even changed out photo cell module) . I un-commented the texting I still get 15 to 20 text more or less. I think that maybe coming for the time the motor runs. I have this set up on bench to test out so there is no real door, I work the switches manually for door open, door shut.If I take a little longer to flip door open switch it seems I get more text.
Thanks Dualxeon
Posts: 8,091
Threads: 154
Joined: Sep 2016
Apr-02-2018, 11:13 AM
(This post was last modified: Apr-02-2018, 11:13 AM by buran.)
After your last post yesterday I was thinking how to minimize the false edge detections
So I change the code to use callback function. Now only event it detects is change in light sensor ans I also increased bounce time to 200 ms, i.e. it will not detect new event within 200 ms of the last one
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():
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 closed'")
# 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')
Posts: 8,091
Threads: 154
Joined: Sep 2016
Apr-02-2018, 11:20 AM
(This post was last modified: Apr-02-2018, 11:24 AM by buran.)
eventually you may add 1 sec sleep also before of the break statements or before the send_sms.
By the way, if you operate the switches by hand, could it be the case that you don't apply constant pressure once the switch is HIGH, thus triggering change/event? But I don't know if my question makes sense at all :-)
Posts: 22
Threads: 1
Joined: Mar 2018
Okay testing out new script, Output: Start monitoring
TypeError: operate_door() takes 0 positional arguments but 1 was given
this what I get. As for door switches I had magnetic switches, but it seemed I had to hit them in same spot each time to get them to work properly. I will probably go with micro or a push button door switch for now I have 2 toggle switches so they are on or off.
Posts: 8,091
Threads: 154
Joined: Sep 2016
Sorry, It looks like I misunderstood the callback function example and it gets an argument - the channel/pin.
In the example there was channel argument, but I decided it's because user wanted to have it, not that it is required.
Let me edit the script and will post shortly
I think this should take care
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 KeyboardInterupt:
print('Stop monitoring and quit')
Posts: 22
Threads: 1
Joined: Mar 2018
Nothing happens when I start script Start monitoring if I make photo cell dark nothing back to light nothing if I do it again I can hear motor try to start(real short)Kinda acts like open is closed and closed is open. When led on photocell is on Normally door open before script reads close Output: ================== RESTART: /home/pi/Desktop/buran4-1-c.py ==================
Start monitoring
Night. Close door
Start motor. Time is 03 Apr 2018 04:38:00.242837
STOP. Door closed. Time is 03 Apr 2018 04:38:00.325023
Day. Open door
Start motor. Time is 03 Apr 2018 04:38:40.633307
STOP. Door open. Time is 03 Apr 2018 04:38:40.713918
Traceback (most recent call last):
File "/home/pi/Desktop/buran4-1-c.py", line 55, in <module>
time.sleep(delay) # you may change this or replace it with just pass
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/Desktop/buran4-1-c.py", line 57, in <module>
except KeyboardInterupt:
NameError: name 'KeyboardInterupt' is not defined
Posts: 8,091
Threads: 154
Joined: Sep 2016
Apr-03-2018, 08:56 AM
(This post was last modified: Apr-03-2018, 08:57 AM by buran.)
It should be KeyboardInterrupt, with double r - my typo. Please fix it and try again
Posts: 8,091
Threads: 154
Joined: Sep 2016
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
Posts: 22
Threads: 1
Joined: Mar 2018
Seems like it starts motor but shuts off in shut time real quick without moving switches to call stop Output: ================== RESTART: /home/pi/Desktop/buran4-1-D.py ==================
Start monitoring
Night. Close door
Start motor. Time is 03 Apr 2018 05:09:12.999704
STOP. Door closed. Time is 03 Apr 2018 05:09:13.049136
Day. Open door
Start motor. Time is 03 Apr 2018 05:09:28.903734
STOP. Door open. Time is 03 Apr 2018 05:09:29.060098
Day. Open door
Start motor. Time is 03 Apr 2018 05:09:29.165132
STOP. Door open. Time is 03 Apr 2018 05:09:29.289408
Day. Open door
Start motor. Time is 03 Apr 2018 05:09:29.400280
STOP. Door open. Time is 03 Apr 2018 05:09:29.456954
Night. Close door
Start motor. Time is 03 Apr 2018 05:09:37.205669
STOP. Door closed. Time is 03 Apr 2018 05:09:37.269150
Day. Open door
Start motor. Time is 03 Apr 2018 05:09:47.873334
STOP. Door open. Time is 03 Apr 2018 05:09:47.968093
Day. Open door
Start motor. Time is 03 Apr 2018 05:09:48.126171
STOP. Door open. Time is 03 Apr 2018 05:09:48.290200
Day. Open door
Start motor. Time is 03 Apr 2018 05:09:48.420007
STOP. Door open. Time is 03 Apr 2018 05:09:48.542477
Day. Open door
Start motor. Time is 03 Apr 2018 05:09:48.668826
STOP. Door open. Time is 03 Apr 2018 05:09:48.752764
Night. Close door
Start motor. Time is 03 Apr 2018 05:09:56.379121
STOP. Door closed. Time is 03 Apr 2018 05:09:56.447723
Day. Open door
Start motor. Time is 03 Apr 2018 05:10:04.187126
STOP. Door open. Time is 03 Apr 2018 05:10:04.264759
Day. Open door
Start motor. Time is 03 Apr 2018 05:10:04.384506
STOP. Door open. Time is 03 Apr 2018 05:10:04.541420
Day. Open door
Start motor. Time is 03 Apr 2018 05:10:04.653870
STOP. Door open. Time is 03 Apr 2018 05:10:04.712705
Traceback (most recent call last):
File "/home/pi/Desktop/buran4-1-D.py", line 56, in <module>
pass #uncomment this if you decide to remove time.sleep above
KeyboardInterrupt
>>>
Posts: 2,955
Threads: 48
Joined: Sep 2016
Apr-03-2018, 09:47 AM
(This post was last modified: Apr-03-2018, 09:47 AM by wavic.)
(Apr-01-2018, 09:13 AM)buran Wrote: Do you read everything before answering?
Sometimes I don't. Lack of time. However, the pointed error is at least a hint for future situations like this.
|