Skip to content

5. Examples

Jaz edited this page Jul 18, 2024 · 10 revisions

CatWAN Arduino LoRa Shield supports LoRa and LoRaWAN technologies. In the following section, we will explore how to utilize each of these technologies with different examples.

For the following examples, we will use two CatWAN USB Sticks, one to send information and the other to sniff it.

Imagen 0
CatWAN USB Stick SAMD21 915 MHz version
Imagen 1
CatWAN USB Stick RP2040 915 MHz version

Remember that to use your CatWAN USB Stick you need to have different libraries installed. If you have not installed it yet, check the section: Install the Libraries.

Using LoRa on Arduino IDE

The LoRa technology, on its own, is ideal for applications that require long-range communication and low energy consumption. Its main uses include sending and receiving information over considerable distances, as well as remote control.

In the LoRa examples, we will show seamless communication between two boards using LoRa technology. To achieve this we will need two CatWAN USB Sticks that have LoRa technology.

In this case, we will use a CatWAN USB Stick RP2040 version and a CatWAN USB Stick SAMD version.

Example Device-to-device communication

For this first example, we will use the basic examples from the repository, for the examples from the CatWAN USB Stick SAMD version you can find them in the LoRa examples folder.

In the case of using a CatWAN USB Stick RP2040 version, you must download the examples directly from the Latest Release.

So, we will upload the "LoRaReceiver example code" to our CatWAN USB Stick SAMD version.

Then, we will upload the "lorasender.uf2" to our CatWAN USB Stick RP2040 version.


CatWAN USB Stick RP2040 Release

After this, our boards should start communicating and now we will be able to see the values we receive through the serial port:


Serial Output

Using LoRa Sniffer Tool

CLI

Typing help in the serial terminal we can see the commands that the LoRa Sniffer tool has:

Fw version: 0.2v

Configuration commands:

set_rx
set_tx
set_tx_hex
set_tx_ascii
set_chann
set_freq
set_sf
set_bw
set_cr

Monitor commands:

get_freq
get_sf
get_bw
get_cr
get_config

Usage

Configuration commands:

set_rx -> LoRa Sniffer goes into rx mode and changes rx_status to 1 if the frequency value is between 902 MHz and 923 MHz, it can accept the frequency value as an argument.
set_freq -> Sets the frequency between 902 MHz and 923 MHz.
set_sf -> Sets the spread factor value between 6 and 12.
set_cr -> Sets the coding rate value between 5 and 8.
set_bw -> Sets the bandwidth value using 0 - 8.
          0 - Bandwidth set to 7.8 kHz
          1 - Bandwidth set to 10.4 kHz
          2 - Bandwidth set to 15.6 kHz
          3 - Bandwidth set to 20.8 kHz
          4 - Bandwidth set to 31.25 kHz
          5 - Bandwidth set to 41.7 kHz
          6 - Bandwidth set to 62.5 kHz
          7 - Bandwidth set to 125 kHz
          8 - Bandwidth set to 250 kHz

set_tx - > Use a series of xxx values separated by spaces. The value xxx represents a 3-digit number.
set_tx_hex -> Use a series of yy values separated by spaces. The value yy represents any pair of hexadecimal digits.
set_tx_ascii -> Use for sending ASCII messages.
set_chann -> Sets the LoRaWAN channel value between 0 and 63.

Monitor commands:

get_freq -> get frequency value.
get_sf -> get spreading factor value.
get_cr -> Get coding rate value. 
get_bw -> Get bandwidth value.
get_config -> Get radio configurations.

Examples

input->

set_freq 903.9
set_sf 8
set_cr 5
set_rx 

output->

Frequency set to 903.90 MHz
Spreading factor set to 8
CodingRate set to 4/5
LoRa radio receiving at 903.90 MHz
...

Received packet '
19 bytes ' 401F80C0ED000305018BB696B5827DFEA36D33
ASCII: '@⸮&⸮⸮⸮⸮⸮}⸮⸮m3' with RSSI -81

Received packet '
19 bytes ' 401F80C0ED00070501D8FF1D53C1976AAEEF71
ASCII: '@⸮&⸮⸮S⸮⸮j⸮⸮⸮q' with RSSI -122

input ->

set_tx_hex AB CD EF 12 34 56 78 90

output ->

171 205 239 18 52 86 120 144 
8 Byte(s) sent

input ->

set_tx 123

output ->

123 
1 Byte(s) sent

input ->

set_tx_ascii hello_world

output ->

hello_world ASCII message sent

Using CircuitPython

LoRa example communication

Necessary libraries:

# Simple demo of sending and receiving data with the RFM95 LoRa radio.
# Author: Tony DiCola
import board
import busio
import digitalio
 
import adafruit_rfm9x
 
 
# Define radio parameters.
RADIO_FREQ_MHZ   = 915.0  # Frequency of the radio in Mhz. Must match your
                          # module! Can be a value like 915.0, 433.0, etc.
 
# Define pins connected to the chip, use these if wiring up the breakout according to the guide:

CS    = digitalio.DigitalInOut(board.RFM9X_CS)
RESET = digitalio.DigitalInOut(board.RFM9X_RST)
 
# Initialize SPI bus.
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
 
# Initialze RFM radio
rfm9x = adafruit_rfm9x.RFM9x(spi, CS, RESET, RADIO_FREQ_MHZ)
 
# Note that the radio is configured in LoRa mode so you can't control sync
# word, encryption, frequency deviation, or other settings!
 
# You can however adjust the transmit power (in dB).  The default is 13 dB but
# high power radios like the RFM95 can go up to 23 dB:
rfm9x.tx_power = 23
 
# Send a packet.  Note you can only send a packet up to 252 bytes in length.
# This is a limitation of the radio packet size, so if you need to send larger
# amounts of data you will need to break it into smaller send calls.  Each send
# call will wait for the previous one to finish before continuing.
rfm9x.send('Hello world!\r\n')
print('Sent hello world message!')
 
# Wait to receive packets.  Note that this library can't receive data at a fast
# rate, in fact it can only receive and process one 252 byte packet at a time.
# This means you should only use this for low bandwidth scenarios, like sending
# and receiving a single message at a time.
print('Waiting for packets...')
while True:
    packet = rfm9x.receive()
    # Optionally change the receive timeout from its default of 0.5 seconds:
    #packet = rfm9x.receive(timeout_s=5.0)
    # If no packet was received during the timeout then None is returned.
    if packet is None:
        print('Received nothing! Listening again...')
    else:
        # Received a packet!
        # Print out the raw bytes of the packet:
        print('Received (raw bytes): {0}'.format(packet))
        # And decode to ASCII text and print it too.  Note that you always
        # receive raw bytes and need to convert to a text format like ASCII
        # if you intend to do string processing on your data.  Make sure the
        # sending side is sending ASCII data before you try to decode!
        packet_text = str(packet, 'ascii')
        print('Received (ASCII): {0}'.format(packet_text))
        # Also read the RSSI (signal strength) of the last received message and
        # print it.
        rssi = rfm9x.rssi
        print('Received signal strength: {0} dB'.format(rssi))

Based on the tutorial of adafruit.