xtrinch / sx127x-node-driver

Node.js driver for Semtech SX1276/77/78/79 LoRa circuits
MIT License
10 stars 6 forks source link

Error on sx127x.open() #7

Open pchambers opened 3 years ago

pchambers commented 3 years ago

I just installed the package on RasPi B v1.2

When testing the examples I get the following in sender.js I believe the first error is being thrown on _spiOpen and when _spi.transfer is being called, it throws the next error.

Any suggestions for troubleshooting this error?

{ [Error: ENOENT, No such file or directory] errno: 2, code: 'ENOENT', syscall: 'open' } Debug sx127x: Sending: hello 0 Error: Spi not defined at SX127x._readRegister (/home/pi/sx127x-node-driver/lib/sx127x.js:472:11) at SX127x.write (/home/pi/sx127x-node-driver/lib/sx127x.js:427:36) at send (/home/pi/sx127x-node-driver/examples/sender.js:29:17) at process._tickCallback (internal/process/next_tick.js:68:7) (node:6870) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

xtrinch commented 3 years ago

It couldn't initialize the SPI. Have you double checked your SPI wiring?

pchambers commented 3 years ago

running ls /dev/spi* I only show /dev/spidev0.1 I'm not sure why it doesn't default to 0.0, but I tried to test 0.1.

I created a new project to test the spi-device library, and using the example on fivdi's spi-device repo I was able to get it to initialize with spi.open(0,1, err ...);

Switching the deviceNumber to 1 from 0 seemed to eliminate the error on open.

When I add to the sender.js constructor the option for spiDevice : 1 I still get the same error as the default example sender.js.

Based on this test, I assume that the wiring was correct and it should work when using device 1.

Thanks

pchambers commented 3 years ago

I think the logic for the options object in the constructor for the class may not be working properly. Most of the values use the || (OR) logical operator to see if they are present in the options object. When I change the constructor to use the same options.hasOwnProperty function as the dio0 and reset Pin for the options.spiDevice and options.spiBus, it actually allows the code to compile.

Lines 65 and 66 from sx127x.js would then be:

this._spiBus = (options.hasOwnProperty('spiBus')) ? options.spiBus : 0; this._spiDevice = (options.hasOwnProperty('spiDevice')) ? options.spiDevice : 0;

If this is a common issue, it may be effecting any other user-selected options in the same way.

xtrinch commented 3 years ago

Can you share with me your example code with modified options? OR'ing on an integer is sure to work if the value passed is undefined, so I think the problem is elsewhere.

pchambers commented 3 years ago

I started with the sender.js example in the repo and just added the line spiDevice : 1 to the constructor, which resulted in the original error posted above.

`var sx127x = new SX127x({

spiDevice: 1, frequency: 434e6, dio0Pin: 6, // BCM numbering (run gpio readall for info) resetPin: 13, // BCM numbering (run gpio readall for info) syncWord: 0x12, debug: true, tempCompensationFactor: 10, }); `

everything else in the example remained the same.

When I went to the lib/sx127x.js file I changed lines 65 and 66 to the following:

this._spiBus = (options.hasOwnProperty('spiBus')) ? options.spiBus : 0; this._spiDevice = (options.hasOwnProperty('spiDevice')) ? options.spiDevice : 0;

and the error was resolved.

If you'd like the download of the repo I have, what is the best way of sharing the whole files?

Thanks

xtrinch commented 3 years ago

Github gists or upload the whole repo to github. You can also paste the full modified example here.

pchambers commented 3 years ago

const express = require('express'); const app = express(); const util = require('util'); const { exec } = require('child_process'); var SX127x = require('../lib/sx127x'); var sx127x = new SX127x({ spiDevice: 1, frequency: 434e6, dio0Pin: 6, // BCM numbering (run gpio readall for info) resetPin: 13, // BCM numbering (run gpio readall for info) syncWord: 0x12, debug: true, tempCompensationFactor: 10, });

async function send() { let count = 0;

    try {
            await sx127x.open();
    } catch(err) {
            console.log(err)
    }

    while(true) {
            // send a message every second
            try {
                    await sx127x.write(new Buffer('hello ' + count++));
                    console.log("successfully sent")
            } catch (err) {
                    console.log(err);
            }

            await util.promisify(setTimeout)(1000);
    }

}

send();

process.on('SIGINT', async function() { // close the device try { await sx127x.close(); } catch (err) { console.log('close failure: ' + err); process.exit(); }

console.log("success"); process.exit(); });

pchambers commented 3 years ago

That's the full example