An API to control the Waveshare RPi Relay Board
npm install rpi-relay-hat-api- Install dependencies (on Raspberry Pi only)
- Install pigpio
- Symlink node, npm \& yarn
- Install and run the package
- Configure the application
- Run the package
- Development options
- Download the package
- Build application
- Configure the application for development
- Run
- Test
- Publish
- Endpoints
- GET /
- GET /sys-info
- GET /:id
- POST /:id/on
- POST /:id/off
- POST /:id/toggle
- Managed process
- Install pm2
- Add process to startup script
An API to control the Waveshare RPi Relay Board.
``sh`
sudo apt-get update
sudo apt-get install pigpio
`sh`
sudo ln -s "$(which node)" /usr/local/bin/node
sudo ln -s "$(which npm)" /usr/local/bin/npm
sudo ln -s "$(which yarn)" /usr/local/bin/yarn
`sh`
yarn dlx rpi-relay-hat-api
Create a rpi-relay-hat-api.env file (in a globally accessible location will allow it to be run as a service, e.g. within /usr/lib/node_modules/rpi-relay-hat-api/) and add details of the channels and server port, something like:
`sh`
RELAY_HAT_CHANNELS=[{ "channelId": "CH1", "pinNo": 26, "mode": 1, "name": "Mirror Light" }, { "channelId": "CH2", "pinNo": 20, "mode": 1, "name": "Mirror Demister" }]
PORT=3000
| Property | Type | Meaning | Example |
|----------|------|---------|---------|
| channelId | string | Unique identifier in the format CH{n}, where n is a number between 1 and 9 | CH1 |
| pinNo | number | BCM pin number, as per RPi Relay Board - Interface description | 20, 21, or 26 |
| mode | number | Default value, 0 for off and 1 for on | 0 or 1 |
| name | string | The display name for the switch | Mirror Light |
`sh`
ENV_FILE_PATH=/usr/lib/node_modules/rpi-relay-hat-api/rpi-relay-hat-api.env rpi-relay-hat-api
Rather than installing the package, these development options allow it to be run and tested:
`sh`
git clone git@github.com:jondarrer/rpi-relay-hat-api
cd rpi-relay-hat-api
`sh`
yarn
yarn build
Create a .env file and add details of the channels and server port, something like:
`sh`
RELAY_HAT_CHANNELS=[{ "channelId": "CH1", "pinNo": 26, "mode": 1, "name": "Mirror Light" }, { "channelId": "CH2", "pinNo": 20, "mode": 1, "name": "Mirror Demister" }]
PORT=3000
`sh`
sudo yarn start
`sh`
yarn test
`sh`
npm version patch -m "%s"
git push && git push --tags
And then create a release based on the tag in GitHub; this will then run the GitHub action to publish the package on npmjs.com.
Get details of all available relays.
Get info about current system.
Get details of a particular relay.
Turn a relay on (digital write 1).
Turn a relay off (digital write 0).
Toggles a relay on and off (digital write 1 or 0), inverting its previous state.
Use pm2 to setup the script when booting up.
`sh`
npm i -g pm2
sudo ln -s "$(which pm2)" /usr/local/bin/pm2
`sh``
sudo pm2 start "ENV_FILE_PATH=/home/jondarrer/code/rpi-relay-hat-api/.env sudo yarn --cwd /home/jondarrer/code/rpi-relay-hat-api start" --name "waveshare-api"
sudo pm2 startup
sudo pm2 savesudo pm2 stop waveshare-api
sudo pm2 restart waveshare-api
sudo pm2 delete waveshare-api