Activating GPIO remotely

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?

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

Thanks

hi @mpous,

We are currently using Raspberry PI 4`s on balenaOS

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?

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

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

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

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:

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

1 Like

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.

1 Like