Code Monkey home page Code Monkey logo

lora's Introduction

Contributors Forks Stargazers Issues MIT License

Outlines

Donate

Is it helpfull?

Buy me a Coffee

Tutorial video

YouTube video

Features

  • Full access to LoRa settings including bandwidth, spreading factor, carrier frequency, coding rate etc.
  • Over current protection
  • Transmiting and receiveing data
  • Sleep mode access for power saving (especially for battery powered systems)
  • RSSI estimation
  • Easy to use :)

Successfully tested on:

  • F103C8
  • F103RE
  • F030C8
  • F411VE (Discovery board)
  • F429ZG
  • L476RG (Nucleo-L476RG)

Contact

Telegram or E-mail: [email protected]

Star history

Star History Chart


Hardware

Requirements

  • Ra-02 LoRa module with SX1278 chip
  • 433MHz antenna
  • STM32 microcontroller LoRa Ra-02

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:

  • NSS pin
  • NSS port
  • Reset pin
  • Reset port
  • DIO0 port
  • DIO0 pin
  • The SPI used for communication
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:

  • 200: LORA_OK - Everything is OK.
  • 404: LORA_NOT_FOUND - Your microcontroller can't communicate with the LoRa module and read the RegVersion.
  • 503: LORA_UNAVAILABLE - Something in LoRa's settings (i.e. NSS port/pin, RESET port/pin or SPI handler) is not correct.

Other status codes:

  • 413: LORA_LARGE_PAYLOAD - Your data is larger than 255 bytes.

Example

#include "stdio.h"
#include "string.h"
.
.
.
char   send_data[200];
uint16_t LoRa_status = LoRa_init(&myLoRa);
memset(send_data,NULL,200);

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:

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:

  • _LoRa: Your LoRa object
  • data: A pointer to the data you wanna send
  • length: Size of your data in Bytes
  • timeOut: Timeout in milliseconds

Returns:

  • 1 in case of success, 0 in case of timeout

Example:

char*  send_data;
send_data = "Hello world!";
LoRa_transmit(&myLoRa, (uint8_t*)send_data, 12, 100);
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:

  1. Single
  2. 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:

  • _LoRa: Your LoRa object
  • data: A pointer to the array that you want to write received bytes in.
  • length: The number of bytes you want to read.

Returns:

  • The size of recieved packet in bytes.

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:

  • _LoRa: Your LoRa object

Returns:

  • An integer representing the power in dBm. For example -43.

lora's People

Contributors

iotthinks avatar smotlaq avatar texx00 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

lora's Issues

Failed to receive LoRa packets with SF 12

Hi all,
I have tested to send packet from Sandeep's Arduino LoRa (ESP32) to SMotlag's LoRa (STM32).
When SF = 7, STM32 can receive packets from ESP32.
However, when SF=12, then STM32 does not receive packets from ESP32.

Compare the set Spreading Factor function, I guess this library misses the part to set LDO (Low data optimization).
This library
https://github.com/SMotlaq/LoRa/blob/master/LoRa/LoRa.c#L184

Sandeep's library
https://github.com/sandeepmistry/arduino-LoRa/blob/master/src/LoRa.cpp#L573

RadioLib's library
https://github.com/jgromes/RadioLib/blob/master/src/modules/SX127x/SX1278.cpp#L188

Have you tested the receiving with SF12?
Thanks a lot.

DIO request

Does the library now support receive on interrupt now?

Thank you for your great work

Transmitting right after reading data

Hi, I wanted to respond to the data I just received.
I'm using external interrupt from DIO0 and there I'm receiving the data but when I'm trying to transmit data right after that it somehow breaks. It doesn't even go into exti callback anymore. Is it somthing you tried yet?
Here is my callback

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
	if(GPIO_Pin == GPIO_PIN_14)
	{
		LoRa_receive(&myLoRa, read_data, 128);
		if(strstr(read_data,myIDcode))
		{
			sprintf(send_data, "received packet no.: %d",read_data[2]);
			//LoRa_transmit(&myLoRa, send_data, 128, 500);		
		}
	}
	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
}

LoRa cannot init

Hi There,

I am trying to use your LoRa library for NUCLEO-G431KB and I always got the ERROR 404.
I checked all the wiring which are correct.

Any suggestion?

Thanks in advance

STM32F407G-DISC1

Hello,

I have STM32F407G-DISC1 board and LoRa E32 433T20D. I want to communicate LoRa with F407. Is this library work with F407. Has anyone tried?

Thank you.

Testing RA-02 SX1278 module with STM32L031

I am trying to get this example project working on the STM32L031 development board:
https://www.st.com/en/evaluation-tools/nucleo-l031k6.html

I cannot fully understand how am I intended to test this. I have 2 identical STM32L031 devices and connected each STM32L031 to the RA-02 SX1278 lora module.

One is running LoRa transmit code:

  while (1)
  {
	  char*  send_data;
	  send_data = "Hello!";
	  if(LoRa_transmit(&myLoRa, (uint8_t*)send_data, 6, 100) == 1){
		  printf("Transmit ok \n");
	  }
		else{
			printf("Transmit failed \n");
		}
		HAL_Delay(1500);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

The other is running receive code:

  LoRa_startReceiving(&myLoRa);
  uint8_t received_data[10];
  uint8_t packet_size = 0;
  while(1){
    packet_size = LoRa_receive(&myLoRa, received_data, 10);
    if(packet_size > 0 ){
    	printf("received data = %s \n",received_data);
    }
    HAL_Delay(500);
  }

The logs side by side:
image

Is that the expected behaviours of this library?
How to ensure I receive the correct data?
Is it normal that it fails to transmit every now and then?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.