JOOP
Posts: 3
Joined: Wed Feb 26, 2020 7:19 pm

Detecting both magnetic door sensor when they are open/closed

Wed Feb 26, 2020 7:35 pm

I'm ony get notification from door 1 only can someone help with the code
here is the code

import RPi.GPIO as GPIO
import time
import smtplib
import cred

try:
need_clean = False
GATE1 = MSG1 = '\nPool Gate1 was '
GATE2 = MSG2 = '\nPool Gate2 was '
GATE1_MSG = {True:'open', False:'closed'}
GATE2_MSG = {True:'open', False:'closed'}
print('Setting up SMS...')

def send_msg(opened:bool):
server = smtplib.SMTP( "smtp.mail.att.net", 587 )
server.starttls()
server.login( cred.FROM, cred.PASS )
str_print =''.join([MSG1, GATE1_MSG[opened], ' at ',
time.strftime('%I:%M:%S %p')])
print(str_print)
server.sendmail(cred.FROM, cred.TO, str_print)
server.quit()

print('Setting up hardware...')
GATE1 = 12
GATE2 = 13
GPIO.setmode(GPIO.BOARD)
GPIO.setup(GATE1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(GATE2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
next_state = True
need_clean = False
print('Ready!')
while True:
if GPIO.input(GATE1) == next_state:
send_msg(next_state)
next_state = not next_state
time.sleep(0.3)
while True:
if GPIO.input(GATE2) == next_state:
send_msg(next_state)
next_state = not next_state
time.sleep(0.3)


except KeyboardInterrupt:
GPIO.cleanup()
need_clean = False

if need_clean:
GPIO.cleanup()
print('\nEnd!')

User avatar
Burngate
Posts: 6290
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Detecting both magnetic door sensor when they are open/closed

Thu Feb 27, 2020 10:45 am

JOOP wrote:
Wed Feb 26, 2020 7:35 pm

Code: Select all

import RPi.GPIO as GPIO
import time
import smtplib
import cred

try:
    need_clean = False
    GATE1 = MSG1  = '\nPool Gate1 was ' 
    GATE2 = MSG2  = '\nPool Gate2 was '
    GATE1_MSG = {True:'open', False:'closed'}
    GATE2_MSG = {True:'open', False:'closed'}
    print('Setting up SMS...') 
   
    def send_msg(opened:bool):
        server = smtplib.SMTP( "smtp.mail.att.net", 587 )
        server.starttls()
        server.login( cred.FROM, cred.PASS )
        str_print =''.join([MSG1, GATE1_MSG[opened], ' at ',
                            time.strftime('%I:%M:%S %p')])
        print(str_print)
        server.sendmail(cred.FROM, cred.TO, str_print)
        server.quit()
   
    print('Setting up hardware...')    
    GATE1 = 12 
    GATE2 = 13     
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(GATE1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(GATE2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    next_state = True
    need_clean = False   
    print('Ready!')    
    while True:        
        if GPIO.input(GATE1) == next_state:
           send_msg(next_state)
           next_state = not next_state
           time.sleep(0.3)
    while True:      
        if GPIO.input(GATE2) == next_state:
           send_msg(next_state)
           next_state = not next_state
           time.sleep(0.3)

except KeyboardInterrupt:
    GPIO.cleanup() 
    need_clean = False

if need_clean:    
    GPIO.cleanup() 
print('\nEnd!')
I'm not a Python guru, but one thing that stands out is that, towards the end, you have

Code: Select all

    while True:        
        if GPIO.input(GATE1) == next_state:
           send_msg(next_state)
           next_state = not next_state
           time.sleep(0.3)
    while True:      
        if GPIO.input(GATE2) == next_state:
           send_msg(next_state)
           next_state = not next_state
           time.sleep(0.3)
Once the first "while true:" loop is reached, it never moves on to the second - the first is never not true!

So try removing the second "while true:" statement - make it all one loop - and see what happens.

JOOP
Posts: 3
Joined: Wed Feb 26, 2020 7:19 pm

Re: Detecting both magnetic door sensor when they are open/closed

Fri Feb 28, 2020 8:36 pm

I rewrite the script, it tells me when both gates are open but if either gate is close it continue run in a loop for EXAMPLE 1 if close gate1 and leave gate 2 open I will get gate1 open constantly until I open the gate)
EXAMPLE 2 ( if close gate 2 and leave gate 1 open I will get gate 2 close , gate 1 open constantly until I open the gate)
IM NOT SURE WHAT I'M DOING WRONG


[/

Code: Select all

import RPi.GPIO as GPIO
import time
import smtplib
import cred

try:
    need_clean = False
    GATE1 = MSG1  = '\nPool Gate1 was ' 
    GATE2 = MSG2  = '\nPool Gate2 was '
    GATE1_MSG = {True:'open', False:'closed'}
    GATE2_MSG = {True:'open', False:'closed'}
    print('Setting up SMS...') 
   
    def send_msg1(opened:bool):
        server = smtplib.SMTP( "smtp.mail.att.net", 587 )
        server.starttls()
        server.login( cred.FROM, cred.PASS )
        str_print1 =''.join([MSG1, GATE1_MSG[opened], ' at ',
                            time.strftime('%I:%M:%S %p')])
        print(str_print1)
        server.sendmail(cred.FROM, cred.TO, str_print1)
        server.quit()
        
    def send_msg2(opened:bool):
        server = smtplib.SMTP( "smtp.mail.att.net", 587 )
        server.starttls()
        server.login( cred.FROM, cred.PASS )
        str_print2 =''.join([MSG2, GATE2_MSG[opened], ' at ',
                            time.strftime('%I:%M:%S %p')])       
        print(str_print2)
        server.sendmail(cred.FROM, cred.TO, str_print2)
        server.quit()    
   
    print('Setting up hardware...')    
    GATE1 = 12 
    GATE2 = 13     
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(GATE1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(GATE2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    next_state = True
    need_clean = False   
    print('Ready!')    
    while True:        
        send_msg1 = send_msg1
        send_msg2 = send_msg2
        
        if GPIO.input(GATE1) == next_state:
           send_msg1(next_state)           
           time.sleep(0.3)
         
        if GPIO.input(GATE2) == next_state:
           send_msg2(next_state)
           next_state = not next_state
           time.sleep(0.3)

except KeyboardInterrupt:
    GPIO.cleanup() 
    need_clean = False

if need_clean:    
    GPIO.cleanup() 
print('\nEnd!')

ghp
Posts: 1488
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Detecting both magnetic door sensor when they are open/closed

Fri Feb 28, 2020 9:30 pm

Hello, assuming you want to track the two doors independent from each other, then a more modular approch is possible.

The idea is to have a message queue between the 'gatekeeper' for door1 and door 2. The gatekeeper feed in messages when state is changing.
On the reading end of the queue is a sender thread which read the queue and puts the messages to the sendmail. The timeout on the queue.get allows for a simple application stop.

Be careful, the code is not tested.

Code: Select all

import RPi.GPIO as GPIO
import time
import smtplib
import cred

import queue
import threading

msgQueue=queue.Queue()

class GateKeeper:
    def __init__(self, gpio, name):
        """start the thread which checks a door.
           Initializes the gpio pin"""
           
        self.gpio = gpio
        self.name = name
        
        GPIO.setup(gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        
        self.runIt=True
        self.t = threading.Thread(target=self.run, name=name)
        self.t.start()
        
    def run(self):
        while self.runIt:
            time.sleep(0.1)
            if GPIO.input(self.gpio) == True:
                msgQueue.put( "Pool {name:s} was open".format(name=self.name))
                while( GPIO.input(self.gpio) == True ):
                    if not self.runIt:
                        return
                    time.sleep(0.1)     
            if GPIO.input(self.gpio) == False:
                msgQueue.put( "Pool {name:s} was closed".format(name=self.name))
                while( GPIO.input(self.gpio) == False ):
                    if not self.runIt:
                        return
                    time.sleep(0.1)     
    
    def stop(self):
        """terminate the thread and wait for completion"""

        self.runIt = False
        self.t.join()
                 
class Sender:
    def __init__(self):
        """start the thread which reads the queue.
           When data arrive, then put them to send_msg"""
           
        self.runIt=True

        self.t = threading.Thread(target=self.run, name="sender")
        self.t.start()
        
    def run(self):
        while self.runIt:
            try:
                msg = msgQueue.get(timeout=0.1)
            except queue.Empty:
                continue
    
            self.send_msg(msg)
            
    def send_msg(self, msg):
        server = smtplib.SMTP( "smtp.mail.att.net", 587 )
        server.starttls()
        server.login( cred.FROM, cred.PASS )
        str_print =''.join([ msg, ' at ', time.strftime('%I:%M:%S %p')])
        print(str_print)
        server.sendmail(cred.FROM, cred.TO, str_print)
        server.quit()

    def stop(self):
        """terminate the thread and wait for completion"""
        self.runIt = False
        self.t.join()
        

GPIO.setmode(GPIO.BOARD)

gateKeeper_1 = GateKeeper( 12, 'Gate1')
gateKeeper_2 = GateKeeper( 13, 'Gate2')

sender = Sender()

wait = threading.Event()
try:
    wait.wait()
except KeyboardInterrupt:
    pass

gateKeeper_1.stop()
gateKeeper_2.stop()
sender.stop()

GPIO.cleanup()
    
print('\nEnd!')

penny09212
Posts: 1
Joined: Thu Apr 30, 2020 4:19 pm
Location: New York City
Contact: Website

Re: Detecting both magnetic door sensor when they are open/closed

Thu Apr 30, 2020 4:31 pm

This thread helps a lot about my Magnetic Screen door

JOOP
Posts: 3
Joined: Wed Feb 26, 2020 7:19 pm

Re: Detecting both magnetic door sensor when they are open/closed

Fri May 01, 2020 2:05 pm

penny09212 wrote:
Thu Apr 30, 2020 4:31 pm
This thread helps a lot about my Magnetic Screen door
are you using it for more than one door?

Return to “Python”