CleverleyDone
Posts: 4
Joined: Wed Jul 18, 2018 2:26 pm

Magnetic Door / Reed Switch Using Rising Edge

Tue Jul 24, 2018 9:43 pm

Hi,

I have written a script to turn my lights on in the office when I open the door and close the door, and when I leave it turns off.

So I have three questions:
  • Is this the most efficient way to perform this task?
    Does this script run "all of the time" and cause SD card burn?
    How would you have gone about this task using the same hardware?
Thanks in advance
Martin

Code: Select all

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

#   SHED DOOR SENSOR
GPIO.setmode(GPIO.BCM)
GPIO.setup(6,GPIO.OUT)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:

	GPIO.wait_for_edge(25, GPIO.RISING)
	time.sleep(2)
	print("Someone entered...")
	GPIO.output(6, GPIO.LOW)
	time.sleep(1)

	GPIO.wait_for_edge(25, GPIO.FALLING)
	time.sleep(2)
	print("Someone left...")
	GPIO.output(6, GPIO.HIGH)
	time.sleep(1)



PhatFil
Posts: 1373
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Magnetic Door / Reed Switch Using Rising Edge

Wed Jul 25, 2018 12:58 am

Its a way, and it looks like it should work fine..

Afaik it wont sd-burn your card.. its a small process and unless your making high ram demands on your pi there should be no reason to swap it out of active memory to swapfile.

with the same h/w i would also install and update Mosquitto Mqtt Broker and NodeRed s/w.

I would then configure Nodered to publish a 'someone entered' mqtt topic/payload on the gpio.rising event
anda 'someone leaving' mqtt topic/payload on the gpio.falling event.

Then Also using Node red i would create flows to respond to the subscription of the topics above. (turn on/off the light) and anything else, If after 8pm set alarm for example ??

Nodered is drag n drop with just a few simple 'object' edits to complete your job, lots of getting started help too etc..

So Why would i add all that extra s/w and make it a bit more complicated??

Mosquitto a MQTT broker provides a method to send and receive or Publish and subscribe to, messages or topics/payloads.
This while core to NodeRed connectivity also provides you with an out of the box wifi interface enabeling you to add remote wifi sensors to your system with a simple configuration edit.

NodeRed provides you with a very powerful control interface for a system building. And a dashboard interface for a nice gui too

Alternatively there are also systems designed specifically for 'home' automation like openhab..

To compliment a Pi with Mosquitto and nodered i find esp8266 based boards and controllers are a nice cheap way to add wireless sensors and relays for input and output controlled by your pi..
Assuming your pi control system will expand.. if not leave as is if happy ;)

hope thats helpful

PhilBr
Posts: 62
Joined: Tue Oct 31, 2017 5:28 pm

Re: Magnetic Door / Reed Switch Using Rising Edge

Tue Aug 07, 2018 3:00 pm

So you want the lights on whilst the door is open, but off when you close it. Like a fridge light?
You will be in darkness if you are inside with the door closed. If that is what you want then it does not need a pi.

If you are saying that you want a door open/close sequence to toggle the light setting (ie turn it off if already on, but on if already off) then the system will 'forget' which state it should be in - eg it will be turned off if you admit a visitor. A switch on the lock bolt or a pir might do better.

Phil

CleverleyDone
Posts: 4
Joined: Wed Jul 18, 2018 2:26 pm

Re: Magnetic Door / Reed Switch Using Rising Edge

Sat Aug 11, 2018 7:25 pm

@PhatFil - Thanks for the reply, very much appreciated and thorough!

I have written my own system that allows me to turn on the GPIO's using a web interface built using PHP HTML CSS MySQL and Python run on Apache on the Pi.

It works well, all of the connected nodes updates if anyone has pressed the switch anywhere else (just garden based at the moment SONOFF inside).

Currently, I am adding automation, like this switch, I don't want to have to turn the light on my phone, table, laptop.. so last year :D :D :D hahha I want the system to react to the environment. I have started recording all of the data ready for version 3 AI enabled :)

@PhillBr Thank mate, what the script does at the moment is:

Door Open > Wait for door to close > Turn the light on > Light stays on > Door Open > Wait for door to close > Turn light off > Back to the start

It works perfectly but I wanted to know if there was a better way and if there was the potential for SD card burn.
Last edited by CleverleyDone on Fri Aug 24, 2018 2:02 pm, edited 1 time in total.

drgeoff
Posts: 9821
Joined: Wed Jan 25, 2012 6:39 pm

Re: Magnetic Door / Reed Switch Using Rising Edge

Sat Aug 11, 2018 9:41 pm

CleverleyDone wrote:
Sat Aug 11, 2018 7:25 pm
Door Open > Wait for door to close > Turn the light on > Light stays on > Door Open > Wait for door to close > Turn light off > Back to the start
So what happens when you are inside with the light on and someone else comes in and closes the door?

PhatFil
Posts: 1373
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Magnetic Door / Reed Switch Using Rising Edge

Mon Aug 13, 2018 12:29 pm

CleverleyDone wrote:
Sat Aug 11, 2018 7:25 pm
@PhatFil - Thanks for the reply, very much appreciated and thorough!

I have written my own system that allows me to turn on the GPIO's using a web interface built using PHP HTML CSS MySQL and Python run on Apache on the Pi.

It works well, all of the connected nodes updates if anyone has pressed the switch anywhere else (just garden based at the moment SONOFF inside).

Currently, I am adding automation, like this switch, I don't want to have to turn the light on my phone, table, laptop.. so last year :D :D :D hahha I want the system to react to the environment. I have started recording all of the data ready for version 3 AI enabled :)

@PhillBr Thank mate, what the script does at the moment is:

Door Open > Wait for door to close > Turn the light on > Light stays on > Door Open > Wait for door to close > Turn light off > Back to the start

It works perfectly but I wanted to know if there was a better way and if there was the potential for CD card burn.
If using sonoff devices the tasmota firmware is really flexible and the addition of a £8 RF-Bridge will allow you to use a shed load (upto 16x per bridge) of cheap as chips 433mhz RF devices my current favorite is the 1,2,or 3 gang rf switches that have a very thin profile allowing simple double sided tape attachment ontop of or next to a standard light switch panel https://www.aliexpress.com/item/Wall-Pa ... autifyAB=0

you can also source these with their own relay modules too ..

fwiw all the digoo range of 433mhz alarm devices i have tried with my sonoff bridges have all worked a-ok such as https://www.aliexpress.com/item/DIGOO-D ... utifyAB=0

if using the sonoff devices there is a very helpful and friendly google group 'sonoffusers' dedicated to the tasmota firmware its settings and installation. Tasmota is a Live project https://github.com/arendst/Sonoff-Tasmota with updates and upgrades being released frequently, the latest set of additions include independent alarm/timers linked via NTP for Dusk/dawn settings for your geographic location. And Rules for independent reaction to connected inputs.

CleverleyDone
Posts: 4
Joined: Wed Jul 18, 2018 2:26 pm

Re: Magnetic Door / Reed Switch Using Rising Edge

Tue May 21, 2019 11:56 pm

I'm sorry I haven't replied earlier.

Those switches look great and I think the 433 bridge is the way I would go if WIFI 6 was not on the horizon, having 15+ devices connected to the wireless just does not work well at the moment.

Looking at those switches it say in the desc:

Working Voltage: DC 12V 27A Battery (not include)
https://www.aliexpress.com/item/Wall-Pa ... 462510.htm

There has to be some mistake no?

I have flashed all the sonoffs to Tasmota and it's amazing, thanks!

Return to “Python”