Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NodeJS example using SPI communication to retrieve analog data through MCP3008 and Raspberry Pi
var rpio = require('rpio');
rpio.spiBegin();
//rpio.spiChipSelect(0); /* Chip select: use CE0 (default) */
//rpio.spiSetCSPolarity(0, rpio.LOW) /* Commonly chip enable (CE) pins are active low, and this is the default. */
//rpio.spiSetClockDivider(256) /* MCP3008 max is ~1MHz, 256 == 0.98MHz */
//rpio.spiSetDataMode(0);
// Prepare TX buffer [trigger byte = 0x01] [channel 0 = 0x80 (128)] [placeholder = 0x01]
var sendBuffer = new Buffer([0x01, (8 + 0 << 4), 0x01]);
// Send TX buffer to SPI MOSI and recieve RX buffer from MISO
var recieveBuffer = rpio.spiTransfer(sendBuffer, sendBuffer.length);
// Extract value from output buffer. Ignore first byte.
var junk = recieveBuffer[0],
MSB = recieveBuffer[1],
LSB = recieveBuffer[2];
// Ignore first six bits of MSB, bit shift MSB 8 positions and
// finally add LSB to MSB to get a full 10 bit value
var value = ((MSB & 3) << 8) + LSB;
console.log('ch' + ((sendBuffer[1] >> 4) - 8), '=', value);
rpio.spiEnd();
@jamms

This comment has been minimized.

Show comment
Hide comment
@jamms

jamms Dec 24, 2015

Hi, I've been trying some node.js experiments on my RPI for the ADC3008 and came across your very interesting blog about it. The issue I have though is that I am not getting the same output with the code examples you have made. When I run 'sudo node spi_example.js' I get this back:

/home/pi/jperkin/node_modules/rpio/lib/rpio.js:446
return binding.spi_transfer(txbuf, rxbuf, len);
^

TypeError: Incorrect arguments
at TypeError (native)
at rpio.spiTransfer (/home/pi/jperkin/node_modules/rpio/lib/rpio.js:446:17)
at Object. (/home/pi/jperkin/spi_example.js:13:26)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
at node.js:980:3

I don't suppose you have any ideas about this?

Thanks,

jamms

jamms commented Dec 24, 2015

Hi, I've been trying some node.js experiments on my RPI for the ADC3008 and came across your very interesting blog about it. The issue I have though is that I am not getting the same output with the code examples you have made. When I run 'sudo node spi_example.js' I get this back:

/home/pi/jperkin/node_modules/rpio/lib/rpio.js:446
return binding.spi_transfer(txbuf, rxbuf, len);
^

TypeError: Incorrect arguments
at TypeError (native)
at rpio.spiTransfer (/home/pi/jperkin/node_modules/rpio/lib/rpio.js:446:17)
at Object. (/home/pi/jperkin/spi_example.js:13:26)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:430:10)
at startup (node.js:141:18)
at node.js:980:3

I don't suppose you have any ideas about this?

Thanks,

jamms

@nick-jonas

This comment has been minimized.

Show comment
Hide comment
@nick-jonas

nick-jonas Feb 3, 2016

I'm getting the same error as @jamms

Any ideas?

nick-jonas commented Feb 3, 2016

I'm getting the same error as @jamms

Any ideas?

@mikaelleven

This comment has been minimized.

Show comment
Hide comment
@mikaelleven

mikaelleven Feb 4, 2016

Could you please post the code executed before the rpio.spiTransfer() call? I have not seen this error myself but could be related to the input (txbuf) or output (rxbuf) buffer.

Or if you used my example without alternation maybe you could verify which version of rpio-lib you have installed?

Owner

mikaelleven commented Feb 4, 2016

Could you please post the code executed before the rpio.spiTransfer() call? I have not seen this error myself but could be related to the input (txbuf) or output (rxbuf) buffer.

Or if you used my example without alternation maybe you could verify which version of rpio-lib you have installed?

@nick-jonas

This comment has been minimized.

Show comment
Hide comment
@nick-jonas

nick-jonas Feb 4, 2016

@mikaelleven I'm using your code unaltered, and with rpio 0.9.7 installed

nick-jonas commented Feb 4, 2016

@mikaelleven I'm using your code unaltered, and with rpio 0.9.7 installed

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Feb 11, 2016

got the exactly same error with this script here:

var rpio = require('rpio');

var rxBuffer = rpio.spiTransfer(new Buffer('HELLOSPI'), 8);

for (var i = 0; i <= 7; i++) { 
 process.stdout.write(String.fromCharCode(rxBuffer[i]) + (i == 7 ? '\n' : ' '));
};

ghost commented Feb 11, 2016

got the exactly same error with this script here:

var rpio = require('rpio');

var rxBuffer = rpio.spiTransfer(new Buffer('HELLOSPI'), 8);

for (var i = 0; i <= 7; i++) { 
 process.stdout.write(String.fromCharCode(rxBuffer[i]) + (i == 7 ? '\n' : ' '));
};
@PrasadBapatla

This comment has been minimized.

Show comment
Hide comment
@PrasadBapatla

PrasadBapatla Mar 22, 2016

looks like the signature of the spiTransfer() has changed. the rxBuffer is now an argument, not a return value
if you look in the file rpio.js , you see that it it needs 3 parameters now .
the following should work

var rpio = require('rpio');

var rxBuffer = newBuffer(8);
rpio.spiTransfer(new Buffer('HELLOSPI'), rxBuffer, 8);

for (var i = 0; i <= 7; i++) {
process.stdout.write(String.fromCharCode(rxBuffer[i]) + (i == 7 ? '\n' : ' '));
};

PrasadBapatla commented Mar 22, 2016

looks like the signature of the spiTransfer() has changed. the rxBuffer is now an argument, not a return value
if you look in the file rpio.js , you see that it it needs 3 parameters now .
the following should work

var rpio = require('rpio');

var rxBuffer = newBuffer(8);
rpio.spiTransfer(new Buffer('HELLOSPI'), rxBuffer, 8);

for (var i = 0; i <= 7; i++) {
process.stdout.write(String.fromCharCode(rxBuffer[i]) + (i == 7 ? '\n' : ' '));
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment