delay gpio to Switch

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

delay gpio to Switch

#1 Post by hestia » 17 Oct 2021, 22:05

Hi,
I've a question / issue regarding the delay between a gpio update et the state on the switch
The devices
Screenshot 2021-10-17 215705.png
Screenshot 2021-10-17 215705.png (114.82 KiB) Viewed 5569 times
The code (extract)

Code: Select all

on Button2#State do
	Event,HandleButton=2,5,14
endon

on HandleButton do // relay, gpio, idx // the water valves
	LogEntry,'lock%eventvalue1%: [INT#%eventvalue1%]'
	if [INT#%eventvalue1%]=1
		gpio,%eventvalue2%,=![Relay%eventvalue1%#State]
		if [Relay%eventvalue1%#State]=1 // !!!!!!!!
			if [Relay4#State]=0 // start the pump if off
				gpio,15,1
			endif
			AsyncEvent,OnOffRelay=%eventvalue1%,0,%eventvalue3%,1
			TimerSet,6,15 // reset next tank level check after opening the valve
			TimerSet,7,1800
			Let,7,1
			TimerSet,8,1200
			Let,8,1
		else
			AsyncEvent,OnOffRelay=%eventvalue1%,0,%eventvalue3%,0
		endif	
		Let,%eventvalue1%,2 // to lock until DzTime
	elseif [INT#%eventvalue1%]=0 // after the boot 
		Let,%eventvalue1%,1
		LogEntry,'%eventvalue1% after boot' 
	else
		LogEntry,'locked'
		Let,%eventvalue1%,1+[INT#%eventvalue1%] // waiting for DzTime
		if [INT#%eventvalue1%]>3
			Let,%eventvalue1%,1 //if not message from dz, unlock the button after 3 times
		endif
	endif
endon
The log

Code: Select all

51573: EVENT: Button2#State=1
51825: ACT  : Event,HandleButton=2,5,14
51827: EVENT: HandleButton=2,5,14
52119: ACT  : LogEntry,'lock2: 1'
52121: lock2: 1
52138: ACT  : gpio,5,=!0
52140: GPIO : port#5: set to 1
52184: ACT  : AsyncEvent,OnOffRelay=2,0,14,0
After gpio,%eventvalue2%,=![Relay%eventvalue1%#State]
I'd think I had the switch with the state updated, but the log seemed to show the reverse

ESP_Easy_mega_20210223_normal_ESP8285_1M

Do I missed something?
Last edited by TD-er on 17 Oct 2021, 23:16, edited 1 time in total.
Reason: Changed quote tag into code tags

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: delay gpio to Switch

#2 Post by Ath » 17 Oct 2021, 22:19

I do recall that there was something fixed in the reporting of the actual GPIO state, after your rather old (~8 months) release of ESPEasy was was made available. Upgrading to a more current ESPEasy will most likely fix your issue.
/Ton (PayPal.me)

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

Re: delay gpio to Switch

#3 Post by TD-er » 17 Oct 2021, 23:23

I changed your message to make the rules be in code tags instead of quote tags, for readability.

I don't see the rules part for the relayonoff.
You are calling that event using asyncevent, but you have to keep in mind that those are being queued.
So by the time those are being executed, the other values not present as eventvalue may be different from when you called the asyncevent.
I think something like that is happening here.

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: delay gpio to Switch

#4 Post by hestia » 18 Oct 2021, 00:04

code tags instead of quote tags
sorry!
My question is about the lines before the AsyncEvent
Just between those two lines

Code: Select all

gpio,%eventvalue2%,=![Relay%eventvalue1%#State]
		if [Relay%eventvalue1%#State]=1 // !!!!!!!!
"Relay" is the switch of the "gpio"

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: delay gpio to Switch

#5 Post by hestia » 18 Oct 2021, 00:09

Ath wrote: 17 Oct 2021, 22:19 Upgrading to a more current ESPEasy will most likely fix your issue.
OTA seems not possible
Screenshot 2021-10-18 000841.png
Screenshot 2021-10-18 000841.png (18.74 KiB) Viewed 5552 times

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: delay gpio to Switch

#6 Post by Ath » 18 Oct 2021, 07:50

hestia wrote: 18 Oct 2021, 00:09 OTA seems not possible
That's usually the case on a unit with 1MB flash memory. But you still can update it via serial, the way you originally flashed it. ;)
/Ton (PayPal.me)

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

Re: delay gpio to Switch

#7 Post by TD-er » 18 Oct 2021, 09:40

hestia wrote: 18 Oct 2021, 00:04
code tags instead of quote tags
sorry!
My question is about the lines before the AsyncEvent
Just between those two lines

Code: Select all

gpio,%eventvalue2%,=![Relay%eventvalue1%#State]
		if [Relay%eventvalue1%#State]=1 // !!!!!!!!
"Relay" is the switch of the "gpio"
I do see some kind of locking mechanism.
First a small optimization, to make the event handler a bit smaller.
You check for the value of your 'locking variable' to be zero, what only happens at boot.
So why not set it to "1" at boot?
That will save a bit more in the event handling rule block.

Another thing to double check is the set of GPIO-4 & -5. They are default assigned for I2C.
If you're using them for a relay, please double check they are no longer assigned to I2C. (Hardware tab)
On more recent builds, these assignments will be shown in the GPIO selection combobox to help find issues like this.

About the problem you're seeing.
You do execute a GPIO command, but that may not immediately update the output of a task value.
The reason here is that the GPIO command is no longer part of the switch plugin.
It is now a core command, and thus it may not call plugins to perform updates (which I think is something that should be done though)

A way around this, is to enable monitoring on the relay GPIO and check for the GPIO value directly.
See the pinstate notation: https://espeasy.readthedocs.io/en/lates ... task-names

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: delay gpio to Switch

#8 Post by hestia » 18 Oct 2021, 13:26

First a small optimization, to make the event handler a bit smaller.
You check for the value of your 'locking variable' to be zero, what only happens at boot.
So why not set it to "1" at boot?
I do the test with the locking variable like that, because at the boot all the buttons (4) are triggered and I don't whant the water to flow at the boot ;-)
So the first time the button is triggered, I ignore it

Code: Select all

8932: EVENT: Time#Initialized
9443: EVENT: Button1#State=1
9716: ACT  : Event,HandleButton=1,12,13
9717: EVENT: HandleButton=1,12,13
9983: ACT  : LogEntry,'lock1: 0'
9984: lock1: 0
10078: ACT  : Let,1,1
10083: ACT  : LogEntry,'1 after boot'
10084: 1 after boot
10539: EVENT: Rules#Timer=4,1
10585: ACT  : timerSet,4,5
10588: ACT  : gpio,13,0
10590: GPIO : port#13: set to 0
11100: EVENT: Button2#State=0
11386: ACT  : Event,HandleButton=2,5,14
11387: EVENT: HandleButton=2,5,14
11657: ACT  : LogEntry,'lock2: 0'
11659: lock2: 0
11746: ACT  : Let,2,1
11750: ACT  : LogEntry,'2 after boot'
11751: 2 after boot
12226: EVENT: Button3#State=0
12515: ACT  : Event,HandleButton=3,4,15
12517: EVENT: HandleButton=3,4,15
12792: ACT  : LogEntry,'lock3: 0'
12793: lock3: 0
12882: ACT  : Let,3,1
12886: ACT  : LogEntry,'3 after boot'
12888: 3 after boot
13342: EVENT: Button4#State=1
13731: ACT  : Let,4,1
13737: ACT  : LogEntry,'4 after boot'
13738: 4 after boot
Perhaps it is possible with something in the Hardware but I didn't find it
GPIO-4 & -5. They are default assigned for I2C
It ok, no assignment
You do execute a GPIO command, but that may not immediately update the output of a task value.
The reason here is that the GPIO command is no longer part of the switch plugin.
Ok, so it is a feature ;-)
enable monitoring on the relay GPIO and check for the GPIO value directly
I'll look at this
Perhaps a another way

Code: Select all

if [Relay%eventvalue1%#State]=0 // !!!!
			gpio,%eventvalue2%,1
			if [Relay4#State]=0 // start the pump if off
				gpio,15,1
			endif
			AsyncEvent,OnOffRelay=%eventvalue1%,0,%eventvalue3%,1
			TimerSet,6,15 // reset next tank level check after opening the valve
			TimerSet,7,1800
			Let,7,1
			TimerSet,8,1200
			Let,8,1
		else
			gpio,%eventvalue2%,0
			AsyncEvent,OnOffRelay=%eventvalue1%,0,%eventvalue3%,0
		endif	
		Let,%eventvalue1%,2 // to lock until DzTim

It is about a SONOFF 4CH R3, perhaps there are some special thing regarding the buttons?

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

Re: delay gpio to Switch

#9 Post by TD-er » 18 Oct 2021, 14:23

The Sonoff 4ch unit is using an ESP8285 as far as I know.
This is important as it can then use GPIO pins for button/relay which were otherwise used by the SPI flash chip.
So make sure to check it is indeed using the ESP8285 and use that build, or else you may not be able to use the GPIO pins unavailable on ESP8266.

hestia
Normal user
Posts: 90
Joined: 06 Mar 2021, 08:27
Location: Paris

Re: delay gpio to Switch

#10 Post by hestia » 21 Oct 2021, 20:41

Thanks for the advice.
All was done like you said.
Usually the button / relay works fine. There was just a side effect on the testing of the relay state.
I did it simple not to change everything

Code: Select all

on HandleButton do // relay, gpio, idx // the water valves
	//LogEntry,'lock%eventvalue1%: [INT#%eventvalue1%]'
	if [INT#%eventvalue1%]=1
		if [Relay%eventvalue1%#State]=0
			gpio,%eventvalue2%,1
			if [Relay4#State]=0 // start the pump if off
				gpio,15,1
			endif
			AsyncEvent,OnOffRelay=%eventvalue1%,0,%eventvalue3%,1
I've tested the state relay before changing it and it's good.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 47 guests