-
Notifications
You must be signed in to change notification settings - Fork 13
5. Examples
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.
CatWAN USB Stick SAMD21 915 MHz version
|
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.
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.
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
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
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.
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.
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
Once your build tools are installed, fetch the CircuitPython source code from its GitHub repository ("repo") and also fetch the git "submodules" it needs. The submodules are extra code that you need that's stored in another repository.
In the commands below, you're cloning from Adafruit's CircuitPython repo. But if you want to make changes, you might want to "fork" that repo on GitHub to make a copy for yourself, and clone from there.
git clone https://github.com/adafruit/circuitpython.git
cd circuitpython
git submodule sync
git submodule update --init --recursive
Build the mpy-cross compiler first, which compiles Circuitpython .py files into .mpy files. It's needed to include library code in certain boards.
Download file Copy Code
make -C mpy-cross
Now you're all set to build CircuitPython. If you're in the master branch of the repo, you'll be building the latest version (3.0 as of this writing). Choose which board you want to build for. The boards available are all the subdirectories in ports/atmel-samd/boards/.
cd ports/atmel-samd
make BOARD=catwan_usbstick
In 4.x we've introduced translated versions of CircuitPython. By default the en_US version will be built. To build for a different language supply a TRANSLATION argument.
cd ports/atmel-samd
make BOARD=catwan_usbstick TRANSLATION=es
When you've successfully built, you'll see output like:
Create build-catwan_usbstick/firmware.bin
Create build-catwan_usbstick/firmware.uf2
python2 ../../tools/uf2/utils/uf2conv.py -b 0x2000 -c -o build-catwan_usbstick/firmware.uf2 build-catwan_usbstick/firmware.bin
Converting to uf2, output size: 485888, start address: 0x2000
Wrote 485888 bytes to catwan_usbstick/firmware.uf2.
Copy firmware.uf2 to your board the same way you'd update CircuitPython: Double-click to get the BOOT drive, and then just copy the .uf2 file:
Double-click the reset button, then:
cp build-circuitplayground_express/firmware.uf2 /media/yourname/CATWAN
The board will restart, and your build will start running.
If you're using a board without a UF2 bootloader, you'll need to use bossac and the firmware.bin file, not the .uf2 file. Detailed instructions are here.
After you make changes to code, normally just doing make BOARD=... will be sufficient. The changed files will be recompiled and CircuitPython will be rebuilt.
However, there are some circumstance where you must do:
make clean BOARD=...
If you have changed the #include file structure in certain ways, or if you have defined QSTR's (a way of defining constants strings in the CircuitPython source), then you must make clean before rebuilding. If you're not sure, it's always safe to make clean and then make. It might take a little longer to build, but you'll be sure it was rebuilt properly.
When there are changes in the GitHub repo, you might want to fetch those and then rebuild. Just "pull" the new code (assuming you haven't made changes yourself), update the submodules if necessary, and rebuild:
git pull
git submodule update --init --recursive
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 in tutorial of adafruit
Thank you for reading our Wiki!