UPDATE: rewritten code but still not working
The new code below is still copying the functionality of the python script, however trying to simplify things (also using a different library https://npmjs.org/package/rpi-gpio ) The problem now is that is always get a similar read back, so it is obviously not using the feedback from the GPIO pins.
var gpio = require('rpi-gpio');
var pin = 12,
delay = 0.0005,
startTime,
endTime,
duration,
distance;
function on(){
setTimeout(gpio.write(pin,1,off),delay);
}
function off(){
setTimeout(gpio.write(pin,0,listen),delay);
}
function listen(){
gpio.setup(pin, gpio.DIR_IN);
startTime = Date.now();
console.log('Start: ' + String(startTime));
return;
}
gpio.on('change', function(channel, value) {
endTime = Date.now();
console.log('End: ' + String(endTime));
duration = endTime - startTime;
console.log('Duration: ' + String(duration));
distance = ((duration / 100) * 3400) / 2;
console.log(distance);
return process.exit(0);
});
gpio.setup(pin, gpio.DIR_OUT, on);
ORIGINAL POST
I am trying to convert a working python script as a part of my RPi development project into node.js to avoid needing to use websockets.
This is the working python script:
import time
import RPi.GPIO as GPIO
import websocket
for x in range(0, 10):
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
#cleanup output
GPIO.output(12, 0)
time.sleep(0.000002)
#send signal
GPIO.output(12, 1)
time.sleep(0.000005)
GPIO.output(12, 0)
GPIO.setup(12, GPIO.IN)
while GPIO.input(12)==0:
starttime=time.time()
while GPIO.input(12)==1:
endtime=time.time()
duration=endtime-starttime
distance=duration*34000/2
print str(x + 1) + ": " + str(distance)
time.sleep(1)
The function of this script is to use an attached proximity sensor to measure distance.
My current (messy) rewrite for node.js (using https://github.com/rakeshpai/pi-gpio for the GPIO parts):
var gpio = require("pi-gpio");
function rightPingOn() {
gpio.write(12, 1, function(err){
//if(err) throw err;
console.log("Sent 1");
}); // Set pin 12 high
}
function rightPingOff() {
gpio.write(12, 0, function(err){
//if(err) throw err;
console.log("Sent 0");
}); // Set pin 12 low
}
function setRightListen() {
gpio.setDirection(12, "input"); // Open pin 16 for input
}
function setRightOutput(){
gpio.setDirection(12, "output"); // Open pin 12 for output
}
function right_listen(){
gpio.read(12, function(err, value){
//if(err) throw err;
console.log("Listened");
return value;
});
}
gpio.open(12);
setRightOutput();
rightPingOff();
setTimeout(rightPingOn, 0.5);
setTimeout(rightPingOff, 0.5);
setRightListen();
while (right_listen == 0){
var startTime = getTime();
}
while (right_listen == 1){
var endTime = getTime();
}
gpio.close(12);
var duration = endTime - startTime;
var distance = duration * 3400 / 2
console.log(distance);
All I currently have as an output of this script is:
NaN
Sent 0
Sent 1
Sent 0
Any help would be appreciated!
oh man. i think you're going to have a hard time with this. I'm guessing the python GPIO interface is synchronous?
To start with, polling like this:
while (right_listen == 0){
var startTime = getTime();
}
while (right_listen == 1){
var endTime = getTime();
}
is likely to result in an infinite loop because the callbacks that are supposed to be changing the value of right_listen
are never going to get a chance to run. If the following section is supposed to occur in a set order:
gpio.open(12);
setRightOutput();
rightPingOff();
setTimeout(rightPingOn, 0.5);
setTimeout(rightPingOff, 0.5);
setRightListen();
and those calls really are asynchronous, you are going to have to chain them together explicitly.
Edit: note this is only a guideline for how the code might end up looking.
(1) Notice I don't handle errors. (2) That deep nesting is often called 'callback hell', modules like async can make the code much nicer (3) There are likely small errors in here.
gpio.open(12, function(error) {
gpio.setDirection(12, "output", function(error) {
gpio.write(12, 0, function(error) {
setTimeout( function() {
var started = getTime();
gpio.setDirection(12, "input", function(error, value) {
console.log("received:", value, "after:", getTime()-started);
})
}, 500)
})
})
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.