Intermediate
20 min

Enable secure and reliable data transfer over long distances with minimal power consumption using 453-00140R and PIC18LF46K40

Ultra-low power LoraWAN solution for 868MHz IoT applications

LR 11 Click - 868MHz with EasyPIC v8

Published Sep 06, 2024

Click board™

LR 11 Click - 868MHz

Dev. board

EasyPIC v8

Compiler

NECTO Studio

MCU

PIC18LF46K40

Perfect choice for any application demanding reliable, long-range, and energy-efficient 868MHz wireless communication

A

A

Hardware Overview

How does it work?

LR 11 Click - 868MHz is based on the 453-00140R, an ultra-low power LoRaWAN module from Ezurio. This module is part of the Ezurio RM126x series, specifically the RM1261, which integrates the Silicon Labs EFR32 series System on Chip (SoC) and the Semtech SX1261 radio. This combination provides an efficient, low-power, long-range solution for developing a wide range of LoRaWAN™ applications certified by the LoRa Alliance. The module has a built-in MHF4 connector, a Temperature Compensated Crystal Oscillator (TCXO), and a DC-DC converter, ensuring reliable performance in various environments. Supporting LoRaWAN classes A, B, and C, the 453-00140R offers secure, scalable, and bi-directional communication. It has broad regulatory region support, including Europe, the UK, Taiwan, Japan, and India, and holds certifications such as FCC, ISED, and AS/NZS, making it a reliable and efficient solution for diverse use cases requiring robust long-range communication. In addition to LoRaWAN capabilities, the 453-00140R features a LoRa Point-to-Point (LoRa P2P) capability, enabling the creation of private, ultra-long-range radio networks between two RM126x modules. This capability supports both unicast and broadcast modes, accommodating up to 64 devices per network. This module is designed to operate in both hosted and hostless modes. In hosted mode, it is programmed with an AT command set, while in hostless mode, it

uses its in-module Cortex-M33 core, which includes 512kB of flash memory and 32kB of RAM. The module's key characteristics also include a frequency range of 863-870MHz, with 868MHz being typical, a maximum transmit power of up to 14dBm, and a communication range of up to 15km in open spaces. This Click board™ is ideal for IoT devices, asset tracking and control, smart home systems, public or private networks, irrigation and agriculture applications, industrial automation, and any long-range, battery-powered sensor application. The 453-00140R and the host MCU are communicated through a UART interface, using standard UART RX and TX pins and hardware flow control pins (CTS/RTS- Clear to Send/Ready to Send) for efficient data transfer. The module defaults to a communication speed of 115200bps, allowing for data exchange over AT commands. At the lower part of the LR 11 Click, an additional unpopulated header offers full support for debugging and programming capabilities. With this header, users can use a Serial Wire Debug interface for programming and debugging, available through the SWD interface pins (SWDIO, SWCLK, and SWO). Besides interface pins, this Click board™ also has a reset pin/button (RESET) for direct module resetting and a BOOT button used to determine when the bootloader must be executed. Upon reset, the bootloader begins execution. When the BOOT button is pressed, the bootloader executes firmware updates via the UART. When

released, the bootloader stops execution and passes control to the main application firmware. A special feature of the LR 11 Click is the additional mikroBUS™ socket, which expands the board's functionality with peripherals such as sensors and LCDs. Added Click boards™ can communicate with the onboard 453-00140R module through UART, SPI, or I2C interfaces. Given that the 453-00140R GPIOs PD02 and PD03 are shared across multiple signals (can be used for all three interfaces), they must be configured appropriately when selecting the communication mode. This is achieved via six pads located on the peripheral mikroBUS™ socket section, enabling the PD02 and PD03 pins of the module to be set for the desired interface. By default, SPI communication is selected with the CIPO and COPI pins connected. If another interface is needed, it is necessary to cut these traces (disconnect them) and then solder the pads of the desired interface pins (UART or I2C). This Click board™ has both mikroBUS™ power rails needed for the peripheral mikroBUS™ socket but only uses a 3.3V voltage level as a main power supply for the 453-00140R module. The board must perform appropriate logic voltage level conversion before using MCUs with different logic levels. It also comes equipped with a library containing functions and example code that can be used as a reference for further development.

LR 11 Click - 868MHz hardware overview image

Features overview

Development board

EasyPIC v8 is a development board specially designed for the needs of rapid development of embedded applications. It supports many high pin count 8-bit PIC microcontrollers from Microchip, regardless of their number of pins, and a broad set of unique functions, such as the first-ever embedded debugger/programmer. The development board is well organized and designed so that the end-user has all the necessary elements, such as switches, buttons, indicators, connectors, and others, in one place. Thanks to innovative manufacturing technology, EasyPIC v8 provides a fluid and immersive working experience, allowing access anywhere and under any

circumstances at any time. Each part of the EasyPIC v8 development board contains the components necessary for the most efficient operation of the same board. In addition to the advanced integrated CODEGRIP programmer/debugger module, which offers many valuable programming/debugging options and seamless integration with the Mikroe software environment, the board also includes a clean and regulated power supply module for the development board. It can use a wide range of external power sources, including a battery, an external 12V power supply, and a power source via the USB Type-C (USB-C) connector.

Communication options such as USB-UART, USB DEVICE, and CAN are also included, including the well-established mikroBUS™ standard, two display options (graphical and character-based LCD), and several different DIP sockets. These sockets cover a wide range of 8-bit PIC MCUs, from the smallest PIC MCU devices with only eight up to forty pins. EasyPIC v8 is an integral part of the Mikroe ecosystem for rapid development. Natively supported by Mikroe software tools, it covers many aspects of prototyping and development thanks to a considerable number of different Click boards™ (over a thousand boards), the number of which is growing every day.

EasyPIC v8 horizontal image

Microcontroller Overview

MCU Card / MCU

PIC18LF46K40

Architecture

PIC

MCU Memory (KB)

64

Silicon Vendor

Microchip

Pin count

40

RAM (Bytes)

3728

You complete me!

Accessories

The Sub-GHz FlexDIPOLE antenna, specifically the Ezurio 868/915 MHz model, is a compact and highly versatile antenna designed to deliver robust and consistent performance across the entire 868 and 915 MHz ISM bands. Its small footprint, coupled with flexible dipole technology, makes it ideal for demanding applications like LoRaWAN and other sub-GHz technologies. The antenna has MHF4L connector options, ensuring easy integration into various systems. Additionally, its adhesive backing allows for quick and easy installation, providing reliable coverage from 863 to 928 MHz in challenging real-world conditions.

LR 11 Click - 868MHz accessories 1 image

Used MCU Pins

mikroBUS™ mapper

NC
NC
AN
Reset / ID SEL
RE1
RST
UART CTS / ID COMM
RE0
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
UART RTS
RB0
INT
UART TX
RC6
TX
UART RX
RC7
RX
NC
NC
SCL
NC
NC
SDA
Power Supply
5V
5V
Ground
GND
GND
1

Take a closer look

Click board™ Schematic

LR 11 Click - 868MHz Schematic schematic

Step by step

Project assembly

EasyPIC v8 front image hardware assembly

Start by selecting your development board and Click board™. Begin with the EasyPIC v8 as your development board.

EasyPIC v8 front image hardware assembly
GNSS2 Click front image hardware assembly
MCU DIP 40 hardware assembly
GNSS2 Click complete accessories setup image hardware assembly
EasyPIC v8 Access DIPMB 1 - upright/background hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
NECTO Compiler Selection Step Image hardware assembly
NECTO Output Selection Step Image hardware assembly
Necto image step 6 hardware assembly
Necto DIP image step 7 hardware assembly
Necto image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Necto PreFlash Image hardware assembly

Track your results in real time

Application Output

1. Application Output - In Debug mode, the 'Application Output' window enables real-time data monitoring, offering direct insight into execution results. Ensure proper data display by configuring the environment correctly using the provided tutorial.

2. UART Terminal - Use the UART Terminal to monitor data transmission via a USB to UART converter, allowing direct communication between the Click board™ and your development system. Configure the baud rate and other serial settings according to your project's requirements to ensure proper functionality. For step-by-step setup instructions, refer to the provided tutorial.

3. Plot Output - The Plot feature offers a powerful way to visualize real-time sensor data, enabling trend analysis, debugging, and comparison of multiple data points. To set it up correctly, follow the provided tutorial, which includes a step-by-step example of using the Plot feature to display Click board™ readings. To use the Plot feature in your code, use the function: plot(*insert_graph_name*, variable_name);. This is a general format, and it is up to the user to replace 'insert_graph_name' with the actual graph name and 'variable_name' with the parameter to be displayed.

Software Support

Library Description

This library contains API for LR 11 Click - 868MHz driver.

Key functions:

  • lr11868mhz_reset_device - This function resets the device by toggling the reset pin logic state.

  • lr11868mhz_cmd_run - This function sends a specified command with or without parameters to the click module.

  • lr11868mhz_cmd_set - This function sets a value to a specified command parameter of the click module.

Open Source

Code example

The complete application code and a ready-to-use project are available through the NECTO Studio Package Manager for direct installation in the NECTO Studio. The application code can also be found on the MIKROE GitHub account.

/*!
 * @file main.c
 * @brief LR 11 868MHz Click Example.
 *
 * # Description
 * This example demonstrates the use of LR 11 868MHz click board by showing
 * the communication between two click boards configured in P2P network mode.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver and logger.
 *
 * ## Application Task
 * Application task is split in few stages:
 *  - LR11868MHZ_POWER_UP:
 * Powers up the device, performs a device factory reset and reads system information.
 *  - LR11868MHZ_CONFIG_EXAMPLE:
 * Configures device for the LoRa P2P network mode.
 *  - LR11868MHZ_EXAMPLE:
 * Performs a LoRa P2P example by exchanging messages with another LR 11 868MHz click board.
 * One device should be set to NODE_0_ADDRESS, and the other to NODE_1_ADDRESS.
 *
 * ## Additional Function
 * - static void lr11868mhz_clear_app_buf ( void )
 * - static void lr11868mhz_log_app_buf ( void )
 * - static err_t lr11868mhz_process ( lr11868mhz_t *ctx )
 * - static err_t lr11868mhz_read_response ( lr11868mhz_t *ctx, uint8_t *rsp )
 * - static err_t lr11868mhz_power_up ( lr11868mhz_t *ctx )
 * - static err_t lr11868mhz_config_example ( lr11868mhz_t *ctx )
 * - static err_t lr11868mhz_example ( lr11868mhz_t *ctx )
 *
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "lr11868mhz.h"
#include "conversions.h"
#include "generic_pointer.h"

// Node address selection macros
#define NODE_0_ADDRESS      0
#define NODE_1_ADDRESS      1
#define NODE_ADDRESS        NODE_0_ADDRESS

// Text message for transmittion
#define DEMO_TEXT_MESSAGE   "MIKROE - LR 11 868MHz click board"

static lr11868mhz_t lr11868mhz;
static log_t logger;

// Application buffer size
#define APP_BUFFER_SIZE     600
#define PROCESS_BUFFER_SIZE 200

static uint8_t app_buf[ APP_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;

/**
 * @brief Example states.
 * @details Predefined enum values for application example state.
 */
typedef enum
{
    LR11868MHZ_POWER_UP = 1,
    LR11868MHZ_CONFIG_EXAMPLE,
    LR11868MHZ_EXAMPLE

} lr11868mhz_app_state_t;

static lr11868mhz_app_state_t app_state = LR11868MHZ_POWER_UP;

/**
 * @brief LR 11 868MHz clearing application buffer.
 * @details This function clears memory of application buffer and reset its length.
 * @note None.
 */
static void lr11868mhz_clear_app_buf ( void );

/**
 * @brief LR 11 868MHz log application buffer.
 * @details This function logs data from application buffer to USB UART.
 * @note None.
 */
static void lr11868mhz_log_app_buf ( void );

/**
 * @brief LR 11 868MHz data reading function.
 * @details This function reads data from device and concatenates data to application buffer. 
 * @param[in] ctx : Click context object.
 * See #lr11868mhz_t object definition for detailed explanation.
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lr11868mhz_process ( lr11868mhz_t *ctx );

/**
 * @brief LR 11 868MHz read response function.
 * @details This function waits for a response message, reads and displays it on the USB UART.
 * @param[in] ctx : Click context object.
 * See #lr11868mhz_t object definition for detailed explanation.
 * @param[in] rsp  Expected response.
 * @return @li @c  0 - OK response.
 *         @li @c -2 - Timeout error.
 *         @li @c -3 - Command error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lr11868mhz_read_response ( lr11868mhz_t *ctx, uint8_t *rsp );

/**
 * @brief LR 11 868MHz power up function.
 * @details This function powers up the device, performs device factory reset and reads system information.
 * @param[in] ctx : Click context object.
 * See #lr11868mhz_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lr11868mhz_power_up ( lr11868mhz_t *ctx );

/**
 * @brief LR 11 868MHz config example function.
 * @details This function configures device for LoRa P2P example.
 * @param[in] ctx : Click context object.
 * See #lr11868mhz_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lr11868mhz_config_example ( lr11868mhz_t *ctx );

/**
 * @brief LR 11 868MHz example function.
 * @details This function performs a LoRa P2P example by exchanging messages with another LR 11 868MHz click board.
 * @param[in] ctx : Click context object.
 * See #lr11868mhz_t object definition for detailed explanation.
 * @return @li @c    0 - OK.
 *         @li @c != 0 - Read response error.
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t lr11868mhz_example ( lr11868mhz_t *ctx );

void application_init ( void ) 
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    lr11868mhz_cfg_t lr11868mhz_cfg;  /**< Click config object. */

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );

    // Click initialization.
    lr11868mhz_cfg_setup( &lr11868mhz_cfg );
    LR11868MHZ_MAP_MIKROBUS( lr11868mhz_cfg, MIKROBUS_1 );
    if ( UART_ERROR == lr11868mhz_init( &lr11868mhz, &lr11868mhz_cfg ) ) 
    {
        log_error( &logger, " Communication init." );
        for ( ; ; );
    }
    
    log_info( &logger, " Application Task " );

    app_state = LR11868MHZ_POWER_UP;
    log_printf( &logger, ">>> APP STATE - POWER UP <<<\r\n\n" );
}

void application_task ( void ) 
{
    switch ( app_state )
    {
        case LR11868MHZ_POWER_UP:
        {
            if ( LR11868MHZ_OK == lr11868mhz_power_up( &lr11868mhz ) )
            {
                app_state = LR11868MHZ_CONFIG_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - CONFIG EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LR11868MHZ_CONFIG_EXAMPLE:
        {
            if ( LR11868MHZ_OK == lr11868mhz_config_example( &lr11868mhz ) )
            {
                app_state = LR11868MHZ_EXAMPLE;
                log_printf( &logger, ">>> APP STATE - EXAMPLE <<<\r\n\n" );
            }
            break;
        }
        case LR11868MHZ_EXAMPLE:
        {
            lr11868mhz_example( &lr11868mhz );
            break;
        }
        default:
        {
            log_error( &logger, " APP STATE." );
            break;
        }
    }
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

static void lr11868mhz_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
}

static void lr11868mhz_log_app_buf ( void )
{
    for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
    {
        log_printf( &logger, "%c", app_buf[ buf_cnt ] );
    }
}

static err_t lr11868mhz_process ( lr11868mhz_t *ctx ) 
{
    uint8_t rx_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
    int32_t overflow_bytes = 0;
    int32_t rx_cnt = 0;
    int32_t rx_size = lr11868mhz_generic_read( ctx, rx_buf, PROCESS_BUFFER_SIZE );
    if ( ( rx_size > 0 ) && ( rx_size <= APP_BUFFER_SIZE ) ) 
    {
        if ( ( app_buf_len + rx_size ) > APP_BUFFER_SIZE ) 
        {
            overflow_bytes = ( app_buf_len + rx_size ) - APP_BUFFER_SIZE;
            app_buf_len = APP_BUFFER_SIZE - rx_size;
            memmove ( app_buf, &app_buf[ overflow_bytes ], app_buf_len );
            memset ( &app_buf[ app_buf_len ], 0, overflow_bytes );
        }
        for ( rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buf[ rx_cnt ] ) 
            {
                app_buf[ app_buf_len++ ] = rx_buf[ rx_cnt ];
            }
        }
        return LR11868MHZ_OK;
    }
    return LR11868MHZ_ERROR;
}

static err_t lr11868mhz_read_response ( lr11868mhz_t *ctx, uint8_t *rsp ) 
{
    #define READ_RESPONSE_TIMEOUT_MS 30000
    uint32_t timeout_cnt = 0;
    lr11868mhz_clear_app_buf ( );
    lr11868mhz_process( ctx );
    while ( ( 0 == strstr( app_buf, rsp ) ) &&
            ( 0 == strstr( app_buf, LR11868MHZ_RSP_ERROR ) ) )
    {
        lr11868mhz_process( ctx );
        if ( timeout_cnt++ > READ_RESPONSE_TIMEOUT_MS )
        {
            lr11868mhz_clear_app_buf( );
            log_error( &logger, " Timeout!" );
            return LR11868MHZ_ERROR_TIMEOUT;
        }
        Delay_ms( 1 );
    }
    Delay_ms ( 200 );
    lr11868mhz_process( ctx );
    if ( strstr( app_buf, rsp ) )
    {
        lr11868mhz_log_app_buf( );
        log_printf( &logger, "--------------------------------\r\n" );
        return LR11868MHZ_OK;
    }
    lr11868mhz_log_app_buf( );
    return LR11868MHZ_ERROR_CMD;
}

static err_t lr11868mhz_power_up ( lr11868mhz_t *ctx )
{
    err_t error_flag = LR11868MHZ_OK;
    
    log_printf( &logger, ">>> Reset device.\r\n" );
    lr11868mhz_reset_device( &lr11868mhz );
    while ( LR11868MHZ_OK == lr11868mhz_process( ctx ) )
    {
        lr11868mhz_log_app_buf( );
        lr11868mhz_clear_app_buf ( );
    }
    log_printf( &logger, "--------------------------------\r\n" );

    log_printf( &logger, ">>> Factory reset.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_FACTORY_RESET, NULL );
    error_flag |= lr11868mhz_read_response( &lr11868mhz, LR11868MHZ_RSP_OK );

    log_printf( &logger, ">>> Check communication.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_AT, NULL );
    error_flag |= lr11868mhz_read_response( &lr11868mhz, LR11868MHZ_RSP_OK );

    log_printf( &logger, ">>> Get device type.\r\n" );
    lr11868mhz_cmd_run( ctx, LR11868MHZ_CMD_GET_INFO, LR11868MHZ_PARAM_DEVICE_TYPE );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );

    log_printf( &logger, ">>> Get firmware version.\r\n" );
    lr11868mhz_cmd_run( ctx, LR11868MHZ_CMD_GET_INFO, LR11868MHZ_PARAM_APP_FW_VERSION );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );

    return error_flag;
}

static err_t lr11868mhz_config_example ( lr11868mhz_t *ctx )
{
    uint8_t data_buf[ 10 ] = { 0 };
    err_t error_flag = LR11868MHZ_OK;
    #define DEVICE_CLASS_P2P "3"
    log_printf( &logger, ">>> Set LoRa operation to P2P.\r\n" );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_DEVICE_CLASS, DEVICE_CLASS_P2P );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );

    #define REGION_EU868 "1"
    log_printf( &logger, ">>> Set operation region to EU868.\r\n" );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_REGION, REGION_EU868 );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );

    int16_to_str ( NODE_ADDRESS, data_buf );
    l_trim ( data_buf );
    r_trim ( data_buf );
    log_printf( &logger, ">>> Set P2P node address to %s.\r\n", data_buf );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_DEVICE_ADDRESS, data_buf );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );

    #define P2P_NETWORK_SIZE "2"
    log_printf( &logger, ">>> Set P2P network size to %s nodes.\r\n", ( char * ) P2P_NETWORK_SIZE );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_NET_SIZE, P2P_NETWORK_SIZE );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    #define P2P_DATA_RATE_DR2 "2"
    log_printf( &logger, ">>> Set P2P data rate to DR2.\r\n" );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_DATA_RATE, P2P_DATA_RATE_DR2 );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    #define P2P_LISTEN_DURATION "0"
    log_printf( &logger, ">>> Set P2P listen duration to 0.\r\n" );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_LISTEN_DURATION, P2P_LISTEN_DURATION );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    #define P2P_LISTEN_INTERVAL "0"
    log_printf( &logger, ">>> Set P2P listen interval to 0.\r\n" );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_LISTEN_INTERVAL, P2P_LISTEN_INTERVAL );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    #define P2P_BEACON_DATA_RATE_DR2 "2"
    log_printf( &logger, ">>> Set P2P beacon data rate to DR2.\r\n" );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_BEACON_DATA_RATE, P2P_BEACON_DATA_RATE_DR2 );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    #define P2P_TX_POWER "1"
    log_printf( &logger, ">>> Set P2P TX power to %s dBm.\r\n", ( char * ) P2P_TX_POWER );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_TX_POWER, P2P_TX_POWER );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Save settings.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_SAVE_SETTINGS, NULL );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Reboot.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_WARM_RESET, NULL );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Get P2P maximum packet size.\r\n" );
    lr11868mhz_cmd_run( ctx, LR11868MHZ_CMD_GET_INFO, LR11868MHZ_PARAM_P2P_SLOT_PACKET_SIZE );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    int16_t packet_size = atoi( &app_buf[ 1 ] ) - 1;
    if ( packet_size < strlen ( DEMO_TEXT_MESSAGE ) )
    {
        log_error( &logger, " DEMO_TEXT_MESSAGE length [%d] exceeds the packet size limit [%d].\r\n", 
                   ( int16_t ) strlen ( DEMO_TEXT_MESSAGE ), packet_size );
        for ( ; ; );
    }
    
    int16_to_str ( strlen ( DEMO_TEXT_MESSAGE ), data_buf );
    l_trim ( data_buf );
    r_trim ( data_buf );
    log_printf( &logger, ">>> Set P2P packet size to %s.\r\n", data_buf );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_PACKET_SIZE, data_buf );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Save settings.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_SAVE_SETTINGS, NULL );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Reboot.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_WARM_RESET, NULL );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Get P2P minimum window length.\r\n" );
    lr11868mhz_cmd_run( ctx, LR11868MHZ_CMD_GET_INFO, LR11868MHZ_PARAM_P2P_MIN_WINDOW_LENGTH );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    int16_t window_len = atoi( &app_buf[ 1 ] ) + 1;
    int16_to_str ( window_len, data_buf );
    l_trim ( data_buf );
    r_trim ( data_buf );
    log_printf( &logger, ">>> Set P2P window length to %s.\r\n", data_buf );
    lr11868mhz_cmd_set( ctx, LR11868MHZ_CMD_PARAM_ACCESS_NUM, LR11868MHZ_PARAM_ID_P2P_WINDOW_LENGTH, data_buf );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Save settings.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_SAVE_SETTINGS, NULL );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Reboot.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_WARM_RESET, NULL );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    log_printf( &logger, ">>> Start a P2P session.\r\n" );
    lr11868mhz_cmd_run( &lr11868mhz, LR11868MHZ_CMD_P2P_START_SESSION, NULL );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );
    Delay_ms ( 1000 );

    return error_flag;
}

static err_t lr11868mhz_example ( lr11868mhz_t *ctx )
{
    err_t error_flag = LR11868MHZ_OK;
    uint8_t param_buf[ 120 ] = { 0 };
    uint8_t msg_hex[ 101 ] = { 0 };
    uint8_t byte_hex[ 3 ] = { 0 };
    uint8_t src_addr[ 10 ] = { 0 };
    uint8_t source[ 10 ] = { 0 };
    uint8_t rssi[ 10 ] = { 0 };
    uint8_t snr[ 10 ] = { 0 };
    uint8_t cnt = 0;

    memset( msg_hex, 0, sizeof ( msg_hex ) );
    for ( cnt = 0; ( cnt < strlen ( DEMO_TEXT_MESSAGE ) ) && ( cnt < 50 ); cnt++ ) 
    {
        uint8_to_hex ( DEMO_TEXT_MESSAGE[ cnt ], byte_hex );
        strcat ( msg_hex, byte_hex );
    }
#if ( NODE_ADDRESS == NODE_0_ADDRESS )
    int16_to_str ( NODE_1_ADDRESS, param_buf );
    l_trim ( param_buf );
    r_trim ( param_buf );
#else
    int16_to_str ( NODE_0_ADDRESS, param_buf );
    l_trim ( param_buf );
    r_trim ( param_buf );
#endif
    log_printf( &logger, ">>> Send message \"%s\" to node address %s.\r\n", ( char * ) DEMO_TEXT_MESSAGE, param_buf );
    strcat ( param_buf, ", \"" );
    strcat ( param_buf, msg_hex );
    strcat ( param_buf, "\"" );
    lr11868mhz_cmd_run( ctx, LR11868MHZ_CMD_P2P_SEND_DATA, param_buf );
    error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_RSP_OK );
    
    if ( LR11868MHZ_OK == error_flag )
    {
        memset( msg_hex, 0, sizeof ( msg_hex ) );
        log_printf( &logger, ">>> Waiting for a P2P RX message.\r\n" );
        error_flag |= lr11868mhz_read_response( ctx, LR11868MHZ_EVT_RX_P2P );
    }
    else
    {
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
        Delay_ms ( 1000 );
    }
    
    if ( LR11868MHZ_OK == error_flag )
    {
        uint8_t * __generic_ptr start_ptr = strstr( app_buf, LR11868MHZ_EVT_RX_P2P );
        uint8_t * __generic_ptr end_ptr = NULL;
        if ( start_ptr )
        {
            start_ptr = start_ptr + strlen ( LR11868MHZ_EVT_RX_P2P );
            end_ptr = strstr ( start_ptr, "," );
            memcpy ( source, start_ptr, end_ptr - start_ptr );
            
            start_ptr = strstr ( end_ptr, ":" ) + 1;
            end_ptr = strstr ( start_ptr, "," );
            memcpy ( rssi, start_ptr, end_ptr - start_ptr );

            start_ptr = strstr ( end_ptr, ":" ) + 1;
            end_ptr = strstr ( start_ptr, "," );
            memcpy ( snr, start_ptr, end_ptr - start_ptr );
            
            start_ptr = end_ptr + 2;
            end_ptr = strstr ( start_ptr, "\n" );
            memcpy ( msg_hex, start_ptr, end_ptr - start_ptr );

            for ( cnt = 0; cnt < strlen ( msg_hex ); cnt += 2 )
            {
                msg_hex[ cnt / 2 ] = hex_to_uint8 ( &msg_hex [ cnt ] );
            }
            msg_hex[ cnt / 2 ] = 0;
            log_printf( &logger, ">>> Parse received message.\r\n" );
            log_printf ( &logger, " Source address: %s\r\n", source );
            log_printf ( &logger, " Message: %s\r\n", msg_hex );
            log_printf ( &logger, " RSSI: %s\r\n", rssi );
            log_printf ( &logger, " SNR: %s\r\n", snr );
            log_printf( &logger, "--------------------------------\r\n" );
        }
    }
    return error_flag;
}

// ------------------------------------------------------------------------ END

Additional Support

Resources

Love this project?

'Buy This Kit' button takes you directly to the shopping cart where you can easily add or remove products.