Page 1 of 1

reading an input switch over mqtt

Posted: 05 Feb 2019, 22:08
by jupiter8
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.

Re: reading an input switch over mqtt

Posted: 05 Feb 2019, 22:33
by kenkoknz
what did you set the switch type to?

Re: reading an input switch over mqtt

Posted: 05 Feb 2019, 22:39
by jupiter8
Normal switch.

Re: reading an input switch over mqtt

Posted: 06 Feb 2019, 05:39
by grovkillen

Code: Select all

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

On PublishState Do
  Publish,/wemos/gpio/12,%eventvalue%
EndOn

Re: reading an input switch over mqtt

Posted: 06 Feb 2019, 06:11
by jupiter8
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

Re: reading an input switch over mqtt

Posted: 06 Feb 2019, 21:49
by kenkoknz
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.

Re: reading an input switch over mqtt

Posted: 06 Feb 2019, 23:50
by jupiter8
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.