SMotlaq / LoRa

Ra-02 LoRa module (SX1278) library for STM32 (ARM processors) using HAL drivers ⚡
MIT License
238 stars 60 forks source link
arm embedded iot lora lorawan microcontroller ra-02 stm32 sx1276 sx1278

Contributors Forks Stargazers Issues MIT License

Outlines

Donate

Is it helpfull?

Buy me a Coffee

Tutorial video

YouTube video

Features

Successfully tested on:

Contact

Telegram or E-mail: pilot.motlaq@gmail.com

Star history

Star History Chart


Hardware

Requirements

Wiring

The wire connections are like following: STM pin Module pin
SPI MISO MISO
SPI MOSI MOSI
SPI CLK CLK
a GPIO output (initially HIGH) NSS
a GPIO output (initially HIGH) RST
a GPIO input (EXTI - Rising edge) DIO0
3.3v 3.3v
GND GND

Installation

Download or copy LoRa.c and LoRa.h , import them into your project and then include LoRa.h in main.c:

#include "LoRa.h"

Initial configurations

First of all, you'll have to create a LoRa object:

 LoRa myLoRa;

Then you'll have to call its constructor in your main function:

 myLoRa = newLoRa();

Now, the default settings are set in myLoRa, but you must set 7 important parameters:

myLoRa.CS_port         = NSS_GPIO_Port;
myLoRa.CS_pin          = NSS_Pin;
myLoRa.reset_port      = RESET_GPIO_Port;
myLoRa.reset_pin       = RESET_Pin;
myLoRa.DIO0_port       = DIO0_GPIO_Port;
myLoRa.DIO0_pin        = DIO0_Pin;
myLoRa.hSPIx           = &hspi3;

Now, calling LoRa_init(), will write these settings in the module's memory.

LoRa_init(&myLoRa);

This function returns a "status code" to represent module response status. The status codes are:

Other status codes:

if (LoRa_status==LORA_OK){ snprintf(send_data,sizeof(send_data),"\n\r LoRa is running... :) \n\r"); LoRa_transmit(&myLoRa, (uint8_t)send_data, 120, 100); HAL_UART_Transmit(&debugUART, (uint8_t)send_data, 200, 200); } else{ snprintf(send_data,sizeof(send_data),"\n\r LoRa failed :( \n\r Error code: %d \n\r", LoRa_status); HAL_UART_Transmit(&debugUART, (uint8_t*)send_data, 200, 200); }

Other parameters are set like the following:
```C
myLoRa.frequency             = 434;             // default = 433 MHz
myLoRa.spredingFactor        = SF_9;            // default = SF_7
myLoRa.bandWidth             = BW_250KHz;       // default = BW_125KHz
myLoRa.crcRate               = CR_4_8;          // default = CR_4_5
myLoRa.power                 = POWER_17db;      // default = 20db
myLoRa.overCurrentProtection = 130;             // default = 100 mA
myLoRa.preamble              = 10;              // default = 8;

Spreading factor values

//--- SPREADING FACTORS ---//
SF_7      7
SF_8      8
SF_9      9
SF_10     10
SF_11     11
SF_12     12

Bandwidth values

//------- BANDWIDTH -------//
BW_7_8KHz     7.8   KHz
BW_10_4KHz    10.4  KHz
BW_15_6KHz    15.6  KHz
BW_20_8KHz    20.8  KHz
BW_31_25KHz   31.25 KHz
BW_41_7KHz    41.7  KHz
BW_62_5KHz    62.5  KHz
BW_125KHz     125   KHz
BW_250KHz     250   KHz
BW_500KHz     500   KHz

Coding rate values

//------ CODING RATE ------//
CR_4_5    4/5
CR_4_6    4/6
CR_4_7    4/7
CR_4_8    4/8

Power values

//------ POWER GAIN ------//
POWER_11db  11db
POWER_14db  14db
POWER_17db  17db
POWER_20db  20db

Over current protection

The maximum current must be a multiple of 5 if it is less than 120, and a multiple of 10 if it is greater than 120. The minimum value is 45 mA and the maximum is 240 mA. See this file to learn about allowed currents.

Transmitting Data

After these configurations, now you can transmit a set of bytes using the LoRa_transmit function:

uint8_t LoRa_transmit(LoRa* _LoRa, uint8_t* data, uint8_t length, uint16_t timeout)

NOTE: After calling this function, the FiFo data buffer will be cleared. It means that all received packets will be deleted. If you want to read data just after transmitting something, you should add a delay.

Arguments:

Example:

  1. char*  send_data;
    send_data = "Hello world!";
    LoRa_transmit(&myLoRa, (uint8_t*)send_data, 12, 100);
  2. char*  send_data;
    send_data = "Hello world!";
    if(LoRa_transmit(&myLoRa, (uint8_t*)send_data, 12, 100) == 1){
    HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
    }

    Receive Data

    LoRa modules have two receive modes:

  3. Single
  4. Continuous

This library only supports "Continuous mode".

First of all, you'll have to enable receiving data:

LoRa_startReceiving(&myLoRa);

This function changes operating mode from STANDBY to RXCONTINUOUS, and after that you can store the last received packet in a variable by calling LoRa_receive. You can call LoRa_receive in a timer interrupt callback (recommended) or in your main loop.

uint8_t LoRa_receive(LoRa* _LoRa, uint8_t* data, uint8_t length)

Arguments:

Returns:

Example:

LoRa_startReceiving(&myLoRa);

uint8_t received_data[10];
uint8_t packet_size = 0;
while(1){
  packet_size = LoRa_receive(&myLoRa, received_data, 10);
  Hal_Delay(500);
}

Signal power estimation

The SX127x series can measure the power of the last received packet. The LoRa_getRSSI(...) can do this.

int LoRa_getRSSI(LoRa* _LoRa)

Arguments: