Skip to content

Latest commit

 

History

History
192 lines (124 loc) · 3.89 KB

API.md

File metadata and controls

192 lines (124 loc) · 3.89 KB

LoRaNow API

Include Library

#include <LoRaNow.h>

Begin

Initialize the library.

LoRaNow.begin();

Returns 1 on success, 0 on failure.

Set pins

Override the default SS, and DIO0 pins used by the library. Must be called before LoRaNow.begin().

LoRaNow.setPins(ss, dio0);
  • ss - new slave select pin to use, defaults to 10 or gpio16
  • dio0 - new DIO0 pin to use, defaults to 2 or gpio15. Must be interrupt capable via attachInterrupt(...).

This call is optional and only needs to be used if you need to change the default pins used.

Set pins SPI

Override the SPI pins and the default SS, and DIO0 pins used by the library. Must be called before LoRaNow.begin().

LoRaNow.setPinsSPI(sck, miso, mosi, ss, dio0);
  • sck - new sck pin to use for SPI communication
  • miso - new miso pin to use for SPI communication
  • mosi - new mosi pin to use for SPI communication
  • ss - new slave select pin to use, defaults to 10 or gpio16
  • dio0 - new DIO0 pin to use, defaults to 2 or gpio15. Must be interrupt capable via attachInterrupt(...).

This call is optional and only works on ESP32 platform.

End

Stop the library

LoRaNow.end()

Sending data

Clear

Clear the payload buffer.

LoRaNow.clear();

Writing

Write data to the payload buffer. Each packet can contain up to 128 bytes.

LoRaNow.write(byte);

LoRaNow.write(buffer, length);
  • byte - single byte to write to packet

or

  • buffer - data to write to packet
  • length - size of data to write

Returns the number of bytes written.

Note: Other Arduino Print API's can also be used to write data into the packet

Send

Send the payload buffer to the LoRa Module.

LoRaNow.send();

Register callback

onMessage

Register a callback function for when a valid payload is received.

LoRaNow.onMessage(onMessage);

void onMessage(uint8_t *buffer, size_t size) {
 // ...
}
  • onMessage - function to call when a valid payload is received.

onSleep

Register a callback function for when a the protocol is on sleep mode.

LoRaNow.onSleep(onSleep);

void onSleep() {
 // ...
}
  • onSleep - function to call when a protocol is on sleep mode.

State machine

Loop

This function need to be on the loop to work properly

  • This function uses millis()
  • All callback is called by LoRaNow.loop()
LoRaNow.loop();

Radio parameters

Frequency

Change the frequency of the radio.

LoRaNow.setFrequency(frequency);
  • frequency - frequency in Hz (433E6, 866E6, 915E6)

Frequency by country

  • LoRaNow.setFrequencyCN() - Select the frequency 486.5 MHz - Used in China
  • LoRaNow.setFrequencyEU() - Select the frequency 868.3 MHz - Used in Europe
  • LoRaNow.setFrequencyUS() - Select the frequency 904.1 MHz - Used in USA, Canada and South America
  • LoRaNow.setFrequencyAU() - Select the frequency 917.0 MHz - Used in Australia, Brazil and Chile

Spreading Factor

Change the spreading factor of the radio.

LoRaNow.setSpreadingFactor(spreadingFactor);
  • spreadingFactor - spreading factor, defaults to 7

Supported values are between 6 and 12.

Protocol parameters

Id

Identification of the node, build in number using the serial number ArduinoUniqueID of the chip (AVR) or the mac adress (ESP)

unsigned long id = LoRaNow.id();
LoRaNow.setId(id);
  • id - identification number (4 bytes)

Count

The count number, always increment by one when a message is send.

byte count = LoRaNow.count();

Gateway

This function defines the board to work like a gateway, this means always listen for messages from the nodes.

LoRaNow.gateway();