# What would be the proper way to add a power button for BalenaOS

**URL:** <https://forums.balena.io/t/what-would-be-the-proper-way-to-add-a-power-button-for-balenaos/40927>\
**Category:** balenaOS\
**Tags:** raspberrypi3\
**Created:** [October 31, 2019, 2:37pm UTC](https://forums.balena.io/t/what-would-be-the-proper-way-to-add-a-power-button-for-balenaos/40927 "2019-10-31T14:37:02Z")\
**Posts on this page:** 1\
**Showing post:** 34

<div class="post-metadata">

**Author:** ![KingstonSteele](https://avatars.discourse-cdn.com/v4/letter/k/77aa72/32.png) [@KingstonSteele](https://forums.balena.io/u/KingstonSteele)\
**Post date:** [December 21, 2019, 12:01am UTC](https://forums.balena.io/t/what-would-be-the-proper-way-to-add-a-power-button-for-balenaos/40927/34 "2019-12-21T00:01:14Z")

</div>

> [@KingstonSteele](#):
>
> FROM resin/%%RESIN\_MACHINE\_NAME%%-python #switch on systemd init system in container ENV INITSYSTEM on RUN install\_packages dbus ENV DBUS\_SYSTEM\_BUS\_ADDRESS=unix:path=/host/run/dbus/system\_bus\_socket # pip install python deps from requirements.txt # For caching until requirements.txt changes COPY ./requirements.txt /requirements.txt RUN pip install -r /requirements.txt COPY . /usr/src/app WORKDIR /usr/src/app CMD [“python”, “button.py”]

Well I changed up my dockerfile quite a bit here:

```auto
FROM balenalib/%%BALENA_MACHINE_NAME%%-debian-python:3.7.4

# Enable systemd init system
ENV INITSYSTEM on

# Set the working directory
WORKDIR /usr/src/app

RUN install_packages dbus

# Upgrade pip
RUN pip install --upgrade pip
COPY requirements.txt .
RUN pip install --user -r requirements.txt --no-cache-dir --disable-pip-version-check \
                --index-url https://www.piwheels.org/simple

# Copy everything into the container
COPY . ./
#Make sure scripts in .local are usable:
ENV PATH=/root/.local/bin:$PATH
ENV DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket
# Start application
CMD ["bash", "start.sh"]

```

## start.sh

```auto
#!/usr/bin/env bash

## connect to the host's system bus from the application container
export DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket

python button.py

```

## button.py

```auto
import RPi.GPIO as GPIO
import time
import dbus

# Set GPIO mode: GPIO.BCM or GPIO.BOARD
GPIO.setmode(GPIO.BOARD)

# Set pin 5 an an input, and enable the internal pull-up resistor
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

oldButtonState1 = True

while True:
    buttonState1 = GPIO.input(5)

    if buttonState1 != oldButtonState1 and buttonState1 == False :
        bus = dbus.SystemBus()
        boolean = dbus.Boolean(True)
        remote_object = bus.get_object('org.freedesktop.login1', '/org/freedesktop/login1')
        interface = dbus.Interface(remote_object, 'org.freedesktop.login1.Manager.PowerOff')

        print (interface)

    oldButtonState1 = buttonState1

time.sleep(1)

```

The result is still:

```auto
<Interface <ProxyObject wrapping <dbus._dbus.SystemBus (system) at 0x768e2fc0> :1.0 /org/freedesktop/login1 at 0x768f13d0> implementing 'org.freedesktop.login1.Manager.PowerOff' at 0x76582910>

```

---

_[View the full topic](https://forums.balena.io/t/what-would-be-the-proper-way-to-add-a-power-button-for-balenaos/40927)._
