temporarily disable a switch input

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
seersucker
Normal user
Posts: 24
Joined: 11 May 2020, 17:01

temporarily disable a switch input

#1 Post by seersucker » 30 Nov 2021, 19:56

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

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

Re: temporarily disable a switch input

#2 Post by ThomasB » 30 Nov 2021, 21:20

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).
Last edited by ThomasB on 30 Nov 2021, 23:48, edited 1 time in total.

seersucker
Normal user
Posts: 24
Joined: 11 May 2020, 17:01

Re: temporarily disable a switch input

#3 Post by seersucker » 30 Nov 2021, 23:21

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!.

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

Re: temporarily disable a switch input

#4 Post by TD-er » 30 Nov 2021, 23:30

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 !!!)

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

Re: temporarily disable a switch input

#5 Post by ThomasB » 30 Nov 2021, 23:46

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

Post Reply

Who is online

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