This tutorial explains how to send and receive messages throughout LoRa networks, leveraging the power of Electronic Cats' CatWAN USB Stick. Using two pieces of this device, we will create a LoRa network, where the first one will send a value changing over time, and the second one will receive the information.
What is LoRa?
LoRa is a long-range, low-power wireless communication technology widely used in sensor networks, especially in the Internet of Things (IoT) applications.
Basic SetupTo get started with the process, some tools must be installed.
1. Install the Arduino IDE software.
2. In Arduino IDE, install the following library:
- LoRa (by Sandeep Mistry)
3. Install the CatWAN USB Stick Cores.
Note: The Electronic Cats cores require Arduino IDE 1.6.7 or above (including 1.8.X).
Installing CatWAN USB Stick core for SAMD21 version
1. Go to Arduino IDE > File > Preferences.
2. Click on the button next to the Additional Boards Manager URLs field.
3. Add the link: https://electroniccats.github.io/Arduino_Boards_Index/package_electroniccats_index.json.
4. Save preferences.
5. Go to Arduino IDE > Tools > Board > Boards Manager and type ArduinoSAMD Boards.
6. Install the latest version.
7. Install the latest version of the Electronic Cats SAMD Boards package.
8. Close the Boards Manager. Then go to Tools > Boards > Electronic Cats SAMD (L)(C) Core for Arduino > Electronic Cats CatWAN USB Stick.
9. Plug the board into the computer. The Blink sketch should be running.
10. Select the correct port by clicking on the Tools > Port menu.
11. You can now download your own sketch.
Installing CatWAN USB Stick core for RP2040 version
1. Go to Arduino IDE > File > Preferences.
2. Click on the button next to the Additional Boards Manager URLs field.
3. Add the link: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
4. Save preferences.
5. Go to Arduino IDE > Tools > Board > Boards Manager and type Raspberry Pi Pico/RP2040/RP2350.
6. Install the latest version.
7. Close the Boards Manager. Then go to Tools > Board > Raspberry Pi RP2040 > Raspberry Pi Pico.
8. Plug the board into the computer. The Blink sketch should be running.
9. Select the correct port by clicking on the Tools > Port menu.
10. You can now download your own sketch.
The FirmwareWe will use two different programs since we will use two CatWAN USB Stick boards for the application.
Let's take a look at the LoRa_Sender.ino sketch:
/**
* Example LoRaSender
* Authors: Electronic Cats
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
* please buy us a round!
* Distributed as-is; no warranty is given.
*/
#include <SPI.h>
#include <LoRa.h>
int counter = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Sender");
LoRa.setPins(SS, RFM_RST, RFM_DIO0);
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
Serial.print("Sending packet: ");
Serial.println(counter);
// send packet
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();
counter++;
delay(5000);
}
This sketch includes the SPI library, which is part of the core and sets the communication between the MCU and the radio module, and the LoRa library, which contains all the necessary instructions for deploying LoRa communication.
Then, it creates the counter variable (int type). This is the variable that will be shared periodically with the LoRa network.
The void_setup() routine initializes the serial communication, and the application won't run until a serial monitor is opened. The pins are designated and set for the radio module:
- SS: Chip select.
- RFM_RST: RFM module reset pin.
- RFM_DIO0: RFM module DIO0 pin. This pin sends a signal to the MCU to indicate whether the reception/transmission process has concluded.
Then, the radio module is initialized at the 915 MHz frequency. The frequency may change depending on the region and the CatWAN USB Stick's version you are using.
Then, the application moves to the void_loop() routine, which prints the message "Sending packet: " and the current value of the counter variable. A LoRa package with the counter is created and sent to the network.
Finally, the application waits 5 seconds before repeating the void_loop() routine.
On the other hand, we have the LoRaReceiver.ino sketch:
/**
* Example LoRaReceiver
* Authors: Electronic Cats
*
* This code is beerware; if you see me (or any other collaborator
* member) at the local, and you've found our code helpful,
* please buy us a round!
* Distributed as-is; no warranty is given.
*/
#include <SPI.h>
#include <LoRa.h>
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver");
LoRa.setPins(SS, RFM_RST, RFM_DIO0);
if (!LoRa.begin(915E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
This sketch also includes the SPI and LoRa libraries.
The void_setup() routine initializes the serial communication, and the application won't run until a serial monitor is opened. The pins are designated and set for the radio module:
- SS: Chip select.
- RFM_RST: RFM module reset pin.
- RFM_DIO0: RFM module DIO0 pin. This pin sends a signal to the MCU to indicate whether the reception/transmission process has concluded.
Then, the radio module is initialized at the 915 MHz frequency. The frequency may change depending on the region and the CatWAN USB Stick's version you are using.
Moving into the void_loop() routine, it creates a packetSize (int type) variable to store the data returned by the LoRa.parsePacket() routine. If packetSize is greater than 0, it informs the user that a packet has been received. The packet will be read and printed as long as the LoRa connection is available. Finally, it prints the received message's power.
Outcomes
Once the network is built and the sketches have been uploaded to both boards, the sender and receiver, we open a Serial Monitor for each device, and should see the following:
The next step is to upload the sketches.
1. Open the LoRaSender.ino example.
2. Connect one of the CatWAN USB Stick boards and select the port assigned to this board. Choose the right option, either the SAMD version or the RP2040 version.
3. Compile and upload the sketch.
4. Open the LoRaReceiver.ino example.
5. Connect one of the CatWAN USB Stick boards and select the port assigned to this board. Choose the right option, either the SAMD version or the RP2040 version.
6. Compile and upload the sketch.
7. Open the Serial Monitor for each board.
Now, you can see both the transmitted and received data in the Serial Monitor, with one CatWAN USB Stick acting as the sender and the other one as the receiver.
We hope you enjoyed the process!
Happy hacking!
Comments