Comments (10)
Hi, please post a complete program which is as short as possible that can be used to reproduce the problem.
from onoff.
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.
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.
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.
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.
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.
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.
No Problem. Good to hear it's working now.
from onoff.
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.
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 is1
and once idle the value parameter is0
.Whenever I write
1
to any of the other output pins (with cheap relays hooked up) thewatch
callbacks of all the input pins are called with the value0
. 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 value1
before0
. (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.
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)
- Initialize GPIO without change pin status. HOT 6
- GPIO not working in a worker_thread - nodejs 14.15 HOT 2
- EACCES: permission denied, open '/sys/class/gpio/export' as superuser HOT 2
- Cannot run on non Rpi system HOT 1
- TypeError: writeSync is not a function HOT 4
- Raspberry Pi GPIO control in electron App HOT 8
- My computer does not have a /sys/class/gpio directory, but has /dev/gpiochip* HOT 1
- Interrupt misfire HOT 3
- Raspberry 4 - malloc(): corrupted top size HOT 3
- Error: EPERM: operation not permitted, write HOT 4
- Does not work with Compute Module 4. HOT 2
- Event is fired multiple times
- Superb library - I am running on Node v20 HOT 2
- Pi 5 Compatibility issues HOT 9
- Compatibility with ppp asked?
- RPI 5 gpio numbers HOT 2
- fs error HOT 8
- Newer version of epoll
- Button.watch spams endlessly HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from onoff.