Experience with using Node + onoff to control GPIO (solved)

I am trying to develop af project using Raspberrypi3 with Balena, running node version 12.

I am currently using onoff to access GPIO’s, but I am having some flaky instability.

Do I need to configure something in Balena for onoff to work?

My Docker file look like this:

# base-image for node on any machine using a template variable,
# see more about dockerfile templates here: https://www.balena.io/docs/learn/develop/dockerfile/#dockerfile-templates
# and about resin base images here: https://www.balena.io/docs/reference/base-images/base-images/
FROM balenalib/raspberrypi3-node:12
# FROM balenalib/raspberrypi4-64-node:12

# use apt-get if you need to install dependencies,
# for instance if you need ALSA sound utils, just uncomment the lines below.
RUN apt-get update && apt-get install -yq \
   python alsa-utils libasound2-dev build-essential && \
   apt-get clean && rm -rf /var/lib/apt/lists/*

# Set up ALSA config
# COPY config/asound.conf /etc/asound.conf

# Defines our working directory in container
WORKDIR /usr/src/app

# Copies the package.json first for better cache on later pushes
COPY package.json package.json
COPY package-lock.json package-lock.json

# This install npm dependencies on the balena build server,
# making sure to clean up the artifacts it creates in order to reduce the image size.
RUN JOBS=MAX npm install --unsafe-perm && rm -rf /tmp/*

# This will copy all files in our root to the working  directory in the container
COPY . ./

# Enable systemd init system in container

# server.js will run when container starts up on the device
CMD ["npm", "start"]

And my node code looks like this:

const Gpio = require("onoff").Gpio;

const button1 = new Gpio(pin1, "in", "both");

button1.watch(function(err, value) {
      if (err) {
        throw err;

Any help is very welcome.


Problem was that onoff does not provide an interface to configure the integrated resistors. Solved this by looking at this table, and using the default GPIO values.

I’m glad you solved your problem. Don’t hesitate to contact us for further assistance.