0.0.2 • Published 4 years ago

sx127x-driver v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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

  • Linux
  • SPI hardware with driver installed and enabled
    • for raspberry pi, uncomment dtparam=spi=on in /boot/config.txt
  • Node.js

Hardware Wiring

Semtech SX1276/77/78/79Generic LinuxRaspberry Pi
VCC3.3V3.3V
GNDGNDGND
SCKSCKSCK (pin 11)
MISOMISOMISO (pin 10)
MOSIMOSIMOSI (pin 9)
NSSChip enable/selectCS0 (pin 8) or CS1 (pin 7)
NRESETGPIO pinGPIO pin
DIO0GPIO pinGPIO pin

Installation

npm install sx127x-driver

API

Initialize

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

let options = {
  // ...
};

let sx127x = new SX127x(options);

Supported options:

NameDefaultDescription
spiBus0SPI bus to use
spiDevice0SPI chip select/enable to use
resetPin24GPIO pin number of reset pin
dio0Pin25GPIO pin number of DIO0 pin
frequency915e6Frequency of radio in Hz, see setFrequency for supported values (make sure your chip supports the frequency you chose)
spreadingFactor7Spreading 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)
signalBandwidth125E3Signal bandwidth of radio in Hz, see setSignalBandwidth for supported values
codingRate4 / 5Coding rate of radio, see setCodingRate for supported values
preambleLength8Preamble length of radio, see setPreambleLength for supported values
syncWord0x12Sync word of radio, see setSyncWord for supported values
txPower17TX power of radio, see setTxPower for supported values
crcfalseEnable or disable CRC usage
tempCompensationFactorfalsecompensation factor for temperature measurements in degrees celsius (+- some degrees)
debugfalseenable / disable debug output
invertIqRegfalseinverts 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);
}
  • txPower - TX power in dB, defaults to 17

    Supported values are between 2 and 17.

Frequency

Change the frequency of the radio.

try {
  await sx127x.setFrequency(frequency);
} catch (err) {
  console.log(err);
}
  • frequency - frequency in Hz (433E6, 866E6, 915E6)

Spreading Factor

Change the spreading factor of the radio.

try {
  await sx127x.setSpreadingFactor(spreadingFactor);
} catch (err) {
  console.log(err);
}
  • spreadingFactor - spreading factor, defaults to 7

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);
}
  • signalBandwidth - signal bandwidth in Hz, defaults to 125E3.

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);
}
  • codingRate - coding rate, defaults to 4/5

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);
}
  • preambleLength - preamble length in symbols, defaults to 8

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);
}
  • syncWord - byte value to use as the sync word, defaults to 0x34

CRC

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

try {
  await sx127x.setCrc(crc);
} catch (err) {
  console.log(err);
}
  • crc - true to enable CRC, false to disable

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.