Page 1 of 1

temporarily disable a switch input

Posted: 30 Nov 2021, 19:56
by seersucker
Hi, I 'm using a Wemos device to control a raspberry pi to power it off correctly.
It works OK, for shutdown a pushbutton long press generates a 500mS pulse to the Pi's GPIO3 which halts the pi OS
Then 15 Secs later, the Pi's power is disconnected after it has fully shut down.
The only problem comes when I forget that the shutdown timer is running and try to power it up again while it's still trying to shut down!
Is there a way to temporarily disable the pb1 switch input while the shutdown timer is active?
on pb1#State do
if %eventvalue% =10 //longpress
if %v2% = 0 and %v1% = 0 //switch is OFF and volts are OK

gpio,5,1 //pi relay power on
let,2,1 //toggle switch status to ON

else
pulse,12,1,500 //pi shutdown signal to gpio3
timerset,2,15 //allow pi to fully shut down before removing power

endif
endon

on rules#timer=2 do
gpio,5,0 // pi power relay off
let,2,0 //toggle switch flag to OFF
endon

Re: temporarily disable a switch input

Posted: 30 Nov 2021, 21:20
by ThomasB
One of your "if" statements is missing the matching endif.

Try this rule set:

Code: Select all

on pb1#State do
  if %eventvalue% =10 //longpress
    let,2,1           //Set power flag (ON)
    timerset,2,15     //pi shutdown timer
    pulse,12,1,500    //pi shutdown signal to GPIO12
  else                //Short press
    if %v2% = 0       //Power flag is OFF
      let,2,1         //Set power flag (ON)
      gpio,5,1        //pi relay power on
    else
      logentry,"Shutdown active, Ignored request to turn pi on"
    endif
  endif
endon

on rules#timer=2 do
  gpio,5,0 //pi power relay off
  let,2,0  //Clear power flag (OFF)
endon 
- Thomas
Edit: fixed typo (as described further below).

Re: temporarily disable a switch input

Posted: 30 Nov 2021, 23:21
by seersucker
Thanks, ThomasB for the speedy reply, which indeed fixed that problem.
It didn't work straight away though, the logs showed this message:

Code: Select all

Command unknown: 'elseif'
I guess this is a quirk of the mega-20190216 firmware I'm using. That's the most recent version that I could get to work with this Wemos D1 R2.

When "elseif" was replaced with "else" it all came good :roll:

There must be a moral or two there, but whatever, it's working, thanks again.
Maybe that Wemos will go back to the junk box!.

Re: temporarily disable a switch input

Posted: 30 Nov 2021, 23:30
by TD-er
A flaky Wemos board often has a very limited (in max current) voltage regulator on board, or a really poor quality USB connector.
You may want to test using a different USB cable and/or adding a capacitor of between 22uF and 100 uF on the 3V3 and GND pins. (mind the polarity !!!)

Re: temporarily disable a switch input

Posted: 30 Nov 2021, 23:46
by ThomasB
When "elseif" was replaced with "else" it all came good
My apologies, that was a typo. Should have been "else" instead of elseif. I'll edit the example code to show the correct syntax.

Glad to hear you solved the problem.

- Thomas