How to use onoff - 10 common examples

To help you get started, we’ve selected a few onoff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webofthings / wot-book / chapter4-gpios / pir.js View on Github external
var Gpio = require('onoff').Gpio,
  sensor = new Gpio(17, 'in', 'both');    //#A

sensor.watch(function (err, value) { //#B
  if (err) exit(err);
  console.log(value ? 'there is someone!' : 'not anymore!');
});

function exit(err) {
  if (err) console.log('An error occurred: ' + err);
  sensor.unexport();
  console.log('Bye, bye!')
  process.exit();
}
process.on('SIGINT', exit);

// #A Initialize pin 17 in input mode
// #B Listen for state changes on pin 17, if a change is detected the anonymous callback function will be called with the new value
github fivdi / lcd / lcd.js View on Github external
constructor(config) {
    super();

    this.cols = config.cols || 16; // TODO - Never used, remove?
    this.rows = config.rows || 1;
    this.largeFont = !!config.largeFont;

    this.rs = new Gpio(config.rs, 'low'); // reg. select, output, initially low
    this.e = new Gpio(config.e, 'low'); // enable, output, initially low
    this.data = config.data.map(gpioNo => new Gpio(gpioNo, 'low'));

    this.displayControl = 0x0c; // display on, cursor off, cursor blink off
    this.displayMode = 0x06; // left to right, no shift

    this.lock = mutexify();

    this._init();
  }
github ArjanKranenburg / virtual-devices / drivers / multi / device.js View on Github external
onAdded() {
    this.log('Adding multi: ' + this.getName() + ' (' + this.getData().id + ')');

    // 1) Set first capability to true
    this.setCapabilityValue('onoff.opt1', true)
      .catch( this.error );

    // 2) Set capability with TOKEN_NAME to first capability name
    console.log('Data is: ' + JSON.stringify(this.getData()));
    console.log('State names is: ' + JSON.stringify(this.getData().state_names));
    console.log('Setting state to: ' + this.getData().state_names["onoff.opt1"]);
    this.setCapabilityValue(TOKEN_NAME, this.getData().state_names["onoff.opt1"])
      .catch( this.error );
  }
github ArjanKranenburg / virtual-devices / drivers / multi / device.js View on Github external
onAdded() {
    this.log('Adding multi: ' + this.getName() + ' (' + this.getData().id + ')');

    // 1) Set first capability to true
    this.setCapabilityValue('onoff.opt1', true)
      .catch( this.error );

    // 2) Set capability with TOKEN_NAME to first capability name
    console.log('Data is: ' + JSON.stringify(this.getData()));
    console.log('State names is: ' + JSON.stringify(this.getData().state_names));
    console.log('Setting state to: ' + this.getData().state_names["onoff.opt1"]);
    this.setCapabilityValue(TOKEN_NAME, this.getData().state_names["onoff.opt1"])
      .catch( this.error );
  }
github lucadentella / HomeAutomationBOT / bot.js View on Github external
// Authorized users, replace with your real IDs
var authorized_users = [
  111111111,
];

// Include required libraries
var sensorLib = require('node-dht-sensor');
var Bot = require('node-telegram-bot');

// Initialize relay board (using onoff library)
var Gpio = require('onoff').Gpio,
  relay1 = new Gpio(2, 'out'),
  relay2 = new Gpio(3, 'out');

// Turn both the relays off
relay1.writeSync(0);
relay2.writeSync(0);

// Initialize DHT11 sensor
sensorLib.initialize(11, 4);

// Initialize and start Telegram BOT (insert your real token)
var bot = new Bot({
  token: '0123456789abcdef0123456789abcdef0123456789ab'
});

// Attach event on every received message 
bot.on('message', function (message) {
  parseMessage(message);
github elaineo / Jellybean / client.js View on Github external
function startMotor (p, time) {
  var pin = new Gpio(p, 'out')
  pin.writeSync(1);
  console.log("writing to " + p.toString() + " for " + time);
  setTimeout(function() { stopMotor(pin); }, time);  
}
function stopMotor(p) {
github webofthings / webofthings.js / plugins / internal / ledsPlugin.js View on Github external
LedsPlugin.prototype.connectHardware = function () { //#F
  var Gpio = require('onoff').Gpio; //#G
  var self = this;
  actuator = new Gpio(self.model.values['1'].customFields.gpio, 'out');
  console.info('Hardware %s actuator started!', self.model.name);
};
github victorg1991 / VoiceAssistantjs / button.js View on Github external
const Gpio = require('onoff').Gpio;
const led = new Gpio(4, 'out');
const button = new Gpio(21, 'in', 'both');

button.watch((err, value) => {
    console.log(`value received: ${value}`);
    if (err) {
        throw err;
    }
    led.writeSync(value);
});

process.on('SIGINT', () => {
    led.unexport();
    button.unexport();
});
github daaru00 / aws-iot-example / tasks / button.js View on Github external
const config = require('../lib/config.js');

const Gpio = require('onoff').Gpio;
const port = new Gpio(config.GPIO_BUTTON, 'in', 'both');

const Button = require("../devices/button.js")
var button = new Button({
  "privateKey": config.PRIVATE_KEY,
  "clientCert": config.CLIENT_CERT,
  "caCert": config.CA_CERT,
}, config.HOST, config.DEBUG);

button.connect(function(){

  port.watch(function (err, value) {
    if (err) {
      button.logger.error(err);
    }
    if(value == 1){
      button.logger.info('button pressed');
github labatrockwell / RemoteCam / cameraComputer / takePhoto.js View on Github external
var spawn   = require('child_process').spawn;
var url = require('url');
var express = require('express');
var app     = express();
var io = require('socket.io').listen(app.listen(3000));
var Gpio = require('onoff').Gpio,
    button = new Gpio(25, 'in','both');
var prev_input = 0;
var urlToGetPic="";
var globalSocket = [];
var timeOutHandle;

//this function takes the photo, and sends the url of the photo location through the websocket
function changePhoto() {
  var command = spawn(__dirname + '/takePictureLoadtoServer');
  var output  = [];

  command.stdout.on('data', function(chunk) {
    output.push(chunk);
  }); 

  command.on('close', function(code) {
    if (code === 0) {

onoff

GPIO access and interrupt detection with Node.js

MIT
Latest version published 4 years ago

Package Health Score

53 / 100
Full package analysis

Popular onoff functions