Control relay with (reed) switch

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
puls8
New user
Posts: 9
Joined: 29 Feb 2020, 18:22

Control relay with (reed) switch

#1 Post by puls8 » 29 Feb 2020, 18:39

Hi all,

I have connected a 4 channel relay, a reed contact switch, a temperature sersor and a OLED SSD1306. The last two work perfectly. Now I want to switch the relay with the reed contact switch. I see the value changes when I open and close the switch in the devices page, the log and the OLED. The relay works when I "call": http://192.168.1.62/control?cmd=GPIO,16,0. The thing is I can't control it with a rule. I tried this:

Code: Select all

on door#state do
  if [door#state]=0
    gpio,16,0
else
   gpio,16,1
  endif
endon
and

Code: Select all

on door#state=0 do
    gpio,16,0
endon

on door#state=1 do
    gpio,16,1
endon
In the last setup I can turn the relay off with the switch and the rule, but I can't turn it back on.

Any help is appreciated

User avatar
ThomasB
Normal user
Posts: 1065
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Control relay with (reed) switch

#2 Post by ThomasB » 01 Mar 2020, 01:13

In the last setup I can turn the relay off with the switch and the rule, but I can't turn it back on.
Couple things come to mind:
1. Switch Input plugin configuration might be incorrect. Post a screenshot to the reed switch's plugin settings.
2. The wire leads to the reed switch might be too long (which invites coupled AC noise). What is the wire length and what is the pull-up resistor value?

- Thomas

puls8
New user
Posts: 9
Joined: 29 Feb 2020, 18:22

Re: Control relay with (reed) switch

#3 Post by puls8 » 01 Mar 2020, 22:01

Hi Thomas,

Thanks for your help
ThomasB wrote: 01 Mar 2020, 01:13 1. Switch Input plugin configuration might be incorrect. Post a screenshot to the reed switch's plugin settings.
See the attachment
2. The wire leads to the reed switch might be too long (which invites coupled AC noise). What is the wire length and what is the pull-up resistor value?
This is a test setup: the wires are 10 cm at max.

Your remarks made me double check the wiki; I connected the switch to ground. After connecting to 3v, nothing is happening at all...
Attachments
Schermafdruk van 2020-03-01 21-17-44.png
Schermafdruk van 2020-03-01 21-17-44.png (64.44 KiB) Viewed 9724 times

User avatar
ThomasB
Normal user
Posts: 1065
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Control relay with (reed) switch

#4 Post by ThomasB » 01 Mar 2020, 22:56

See the attachment
Since you are using a electro-mechanical switch a De-bounce value is needed. For your magnetic reed switch application I suggest changing the 0mS value to 100mS.

The settings at the bottom of the plugin's screenshot are missing. So I cannot see the Values name. Your rules use "door#state" for the switch so please confirm that "state" is what is entered as the Values name.
This is a test setup: the wires are 10 cm at max.
The GPIO's internal PullUp should be fine for the reed switch's short 10cm wires. An external pull-up resistor (with a low R value) will be needed on installations with long wire leads.
Your remarks made me double check the wiki; I connected the switch to ground. After connecting to 3v, nothing is happening at all...
One wire of the switch should go to GPIO-14. The switch's other wire should go to ground (not 3V).

Keep in mind that if the door is opened, then quickly shut, there is the possibility that the light may never turn on. And vice versa. So allow sufficient open and close times during the tests.
The relay works when I "call": http://192.168.1.62/control?cmd=GPIO,16,0
Does the relay reliably turn on too? For example: http://192.168.1.62/control?cmd=GPIO,16,1

If you are experiencing issues with GPIO control of the 4-channel relay then please post a vendor link to it.

Generally speaking, I don't recommend directly controlling GPIO from the http URI. Instead create a RelayOn and RelayOff rule and http control it using the "event" command. See Event:
https://www.letscontrolit.com/wiki/inde ... _Reference

- Thomas

puls8
New user
Posts: 9
Joined: 29 Feb 2020, 18:22

Re: Control relay with (reed) switch

#5 Post by puls8 » 02 Mar 2020, 21:35

ThomasB wrote: 01 Mar 2020, 22:56 Since you are using a electro-mechanical switch a De-bounce value is needed. For your magnetic reed switch application I suggest changing the 0mS value to 100mS.
Changed that as you said
The settings at the bottom of the plugin's screenshot are missing. So I cannot see the Values name. Your rules use "door#state" for the switch so please confirm that "state" is what is entered as the Values name.
added the bottom of the switch settings page to rule out typo's
One wire of the switch should go to GPIO-14. The switch's other wire should go to ground (not 3V).
Changed that back
Keep in mind that if the door is opened, then quickly shut, there is the possibility that the light may never turn on. And vice versa. So allow sufficient open and close times during the tests.
I am a patient guy; I wait till the value changes in the devices page
Does the relay reliably turn on too? For example: http://192.168.1.62/control?cmd=GPIO,16,1
Yes, no problem.

I tried this rule, but this doesn't work either at all:

Code: Select all

on door#state do
   if [door#state]=1
    gpio,16,0
   endif
   if [door#state]=0
    gpio,16,1
   endif
endon
To be complete, here is my full rules page:

Code: Select all

on System#Boot do
   gpio,0,1
   gpio,2,1
   gpio,5,1
   gpio,16,1
endon

on Clock#Time=All,03:10 do
   timerSet 8,59
endon

on Rules#Timer=8 do
   WifiDisconnect
   Reboot
endon 

on door#state=0 do
   gpio,16,1
endon

on door#state=1 do
   gpio,16,0
endon
@thomas: thanks for taking the time to help me out.
Attachments
Schermafdruk van 2020-03-02 21-09-35.png
Schermafdruk van 2020-03-02 21-09-35.png (37.32 KiB) Viewed 9656 times

puls8
New user
Posts: 9
Joined: 29 Feb 2020, 18:22

Re: Control relay with (reed) switch

#6 Post by puls8 » 02 Mar 2020, 22:03

Updating to the latest firmware does not help.

I was thinking to reverse the logic of the rule:

Code: Select all

on door#state=0 do
   gpio,16,0
endon

on door#state=1 do
   gpio,16,1
endon
I was thinking that it would turn the relay on, but nothing is happing at all. I am clueless...

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Control relay with (reed) switch

#7 Post by grovkillen » 02 Mar 2020, 23:20

Code: Select all

on door#state do
   gpio,16,[door#state]
endon
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

puls8
New user
Posts: 9
Joined: 29 Feb 2020, 18:22

Re: Control relay with (reed) switch

#8 Post by puls8 » 03 Mar 2020, 21:21

grovkillen wrote: 02 Mar 2020, 23:20

Code: Select all

on door#state do
   gpio,16,[door#state]
endon
Does not work either...

Next step to avoid typos: I cleared the rules section and added only these three lines:

Code: Select all

on System#Boot do
   gpio,16,0
endon
This should turn on the relay on boot, but no... (also tried gpio,16,1 (to double check)). Looks like the rules aren't picked up?!

update 1:
First I did a factory reset, but no different behavior. Next I made the following to check if rules are working:

Code: Select all

on System#Boot do
   gpio,16,0
   TaskValueSet 1,1,256
endon
256 is indeed added to the value of the dummy device, but the relay is still not turned on at boot...

update 2:
Changed the GPIO pin; still the same.

TD-er
Core team member
Posts: 8749
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Control relay with (reed) switch

#9 Post by TD-er » 03 Mar 2020, 22:22

GPIO-16 doesn't have a pull up resistor.
See: https://espeasy.readthedocs.io/en/lates ... on-esp8266

User avatar
ThomasB
Normal user
Posts: 1065
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Control relay with (reed) switch

#10 Post by ThomasB » 03 Mar 2020, 22:26

Very odd problem.

1. Get rid of any unused rule files by going to Tools=>FileSystem=>FileBrowser. Delete all rulesX.txt files except rules1.txt.

2. Assign a spare GPIO to a LED (with current limit resistor). Write some rules to test the LED (like you did to test the relay).

If the LED works correctly then there is something wrong with the relay module (or it is being used incorrectly).

Some 4-Channel relays use the serial port to control them. Some use GPIO, but can be unreliable if the pin's drive current is insufficient. Please post a link to the online store's relay module that you are using.

- Thomas

puls8
New user
Posts: 9
Joined: 29 Feb 2020, 18:22

Re: Control relay with (reed) switch

#11 Post by puls8 » 04 Mar 2020, 21:43

Thanks, I tried 3 totally different relays (3v, 5v, one channel and four channel). I could not image all those relays were suddenly broken. Also I used another NodeMCU with the same code and still it wasn't working. Then I copy-paste a rule set (only the boot thing) from a working setup. Adapted the gpio and voila, it worked!

The working code is really exactly the same as the non-working code. What's going on!? So I put both codes in a ascii decoder and it appeared that the not working code had for space not ascii code 32 but 160 (non-breaking space). Probably, I recycled the code from somewhere (wiki?) and copy-pasted the "wrong" spaces.

I would like to thank ThomasB, TD-er and Grovkillen for their help!

TD-er
Core team member
Posts: 8749
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Control relay with (reed) switch

#12 Post by TD-er » 04 Mar 2020, 21:54

Hmm in the rules?
I guess it would be nice then to filter out characters > 127.
We can use them only in strings, or references to task#varname

User avatar
ThomasB
Normal user
Posts: 1065
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Control relay with (reed) switch

#13 Post by ThomasB » 04 Mar 2020, 21:55

So I put both codes in a ascii decoder and it appeared that the not working code had for space not ascii code 32 but 160 (non-breaking space).
Good catch.

- Thomas

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 61 guests