Skip to content
View in the app

A better way to browse. Learn more.

Armbian Community Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

A pyGPIO python script for shutting down a BPI M2Z

Featured Replies

Hardware: Banana pi M2 Zero

OS Armbian_community_25.5.0-trunk.185_Bananapim2zero_bookworm_current_6.6.75_minimal

pyGPIO - A 'more general' python GPIO library from "chwe".

 

Question:

Could the below python script work for a safe "shutdown" of the MBI M2Z?

_____________________________________________________________________________

#!/usr/bin/python
# shutdown/reboot Banana Pi M2 Zero with pushbutton

from pyGPIO.gpio import gpio, connector
from subprocess import call
from datetime import datetime
import time

# Initialize GPIO
gpio.init()

# Pushbutton connected to this GPIO pin

shutdownPin = connector.GPIOp11  # Adjust based on your wiring

# If button pressed for at least this long, shut down. If less, reboot.
shutdownMinSeconds = 3

# Button debounce time in seconds
debounceSeconds = 0.1


# Set up the GPIO pin as input with pull-up resistor
gpio.setcfg(shutdownPin, gpio.INPUT)
gpio.pullup(shutdownPin, gpio.PULLUP)

buttonPressedTime = None

def buttonStateChanged(pin):
    global buttonPressedTime

    if gpio.input(pin) == gpio.LOW:  # Button is down (active low)
        if buttonPressedTime is None:
            buttonPressedTime = datetime.now()
    else:  # Button is up
        if buttonPressedTime is not None:
            elapsed = (datetime.now() - buttonPressedTime).total_seconds()
            buttonPressedTime = None
            if elapsed >= shutdownMinSeconds:
                # Button pressed for more than specified time, shutdown
                call(['shutdown', '-h', 'now'], shell=False)
            # Uncomment the following lines if you want to enable reboot functionality
            # elif elapsed >= debounceSeconds:
            #     # Button pressed for a shorter time, reboot
            #     call(['shutdown', '-r', 'now'], shell=False)

# Subscribe to button presses
gpio.set_irq(shutdownPin, gpio.BOTH, buttonStateChanged)

while True:
    # Sleep to reduce unnecessary CPU usage
    time.sleep(5)

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.