Using the python script below I can make my motor run:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BOARD)
Motor1A = 16
Motor1B = 18
Motor1E = 22
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor1E,GPIO.OUT)
print "Turning motor on"
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor1E,GPIO.HIGH)
sleep(2)
print "Stopping motor"
GPIO.output(Motor1E,GPIO.LOW)
GPIO.cleanup()
I would like to start and stop the engine from my nodejs server. I have already tried to run python scripts from nodejs, but this gave permission problems and it is not ideal to use multiple languages for just a few commands.
For these reasons I want to use https://www.npmjs.com/package/rpi-gpio
This is my first attempt to convert the code to nodejs. I get the error "Error: Pin has not been exported for write"
var gpio = require('rpi-gpio');
var Motor1A = 16;
var Motor1B = 18;
var Motor1E = 22;
gpio.setup(Motor1A, gpio.DIR_OUT);
gpio.setup(Motor1B, gpio.DIR_OUT);
gpio.setup(Motor1E, gpio.DIR_OUT);
gpio.write(Motor1A, gpio.HIGH, function(err) {
if (err) throw err;
console.log('Written to pin Motor1A');
});
gpio.write(Motor1B, gpio.LOW, function(err) {
if (err) throw err;
console.log('Written to pin Motor1B');
});
gpio.write(Motor1E, gpio.HIGH, function(err) {
if (err) throw err;
console.log('Written to pin Motor1E');
});
setTimeout(stopMotor_1, 2000);
function stopMotor_1() {
gpio.write(Motor1E, gpio.LOW, function(err) {
if (err) throw err;
console.log('Stopped');
});
}
second attempt:
gpio.setup(Motor1A, gpio.DIR_OUT, writeMotor1A);
gpio.setup(Motor1B, gpio.DIR_OUT, writeMotor1B);
gpio.setup(Motor1E, gpio.DIR_OUT, writeMotor1E);
function writeMotor1A(){
gpio.write(Motor1A, gpio.HIGH, function(err) {
if (err) throw err;
console.log('Written to pin Motor1A');
});
}
function writeMotor1B(){
gpio.write(Motor1B, gpio.LOW, function(err) {
if (err) throw err;
console.log('Written to pin Motor1B');
});
}
function writeMotor1E(){
gpio.write(Motor1E, gpio.HIGH, function(err) {
if (err) throw err;
console.log('Written to pin Motor1C');
});
}
results in the same error 'Error: Pin has not been exported for write'
Interestingly, when I do 'gpio-admin export 16' I get the following error:
gpio-admin: could not flush data to /sys/class/gpio/export: Device or resource busy
The same command for pin 18 and 22 does not return any messages.
Can anyone help me convert the above script to a nodejs / rpi-gpio script? I have no experience doing this in nodejs and I can't find any good examples.