Problems with GPIO push button on self build emonPi

Hi @pb66 i have been trying to do it this whole week,i am stumped, i looked for example code,but it does not seem to be, a very common thing that i am attempting.Never the less, i wont give up trying, i know that there must be a way, to solve one button.

Hi @pb66, looking at the current code here i have to define a new button that’s my 1st step.

    # Set up the buttons and install handlers

    # emonPi LCD push button Pin 16 GPIO 23
    # Uses gpiozero library to handle short and long press https://gpiozero.readthedocs.io/en/stable/api_input.html?highlight=button
    # push_btn = Button(23, pull_up=False, hold_time=5, bounce_time=0.1)
    # No bounce time increases responce time but may result in switch bouncing...
    logger.info("Attaching push button interrupt...")
    try:
        push_btn = Button(12, pull_up=False, hold_time=5)
        push_btn.when_pressed = buttonPress
        push_btn.when_held = buttonPressLong
    except: 
        logger.error("Failed to attach LCD push button interrupt...")

    logger.info("Attaching shutdown button interrupt...")
    try:
        shut_btn = Button(26, pull_up=False, hold_time=5) 
        shut_btn.when_pressed = preShutdown
        shut_btn.when_held = shutdown 
    except: 
        logger.error("Failed to attach shutdown button interrupt...")

     logger.info("Attaching push button interrupt...")
    try:
        back_btn = Button(16, pull_up=False, hold_time=5)# new back button
        back_btn.when_pressed = buttonPress
        back_btn.when_held = buttonPressLong
    except: 
        logger.error("Failed to attach LCD push button interrupt...")

I have realized that the page +=1

else:
            lcd[0] = 'Connecting...'
            lcd[1] = 'Please Wait'
            page +=1
its not how the display, shows the next page, to me it seems like the code sits and waits for push_btn to be pressed then just wrights the page to the displays.

I Think this is where it has to be altered. Could be wrong

def buttonPress():
    global page
    global lcd
    global logger

    now = time.time()


    if lcd.backlight:
        page += 1
    if page > max_number_pages:
        page = 0
    buttonPress_time = now
    if not lcd.backlight:
        lcd.backlight = 1
    logger.info("Mode button SHORT press")
    logger.info("Page: " + str(page))
    updateLCD()

My thought on this

def buttonPress_back():
    global page
    global lcd
    global logger

    now = time.time()


    if lcd.backlight:
        page += 1
    if page > max_number_pages:
        page = 0
    buttonPress_time = now
    if not lcd.backlight:
        lcd.backlight = 1
    logger.info("Mode button SHORT press")
    logger.info("Page: " - str(page))
    updateLCD()

Ok so it does not like -

 self.when_activated()
  File "emonPiLCD.py", line 161, in buttonPress
    logger.info("Page: " - str(page))
TypeError: unsupported operand type(s) for -: 'str' and 'str'

You cannot subtract a string from a string!

I think you misunderstand what the original “+” does. The “+” in this instance was concatenating 2 strings (not summing them mathematically). That should remain as a “+” not a “-” as regardless of your direction of travel through the pages

    logger.info("Page: " + str(page))

is correct, it is printing "Page: " PLUS the page number as a string.

Thank you for the explanation, back to the drawing board :slight_smile:

That was only a single error message, you are on the right tracks with defining another button, you just didn’t need to change that particular “+”. The Error messages given to you when your code fails are very useful, you need to digest what they are telling you and use that info to progress. Do not be disheartened by a error message, they are just a tool, I get them all the time, often I entice them to happen so that I can gain insight as to what is going on.

Thank you for that, it seems like the most simplest thing, give you the hardest time.I must say once again thank you, because without your help, and a push here and there, i would probably have given up hope, you have helped me better ,this product, and i just know, i cant be the only one, who has thought about doing these changes . I really hope one off the emonpi developers see what you have done here, and include your changes, an credit you for it. Allot of time spent here helping me, and i see others, to me you a code GENIUS and ill never forget your help,thank you again

    logger.info("Attaching push button interrupt...")
    try:
        back_btn = Button(16, pull_up=False, hold_time=5)
        back_btn.when_pressed = buttonPress
        back_btn.when_held = buttonPressLong
    except: 
        logger.error("Failed to attach LCD push button interrupt...")


that i take it is correct?

now changing that

    if lcd.backlight:
        page -= 1 #from + to - makes the page go back but only goes as far as wifi
    if page > max_number_pages:
        page = 0
    buttonPress_time = now
    if not lcd.backlight:
        lcd.backlight = 1
    logger.info("Mode button SHORT press")
    logger.info("Page: " + str(page))
    updateLCD()

So i think that this is how it must be


    logger.info("Attaching push button interrupt...")
    try:
        push_btn = Button(12, pull_up=False, hold_time=5)
        push_btn.when_pressed = buttonPressback #added (back) next def buttonpressback
        push_btn.when_held = buttonPressLong
    except: 
        logger.error("Failed to attach LCD push button interrupt...")
def buttonPress():
    global page
    global lcd
    global logger

    now = time.time()


    if lcd.backlight:
        page += 1
    if page > max_number_pages:
        page = 0
    buttonPress_time = now
    if not lcd.backlight:
        lcd.backlight = 1
    logger.info("Mode button SHORT press")
    logger.info("Page: " + str(page))
    updateLCD() 

def buttonPressback():# added (back)
    global page
    global lcd
    global logger

    now = time.time()


    if lcd.backlight:
        page -= 1 #changed from + to -
    if page > max_number_pages:
        page = 0
    buttonPress_time = now
    if not lcd.backlight:
        lcd.backlight = 1
    logger.info("Mode button SHORT press")
    logger.info("Page: " + str(page))
    updateLCD()

What stumps me is that it stops at wifi, but if i look at wifi page its at page 0, so that can only mean it can only go that far

    # Now display the appropriate LCD page
    if page == 0:
        # Update ethernet
        eth0ip = ipaddress.get_ip_address('eth0')
        r.set("eth:active", bool(eth0ip))
        r.set("eth:ip", eth0ip)

        wlan0ip = ipaddress.get_ip_address('wlan0')
        r.set("wlan:active", bool(wlan0ip))

        if eval(r.get("eth:active")):
            lcd[0] = "Ethernet: YES"
            lcd[1] = r.get("eth:ip")
        elif eval(r.get("wlan:active")) or eval(r.get("gsm:active")):
            page += 1
        else:
            lcd[0] = "Ethernet:"
            lcd[1] = "NOT CONNECTED"

I’m only passing through so I haven’t read all your changes, but when you go up the page numbers you eventually run out of pages, this bit

if page > max_number_pages:
        page = 0

rolls over the page number back to zero for another lap.

You need a similar mechanism or when you go down the pages, ie when you are on the wifi page aks page 0 what page comes next when you minus 1? there is no page “-1”, it needs to be rolled over to the last page aka “max_number_pages”. something like

if page < 0:
        page = max_number_pages

Or even (max_number_pages - 1) ?

I did wonder, but since the original is if page > max_number_pages: I presume max_number_pages is a valid page otherwise it would need to be >= not >.

Either way, it’s a trap to be wary of.

The benefit of meaningful variable names can never be underestimated. If the max number of pages is 5, but the first page number is zero, then the last page number would be 4.

I like rotating through something like a page number using mod or rem functions (depending on language).

Very true. I mentioned it because @tonertiffi appears to be struggling with the coding, and could well have been confused if he landed on a non-existent page, one above the actual “last page”.

FTFY.

If the number of active pages is really (max_number_pages + 1), then it is sloppy coding and a fault by design, not a bug, in the original code.
[Or the name should be max_page_number.]

What I haven’t looked at is where the page number increments/decrements in relation to the statements that display the page. There could be another problem associated with that.