Skip to content

Domapic module for controlling a relay in a Raspberry Pi

License

Notifications You must be signed in to change notification settings

domapic/relay-domapic-module

Repository files navigation

Relay Domapic Module

Domapic Module for controlling a relay.

Build status Coverage Status Quality Gate js-standard-style

NPM dependencies Last commit Last release

NPM downloads License


Intro

This package starts a Domapic Module with an ability called "relay" that handles a GPIO in "out" mode. It is intended to be used in a Raspberry Pi or any other system supporting the onoff library, such as C.H.I.P. or BeagleBone.

It can be used alone, but also can be connected to a Domapic Controller to get the most out of it.

Relay connection schema

Installation

npm i relay-domapic-module -g

Start the server

relay start

The server will be started in background using pm2

To display logs, type:

relay logs #--lines=300

Options

The module, apart of all common domapic services options, provides custom options for configuring the relay:

  • gpio - <number> Number defining the Gpio where the relay to be controlled is connected.
  • initialStatus - <boolean> Boolean defining the initial status of the relay. Only used first time it is started, next times the server will remember the last status set. Default is false
  • invert - <boolean> If true, the values read from or written to the GPIO should be inverted. Equivalent to activeLow option of the onoff library.
  • remember - <boolean> If true, the relay will remember last status when restarted.
  • pressTime - <number> Defines time in miliseconds that relay will be activated when short-press action is executed. (Default is 2000)
relay --gpio=2 --initialStatus=true --remember=true --invert=false --save

Connection with Domapic Controller

Connect the module with a Domapic Controller providing the Controller url and connection token (you'll find it the Controller logs when it is started):

relay start --controller=http://192.168.1.110:3000 --controllerApiKey=foo-controller-api-key

Now, the module can be controlled through the Controller interface, or installed plugins.

Stand alone usage

Domapic modules are intended to be used through Domapic Controller, but can be used as an stand-alone service as well. Follow next instructions to use the built-in api by your own:

Rest API

When the server is started, you can browse to the provided Swagger interface to get all the info about the api resources:

Swagger ui

Apart of all api methods common to all Domapic Services, the server provides extra Domapic Abilities for controlling the relay, which generates specific API resources:

  • /api/abilities/switch/action - Changes the relay status to the provided value.
  • /api/abilities/switch/state - Returns the relay status.
  • /api/abilities/toggle/action - Changes the relay status inverting the current value.
  • /api/abilities/short-press/action - Changes the relay status inverting the current value during a defined period of time, and revert it again to original state. The period of time is defined by the pressTime option.

Authentication

The server includes the Domapic Services authentication method, which is disabled by default for 127.0.0.1. You can disable the authentication using the --authDisabled option (not recommended if your server is being exposed to the Internet). Read more about available options in the domapic services documentation.

If you want to authenticate when requesting from another IPs, look for the api key automatically generated and intended to be used by Domapic Controller when the server is started. You'll find it in the server logs:

-----------------------------------------------------------------
Try adding connection from Controller, using the next service Api Key: HMl6GHWr7foowxM40CB6tQPuXt3zc7zE
-----------------------------------------------------------------

Provide this api when making any request using the X-Api-Key header.

Use the mentioned api key also for authenticating when using the Swagger interface.

Alternative command line methods

Not global installation

If the package is not installed globally, you can replace the relay command in examples above by npm run relay -- (commands must be executed inside the package folder in that case)

Not background mode

If you don't want to use the built-in background runner, you can start the server directly, attaching logs to current stdout. Move to the package folder and replace the relay command of examples above by node server.js. Press CTRL+C to stop the server.