Code Monkey home page Code Monkey logo

Comments (10)

fivdi avatar fivdi commented on June 12, 2024

Hi, please post a complete program which is as short as possible that can be used to reproduce the problem.

from onoff.

idmarco avatar idmarco commented on June 12, 2024

Thanks for your reply. I try to explain better.
I use an IR sensor with remote control, and I need to know if I am pressing the button.
image

I test the IR sensor with python, works fine:

import time
import sys
 
SENSOR_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)

def test(channel):
  print('State change!')

def main():
  try:
    GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=test)
    while True:
        time.sleep(1)
  except KeyboardInterrupt:
    print ("Finish...")
  GPIO.cleanup()
  
if __name__ == '__main__':
  main()

In nodejs, i create a module for magicMirror.
This code works fine:

'use strict'

var NodeHelper = require('node_helper')
const Gpio = require('onoff').Gpio;

module.exports = NodeHelper.create({
  
  isLoaded: false,
  config:   null,
  
  start: function () {
    console.log(this.name + ' helper started ...')
  },
  
  socketNotificationReceived: function (notification, payload) {
    var self = this
    if (notification === 'CONFIG') {
        if (!this.isLoaded) {
            this.config = payload
            this.isLoaded = true
        }
        
        this.pirRemoteControl = new Gpio(17, 'in', 'both')
        
        setInterval(function(){
          self.pirRemoteControl.read((err, value) => {
            console.log("Status change to:"+value)
            if (err) {
              throw err;
            }
          });
        },100);
    }
  },
})

But this one, dont work;

'use strict'

var NodeHelper = require('node_helper')
const Gpio = require('onoff').Gpio;

module.exports = NodeHelper.create({
  
  isLoaded: false,
  config:   null,
  
  start: function () {
    console.log(this.name + ' helper started ...')
  },
  
  socketNotificationReceived: function (notification, payload) {
    var self = this
    if (notification === 'CONFIG') {
        if (!this.isLoaded) {
            this.config = payload
            this.isLoaded = true
        }
        
        this.pirRemoteControl = new Gpio(17, 'in', 'both')
        
        this.pirRemoteControl.watch(function (err, value) {
          if (err) {
            console.log("Error: "+err);
          }else{
            console.log("Status change: "+value)
          }
        });
    }
  },
})

Its possible thant watch function dont work with relay?

Thanks!!

from onoff.

fivdi avatar fivdi commented on June 12, 2024

In general, watch should work. I don't understand why it doesn't work here though.

If the following program is run, does it print messages when the button the on remote control is pressed?

'use strict'

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

let pirRemoteControl = new Gpio(17, 'in', 'both');
        
pirRemoteControl.watch(function (err, value) {
  if (err) {
    console.log("Error: "+err);
  } else {
    console.log("Status change: "+value);
  }
});

If you change this line from your program:

        this.pirRemoteControl = new Gpio(17, 'in', 'both')

to this:

        console.log('About to create the Gpio object...');
        this.pirRemoteControl = new Gpio(17, 'in', 'both')

is the message 'About to create the Gpio object...' displayed? How often is the message displayed?

from onoff.

idmarco avatar idmarco commented on June 12, 2024

Yes, the message display in log file.
I use the watch function too with other sensor (move sensor) and works correcty.
Tomorrow I will do some tests without the use of magicmirror.
Could the relay have something to do with it?

Thanks

from onoff.

fivdi avatar fivdi commented on June 12, 2024

Just so that I understand, you are saying that when the following program is run, it functions correctly?

'use strict'

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

let pirRemoteControl = new Gpio(17, 'in', 'both');
        
pirRemoteControl.watch(function (err, value) {
  if (err) {
    console.log("Error: "+err);
  } else {
    console.log("Status change: "+value);
  }
});

from onoff.

idmarco avatar idmarco commented on June 12, 2024

Hi!

I have done several tests starting from 0, and so now it works correctly. I can't quite understand what the problem was, maybe because of some other bookstore. I do not know.
But now it works perfectly, thank you very much and sorry for the inconvenience

from onoff.

fivdi avatar fivdi commented on June 12, 2024

No Problem. Good to hear it's working now.

from onoff.

gwest7 avatar gwest7 commented on June 12, 2024

I hope I can use this thread for a related question as I don't want to log an issue if there is no need.

I watch a few pins setup with "in" and "both". I have a few PIR sensors wired to these. With movement the callback value parameter is 1 and once idle the value parameter is 0.

Whenever I write 1 to any of the other output pins (with cheap relays hooked up) the watch callbacks of all the input pins are called with the value 0. The relays control 10W LED lights (set up not to interfere with the PIR sensors).

I suspect this is circuit related, but even so I would expect the watch callbacks to be called with the value 1 before 0. (I'm grateful they don't though!)

@fivdi thank you for the work you've put into onoff!

from onoff.

fivdi avatar fivdi commented on June 12, 2024

I hope I can use this thread for a related question as I don't want to log an issue if there is no need.

I watch a few pins setup with "in" and "both". I have a few PIR sensors wired to these. With movement the callback value parameter is 1 and once idle the value parameter is 0.

Whenever I write 1 to any of the other output pins (with cheap relays hooked up) the watch callbacks of all the input pins are called with the value 0. The relays control 10W LED lights (set up not to interfere with the PIR sensors).

I suspect this is circuit related, but even so I would expect the watch callbacks to be called with the value 1 before 0. (I'm grateful they don't though!)

@fivdi thank you for the work you've put into onoff!

I would also expect the problem to be circuit related rather than being software related. One way to confirm that the problem is circuit related would be to replace the relays with LEDs. If everything works as expected when using LEDs rather than relays I'd say there's a problem with the circuit. If there is still a problem when LEDs are used, it may be a software problem.

Regarding your expectations of the value being 1 or 0 when the watch callback is called; if circuit problems with relays are causing very short spikes on input pins, the input pin may have gone high and then low in a very short time. In this case, the onoff module may have read the pins value as 0 rather than 1.

from onoff.

gwest7 avatar gwest7 commented on June 12, 2024

This makes sense. I'll replace the relays with LEDs sometime and confirm this. Cheap hardware can sometime be quite expensive. 😉

Thank you for sharing your insights.

from onoff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.