reading an input switch over mqtt

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
jupiter8
Normal user
Posts: 10
Joined: 05 Feb 2019, 21:39

reading an input switch over mqtt

#1 Post by jupiter8 » 05 Feb 2019, 22:08

All wright, been reading and fiddling for 3 days on how to use a normal switch to flip an input pin high and low and read the result on Node-Red via mqtt.

The goal is to monitor the state of switches in the field like i do with arduinos or pi's.

Hardware: gpio 12 as input
device: switch input, normal switch, input pullup inverse logic,

rules:
on sw1#switch=1 do
publish /wemos1/gpio/12,1
endon

on sw1#switch=0 do
publish /wemos1/gpio/12,0
endon

switch leg1------>grnd
leg2----->1k resistor---->3.3
leg2 ---- >gpio0

So when i close the switch i'm receiving a 1 in node red, so fine, but when i open it it toggles between 0 and 1 at the rate of the debounce field in the device menu.
This has to do with the rule set because without rule it behave ok in the log screen. I tried many examples and i can't make it work otherwise.

What i'm i doing wrong?

Thanks for your help, wonderfull project.
Last edited by jupiter8 on 06 Feb 2019, 01:08, edited 1 time in total.

kenkoknz
Normal user
Posts: 64
Joined: 03 Jul 2018, 23:46
Location: New Zealand

Re: reading an input switch over mqtt

#2 Post by kenkoknz » 05 Feb 2019, 22:33

what did you set the switch type to?

jupiter8
Normal user
Posts: 10
Joined: 05 Feb 2019, 21:39

Re: reading an input switch over mqtt

#3 Post by jupiter8 » 05 Feb 2019, 22:39

Normal switch.

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

Re: reading an input switch over mqtt

#4 Post by grovkillen » 06 Feb 2019, 05:39

Code: Select all

on sw1#switch do
 Event,PublishState=[sw1#switch]
EndOn

On PublishState Do
  Publish,/wemos/gpio/12,%eventvalue%
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:

jupiter8
Normal user
Posts: 10
Joined: 05 Feb 2019, 21:39

Re: reading an input switch over mqtt

#5 Post by jupiter8 » 06 Feb 2019, 06:11

Thanks, but still no go, tried with a simple push button pin 12 to ground and on release still bouncing from 0 to 1 each 200 millisec.

Using mega-20190202

kenkoknz
Normal user
Posts: 64
Joined: 03 Jul 2018, 23:46
Location: New Zealand

Re: reading an input switch over mqtt

#6 Post by kenkoknz » 06 Feb 2019, 21:49

If you just want to send to mqtt the state of the switch, you can just tick the send to controller box, presuming you have set the mqtt broker correctly in controller, it will sent the state of the switch every time it changes state (interval set to 0) without the need to use any rules.

I had it working on a nodemcuv3, using gpio0 (flash) button which is on the board for convenience. In node red, I use simple flow to monitor the mqtt topic with just a mqtt node and the debug, the debug node shows the switch state correctly with no debounce issues. You can of course add a function node in NR to do what you want on the state change.

Another thing to try maybe is an external pull up resistor and not inverse.

jupiter8
Normal user
Posts: 10
Joined: 05 Feb 2019, 21:39

Re: reading an input switch over mqtt

#7 Post by jupiter8 » 06 Feb 2019, 23:50

Well, thanks for this.

I had this whole topic naming thing mixed up. :oops: I was subscribed on: /wemos2/gpio/12 like i did for a relay...

so i just subscribed on: /wemos2/# to see what's going on and discovered that the topic is in fact: /wemos2/sw1/Switch

now works perfectely.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 11 guests