# Activating GPIO remotely

**URL:** <https://forums.balena.io/t/activating-gpio-remotely/369494>\
**Category:** Project help\
**Created:** [December 11, 2023, 12:00pm UTC](https://forums.balena.io/t/activating-gpio-remotely/369494 "2023-12-11T12:00:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tek](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tek](https://forums.balena.io/u/tek)\
**Post date:** [December 11, 2023, 12:00pm UTC](https://forums.balena.io/t/activating-gpio-remotely/369494/1 "2023-12-11T12:00:50Z")

</div>

We are working on a project where we would have to activate GPIO remotely in either way.

I initially thought of activating GPiO from actions and use the API to integrate with that. However that seems not possible, and i started to hink of having a service container with a single function - to activate GPIO sleep and then shut off.

However i dont seem to find a way to do this from the API docs. Are there any way where i can connect to the public Balena-Cloud API and remotely start a container?

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [December 11, 2023, 12:06pm UTC](https://forums.balena.io/t/activating-gpio-remotely/369494/2 "2023-12-11T12:06:34Z")

</div>

Hello @tek could you please confirm what hardware are you using?

Thanks

---

<div class="post-metadata">

**Author:** ![tek](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tek](https://forums.balena.io/u/tek)\
**Post date:** [December 11, 2023, 2:59pm UTC](https://forums.balena.io/t/activating-gpio-remotely/369494/3 "2023-12-11T14:59:59Z")

</div>

hi @mpous,

We are currently using Raspberry PI 4`s on balenaOS

---

<div class="post-metadata">

**Author:** ![tek](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tek](https://forums.balena.io/u/tek)\
**Post date:** [December 12, 2023, 11:56am UTC](https://forums.balena.io/t/activating-gpio-remotely/369494/4 "2023-12-12T11:56:20Z")

</div>

I have re-read the documentation once more, and i think what i originally suggested is possible with the `POST /supervisor/` proxy.

By using the supervisor proxy, i would be able to remote start the container with singel functionality of activating GPIO. If i am not mistaken `POST /supervisor/v2/applications/:appId/start-service` would be the correct endpoint in combination with restart: “no” in the docker-compose for the service container?

Or are there any other quicker way of activting GPIO like directly from the supervisor api etc?

---

<div class="post-metadata">

**Author:** ![chrisys](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/chrisys/32/1731_2.png) [@chrisys](https://forums.balena.io/u/chrisys)\
**Post date:** [December 12, 2023, 9:58pm UTC](https://forums.balena.io/t/activating-gpio-remotely/369494/5 "2023-12-12T21:58:27Z")

</div>

Hey @tek

I think you’re probably heading down the wrong path here and expecting the balena platform to do more of what you’re looking for than it’s intended. The platform is very much intended to provide the foundation for you to build upon, so it doesn’t really get involved with controlling GPIOs or providing API endpoints to control GPIOs, you’ll need to figure out the solution for that for your use case.

I guess what you’re proposing _might_ work - assuming you’re doing something very simple and infrequent, but a better solution would be to run a service in a container that has it’s own API endpoint that can then control the API. In the past I’ve used pigpiod to accomplish this: [pigpio library](https://abyz.me.uk/rpi/pigpio/pigpiod.html)

You can then connect to that daemon over the network to control the GPIOs remotely.

I created a block (based on a fork of someone else’s implementation) to use for this which you can see here: [balenaHub - IoT embedded device apps, edge computing resources, and developer tools](https://hub.balena.io/blocks/2055230/pigpio)

Then, to run it is as simple as pushing a docker-compose.yml file with the following contents:

```auto
version: '2'
services:
  pigpiod:
    image: bh.cr/gh_chrisys/pigpio
    ports:
     - "8888:8888"
    privileged: true

```

You can then use something like this to control the GPIO over the network:

```auto
import pigpio
import time

# Replace with the IP address of the device running pigpiod
PIGPIOD_HOST = '192.168.1.100'
GPIO_PIN = 17 # Replace with your GPIO pin number

# Connect to pigpiod
pi = pigpio.pi(PIGPIOD_HOST)

if not pi.connected:
    print("Failed to connect to pigpiod. Make sure it's running and accessible.")
    exit()

try:
    # Set the GPIO pin as an output
    pi.set_mode(GPIO_PIN, pigpio.OUTPUT)

    # Toggle the GPIO pin
    print("Toggling GPIO pin")
    pi.write(GPIO_PIN, 1) # Turn on
    time.sleep(1) # Wait for 1 second
    pi.write(GPIO_PIN, 0) # Turn off

finally:
    # Cleanup and disconnect
    pi.stop()
    print("Disconnected from pigpiod")

```

---

<div class="post-metadata">

**Author:** ![tek](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tek](https://forums.balena.io/u/tek)\
**Post date:** [December 13, 2023, 8:08am UTC](https://forums.balena.io/t/activating-gpio-remotely/369494/6 "2023-12-13T08:08:14Z")

</div>

Thanks for your answers @chrisys ,

Ideally what you are suggesting would be the preferred way, and this might not be the route balena was intended for. However it would add complexity on all sites including a way of handling private VPN`s and a centralized setup for this if we where to utilize an API running on the edge.

All of the edge devices will be remote without direct access from our centralized solution. Balena already have cloud connection that we would utilize for management and the plan was to utilize this to reduce complexity. And for this reason i where looking into handling this by starting a service container as this is the only functionality that needs to be remotely available.
