xtrinch / sx127x-node-driver

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

npm version [npm]()

sx127x-driver

Node.js driver for Semtech SX1276/77/78/79 based LoRa radios.

Based on node-sx127x, built on top of @fivdi's onoff and spi-device modules.

Prerequisites

Hardware Wiring

Semtech SX1276/77/78/79 Generic Linux Raspberry Pi
VCC 3.3V 3.3V
GND GND GND
SCK SCK SCK (pin 11)
MISO MISO MISO (pin 10)
MOSI MOSI MOSI (pin 9)
NSS Chip enable/select CS0 (pin 8) or CS1 (pin 7)
NRESET GPIO pin GPIO pin
DIO0 GPIO pin GPIO pin

Installation

npm install sx127x-driver

API

Initialize

let SX127x = require('sx127x-driver');

let options = {
  // ...
};

let sx127x = new SX127x(options);

Supported options:

Name Default Description
spiBus 0 SPI bus to use
spiDevice 0 SPI chip select/enable to use
resetPin 24 GPIO pin number of reset pin
dio0Pin 25 GPIO pin number of DIO0 pin
frequency 915e6 Frequency of radio in Hz, see setFrequency for supported values (make sure your chip supports the frequency you chose)
spreadingFactor 7 Spreading factor of radio, see setSpreadingFactor for supported values (spreading factors are orthogonal, so make sure they match when trying to communicate from one chip to another)
signalBandwidth 125E3 Signal bandwidth of radio in Hz, see setSignalBandwidth for supported values
codingRate 4 / 5 Coding rate of radio, see setCodingRate for supported values
preambleLength 8 Preamble length of radio, see setPreambleLength for supported values
syncWord 0x12 Sync word of radio, see setSyncWord for supported values
txPower 17 TX power of radio, see setTxPower for supported values
crc false Enable or disable CRC usage
tempCompensationFactor false compensation factor for temperature measurements in degrees celsius (+- some degrees)
debug false enable / disable debug output
invertIqReg false inverts IQ register on call to open()

Open

Open and configure the device:

try {
  await sx127x.open();
} catch(err) {
  console.log('Failure to open device: ' + err)
}

Close

Close the device:

try {
  await sx127x.close();
} catch (err) {
  console.log('Close failure: ' + err);
  process.exit();
}

Sending data

try {
  await sx127x.write(new Buffer('hello ' + count++));
  console.log("successfully sent")
} catch (err) {
  console.log('Fail to send: ' + err);
} 

Blocking receive

try {
  let packetLength = await sx127x.receiveSingle();
  if (packetLength > 0) {
    let incoming = "";

    while (await sx127x.available()) {
      incoming += String.fromCharCode(await sx127x.read());
    }
  }
} catch (err) {
  console.log('Fail to receive: ' + err);
}

Interrupt receive

try {
   await sx127x.open();
   await sx127x.setContinuousReceiveMode();
} catch(err) {
   console.log('Fail to put into continuous receive mode: ' + err)
}

sx127x.on('data', function(data, rssi, snr) {
   console.log('data: ' +  data.toString() + ", rssi: " + rssi);
});

Sleep mode

Put the radio in sleep mode.

try {
  await sx127x.sleep();
} catch (err) {
  console.log('Fail to put into sleep mode: ' + err);
}

Stand by mode

Put the radio in stand by mode.

try {
  await sx127x.standBy();
} catch (err) {
  console.log('Fail to put into stand by: ' + err);
}

Radio parameters

TX Power

Change the TX power of the radio.

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

Frequency

Change the frequency of the radio.

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

Spreading Factor

Change the spreading factor of the radio.

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

Supported values are between 6 and 12. If a spreading factor of 6 is set, implicit header mode must be used to transmit and receive packets.

Signal Bandwidth

Change the signal bandwidth of the radio.

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

Supported values are 7.8E3, 10.4E3, 15.6E3, 20.8E3, 31.25E3, 41.7E3, 62.5E3, 125E3, 250E3 and 500E3.

Coding Rate

Change the coding rate of the radio.

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

Supported values are 4/5, 4/6, 4/7 and 4/8.

Preamble Length

Change the preamble length of the radio.

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

Supported values are between 6 and 65535.

Sync Word

Change the sync word of the radio.

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

CRC

Enable or disable CRC usage, by default a CRC is not used.

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

Other functions

Random

Generate a random byte, based on the Wideband RSSI measurement.

try {
  let random = await sx127x.readRandom(crc);
} catch (err) {
  console.log(err);
}

Examples

See examples folder.

License

This library is licensed under the MIT Licence.