Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Knowing weather a gate is open or closed

Tue Oct 20, 2020 3:28 pm

Hi All,

Fairly new to the Pi and Python.
I have done some Python for controlling an LED and some of the other "beginner" tutorials found on-line and in the book "Programming the Raspberry Pi: Getting Started with Python - Second Edition".
I just installed openers for our driveway gates and I am planing on using one of my RPI Zero W's to let us know if the gate is open or closed when we are away from home.
The gates are powered by a 12 volt battery that is charged by a transformer plugged into an outlet and run thru the control board.
My plan is to get a converter to drop the 12 volts to 5 volts and power the PI from that, great advantage is that it will basically have a built in UPS as the battery is "constantly" charged and if the power goes out it shouldn't see an interruption, unless the power is out long enough to drain the battery, in which case I should be able to safely shut the PI down before the power is completely gone..
The company I work for makes all kinds of safety sensors, like the ones used in the food industry, for when a door is opened on a machine it will cut power, so I have access to a wide variety of sensors and actuators.
I want the PI to read the sensor, and indicate if the gate is closed or opened, based on the signal it receives from the sensor, then "print" out "OPEN" or "CLOSED", and for us to be able to access from the web, probably through an open port on the router.
I believe I can run headless, use the Lite version of the RPIOS, and set the PI so that the program runs after the PI is booted up.
Besides the tutorials, I am currently running MotionEyeOS on a PI0W, and have a PI3B+ that I can either run RetroPie or RPIOS, depending on what SD card I have in it at the time, that I use for the tutorials and just generally trying to figure out Linux as I have been using Windows since the 3.1 days.
Anyhow, this will be a project for me to work on this fall/winter.
The last time I did any real "programming" was about 35+ years ago in BASIC on a Texas Instruments Ti99-4/A, but I think I should be able to come up with what I need for this.
Just thought I'd share my project and I will post updates to this as I go.

Thanks for reading,
Mike
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

duckredbeard
Posts: 152
Joined: Mon Apr 29, 2019 1:53 am

Re: Knowing weather a gate is open or closed

Tue Oct 20, 2020 8:45 pm

If you can put a mechanical limit switch or a reed switch somewhere on the gate that can detect its position, then that switch can be a "button" in gpiozero. This is how I have my home security system. When the door gets opened or closed, the Pi sees it "pressed" or "released" and does a requests.post to a URL that is a Join URL (think Android app Tasker and Join). A hit on that URL's server initiates a keyword message to my phone and Tasker responds to that message by giving me a notification (and updates a widget, adds a line to a google sheet with date and time stamp of the event). Sounds complicated, but it is near instantaneous.

Since Tasker is managing the notifications, I can have it do different things based on different conditions. Late at night, the notification makes a siren sound when an exterior door is opened. During the day, it is just a simple gong.

Tasker and Join are Android only apps. Amazingly powerful. (Plugging in my phone + placing it face down = ringer volume to 0, brightness to 5, SSH to my Pi to make GPIO 4 high [powers relay that simulates a button push on my car's remote which locks my car door])
You can't know your limits until you exceed them.

Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Re: Knowing weather a gate is open or closed

Wed Oct 21, 2020 12:54 am

That’s kind of my plan, the sensor anyhow. We have a lot of sensors that are reed switches. Just need to get the correct actuator (magnet) to “cross” the gap.
As for your suggestion of the app, unfortunately we both have iPhones. Not my choice for my phone, but it’s a company phone so I have to go with what they want.
Thanks for the feedback though.
Last edited by Dragontail281 on Wed Oct 21, 2020 12:59 pm, edited 1 time in total.
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

User avatar
Gavinmc42
Posts: 5165
Joined: Wed Aug 28, 2013 3:31 am

Re: Knowing weather a gate is open or closed

Wed Oct 21, 2020 1:47 am

I have Pi's that send me emails when something changes.
A bit of shell script checks GPIO pins and sends a message via SMTP.
I usually attach the html address that serves the webpage so I can check what the plotted data says.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Re: Knowing weather a gate is open or closed

Wed Oct 21, 2020 2:12 am

I was thinking of it sending an email saying “open” or “closed” based on the data from the sensor.
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

User avatar
Gavinmc42
Posts: 5165
Joined: Wed Aug 28, 2013 3:31 am

Re: Knowing weather a gate is open or closed

Wed Oct 21, 2020 5:04 am

I just used netcat (nc) to sends a smtp formatted text file to the mail server on port 25.
An old Linux method google found.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

boyoh
Posts: 1581
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Knowing weather a gate is open or closed

Wed Oct 21, 2020 7:57 pm

I think you are going over the top with your project, Keep it simple, you will encounter less problems, Your program will
be easer to write and understand. To prove your gate position , I would use Opto Isolators ( slotted type) Micro switches
don't fair well out side tend to corrode and stick I would use a Camera for showing if your gates are open or closed Your
phone will show you this . In you opening and closing program put a time limit , If it exceeds this use it as a failed signal..

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Re: Knowing weather a gate is open or closed

Wed Oct 21, 2020 10:50 pm

Not using “switches” per say.
My company make sensors for safety systems, many that are used in the food industry, that are waterproof.
The sensors have reed switches in them that are actuated by a corresponding magnet.
When the gate is closed, the magnet and sensor will be by each other, closing the reed switch and closing the circuit. When the gate is opened, the units are separated and the circuit is open.
Was going to use a program similar to the one with the push button controlling LEDs, but instead of lighting LEDs, it will send messages.
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

DarkElvenAngel
Posts: 1411
Joined: Tue Mar 20, 2018 9:53 pm

Re: Knowing weather a gate is open or closed

Wed Oct 21, 2020 11:19 pm

If this is an industrial applications why not use proper safety switches these have contacts like N/O and N/C or if you don't need them for a safety application there are proximity switches. These typically work on 24VDC and have a nice digital out signal. You can get either or these in an IP rating you need for the environment.

Edit:
Okay wait I see this is a gate on a driveway I would suggest looking at the automation phat since this is a Pi Zero it can handle all the isolation required to protect the Pi.

Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Re: Knowing weather a gate is open or closed

Thu Oct 22, 2020 1:54 pm

Our sensors aren't anything that "drastic".
The ones I'm considering are basically a couple of NO or NC reed switches epoxied into a small housing that are actuated by an external magnet.
In a safety system these run to a relay which controls all the rest of the safety measures, alarms, contacts, etc.
I talked to a couple of the engineers here when I first thought of the project and they agreed that we would have something that would work, using about 3.3v (or less) and in low milliamps.
Finding an actuator (magnet) that is strong enough for the 3/8" to 1/2" gap between panels will be the harder problem.
Of course getting the program right will be the other, but should be pretty straightforward, similar to the switch/led ones that I have done in the tutorials.
The harder part of the programming will be getting it to send the email/text and continuously looping so that the program keeps working each time the gate is opened or closed. I have an "outline" in my head on how I would like the program to work/flow, but that is another step after we come up with the best sensor/actuator combo.

Thanks to everyone for the thoughts/ideas/input so far.
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Re: Knowing weather a gate is open or closed

Wed Nov 04, 2020 5:48 pm

OK, just a little update.
I have the sensor, and the actuating magnet for it, just need to test to see how big a gap it can "cross".
Found a couple of examples of Python programs that will send an email on a button press, which my sensor will act like, basically the sensor is either open or closed depending on where it is in relation to the magnet.
Once I settle on which of those programs to use, I will have to figure out how to make it run on start-up, and continuously "listen" for the "signal" to change.
Also will have to figure out how to make is only send 1 email at the time the "signal" changes, kind of like with a button you press and it sends the email, but then sends another when you let go of the button, say like after 30 seconds, nothing in between, and so on and so on.
Once I get the initial code figured out, I'll post up.
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

pcmanbob
Posts: 10720
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Knowing weather a gate is open or closed

Thu Nov 05, 2020 10:24 am

All you want to do is possible using python, cron and a gmail account set up just for use with your pi.

Setup the gmail account with a user name and password that only has 0-9, A-Z, A-Z and full stops in , then enable less secure apps.

you can send an email from python using smtplib

Here is a simple test program just to send a test email and check your new gmail account is setup and working.

Code: Select all

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

email_user = "account@gmail.com"
email_password = "password"
email_send = "address to send to"

subject = "Test email from pi"

msg = MIMEMultipart()
msg["From"] = email_user
msg["To"] = email_send
msg["Subject"] = subject

body = "Hi there, sending this email from Python!"
msg.attach(MIMEText(body,"plain"))

text = msg.as_string()
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(email_user,email_password)


server.sendmail(email_user,email_send,text)
server.quit()
you can also do the switch checking in python and then add the email part and simple polling loop so that only one email is sent on each change of switch state.

once you code is complete and test you can start it on pi boot using cron with just one line like this.

Code: Select all

@reboot sleep 30 && python3 /home/pi/myprog.py
the sleep just allows time for your pi to finish booting and make a connection to your network.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Re: Knowing weather a gate is open or closed

Thu Nov 05, 2020 2:49 pm

Hi pcmanbob,
I have already created a gmail to use, just need to set it for the less secure apps, thanks for that bit of info.
The 2 programs that I found have pretty much the email portion that you mention as well as some of the script for "listening" and "sending" the email.
I also found the same cron script.
Once I get a program set up on the pi, I'll post it up here, expecially if I have any probs, which I'm sure I will as almost every program I have done has had some sort of flaw.

Thanks again.
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

pcmanbob
Posts: 10720
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Knowing weather a gate is open or closed

Thu Nov 05, 2020 2:57 pm

Ok one other point to be aware of with Gmail accounts , when you try to send your first email from the pi it may fail, this is especially true if you set the account up on another PC.

If this happens log in to the Gmail account from the PC and look for security alert email from Google,
if you follow the link in the email it will give you the option to allow the device, this occurs because Gmail sees you pi as a new device trying to connect to the account and blocks it.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Re: Knowing weather a gate is open or closed

Thu Nov 05, 2020 4:15 pm

Thanks, I get that every time we change phones or get new computers at home, or work for that matter.
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

Dragontail281
Posts: 47
Joined: Thu Mar 07, 2019 8:56 pm

Re: Knowing weather a gate is open or closed

Fri Dec 04, 2020 5:36 pm

So here is the program I am thinking of using.
I have modified a couple things from the original, like what the email subject and body is and the sleep time.

Code: Select all

import smtplib
import RPi.GPIO as GPIO
import time
  
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!)
SMTP_PORT = 587 #Server Port (don't change!)
GMAIL_USERNAME = 'youremail@email.com' #change this to match your gmail account
GMAIL_PASSWORD = 'yourPassword'  #change this to match your gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 17(BCM) to an input
GPIO.setup(17, GPIO.IN)
 
#Set digital pin 17(BCM) to an input and enable the pullup 
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect button press
GPIO.add_event_detect(17, GPIO.FALLING)
 
  
class Emailer:
    def sendmail(self, recipient, subject, content):
          
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(17):
        sendTo = 'anotheremail@email.com'
        emailSubject = "Driveway gate open!"
        emailContent = "The gate is opened at: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent)
       
 
    time.sleep(900)
There original program is for a button press, but I will be using a sensor with a reed switch that is activated by an actuator (magnet).
When the gate is closed, the reed switch is closed, when the gate opens, the switch opens.
I set the sleep to be 15 minutes, and I'm hoping that this program then tries to run again and will send me another email if the circuit is still open, and keeps doing this until the circuit is closed..
My sensor is NO/NC, so there is another circuit in there that I could use.
I am using the NO circuit, which closes when in proximity to the magnet, and opens when no magnet is present, but if it makes more sense to start with the normally closed circuit, which would be open when the magnet is present and closed when not, I could use that one instead.
Of course, I have changed email variables that I will be using on the finished program, but does anyone see anything I might need to change/modify to make this work as I want to?
I will be running this on a RPI0W.
RPI3B+ - Raspberry Pi OS -general "playing" and/or RetroPi
RPI0W#1 - MotionEyeOS - yard surveillance
RPI0W#2 - Driveway gate monitor - coming soon
RPI0W#3 - ?????

Return to “Beginners”