Rules GPIO state and toggle

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
marci
Normal 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
Normal 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: 3106
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
Normal 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: 62
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: 1268
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: 3106
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: 3106
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: 156
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: 3
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: Google [Bot] and 2 guests