Create rules for 8 buttons/relays + LCD display

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
rudloffl
Normal user
Posts: 43
Joined: 04 Jul 2019, 07:17

Create rules for 8 buttons/relays + LCD display

#1 Post by rudloffl » 20 Jul 2019, 16:45

Hello all,

Still working on my sprinkler system. The esp8266 is connected to 2 PCF8574 (relays and buttons), 1 LCD display and thermometer (GPIO).

I tried to link the button pressing acting on the relay:
- one pression turns the selected relay on
- one other pression turns off the relay
- A pression on a different zone turns on the selected relay and turns off the previous relay
- I must update the display
- I'm using 2 dummy variable (quad) to publish the relay status to the home automation system
- I will use timer to turn off the relays automatically

My problem is I can't get the rules to run properly, most button press (if tight with a rule) results in a crash. Calling the event via http works OK.

Can someone review my code and guide to some improvments/solutions:
- Is the syntax OK ?
- What can be wrong on the hardware side, am i asking too much to the esp ?
- Catch more errors in the log ?
- How deep can I nest the events (I'm coming from python...)
- I noticed sometime that IF/THEN/ELSE with %eventvalues% behaves not as expected
- Anything else...

Code: Select all

On System#Boot Do
event,alloff
endOn

//Button actions
on button8#State Do
if [button8#State]=1
Event,button=8,5
endif
EndOn

on button7#State Do
if [button7#State]=1
Event,button=7,10
endif
EndOn

//General feature called
on button Do
if [VAR#%eventvalue1%]=0
event,relayon=%eventvalue1%,%eventvalue2%
else
event,relayoff=%eventvalue1%
endif
endon

//Turns everything down
On alloff Do
Let,16,0
event,relayoff=1
event,relayoff=2
event,relayoff=3
event,relayoff=4
event,relayoff=5
event,relayoff=6
event,relayoff=7
event,relayoff=8
EndOn

On relayoff Do //turns off the relay number
let,%eventvalue%,0 // Sets the variable
pcfgpio,%eventvalue%,1
timerSet,%eventvalue%,0
endif
endon

On relayon Do //Turns on the relay number for defined number of secondes
let,%eventvalue%,1
let,16,1
pcfgpio,%eventvalue1%,0
timerSet,%eventvalue1%,%eventvalue2%
endif
endon

On WiFi#Connected Do
LCD,1,1,%ip%
EndOn
Any guidance will be appreciated
Last edited by rudloffl on 20 Jul 2019, 16:50, edited 1 time in total.

rudloffl
Normal user
Posts: 43
Joined: 04 Jul 2019, 07:17

Re: Cretae rules for 8 buttons/relays + LCD display

#2 Post by rudloffl » 20 Jul 2019, 16:50

A picture of the beast for some context
20190718_223836_resized.jpg
20190718_223836_resized.jpg (669.48 KiB) Viewed 6650 times

User avatar
ThomasB
Normal user
Posts: 1065
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Create rules for 8 buttons/relays + LCD display

#3 Post by ThomasB » 20 Jul 2019, 20:13

For better review of the installation you should post a screenshot of the devices page.
- Thomas

rudloffl
Normal user
Posts: 43
Joined: 04 Jul 2019, 07:17

Re: Create rules for 8 buttons/relays + LCD display

#4 Post by rudloffl » 20 Jul 2019, 21:12

Let me know if you need anything else
Capture d’écran du 2019-07-20 14-11-28.png
Capture d’écran du 2019-07-20 14-11-28.png (220.48 KiB) Viewed 6636 times

rudloffl
Normal user
Posts: 43
Joined: 04 Jul 2019, 07:17

Re: Create rules for 8 buttons/relays + LCD display

#5 Post by rudloffl » 20 Jul 2019, 21:58

I think the problem comes from my schematic (the short test I did over the HTTP are ok), see the code below.

What's the best way to connect a switch to the pcf8574 (where to put the resistor) ?

Code: Select all

On System#Boot Do
event,alloff
endOn

on button8#State Do
if [button8#State]=1
Event,button=8,5
endif
EndOn

on button7#State Do
if [button7#State]=1
Event,button=7,10
endif
EndOn

on button Do
if [VAR#%eventvalue1%]=0
if [VAR#16]=1
event,alloff
endif
event,relayon=%eventvalue1%,%eventvalue2%
else
event,relayoff=%eventvalue1%
endif
endon

On alloff Do
Let,16,0
event,relayoff=1
event,relayoff=2
event,relayoff=3
event,relayoff=4
event,relayoff=5
event,relayoff=6
event,relayoff=7
event,relayoff=8
EndOn

On relayoff Do
let,%eventvalue%,0
pcfgpio,%eventvalue%,1
let,16,0
//timerSet,%eventvalue%,0
endif
endon

On relayon Do
let,%eventvalue1%,1
let,16,1
pcfgpio,%eventvalue1%,0
timerSet,%eventvalue1%,%eventvalue2%
endif
endon

On WiFi#Connected Do
LCD,1,1,%ip%
EndOn

User avatar
ThomasB
Normal user
Posts: 1065
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Create rules for 8 buttons/relays + LCD display

#6 Post by ThomasB » 21 Jul 2019, 00:22

- Is the syntax OK ?
Some comments that deserve your attention:
1.
if [VAR#%eventvalue1%]=0
AFAIK, that trickery is not allowed. The wiki says to copy the %eventvalue% to a dummy. Then act on the dummy value.
2.
let,%eventvalue%,0
Just to be cautionary, this might not be doing what you want. It would clear system variables (%v1% - %v8%), not the dummy's in your event list.
3.
timerSet,%eventvalue1%,%eventvalue2%
Timers are being initialized, but no event routines have been defined. Either remove the timer code or create the eight timer event routines.
- What can be wrong on the hardware side, am i asking too much to the esp ?
If you observe random ESP resets when a relay is turned On or Off then it could be due to transient voltage spikes from the relay coil. To confirm if resets are related to the relay operation then test the device with the relay module disconnected.
- Catch more errors in the log ?
Log message detail can be changed at Tools=>Advanced=>Log Settings
- How deep can I nest the events (I'm coming from python...)
Good question. I've never nested more than a couple deep using the MEGA releases, so I can't say how much nesting is possible before the stack goes wacky. Nesting success probably depends on the firmware version and the plugins that are being used.
- I noticed sometime that IF/THEN/ELSE with %eventvalues% behaves not as expected
Copy %eventvalues% to dummy vars (or system vars), then use the copies in your if/then/else.
What's the best way to connect a switch to the pcf8574 (where to put the resistor) ?
The PCF8574 has a weak pull-up resistors built into each I/O pin, so an external pull-up is rarely needed. Your push button switch should ground the input when it is pressed.

- Thomas

rudloffl
Normal user
Posts: 43
Joined: 04 Jul 2019, 07:17

Re: Create rules for 8 buttons/relays + LCD display

#7 Post by rudloffl » 21 Jul 2019, 01:10

Thanks for your feedback Thomas, it helps a lot.
I will update the code and keep you informed.

Post Reply

Who is online

Users browsing this forum: No registered users and 107 guests