Im running raspi Model B Rev 2 512MB, I installed the module and succesfully opened a pin for writing on it. Succesfully made a LED blink.
Then I try to open a port for reading and I get and error
{ Error: EIO: i/o error, write errno: -5, code: 'EIO', syscall: 'write' }
This the relevant part of the code.
var gpio = require('rpi-gpio');
server.listen(8080, function() {
console.log('Servidor corriendo en http://localhost:8080');
gpio.setup(7, gpio.DIR_OUT, control);
gpio.setup(22, gpio.DIR_IN, gpio.EDGE_BOTH, control);
});
function control(err)
{
if(err)
console.log("Control function: " + err);
else
console.log('ok');
}
This is the pinout I see using gpio readall, I want to read from GPIO6, therefore I call to 22 (Physical port) I also tried 25 (BCM port) with the same result.
Any clue ??
the problem is that the syntax is slightly different, if you add the third parameter for the edge you cannot add the callback, changing this:
gpio.setup(22, gpio.DIR_IN, gpio.EDGE_BOTH, control);
to this
gpio.setup(22, gpio.DIR_IN, gpio.EDGE_BOTH);
solved the problem
PIN 6 is Ground, so you cannot use it. You can use PIN 5 for input. (PINs not GPIOs)
Pin diagram for reference:
Related
I've tried numerous tutorials, and I can't get it to work.
Current situation:
- 12V Access control device that is connected like this i.e. Wiegand D0 to GPIO14 (pin 8/Tx) and D1 to GPIO15 (pin 10/Rx), with voltage dividers, shifting 5V to 3.3V.
- Raspberry Pi 3 with Raspbian Lite OS.
- Configured the GPIO serial port i.e. enabling uart and disabling the console.
I am using the onoff NPM package to read the signals, but I am not getting anything.
const Gpio = require('onoff').Gpio;
const d0 = new Gpio(8, 'in');
const d1 = new Gpio(10, 'in');
d0.watch((err, value) => {
if (err) {
throw err;
}
d0.readSync(value);
});
d1.watch((err, value) => {
if (err) {
throw err;
}
d1.readSync(value);
});
process.on('SIGINT', () => {
d0.unexport();
d1.unexport();
});
What am I doing wrong?
The NPM package you're trying to use can only detect changing logic levels on GPIO pins.
You should use Wiegand NPM instead.
And I think you have set the wrong pins on your code. Pins 8 and 10 are in fact GPIO14 and GPIO15. The library I linked uses GPIO17 and GPIO18 by default, which are located on pins 11 and 12 of the connector.
Having said that, there is no need to disable the UART to use pins 8 and 10 as digital GPIO.
I'm using the johnn-five and raspi-io packages for node and I'm trying to read the events created on a PIR motion sensor, but everytime I run the application, an error is thrown.
console
pi#raspberrypi ~/poc $ sudo node app.js
1439476578608 Device(s) RaspberryPi-IO
1439476578749 Connected RaspberryPi-IO
1439476578825 Repl Initialized
>> /home/pi/poc/node_modules/raspi-io/lib/index.js:316
throw new Error("Unknown pin \"" + pin + "\"");
^
Error: Unknown pin "null"
at Raspi._defineProperty.value (/home/pi/poc/node_modules/raspi-io/lib/index.js:316:17)
at Raspi.pinMode (/home/pi/poc/node_modules/raspi-io/lib/index.js:327:47)
at Motion.Controllers.PIR.initialize.value (/home/pi/poc/node_modules/johnny-five/lib/motion.js:27:17)
at new Motion (/home/pi/poc/node_modules/johnny-five/lib/motion.js:180:10)
at Board.<anonymous> (/home/pi/poc/app.js:9:16)
at Board.emit (events.js:104:17)
at process._tickDomainCallback (node.js:381:11)
package.js
{
"name": "poc",
"version": "0.0.1",
"main": "app.js",
"private": true,
"dependencies": {
"johnny-five": "0.8.86",
"raspi-io": "3.3.4"
}
}
app.js
var raspi = require('raspi-io');
var five = require('johnny-five');
var board = new five.Board({
io: new raspi()
});
board.on('ready', function() {
var motion = new five.Motion({pin: 'PI-17'});
motion.on('calibrated', function() {
console.log('calibrated');
});
motion.on('motionstart', function() {
console.log('motionstart');
});
motion.on('motionend', function() {
console.log('motionend');
});
});
However, the following code DOES seem to work:
snipped from another poc
var raspi = require('raspi');
var gpio = require('raspi-gpio');
raspi.init(function() {
var input = new gpio.DigitalInput({
pin: 'P1-17'
});
var logInput = function() {
console.log('Input 17: ' + input.read());
setTimeout(logInput, 1000);
};
logInput();
});
The Raspberry is a Rev. 2 model B.
The Motion detector I'm using is this one.
The cables are connected as follows (physical pin, taken from this schema)
Motion Sensor -> Pi
1 GND -> 6 GND
2 VDD -> 1 +3/V3 OUT
3 OUT -> 11 GPIO17
Any help would be greatly appreciated.
After more searching I came across this image.
It provided me with more insight on the WiringPi Pin numbers, BCM GPIO naming and P1 naming.
The pin GPIO0 on Pin 0 (BCM 17) seems to be throwing the error.
I switched to using pin GPIO4 on Pin 4 (BCM 23).
It doesn't really make sense why the 0 doesn't work, but for now, the poc I'm working on can progress again.
Your issue wasn't with any of your code, or a naming issue. You unfortunately chose poorly for which pin to use. P1-17 isn't able to accept an input, as it is dedicated 3.3v out. Choose another pin, and you should be fine.
Any pin reserved for I2C or Serial can't be used for regular GPIO function within Johnny Five, along with the dedicated power and ground pins which can't be used for IO, ever. As long as you avoid these ports, you'll be fine. A good resource on Raspberry Pi pinouts is here.
I have two Arduino unos flashed with the standard StandardFirmata and i'm trying the multi board demo with a simple node project (johnny-five npm package). Both arduinos work when I try them separately. with the following code:
var five = require("johnny-five");
var boardOne = new five.Board({ id: "A", port: "/dev/cu.usbmodem1d1141" });
boardOne.on("ready", function(){
var led = new five.Led({
pin: 13,
board: this
});
led.on();
});
node index.js
1418288836782 Connected /dev/cu.usbmodem1d1141
1418288836784 Repl Initialized
>>
When trying the multi board example I get:
Device or Firmware Error A timeout occurred while connecting to the Board.
Please check that you've properly flashed the board with the correct firmware.
var five = require("johnny-five");
var ports = [
{ id: "A", port: "/dev/cu.usbmodem1d1141" },
{ id: "B", port: "/dev/cu.usbmodem1d1131" }
];
new five.Boards(ports).on("ready", function(){
var led = new five.Led({
pin: 13,
board: this[0]
});
led.on();
});
Update #1:
Out of curiosity I tried to switch around the usb cables and got some different results:
1) Only one arduino seems to connect:
1418318698635 Device(s) /dev/cu.usbmodem1a1231,/dev/cu.usbmodem1a1241
1418318698642 Device(s) /dev/cu.usbmodem1a1241
1418318701849 Connected /dev/cu.usbmodem1a1231
1418318701850 Board ID: A
or 2) I get an error:
.../johnny-five-master/node_modules/firmata/lib/firmata.js:246
board.pins[pin].analogChannel = currentValue;
^
TypeError: Cannot set property 'analogChannel' of undefined
at Object.SYSEX_RESPONSE.(anonymous function) [as 106]
(.../johnny-five-master/node_modules/firmata/lib/firmata.js:246:35)
Update #2:
I did the above test with cylon.js and got the same results. Still no clue how to fix this :(
One arduino works fine, multiple do nothing. (Maybe an osx related problem?)
Update #3:
I added some logs in the johnny-five code and it's definitely a connection problem(I think!?). The second Arduino never responds. I switched the order of the arduinos and get the same result (first one connects, the other fails to respond). The connection is asynchronous, so maybe it gets blocked somewhere. The lights on both arduinos definitely show some action is going on.
node index.js
err: undefined --- type: connect --- io: /dev/tty.usbmodem1d1111
err: undefined --- type: connect --- io: /dev/tty.usbmodem1d1121
err: undefined --- type: ready --- io: /dev/tty.usbmodem1d1111
1418467187527 Connected /dev/tty.usbmodem1d1111
1418467187527 Board ID: A
1418467284327 Device or Firmware Error A timeout occurred while connecting to the Board.
Please check that you've properly flashed the board with the correct firmware.
Thanks to #izar for posting this and then bringing the question to us in the Johnny-Five gitter channel. From there, Divan Visagie (from Johnny-Five core team) worked to triage the bug and was able to confirm via reproduction. This revealed a bug in Firmata.js, where the options passed to Serialport were being extended by that class. Since the defaults object was reused and Object.assign is not a "deep" operation, the changes were being made to a reference, not a copy. The result was that the second initialization was getting a set of "defaults" that were loaded up with the first instance's own data. The issue was fixed by changing Firmata to use fresh defaults for every instance. Here's the patch
I'm using node.js with socket.io to give my web page access to character data served by a TCP socket. I'm quite new to node.js.
User ----> Web Page <--(socket.io)--> node.js <--(TCP)--> TCP Server
The code is mercifully brief:
io.on('connection', function (webSocket) {
tcpConnection = net.connect(5558, 'localhost', function() {});
tcpConnection.on('error', function(error) {
webSocket.emit('error', error);
tcpConnection.close();
});
tcpConnection.on('data', function(tcpData) {
webSocket.emit('data', { data: String.fromCharCode.apply(null, new Uint8Array(tcpData))});
});
});
It all works just fine in the normal case, but I can't guarantee that the TCP server will be there all the time. When it isn't, the TCP stack returns ECONNREFUSED to node.js - this is entirely expected and I need to handle it gracefully. Currently, I see:
events.js:72
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
... and the whole process ends.
I've done a lot of searching for solutions to this; most hits seem to be from programmers asking why ECONNREFUSED is received in the first place - and the advice is simply to make sure that the TCP server is available. No discussing of handling failure cases.
This post - Node.js connectListener still called on socket error - suggests adding a handler for the 'error' event as I've done in the code above. This is exactly how I would like it to work ... except it doesn't (for me), my program does not trap ECONNREFUSED.
I've tried to RTFM, and the node.js docs at http://nodejs.org/api/net.html#net_event_error_1 suggest that there is indeed an 'error' event - but give little clue how to use it.
Answers to other similar SO posts (such as Node.js Error: connect ECONNREFUSED ) advise a global uncaught exception handler, but this seems like a poor solution to me. This is not my program throwing an exception due to bad code, it's working fine - it's supposed to be handling external failures as it's designed to.
So
Am I approaching this in the right way? (happy to admit this is a newbie error)
Is it possible to do what I want to do, and if so, how?
Oh, and:
$ node -v
v0.10.31
I ran the following code:
var net = require('net');
var client = net.connect(5558, 'localhost', function() {
console.log("bla");
});
client.on('error', function(ex) {
console.log("handled error");
console.log(ex);
});
As I do not have 5558 open, the output was:
$ node test.js
handled error
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
This proves that the error gets handled just fine... suggesting that the error is happening else-where.
As discussed in another answer, the problem is actually this line:
webSocket.emit('error', error);
The 'error' event is special and needs to be handled somewhere (if it isn't, the process ends).
Simply renaming the event to 'problem' or 'warning' results in the whole error object being transmitted back through the socket.io socket up to the web page:
webSocket.emit('warning', error);
The only way I found to fix this is wrapping the net stuff in a domain:
const domain = require('domain');
const net = require('net');
const d = domain.create();
d.on('error', (domainErr) => {
console.log(domainErr.message);
});
d.run(() => {
const client = net.createConnection(options, () => {
client.on('error', (err) => {
throw err;
});
client.write(...);
client.on('data', (data) => {
...
});
});
});
The domain error captures error conditions which arise before the net client has been created, such as an invalid host.
See also: https://nodejs.org/api/domain.html
I am trying to create a NodeJS library that allows usage of the BeagleBone's serial (uart) ports. Some of the pins are muxed so some configuration bits have to be written to two files. Here is my function that writes the configuration bits to enable the uart:
var setMuxForUart = function (uart, next) {
var txFd, rxFd;
var txBuf = new Buffer(uart.muxTx.config, 'ascii');
var rxBuf = new Buffer(uart.muxRx.config, 'ascii');
var txBytesWritten, rxBytesWritten;
console.log ("Configuring UART MUX for " + uart.path);
txFd = fs.openSync (MUX_PATH + uart.muxTx.path, 'w');
rxFd = fs.openSync (MUX_PATH + uart.muxRx.path, 'w');
if (txFd && rxFd) {
try {
txBytesWritten = fs.writeSync (txFd, txBuf, 0, txBuf.length, 0);
}
catch (e) {
fs.closeSync (txFd);
fs.closeSync (rxFd);
console.log ('Error Writing to file: '+ MUX_PATH + uart.muxTx.path + ' | ' + util.inspect (e));
return;
}
try {
rxBytesWritten = fs.writeSync (rxFd, rxBuf, 0, rxBuf.length, 0);
}
catch (e) {
fs.closeSync (txFd);
fs.closeSync (rxFd);
console.log ('Error Writing to file: ' + MUX_PATH + uart.muxRx.path + ' | ' + util.inspect(e));
return;
}
fs.closeSync (txFd);
fs.closeSync (rxFd);
if (txBytesWritten && rxBytesWritten) {
console.log ("Uart MUX finished configuration");
next ();
}
else {
console.log ("An error occured writing to the UART MUX.");
}
}
else {
console.log ("An error occured while opening the UART MUX files.");
}
};
Here is the file containing this funcion.
Here is the output from running this function:
root#beaglebone:~/workspace/BonescriptSerial# node BonescriptSerial.js
The "sys" module is now called "util". It should have a similar interface.
Opening Serial Port for: /dev/ttyO1
Configuring UART MUX for /dev/ttyO1
Error Writing to file: /sys/kernel/debug/omap_mux/uart1_txd | { [Error: UNKNOWN, unknown error] errno: -1, code: 'UNKNOWN', syscall: 'write' }
I have verified that the correct output is written to test files, I have tried numerous mode parameters ('0777' doesn't matter), I have tried this with the sync and async functions to no avail, and I have also tried, successfully, writing to these files in python. If you have any ideas that would help solve this problem I would be very grateful.
Here is a github repo of the project, its in its infancy right now so there isn't alot of documentation. The python version is in the repo as well.
Thanks to Ben Noordhuis on the NodeJS google group I was pointed to the following issue that was causing the problem. The device driver that I was trying to write to apparently doesn't accept arbitrary seek writes, so in order to get around that I needed to trick NodeJS into using write instead of pwrite. The trick is to tell the write command to start writing at -1 instead of 0.
fs.writeSync (txFd, txBuf, 0, txBuf.length, -1);