Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie with Pi3
#42
Can you run this script

I added a detailed logging to a file
import RPi.GPIO as GPIO
import time
from datetime import datetime
from twilio.rest import Client
import logging

debug = True

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

MY_PATH = os.path.dirname(sys.argv[0])
log = logging.getLogger("GPIO.LOGGER")

def setup_logger(*args):
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    formatter2 = logging.Formatter('%(message)s')

    # region file handler
    if 'fh' in args:
        # create the logging file handler
        fh = logging.FileHandler(os.path.join(MY_PATH, 'GPIO.log'), mode='w')
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)
    # endregion

    # region console handler
    if 'ch' in args:
        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)
        ch.setFormatter(formatter2)
    # endregion

    # add handler to logger object
    for h in args:
        if h in locals():
            log.addHandler(locals().get(h))

   
   
def send_sms(sms_text="Hello from Python!"):
    twilio_client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz")
    client.messages.create(to="+19732644152", 
                           from_="+12023351278", 
                           body=sms_text)
                           
def pin_status():
    pins = [7, 11, 12, 13, 15]
    pin_statuses = ['pin {}: {}'.format (pin, GPIO.input(pin)) for pin in pins]
    return ', '.join(pin_statuses)

 
def operate_door(channel):
    try:
        if channel == 7:
            log.debug('Callback1, {}'.format(pin_status()))
            if not GPIO.input(7) and not GPIO.input(11): # it's day and door is not open
                log.debug('Callback2, {}'.format(pin_status()))
                log.info("Day. Open door")
                log.info("Start motor. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f')))
                GPIO.output(15, True)
                log.debug('Callback3, {}'.format(pin_status()))
                while True:
                    log.debug('Callback4, {}'.format(pin_status()))
                    if GPIO.input(11):
                        GPIO.output(15,False)
                        log.debug('Callback5, {}'.format(pin_status()))
                        log.info("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
                log.debug('Callback6, {}'.format(pin_status()))
                log.info("Night. Close door")
                log.debug("Start motor. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f')))
                GPIO.output(13, True)
                log.debug('Callback7, {}'.format(pin_status()))
                while True:
                    log.debug('Callback8, {}'.format(pin_status()))
                    if GPIO.input(12):
                        GPIO.output(13,False)
                        log.debug('Callback9, {}'.format(pin_status()))
                        log.info("STOP. Door closed. Time is {}".format(datetime.now().strftime('%d %b %Y %H:%M:%S.%f')))
                    break
            #send_sms("sms_text='Door opened'")
    except KeyboardInterrupt:
        pass

if debug:
    setup_logger('ch', 'fh')
    log.setLevel(logging.DEBUG)
else:
    setup_logger('ch')
    log.setLevel(logging.INFO)        
 
# add events detection, using separate thread
GPIO.add_event_detect(7, GPIO.BOTH, callback=operate_door, bouncetime=200)
delay = 1 # 1 sec. delay
log.info('Start monitoring')
log.debug('Main thread start, {}'.format(pin_status()))
try:
    while True:
        #time.sleep(delay) # you may change this or replace it with just pass
        #pass #uncomment this if you decide to remove time.sleep above
        log.debug('Main thread, {}'.format(pin_status()))
except KeyboardInterrupt:
    log.info('Stop monitoring and quit')
finally:
    GPIO.cleanup()
I hope I don't have syntax or typo errors.
It will create GPIO.log file in the same folder where the script is.

I also added GPIO.cleanup() based on http://raspi.tv/2013/rpi-gpio-basics-3-h...ct-your-pi
it's not related to our problem, but nevertheless....
I know it's frustrating, but you can always return to original script
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


Messages In This Thread
Newbie with Pi3 - by Dualxeon - Mar-30-2018, 08:39 AM
RE: Newbie with Pi3 - by buran - Mar-30-2018, 09:32 AM
RE: Newbie with Pi3 - by Dualxeon - Mar-30-2018, 10:26 AM
RE: Newbie with Pi3 - by Dualxeon - Mar-30-2018, 10:19 PM
RE: Newbie with Pi3 - by snippsat - Mar-30-2018, 10:38 PM
RE: Newbie with Pi3 - by buran - Mar-31-2018, 05:53 AM
RE: Newbie with Pi3 - by Dualxeon - Mar-31-2018, 08:28 AM
RE: Newbie with Pi3 - by buran - Mar-31-2018, 08:48 AM
RE: Newbie with Pi3 - by Dualxeon - Mar-31-2018, 09:25 AM
RE: Newbie with Pi3 - by buran - Mar-31-2018, 10:00 AM
RE: Newbie with Pi3 - by Dualxeon - Mar-31-2018, 10:28 AM
RE: Newbie with Pi3 - by buran - Mar-31-2018, 10:47 AM
RE: Newbie with Pi3 - by Dualxeon - Mar-31-2018, 12:09 PM
RE: Newbie with Pi3 - by buran - Mar-31-2018, 12:23 PM
RE: Newbie with Pi3 - by buran - Mar-31-2018, 07:29 PM
RE: Newbie with Pi3 - by buran - Mar-31-2018, 07:47 PM
RE: Newbie with Pi3 - by Dualxeon - Mar-31-2018, 08:55 PM
RE: Newbie with Pi3 - by buran - Mar-31-2018, 09:13 PM
RE: Newbie with Pi3 - by Dualxeon - Apr-01-2018, 08:51 AM
RE: Newbie with Pi3 - by wavic - Apr-01-2018, 09:07 AM
RE: Newbie with Pi3 - by buran - Apr-01-2018, 08:57 AM
RE: Newbie with Pi3 - by buran - Apr-01-2018, 09:13 AM
RE: Newbie with Pi3 - by wavic - Apr-03-2018, 09:47 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-01-2018, 09:14 AM
RE: Newbie with Pi3 - by buran - Apr-01-2018, 09:27 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-01-2018, 09:36 AM
RE: Newbie with Pi3 - by buran - Apr-01-2018, 09:40 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-01-2018, 11:34 AM
RE: Newbie with Pi3 - by buran - Apr-01-2018, 12:14 PM
RE: Newbie with Pi3 - by Dualxeon - Apr-01-2018, 06:32 PM
RE: Newbie with Pi3 - by buran - Apr-01-2018, 08:08 PM
RE: Newbie with Pi3 - by Dualxeon - Apr-02-2018, 10:19 AM
RE: Newbie with Pi3 - by buran - Apr-02-2018, 11:13 AM
RE: Newbie with Pi3 - by buran - Apr-02-2018, 11:20 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-03-2018, 07:33 AM
RE: Newbie with Pi3 - by buran - Apr-03-2018, 07:41 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-03-2018, 08:46 AM
RE: Newbie with Pi3 - by buran - Apr-03-2018, 08:56 AM
RE: Newbie with Pi3 - by buran - Apr-03-2018, 09:01 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-03-2018, 09:17 AM
RE: Newbie with Pi3 - by buran - Apr-03-2018, 10:14 AM
RE: Newbie with Pi3 - by buran - Apr-03-2018, 11:04 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-04-2018, 07:40 AM
RE: Newbie with Pi3 - by buran - Apr-04-2018, 08:02 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-04-2018, 08:30 AM
RE: Newbie with Pi3 - by buran - Apr-04-2018, 09:08 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-04-2018, 09:15 AM
RE: Newbie with Pi3 - by buran - Apr-04-2018, 09:22 AM
RE: Newbie with Pi3 - by Dualxeon - Apr-04-2018, 08:14 PM
RE: Newbie with Pi3 - by buran - Apr-04-2018, 08:21 PM
RE: Newbie with Pi3 - by Dualxeon - Apr-04-2018, 10:58 PM

Forum Jump:

User Panel Messages

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