Rules GPIO state and toggle

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Rules GPIO state and toggle

#1 Post by iron » 24 Sep 2016, 09:32

Hello,

I searched the forum for GPIO status return within the Rules but without any luck.

I have a push button switch on input GPIO 14 (ONOFF#Switch) and a Solid State Relay on output GPIO 16

The objective is to toggle the status of GPIO 16 when there is an event on GPIO 14

Something like :

On ONOFF#Switch do
if [gpio,16]=1
then
gpio,16,0
else
gpio,16,1
endif
endon

I could not get the GPIO status return condition to work even after guestimating various syntax combintations.

Thank you
Dimitri
-D

jgrad
Normal user
Posts: 92
Joined: 29 Aug 2016, 22:03
Location: Slovenia

Re: Rules GPIO state and toggle

#2 Post by jgrad » 26 Sep 2016, 11:14

similar question (and still without answer) here
http://www.esp8266.nu/forum/viewtopic.php?f=6&t=2029.

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: Rules GPIO state and toggle

#3 Post by iron » 26 Sep 2016, 11:37

I get the log of the pin with :

status,gpio,16

{
"log": "GPIO 16 Set to 1",
"plugin": 1,
"pin": 16,
"mode": "output",
"state": 1
}

One would expect the value of state (0 or 1)to be comperatable by default as Rules do not parse the entire expression.
-D

uhrheber
Normal user
Posts: 22
Joined: 26 Sep 2016, 14:03

Re: Rules GPIO state and toggle

#4 Post by uhrheber » 26 Sep 2016, 15:07

What about using the switch type "push button" like shown in the wiki: http://www.esp8266.nu/index.php/Switch

Code: Select all

Instead of a conventional state or latching push button, you could also use a momentary push button switch. In this case the ESP Easy will toggle between on/off.
Also, this pic shows how to get the state of a switch and change it's state accordingly (toggle it):
Image

In both cases, you have to define the GPIO as a switch first.

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: Rules GPIO state and toggle

#5 Post by iron » 26 Sep 2016, 18:11

The input Switch is defined as "input : pin mode" (under hardware tab) and assigned ONOFF name. The value of that returns without a problem.

It is the state of the output gpio I am looking for. The same gpio pin I want to toggle.
-D

uhrheber
Normal user
Posts: 22
Joined: 26 Sep 2016, 14:03

Re: Rules GPIO state and toggle

#6 Post by uhrheber » 27 Sep 2016, 09:22

The example from the wiki works, it toggles the output.
So they just define also the output as a switch, and read it's state.

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: Rules GPIO state and toggle

#7 Post by iron » 27 Sep 2016, 09:33

"Input Switch" is the only switch option definition available. (R120)

What am i missing ?
-D

uhrheber
Normal user
Posts: 22
Joined: 26 Sep 2016, 14:03

Re: Rules GPIO state and toggle

#8 Post by uhrheber » 27 Sep 2016, 21:14

Define your switch as "Switch Button Type: Push Button Active Low".

The write the rule:

on Button#Value do
if [Button#Value] = 1
EXTGPIO,16,1
else
EXTGPIO,16,0
endif
endon

Just tried it, works as a toggle button.

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: Rules GPIO state and toggle

#9 Post by iron » 27 Sep 2016, 21:25

This way GPIO,16 toggles based on the state of the push button switch on GPIO,14, but push button will go high when pressed and then back to low when released (or vice versa) so GPIO,16 will always end up with 1, no ? This works with a latch type switch, but I do not see how this would work with a push button.
Need to be able to compare the state of GPIO,16 within the IF condition. (Not the state of the input switch. This is just for triggering the event)
-D

uhrheber
Normal user
Posts: 22
Joined: 26 Sep 2016, 14:03

Re: Rules GPIO state and toggle

#10 Post by uhrheber » 28 Sep 2016, 10:14

Just try it, it'll work with a push button.

The button will be debounced and the button value only changes when a high to low edge is detected (active low switch), or when a low to high edge is detected (active high switch).

Meaning, every time you push the button, the value Button#Value toggles.

It works, I tried it yesterday.

You can even configure it as a one button dimmer.

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: Rules GPIO state and toggle

#11 Post by iron » 28 Sep 2016, 11:35

Hi,

This way we toggle the output based on the state of the input (while assuming the state of the output has not changed in the meantime).

The project is a light switch that can be toggled manualy via push button, actually a capacitive touch sensor (http://www.aerial.net/shop/product/161_ ... ensor.html ) that acts as a push button (high while touch sensed then back to low when finger removed).

The same light switch can also be controlled by external HTTP request so the correct way to handle this is to read the state of the actual output switch.
-D

uhrheber
Normal user
Posts: 22
Joined: 26 Sep 2016, 14:03

Re: Rules GPIO state and toggle

#12 Post by uhrheber » 28 Sep 2016, 21:19

Ok, found it.

Define device one as Switch Input, Push Button Active Low, pullup on, GPIO 13. Name it Lightswitch. Name the value1 as value.
Define device two as Switch Input, Normal Switch, pullup off, GPIO 12. Name it GPIO12. Name the value1 as value.

Connect a push button from GPIO 13 to ground, and an LED from GPIO 12 to ground.

Now write the following rules:

Code: Select all

on Lightswitch#value do
  if [GPIO12#value] = 0
    GPIO,12,1
  else 
    GPIO,12,0
  endif
endon

on toggle do
  if [GPIO12#value] = 1
     GPIO,12,0
  else
     GPIO,12,1
  endif
endon
Now you can push the button to toggle the LED.
And every time you call http://<IP_of_your_ESP8266>/control?cmd=event,toggle , the LED toggles as well.

You can also switch the LED explicitly on or off by calling: http://<IP_of_your_ESP8266>/control?cmd=GPIO,12,on (or ,off).

The next button press or call of the toggle URL will toggle the LED to the opposite state.

Have fun!

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: Rules GPIO state and toggle

#13 Post by iron » 29 Sep 2016, 11:48

You are the man !!!
I owe you a cold one (or two)

Thank you
Dimitri
-D

uhrheber
Normal user
Posts: 22
Joined: 26 Sep 2016, 14:03

Re: Rules GPIO state and toggle

#14 Post by uhrheber » 29 Sep 2016, 12:54

Glad I could help.

Next thing to try would be coupling two or more ESP8266 directly (without AP), and using one as relay driver and switch input, the other ones only as remote switches.
Maybe by just letting the remote ones call the event URL, or using the new EasyGlobalSync function.

BTW:
The Wiki is really great, but it could use some more real world examples. Would it be of interest if I put together some popular scenarios like this one and write a step-by-step
tutorial?

User avatar
iron
Normal user
Posts: 221
Joined: 24 Sep 2016, 08:37
Location: Greece
Contact:

Re: Rules GPIO state and toggle

#15 Post by iron » 29 Sep 2016, 13:04

You got my vote !! documentation and examples could use all the help they can take.
-D

NietGiftig
Normal user
Posts: 103
Joined: 16 Sep 2015, 20:32

Re: Rules GPIO state and toggle

#16 Post by NietGiftig » 29 Sep 2016, 21:03

uhrheber wrote: The Wiki is really great, but it could use some more real world examples. Would it be of interest if I put together some popular scenarios like this one and write a step-by-step tutorial?
Please do!!

uhrheber
Normal user
Posts: 22
Joined: 26 Sep 2016, 14:03

Re: Rules GPIO state and toggle

#17 Post by uhrheber » 29 Sep 2016, 21:12

So I tried to add another remote switch, using the udp sync method, as calling urls from rules isn't yet implemented.

I defined a toggle switch on the new board, and added the following rule:

Code: Select all

on Lightswitch2#value do
  sendTo 1,event,toggle
endon
This works, but isn't stable. After a while, the remote switch looses contact to the main node.

Anybody know of any better method? (Besides using mqtt).

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Rules GPIO state and toggle

#18 Post by papperone » 23 Oct 2016, 19:33

I use MQTT and all works with multiple ESPEasy switches controlling a single relay.
As well using NodeRed you can create soft link (switch to relay) and control all node behaviour.

I only still need to find a solution to fix the >1 sec delay in switch-GPIO reset once button is released (bounce)
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Rules GPIO state and toggle

#19 Post by xbmcnut » 14 Feb 2017, 03:19

I'm using R148 and I have two switches, one on GPIO12 as a sensor for my garage door and another on GPIO14 to drive the relay. The relay needs to turn on for one second then off again to emulate the integrated garage controller. The GPIO12 switch is called 'reed' and is set to 'Push Button Active Low to toggle and the GPIO14 switch is called 'relay'. This is rule I applied to turn the relay off after 1 second. The board is a NodeMCU v3 but I'll be using a low voltage Sonoff in the production unit.

Code: Select all

on System#Boot do
gpio,14,0 // Prevent relay turning on during boot
endon

on relay#state do
  if [relay#state]=1
  timerSet,1,1 // 1 second timer
endon

on Rules#Timer=1 do
  gpio,14,0 // Turn off relay
endon
If I send a MQTT message to /devicename/gpio/14 with a payload on 1, the relay turns on for one second which is great! I have the Value Name 1: for the 'reed' switch set to state so that when the door is closed, I get /garage/reed/state with either 1 or 0 (opened or closed).
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Rules GPIO state and toggle

#20 Post by xbmcnut » 16 Feb 2017, 04:36

uhrheber wrote:Ok, found it.

Code: Select all

on Lightswitch#value do
  if [GPIO12#value] = 0
    GPIO,12,1
  else 
    GPIO,12,0
  endif
endon

on toggle do
  if [GPIO12#value] = 1
     GPIO,12,0
  else
     GPIO,12,1
  endif
endon
Thank you! This is exactly what I needed. Can someone suggest a code tidy up for that below? I added code to yours to activate the LED on GPIO13 and popped some UDP commands at the bottom to allow two Sonoff's to work in unison in a 2-way light circuit. It my situation, the bulb (load) is only connected to one Sonoff which is common (power in on one switch, load on the other). This works a treat and both Sonoff's come on an off together as do the LED's. Send /devicename/gpio/12,1 also turns on both relays at the same time and the button/s can be used to turn off again.

Code: Select all

on button#state do
  if [relay#state] = 0
    GPIO,12,1
  else
    GPIO,12,0
  endif
endon

on toggle do
  if [relay#state] = 1
     GPIO,12,0
  else
     GPIO,12,1
  endif
endon

on relay#state do
if [relay#state] = 1
    gpio,13,0
else
   gpio,13,1
endif
endon

on relay#state do
if [relay#state] = 1
sendTo 2,GPIO,12,1
else
sendTo 2,GPIO,12,0
endif
endon
Other device has sendTo 2,GPIO,12,1.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

User avatar
toffel969
Normal user
Posts: 469
Joined: 03 Jan 2017, 10:58
Location: Germany

Re: Rules GPIO state and toggle

#21 Post by toffel969 » 16 Feb 2017, 07:59

xbmcnut wrote:I'm using R148 and I have two switches, one on GPIO12 as a sensor for my garage door and another on GPIO14 to drive the relay. The relay needs to turn on for one second then off again to emulate the integrated garage controller. The GPIO12 switch is called 'reed' and is set to 'Push Button Active Low to toggle and the GPIO14 switch is called 'relay'. This is rule I applied to turn the relay off after 1 second. The board is a NodeMCU v3 but I'll be using a low voltage Sonoff in the production unit.

Code: Select all

on System#Boot do
gpio,14,0 // Prevent relay turning on during boot
endon

on relay#state do
  if [relay#state]=1
  timerSet,1,1 // 1 second timer
endon

on Rules#Timer=1 do
  gpio,14,0 // Turn off relay
endon
If I send a MQTT message to /devicename/gpio/14 with a payload on 1, the relay turns on for one second which is great! I have the Value Name 1: for the 'reed' switch set to state so that when the door is closed, I get /garage/reed/state with either 1 or 0 (opened or closed).

hi, in a similiar setup(using udp sento> i had problems with reliability. after some weeks i found, the 1-state was not detected--> the timer never fired --> relais stayed at1. now im using "pulse" command, which includes the off command. not sure how to use it with mqtt.

Short pulses
To send a pulse to a certain pin:
http://<ESP IP address>/control?cmd=Pulse,<pin>,<state>,<duration>
Example to send an active high pulse on GPIO 2 for 500 mSeconds:
http://<ESP IP address>/control?cmd=Pulse,2,1,500
Domoticz on Raspi 2 -- 14 ESP units (hacked Sonoff,NodeMCUs, Wemos, self-built units) running with RC140- Mega 2.0.0 dev8

paxi
Normal user
Posts: 121
Joined: 02 Feb 2017, 00:48
Location: Germany

Re: Rules GPIO state and toggle

#22 Post by paxi » 16 Feb 2017, 09:21

uhrheber wrote:... Can someone suggest a code tidy up for that below? ...

Code: Select all

on button#state do
  if [relay#state] = 0
    GPIO,12,1
  else
    GPIO,12,0
  endif
endon

on toggle do
  if [relay#state] = 1
     GPIO,12,0
  else
     GPIO,12,1
  endif
endon

on relay#state do
if [relay#state] = 1
    gpio,13,0
else
   gpio,13,1
endif
endon

on relay#state do
if [relay#state] = 1
sendTo 2,GPIO,12,1
else
sendTo 2,GPIO,12,0
endif
endon
Other device has sendTo 2,GPIO,12,1.
Other device has sentto 1 I guess. ;)

Code: Select all

on button#state do
  event,toggle
  SendTo 2,event,toggle
endon

on toggle do
  if [relay#state] = 1
     GPIO,12,0
     GPIO,13,1
  else
     GPIO,12,1
     GPIO,13,0
  endif
endon
Should do the work with the same rules in unit 2 (with sendto 1 of course). The only problem with this is whenever a sendto command is missed (connection-less UDP!) both units will be out of sync - better replace sendto with sendtohttp. The remote command changes from gpio to TaskValueSet that alters the value of button#state.

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Rules GPIO state and toggle

#23 Post by xbmcnut » 16 Feb 2017, 09:31

paxi wrote:
uhrheber wrote:... Can someone suggest a code tidy up for that below? ...

The only problem with this is whenever a sendto command is missed (connection-less UDP!) both units will be out of sync - better replace sendto with sendtohttp. The remote command changes from gpio to TaskValueSet that alters the value of button#state.
Thank you! I've seen that already. Units do occasionally get out of sync. Not very often but enough to be annoying. Any change you could provide some sample code for your recommended approach using TaskValueSet?
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Rules GPIO state and toggle

#24 Post by xbmcnut » 16 Feb 2017, 10:04

paxi wrote:
uhrheber wrote:... Can someone suggest a code tidy up for that below? ...

Code: Select all

on button#state do
  if [relay#state] = 0
    GPIO,12,1
  else
    GPIO,12,0
  endif
endon

on toggle do
  if [relay#state] = 1
     GPIO,12,0
  else
     GPIO,12,1
  endif
endon

on relay#state do
if [relay#state] = 1
    gpio,13,0
else
   gpio,13,1
endif
endon

on relay#state do
if [relay#state] = 1
sendTo 2,GPIO,12,1
else
sendTo 2,GPIO,12,0
endif
endon
Other device has sendTo 2,GPIO,12,1.
Other device has sentto 1 I guess. ;)

Code: Select all

on button#state do
  event,toggle
  SendTo 2,event,toggle
endon

on toggle do
  if [relay#state] = 1
     GPIO,12,0
     GPIO,13,1
  else
     GPIO,12,1
     GPIO,13,0
  endif
endon
Should do the work with the same rules in unit 2 (with sendto 1 of course).
Unfortunately with your much cleaner version, I can no longer publish a MQTT topic to /sonoff2/gpio/12 with a payload of 1 to control both Sonoff's. It only turns on the one I instructed. The button on the Sonoff controls both however.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

paxi
Normal user
Posts: 121
Joined: 02 Feb 2017, 00:48
Location: Germany

Re: Rules GPIO state and toggle

#25 Post by paxi » 16 Feb 2017, 11:07

Too bad that TaskValueSet doesn't work via mttq...
We need another rule in the "master":

Code: Select all

on relay#state do
  if [relay#state]=1
    GPIO 13,0
  else 
    GPIO 13,1 
  endif 
  SendTo 2,event,toggle
endon 
Btw. how was the toggle event triggered in your original code?

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Rules GPIO state and toggle

#26 Post by xbmcnut » 16 Feb 2017, 11:48

paxi wrote:Too bad that TaskValueSet doesn't work via mttq...
We need another rule in the "master":

Code: Select all

on relay#state do
  if [relay#state]=1
    GPIO 13,0
  else 
    GPIO 13,1 
  endif 
  SendTo 2,event,toggle
endon 
Btw. how was the toggle event triggered in your original code?
I had not tried the TaskValueSet method as I'm a hardware guy trying to work my way through the code. This is my full code that works well albeit with a 95% success rate due to UDP.

Code: Select all

on button#state do
  if [relay#state] = 0
    GPIO,12,1
  else
    GPIO,12,0
  endif
endon

on toggle do
  if [relay#state] = 1
     GPIO,12,0
  else
     GPIO,12,1
  endif
endon

on relay#state do
if [relay#state] = 1
    gpio,13,0
else
   gpio,13,1
endif
endon

on relay#state do
if [relay#state] = 1
sendTo 2,GPIO,12,1
else
sendTo 2,GPIO,12,0
endif
endon
I figured that 'toggle' was natively supported and somehow it works fine. I can control both Sonoff's together by pushing either devices button or by sending MQTT to either devices GPIO. Reboot one Sonoff though and it comes back with the relay off (which is OK) but it creates and out of sync situation.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

paxi
Normal user
Posts: 121
Joined: 02 Feb 2017, 00:48
Location: Germany

Re: Rules GPIO state and toggle

#27 Post by paxi » 16 Feb 2017, 12:14

"toggle" is a user defined event, no native implementation in the firmware. If you dont call it from elsewhere its just a dead piece of code that never runs. It does the same as your button#state rule, that's why you think it works. Takes a while to get the hang of event driven programming ;)

Have a look at the global sync feature. It works only in one direction though, you'd need a dummy device for the other direction - and more rules...

Or you set up some pull/push rules triggered by system#boot.
Edit: like this

Code: Select all

On System#Boot do Sendto (otherdevice),event,pushstate

On pushstate do Sendto (otherdevice),event,getstate=[relay#state] 

On getstate do gpio 12,%eventvalue%

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Rules GPIO state and toggle

#28 Post by xbmcnut » 17 Feb 2017, 11:28

paxi wrote:"toggle" is a user defined event, no native implementation in the firmware.
Thanks for clearing that up. So much for my copy and paste from other threads. It clearly was not needed. To get around my intermittent UDP issue, I went with MQTT instead. This works flawlessly. Ensure that 'send boot state' is off for the relay on each device and that the retain flag is checked under advanced settings. With those set, if one unit reboots for any reason, it will come back up, connect with the broker and return to the 'last known state' keeping the two switches it sync. Nailed!

Code: Select all

on button#state do
  if [relay#state] = 0
    GPIO,12,1
  else
    GPIO,12,0
  endif
endon

on relay#state do
if [relay#state] = 1
    gpio,13,0
else
   gpio,13,1
endif
endon

on relay#state do
if [relay#state] = 1
Publish /sonoff2/gpio/12,1
else
Publish /sonoff2/gpio/12,0
endif
endon
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

paxi
Normal user
Posts: 121
Joined: 02 Feb 2017, 00:48
Location: Germany

Re: Rules GPIO state and toggle

#29 Post by paxi » 17 Feb 2017, 13:01

Glad you got it working now! And so easy - I don't use a sever thus wasn't aware of that tickbox.

You can still sum up the two seperate relay#state rules into one (like in my toggle example above). ;)

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: Rules GPIO state and toggle

#30 Post by xbmcnut » 20 Feb 2017, 09:18

paxi wrote:You can still sum up the two seperate relay#state rules into one (like in my toggle example above). ;)
Thank you! I tidied up my code and it works religiously via MQTT so very happy. I appreciate your input. :)
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

marci
New user
Posts: 9
Joined: 18 Aug 2017, 20:53

Re: Rules GPIO state and toggle

#31 Post by marci » 19 Aug 2017, 13:04

uhrheber wrote: 27 Sep 2016, 21:14 Define your switch as "Switch Button Type: Push Button Active Low".

The write the rule:

on Button#Value do
if [Button#Value] = 1
EXTGPIO,16,1
else
EXTGPIO,16,0
endif
endon

Just tried it, works as a toggle button.

I tried the use of EXTGPIO command inside a rule with a recent version (Mega dev10) and it doesn't work!
On which version did you try ? To understand when it broke.

Tks

momon
New user
Posts: 6
Joined: 27 Nov 2017, 13:56

Re: Rules GPIO state and toggle

#32 Post by momon » 06 Dec 2017, 11:15

Hi

Im following this thread. I tried the set up. I gave the result I want.

I use capacitive touch module. When i touch the module the led on.. then when i touch again it off..

The problem now is the second touch gave a delay result.. its mean i need to touch again n again until the led off.. from my serial connection i saw that the system gave a HTTP connection failed. I wonder what is wrong. Please help me.. Thanks

Attached picture.
https://imgur.com/a/rbEXF

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

Re: Rules GPIO state and toggle

#33 Post by grovkillen » 06 Dec 2017, 12:44

What message delay you have?
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:

momon
New user
Posts: 6
Joined: 27 Nov 2017, 13:56

Re: Rules GPIO state and toggle

#34 Post by momon » 06 Dec 2017, 21:33

grovkillen wrote: 06 Dec 2017, 12:44 What message delay you have?
i attached an image in the link bottom my post

leel1967l
Normal user
Posts: 73
Joined: 30 Nov 2017, 18:29

Re: Rules GPIO state and toggle

#35 Post by leel1967l » 01 Feb 2018, 21:34

uhrheber wrote: 28 Sep 2016, 21:19 Ok, found it.

Define device one as Switch Input, Push Button Active Low, pullup on, GPIO 13. Name it Lightswitch. Name the value1 as value.
Define device two as Switch Input, Normal Switch, pullup off, GPIO 12. Name it GPIO12. Name the value1 as value.

Connect a push button from GPIO 13 to ground, and an LED from GPIO 12 to ground.

Now write the following rules:

Code: Select all

on Lightswitch#value do
  if [GPIO12#value] = 0
    GPIO,12,1
  else 
    GPIO,12,0
  endif
endon

on toggle do
  if [GPIO12#value] = 1
     GPIO,12,0
  else
     GPIO,12,1
  endif
endon
Now you can push the button to toggle the LED.
And every time you call http://<IP_of_your_ESP8266>/control?cmd=event,toggle , the LED toggles as well.

You can also switch the LED explicitly on or off by calling: http://<IP_of_your_ESP8266>/control?cmd=GPIO,12,on (or ,off).

The next button press or call of the toggle URL will toggle the LED to the opposite state.

Have fun!
Hi all,
with version R147 the previous code works, with MEGA-dev12 or dev13 it doesn't work. Actually it works for a few clicks then the unit reboots.
To be more precise, what does not work anymore in MEGA is the fact to declare a relay as an Input Switch.
It crashes the ESP.

Any idea why?

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

Re: Rules GPIO state and toggle

#36 Post by TD-er » 01 Feb 2018, 22:35

Please also check with later release builds.
Since dev12 and dev13, a lot of builds were made.

HAbuild
Normal user
Posts: 15
Joined: 10 Feb 2018, 08:16

Re: Rules GPIO state and toggle

#37 Post by HAbuild » 10 Feb 2018, 15:50

toffel969 wrote: 16 Feb 2017, 07:59
xbmcnut wrote:I'm using R148 and I have two switches, SHORTENED

hi, in a similiar setup(using udp sento> i had problems with reliability. after some weeks i found, the 1-state was not detected--> the timer never fired --> relais stayed at1. now im using "pulse" command, which includes the off command. not sure how to use it with mqtt.

Short pulses
To send a pulse to a certain pin:
http://<ESP IP address>/control?cmd=Pulse,<pin>,<state>,<duration>
Example to send an active high pulse on GPIO 2 for 500 mSeconds:
http://<ESP IP address>/control?cmd=Pulse,2,1,500
Thanks Pulse control works nicely! In the rules no less.

Code: Select all

On System#Boot do
  gpio,15,0
endon

On garage#door=1 do
  pulse,15,1,100
  gpio,15,0
  publish /MyHome/Garage/Door/,The Garage Door is Moving
   //if else needed for garage#door=1 > 1000
   //publish /MyHome/Garage/, RELAY FAULT - URGENT
endon
MQTT

Code: Select all

// Topic == Payload  
/%sysname%/Garage/cmd ==  GPIO,15,1,status,
/%sysname%/Garage/gpio/15/[b],[/b]1   //This one I don't like because I can't figure out how to get a JSON output to /$sysname$/status
/%sysname%/Garage/cmd  == PULSE,15,1,status,   // With this one I don't actually need the rule in the Code above but will keep it for redundancy, so I can't leave the relay on. 
	
	 
I haven't figured out how to code the next bit yet, just started Today lol. Feel free to poke best practice suggestions on that code.

grhhm
Normal user
Posts: 29
Joined: 23 Oct 2016, 20:15

Re: Rules GPIO state and toggle

#38 Post by grhhm » 02 Mar 2018, 13:40

I use R147 over Sonoff S20. Not a problem to tie a button to a relay:

Code: Select all

On btn#Switch do
   gpio,12,[btn#Switch]
endon
Button (btn) listens on GPIO0, active low. The problem is to control the relay from multiple sources:
- with button,
- with MQTT,
- with events.

The problem comes from button state: every press-release it changes from 0 to 1 and vice versa.
That means
if (button turned relay on and event turned relay off) then
on next button click the btn#switch value will be 0.
This won't activate relay despite it's off.

My question is:
How to toggle relay?
I.e. how change the GPIO status in dependency of its current state?
I.e. how to read the output GPIO status and invert it?

Tried This solution from earlier post. In R147 the [GPIO12#value] does not work: it's always (null). Maybe should I define something in "Devices"?

Thank you.

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

Re: Rules GPIO state and toggle

#39 Post by grovkillen » 02 Mar 2018, 14:53

The solution is to funnel all the inputs to an event.

Code: Select all

On btn#Switch Do
 If [btn#Switch]=0
  event,ToggleSwitch=1
 Else
  event,ToggleSwitch=0
 EndIf
EndOn

On ToggleSwitch Do
  gpio,12,%eventvalue%
EndOn
MQTT message/payload (topic <unit_name/cmd):

Code: Select all

event,ToggleSwitch=1
Or

Code: Select all

event,ToggleSwitch=0
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:

grhhm
Normal user
Posts: 29
Joined: 23 Oct 2016, 20:15

Re: Rules GPIO state and toggle

#40 Post by grhhm » 02 Mar 2018, 18:54

Thank you for quick response.
Here is full code block made with your help:

Code: Select all

On btn#Switch Do
 publish,shm/%sysname%/button,event[btn#Switch]
 If [btn#Switch]=0
  event,ToggleSwitch=1
 Else
  event,ToggleSwitch=0
 EndIf
EndOn
On ToggleSwitch=0 Do
 gpio,12,%eventvalue%
 publish,shm/%sysname%/relay,%eventvalue%
EndOn
On ToggleSwitch=1 Do
 gpio,12,%eventvalue%
 publish,shm/%sysname%/relay,%eventvalue%
 publish,shm/%sysname%/timerset,%systime%
 timerset,1,10 // run timer for 10 sec
EndOn
On Rules#Timer=1 do
 event,ToggleSwitch=0
 publish,shm/%sysname%/timerend,%systime%
endon
There are 2 line which create event,ToggleSwitch=0: in a button and in timer end sections.
This works while no button pressed again BEFORE timer ends. Or timer shuts relay off.
If relays off by timer then next button click just skipped. The reason is the same - button keeps own state and flips it with no relation to relay status.

Is there a way to read ToggleSwitch value when button pressed to decide which new ToggleSwitch value to set?
I think the "status" variable can be help by Dummy Device. However, I can't catch how to work with it :(

Thank you.
Last edited by grhhm on 02 Mar 2018, 19:48, edited 1 time in total.

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

Re: Rules GPIO state and toggle

#41 Post by grovkillen » 02 Mar 2018, 19:37

You could use a dummy device value to dump and check the current state, or use a switch device.
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:

grhhm
Normal user
Posts: 29
Joined: 23 Oct 2016, 20:15

Re: Rules GPIO state and toggle

#42 Post by grhhm » 03 Mar 2018, 13:10

OK, managed with it. Short guide for others.

Objective: Use Dummy device as internal variable to synchronize between different events and single actuator.
1. Add task DummyDevice. I use task #6, name 'glob' (global variable), type: SENSOR_TYPE_SINGLE, value name: rs (relay status)
2. In rules you can write into rs variable as: taskvalueset 6,1,0 // where 6 is a task#, 1 is a rs variable number, 0 is a value.
3. In rules to read the variable value you can use [glob#rs] // i.e. not numbers of task and variable, but their names. (grass?)
4. In rules you can not create trigger on task value change (at least, I could not in R147). I used taskvalueset to set required value and then event,Toggle,[glob#rs] to generate the event. Then event Toggle is processed as usual.

Hope this helps :)

kimot
Normal user
Posts: 190
Joined: 12 Oct 2017, 20:46

Re: Rules GPIO state and toggle

#43 Post by kimot » 05 Mar 2018, 14:04

Here is my solution to switch relay by push button, by event from another ESP ( sending command "lamp_change" ) or by command "lamp_on" or "lamp_off" from Domoticz

Code: Select all

on lamp_on do
    gpio,12,1
    gpio,13,0
endon

on lamp_off do
    gpio,12,0
    gpio,13,1
endon

on lamp_change do
  if [rele#Switch]=1
    event,lamp_off
    SendToHTTP 192.168.1.253,8080,/json.htm?type=command&param=switchlight&idx=19&switchcmd=Off
  else
    event,lamp_on
    SendToHTTP 192.168.1.253,8080,/json.htm?type=command&param=switchlight&idx=19&switchcmd=On
  endif
endon

on SW01#Switch do
  event,lamp_change
endon
GPIO 12 relay, GPIO 13 LED ( sonoff )

Local switch is defined like SW01#Switch, relay like rele#Switch on GPIO 12

With this rule, you can control your sonoff by local switch, by switch on others ESPeasys by sending event "lamp_change"
In this cases state of relay is reported to Domoticz, so Domoticz dashboard switch state corresponds actual state of relay.
And you can control relay from Domoticz by sending events "lamp_off" and "lamp_on
mega 2.0-dev13

jvm1000
Normal user
Posts: 11
Joined: 11 Feb 2017, 14:23

Re: Rules GPIO state and toggle

#44 Post by jvm1000 » 08 Oct 2018, 21:25

thanks a lot Kimot
works perfect

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests