Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie with Pi3
#11
(Mar-31-2018, 10:00 AM)buran Wrote: Again, not able to test, but I think this should work

import RPi.GPIO as GPIO
import time
from twilio.rest import Client
 
 
def send_sms(client, sms_text="Hello from Python!"):
    client.messages.create(to="+19732644152", 
                           from_="+12023351278", 
                           body=sms_text)
 
 
twilio_client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
                            
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

# add events detection
GPIO.add_event_detect(7, GPIO.BOTH)
GPIO.add_event_detect(11, GPIO.RISING)
GPIO.add_event_detect(12, GPIO.RISING)

while True:
    if GPIO.event_detected(7) # change in light sensor
        if GPIO.input(7) == False and GPIO.input(11) == False: # it's day and door is not open
            print ("Day")
            print ("Open door")
            GPIO.output(15,True)
            print ("Start motor")
            print ("Current time %s"  % now )
        elif GPIO.input(7) == True and GPIO.input(12) == False: # it's night and door is not closed
            print ("Night")
            print ("Close door")
            GPIO.output(13,True) 
            print ("Drive motor")

    if GPIO.event_detected(11): # top switch change to HIGH
        GPIO.output(15,False)
        print ("STOP door open")
        send_sms(client=twilio_client, sms_text='Door open')
    
    if GPIO.event_detected(12): # bottom switch change to HIGH
        GPIO.output(13,False)
        print ("STOP door closed")
        send_sms(client=twilio_client, sms_text='Door closed')
Maybe it can be written better, but let's make it work as expected
not sure still looking at it but won't run getting invalid syntax looks like line 30
if GPIO.event_detected(7) # change in light sensor
Reply
#12
It's missing colon : at the end
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

Reply
#13
(Mar-31-2018, 10:47 AM)buran Wrote: It's missing colon : at the end

I'm not much help I looked and looked try comparing did not see it. The script runs and sends text but something is out of sync I'm looking now but when I cover photo cell it prints
Output:
Night Close door Drive motor STOP door open STOP door closed Night Close door Drive motor STOP door open STOP door closed Night Close door Drive motor STOP door open STOP door closed Night Close door Drive motor STOP door open STOP door closed Night Close door Drive motor STOP door open STOP door closed STOP door open STOP door closed STOP door closed Traceback (most recent call last): File "/home/pi/Desktop/buran.py", line 30, in <module> if GPIO.event_detected(7): # change in light sensor KeyboardInterrupt
I understand its hard to trouble shoot something you can't see I appreciate all the help from everyone.
Reply
#14
I have to go out. will look into it in more detail.\
can you comment out the send_sms lines and do some more tests?
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

Reply
#15
I found this https://www.raspberrypi.org/forums/viewt...p?t=134394
I need some time to fully understand and decide how is best to implement in the code
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

Reply
#16
I implemented simplest solution in my opinion - adding 50 ms bouncetime
import RPi.GPIO as GPIO
import time
from twilio.rest import Client
 
 
def send_sms(client, sms_text="Hello from Python!"):
    client.messages.create(to="+19732644152", 
                           from_="+12023351278", 
                           body=sms_text)
 
 
twilio_client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
                            
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

# add events detection
GPIO.add_event_detect(7, GPIO.BOTH, bouncetime=50)
GPIO.add_event_detect(11, GPIO.RISING, bouncetime=50)
GPIO.add_event_detect(12, GPIO.RISING, bouncetime=50)

while True:
    if GPIO.event_detected(7) # change in light sensor
        if not GPIO.input(7) and not GPIO.input(11): # it's day and door is not open
            print ("Day")
            print ("Open door")
            GPIO.output(15,True)
            print ("Start motor")
            print ("Current time %s"  % now )
        elif GPIO.input(7) and not GPIO.input(12): # it's night and door is not closed
            print ("Night")
            print ("Close door")
            GPIO.output(13,True) 
            print ("Drive motor")

    if GPIO.event_detected(11) and GPIO.input(11): # top switch change to HIGH
        GPIO.output(15,False)
        print ("STOP door open")
        #send_sms(client=twilio_client, sms_text='Door open')
    
    if GPIO.event_detected(12) and GPIO.input(12): # bottom switch change to HIGH
        GPIO.output(13,False)
        print ("STOP door closed")
        #send_sms(client=twilio_client, sms_text='Door closed')
I temporary commented out the sms. We will uncomment it later.
I expect it will detect each event only once (due to 50 ms bouncetime). Also bu adding check that pins 11 and 12 are actually HIGH in case of event, it should avoid detecting GPIO.FALLING events

I want it to print once that it will open/close door and only once print that the door is opene/close once it is.
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

Reply
#17
Okay I just got back to the computer may not be able to stay long. I did the comment out this what I get motor does not start.
Output:
Night Close door Drive motor Night Close door Drive motor STOP door closed Night Close door Drive motor Night Close door Drive motor STOP door closed STOP door open STOP door open STOP door open Night Close door Drive motor STOP door open STOP door closed Day Open door Start motor STOP door open STOP door closed STOP door open Traceback (most recent call last): File "/home/pi/Desktop/virginburan.py", line 48, in <module> if GPIO.event_detected(12): # bottom switch change to HIGH KeyboardInterrupt
Then I tried the new script and motor starts but does not stop or change
Output:
Day Open door Start motor Traceback (most recent call last): File "/home/pi/Desktop/burannew.py", line 36, in <module> print ("Current time %s" % now ) NameError: name 'now' is not defined >>>
I will try work on it some more maybe later tonight or early morning again Thanks I do appreciate all the help I am definitely over my head
Reply
#18
final try for today as I will go to sleep

import RPi.GPIO as GPIO
from twilio.rest import Client
from datetime import datetime
  
  
def send_sms(client, sms_text="Hello from Python!"):
    client.messages.create(to="+19732644152", 
                           from_="+12023351278", 
                           body=sms_text)
  
  
twilio_client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
                             
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
 
# add events detection
GPIO.add_event_detect(7, GPIO.BOTH, bouncetime=50)
GPIO.add_event_detect(11, GPIO.RISING, bouncetime=50)
GPIO.add_event_detect(12, GPIO.RISING, bouncetime=50)
 
while True:
    if GPIO.event_detected(7) # change in light sensor
        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)
        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) 
 
    if GPIO.event_detected(11) and GPIO.input(11): # top switch change to HIGH
        GPIO.output(15,False)
        print ("STOP. Door open. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f'))
        #send_sms(client=twilio_client, sms_text='Door open')
     
    if GPIO.event_detected(12) and GPIO.input(12): # bottom switch change to HIGH
        GPIO.output(13,False)
        print ("STOP. Door closed. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f'))
        #send_sms(client=twilio_client, sms_text='Door closed')
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

Reply
#19
Tried this I am getting Invalid syntax looks like line 34
Error:
GPIO.output(15,True)
Reply
#20
sorry, it's really difficult when I cannot test - I have missing closing parenthesis on all print functions
import RPi.GPIO as GPIO
from twilio.rest import Client
from datetime import datetime
   
   
def send_sms(client, sms_text="Hello from Python!"):
    client.messages.create(to="+19732644152", 
                           from_="+12023351278", 
                           body=sms_text)
   
   
twilio_client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
                              
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
  
# add events detection
GPIO.add_event_detect(7, GPIO.BOTH, bouncetime=50)
GPIO.add_event_detect(11, GPIO.RISING, bouncetime=50)
GPIO.add_event_detect(12, GPIO.RISING, bouncetime=50)
  
while True:
    if GPIO.event_detected(7) # change in light sensor
        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)
        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) 
  
    if GPIO.event_detected(11) and GPIO.input(11): # top switch change to HIGH
        GPIO.output(15,False)
        print ("STOP. Door open. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f')))
        #send_sms(client=twilio_client, sms_text='Door open')
      
    if GPIO.event_detected(12) and GPIO.input(12): # bottom switch change to HIGH
        GPIO.output(13,False)
        print ("STOP. Door closed. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f')))
        #send_sms(client=twilio_client, sms_text='Door closed')
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

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020