Building an Arduino or Raspberry Pi project which needs to send data back-and-forth? Wi-Fi would work fine for that.

But what if you need to send data back-and-forth over a long-range and also do not wish to spend money to invest in a Wi-Fi setup on routers, access points and also network security programs?

This is where LoRa and LoRaWAN come in. They act as a radio to periodically transmit sensor or device data over a long distance of up to 15 to 20km at a low cost that is secure as well!

Through this blog, we will learn about how to use LoRa and LoRaWAN with the Arduino and Raspberry Pi! This blog will cover:

  • What is LoRa and LoRaWAN?
  • LoRa vs LoRaWAN – What is the difference?
  • Why use LoRa and LoRaWAN Technology?
  • LoRa with Arduino
  • LoRaWAN with Raspberry Pi and Arduino Tutorial

What is LoRa and LoRaWAN?

LoRa

  • Short for Long Range, is a spread spectrum modulation technique derived for chirp spread spectrum technology.
  • They are long-range, low power and also allows you to transmit data securely for your IoT applications.
  • They can be used to connect sensors, gateways, machines, devices, people, etc. wirelessly to the cloud
  • LoRa operates in various frequency bands which ranges depending on the region.
    • For example, US operates in the 915 MHz band, Asia operates in the 865 to 867 MHz and 920 to 923 MHz band.

LoRaWAN

Image result for what is lora
Ref: DigiKey
  • LoRaWAN is a Low power, Wide-area (LPWA) networking protocol that wirelessly connects battery-operated devices in a regional, national or global network.
  • LoRaWAN targets IoT needs like secure bi-directional communication, mobility and localization services by leveraging on unlicensed radio spectrum in the industrial, scientific and medical (ISM) band.
  • The network architecture of LoRaWAN is laid out in a star-of-stars topology where gateways are a transparent bridge that relays messages between end-devices and a central network server in the back end as seen on the image above.
    • Gateways are connected to a network server via Standard IP connection and end devices uses single-hop wireless communication to one or many gateways

LoRa vs LoRaWAN – What is the difference?

People may mean LoRaWAN when they talk about LoRa. But in reality, they are different.

  • LoRa contains only the link layer protocol which is perfect to be used in P2P communications between nodes.
    • They exist in the PHY (Physical) layer only which enables long-range communication link.
    • The PHY layer defines how the electronic signal is modulated which is connected to a data link layer. The data link layer detects changes in the PHY layer and establishes a protocol to send data to adjacent nodes.
  • On the other hand, LoRaWAN is a media access control (MAC) layer protocol that is built on top of LoRa. It includes the network layer too which allows it to be possible to send information to any base station that is already connected to a cloud platform.
    • LoRaWAN is the MAC sub-layer which is designed for large-scale public networks with a single operator.
    • LoRaWAN can work in different frequencies by just connecting the right antenna to its socket

In simpler terms, LoRa is the PHY layer while LoRaWAN is the data link layer. They come together to comprise the LPWA networking technology stack.

Why use LoRa and LoRaWAN technology?

There are several advantages of using LoRa and LoRaWAN technology. They are:

Long Range

  • Connects devices over longer range compared to other wireless communication technology like Wi-Fi or BLE

Low Power

  • Devices like sensors working in a LoRa network do not require much power. Battery replacement costs will be minimized as battery life can be up to a few years.

Secure

  • Features end-to-end AES128 encryption, mutual authentication, integrity protection, and confidentiality.

Standardized

  • Offers device interoperability and global availability of LoRaWAN networks for speedy deployment of IoT applications anywhere

Low Cost

  • Reduces battery replacement expenses and operating expenses (eg. LoRa operates on unlicensed frequency spectrums = no fees or license charges).

Flexible

  • Like Wi-Fi, LoRaWAN operates in the unlicensed band and supports indoor applications; like Cellular, LoRa Technology is highly secure from end devices to the application server, and is suitable for outdoor applications.
  • With these features, they are flexible and efficient which makes them ideal for your indoors or outdoors IoT applications which need to be installed in public, private or hybrid networks.

LoRa with Arduino Tutorial

With this tutorial, you can create low-power applications in your smart home, IoT projects and many more after learning how to use LoRa with Arduino!

Do note that LoRa is not suitable for projects that require high data-rate transmission, frequent transmissions or in highly populated networks.

You can use LoRa in two ways:

  • Point to point communication
  • Network (Building a LoRa network using LoRaWAN)

For today first tutorial, we are making a demo on P2P(point to point) communication with the Grove – Lora Radio and the Arduino where the devices can talk with each other using RF signals.

What do you need

Step by Step tutorial: LoRa with Arduino

  • Step 1: Connecting Hardware
    • The Seeeduino Lotus is a combination of Seeeduino and Base Shield. We can connect the LoRa Radio module to the D5 socket directly as seen below.
  • Step 2: Download Library
    • Click here to download the library and install it. (If you are unsure on how to install an Arduino library, check out our tutorial here)

The library supports AVR/SAMD/STM32F4 devices, both hardware and software serial too.

There are 2 kinds of serial ports. One is COMSerial, stands for communication port(connecting with Grove-LoRa Radio). The other is ShowSerial, stands for serial info display port(connecting with PC).

Most Arduino boards have at least one serial port. It communicates via digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. So if you connect UART device on pin D0 and pin D1, you have to remove them before downloading the program through USB. Or else it will cause upload fails.

Sometimes you need more serial ports than the number of hardware serial ports available. If this is the case, you can use a Software Serial that uses software to emulate serial hardware. Software serial requires a lot of help from the Arduino controller to send and receive data, so it’s not as fast or efficient as hardware serial. For more info about the Serial, please refer to Seeed Arduino Serial.

  • AVR: For the below example, We define Software Serial as SSCOM(connectiong with PC), you need USB to TTL adapter to connect with PC. NOT all the digital pins can be used for software serial. You can refer to Software Serial for detail pins. We define hardware Serial as COMSerial(connecting with Grove-LoRa Radio). If you want to use the hardware serial as COMSerial and software serial as ShowSerial, you can define as #define ShowSerial Serial and #define COMSerial SSerial. If you use Arduino Mega, you can connect the hardware Serial to ShowSerial and the other Serial1/Serial2/Serial3 to COMSerial. So you can refer to AVR Mega setting.
  • SAMD: For the below example, The SAMD does not support software serial. We use the hardware serial Serial1 to commuincate with Grove-LoRa Radio and SerialUSB to print message on PC.
  • STM32F4: For the below example, We use the hardware serial Serial to commuincate with Grove-LoRa Radio and SerialUSB to print message on PC.
#include <RH_RF95.h>

#################### AVR #######################

#ifdef __AVR__

#include <SoftwareSerial.h>
SoftwareSerial  SSerial(10, 11); // RX, TX

#define COMSerial Serial
#define ShowSerial SSerial 

RH_RF95<HardwareSerial> rf95(COMSerial);

#endif

#################### AVR Mega ###################

#ifdef __AVR__

#define COMSerial Serial1
#define ShowSerial Serial 

RH_RF95<HardwareSerial> rf95(COMSerial);

#endif

#################### SAMD ######################

#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE

#define COMSerial Serial1
#define ShowSerial SerialUSB 

RH_RF95<Uart> rf95(COMSerial);

#endif

#################### STM32F4 ###################

#ifdef ARDUINO_ARCH_STM32F4

#define COMSerial Serial
#define ShowSerial SerialUSB 

RH_RF95<HardwareSerial> rf95(COMSerial);

#endif
  • Step 3: Open the example
    • Open your Arduino IDE, click File > Examples>Grove_LoRa_433MHz_and_915MHz_RF-master where you will get many examples for the module.
NodeExample NameFunction
Senderrf95_clientSend “Hello World” every 1sec
Receiverrf95_serverReceive data and print it

Click Tools>Board to choose “Seeeduino Lotus” and select respective serial port then click on Upload button to finish the steps. If you are using the Grove – LoRa Radio 868 MHz module, change it to the following code:

//rf95.setFrequency(434.0);
rf95.setFrequency(868.0);
  • Step 4: Review Results
    • After the upload is completed, you should be able to see the results by opening the serial monitor.
enter image description here
  • The below chart shows the relationships between the band rate signal bandwidth spreading factor and sensitivity:
enter image description here

LoRaWAN with Raspberry Pi and Arduino Tutorial

A typical LoRa network consists of 4 parts: Devices (eg. Sensors, Radio Module), Gateways, a network service, and an application.

Similarly to a Wi-Fi network, devices and gateways are needed to set up a LoRaWAN network. The gateways help to scan the spectrum and capture LoRa packets. No devices are associated with a single gateway which allows all gateways within range of a device to receive the signal where data will be forwarded to a network service that handles the packet.

The network service act as the entity that speaks LoRaWAN to the gateway which gets the data to the application. They also handle other LoRa features like adaptive data rating. After that, the network service will then forward the decrypted data to your application where it will use the data sent.

Through this second tutorial, you will learn how to build your own private LoRaWAN network with the Arduino and the Raspberry Pi using the 4 parts stated above.

What do you need

LoRa LoRaWAN Gateway - 868MHz Kit with Raspberry Pi 3

This Kit provides you with all the basic elements you need to create your own LoRaWAN gateway. It contains:

Parts NumberParts
11 x Raspberry Pi 3
21 x Gateway module RHF0M301–868
31 x PRI 2 Bridge RHF4T002
41 x Seeeduino LoRaWAN with GPS (RHF76-052AM)
51 x USB to UART Adapter
61 x Upgrade to 16GB Micro SD Card – Class 10
71 x 0dBi Rubber Duck Antenna
81 x 5V/2.1A American Standard Adapter with Micro USB Connector
91 x Micro USB Cable 20cm
101 x Micro USB Cable 100cm
111 x RJ45 Ethernet Cable 200cm
121 x JST2.0 Cable 10cm
  • This kit provides all the basic elements you need: a Raspberry Pi 3, a Seeeduino LoRaWAN with GPS and a gateway & local server that allows you to collect and transfer data among all your LoRa nodes. By connecting the gateway with Seeeduino LoRaWAN and Grove modules, you can build your IoT prototype within minutes.
  • Regarding the gateway module RHF0M301, it is a 10 channel(8 x Multi-SF + 1 x Standard LoRa + 1 x FSK) LoRaWan gateway module with a 24pin DIP port on board, users can easily connect the RHF0M301 with PRI 2 bridge RHF4T002, adapter for Raspberry Pi 3 and RHF0M301. We also included an 868MHz antenna, an 8GB SD card and USB cables, Ethernet Cables and other accessories.

We also offer a 915 MHz Kit for Raspberry Pi 3 that is for North America Region.

Step by Step tutorial: Build your own LoRa Network using LoRaWAN

Connecting the Hardware

Since there are many interfaces here, it is necessary to know the capabilities of these interfaces. Please refer to the following figure for details:

  • Step 1: Plug Gateway module RHF0M301–868 into PRI 2 Bridge RHF4T002.
  • Step 2: Plug PRI 2 Bridge RHF4T002 into Raspberry Pi 3.
  • Step 3: Connect ❷ and ❸ via a 20cm Micro-USB cable.
  • Step 4: Connect the USB to UART Adapter to the GPIO of Raspberry Pi 3. Please connect them as the picture shown below.
  • Step 5: Plug the USB to UART Adapter into your PC
  • Step 6: Connect ❶ with 5V/2.1A Standard Adapter via 100cm Micro-USB cable

Your hardware connection should look like this:

Connecting Software

Before starting, you will require a few software where you will need to install them on your computer. They are:

  • Arduino IDE – Portable serial tool, used to open the serial port of Seeeduino LoRaWAN with GPS (RHF76-052AM) and send AT commands to it
  • PuTTY – Terminal tool includes both serial and SSH terminal used to control Raspberry Pi.
  • Internet browser – used to access RHF2S001 integrated LoRaWAN server (Recommended to use Chrome or Firefox).
Connecting to local server

Step 1: Power up and connect to PuTTY.

  • First, make sure the serial tool and RPi (RHF4T002 Adapter) are connected correctly.
  • Plug FT232 tool to PC (If COM port is not recognized correctly, please refer to Virtual COM Port Drivers)
  • Open Device Manager of your PC to get the right COM port. Like COM15 for example. Configure ExtraPuTTY according to below picture (Speed 115200, others use defaults), click Open. As the gateway is still not opened, so there is nothing in the terminal.
  • Power the gateway up. Booting log will be showed in the PuTTY terminal, at the end it will prompt you to input your login name. Please note it takes 1 or 2 minutes to get the prompt information.
  • Please use RHF2S001 default user name and password to log in. ( Username: rxhf, Password: risinghf ). Note, when inputting the password, there is no echo
  • Connect RHF2S001 with the router through ethernet cable and run ifconfig to check the IP address and MAC address.

IP IS IN THE BLUE SQUARE, MAC ADDRESS IS IN ORANGE SQUARE (FORMAT: B8:27:EB:XX:XX:XX)

To login through SSH, you need to fill in the Hostname with the IP address you’ve just got and use port 22 where you will choose the SSH connection type. Just leave the other options by default. Then simply click Open. (We are using SSH as it is faster and more stable compared to Ethernet and UART.

Step 2: Expand SD Card File System

  • By default, the image enables only 2GB for Raspbian System, it is recommended to expand to use the whole SD card (8GB or 16GB). Or the SD card will be full soon. Run below command in the PuTTY terminal to start raspi-config,
sudo raspi-config
  • Choose “Expand Filesystem”, when finished reboot to make it effect. Run below command in the PuTTY terminal to know the SD card capacity and usage.
df -h

Please refer to Raspberry Pi raspi-config tool instruction for details. Click here see more.

Step 3: Use RHF2S001 integrated LoRaWAN server

  • Connect Gateway with the internal server by running the following commands in the PuTTY terminal and check the status:

sudo systemctl status pktfwd
  • If pktfwd service is not active, run the following command to start it:
sudo systemctl enable pktfwd
sudo systemctl restart pktfwd
  • Here is the Frequency Plan for EU868
  • Frequency Plan for US915 HYBRID
  • Now, we will configure Seeeduino LoRaWAN with GPS RHF76-052AM Settings by connecting the Seeeduino LoRaWAN GPS to your PC first
  • Secondly, open the Arduino IDE, and copy the code blew into a new sketch.
 void setup()
 {
     Serial1.begin(9600);
     SerialUSB.begin(115200);
 }

 void loop()
 {
     while(Serial1.available())
     {
         SerialUSB.write(Serial1.read());
     }
     while(SerialUSB.available())
     {
         Serial1.write(SerialUSB.read());
     }
 }
  • Then choose the right serial port of Seeeduino Lora GPS, and choose the board Tool->Board->Seeeduino_LoRAWAN.
  • After that you can click the upload button. If you can not find Seeeduino_LoRAWAN in the board list or do not know how to update the code, please click here for more information.
  • Now please open the serial monitor in the upper right corner ( or you can press Ctrl+Shift+M at the same time ).
  • Choose Newline (This option will add “\r\n” at the end of each command.), set the baud rate 9600. Then tap the commands below and press send.
  • For EU868:
AT+FDEFAULT=RISINGHF
AT+DR=EU868
  • For US915:
 AT+FDEFAULT=RISINGHF
 AT+DR=US915HYBRID
 AT+RXWIN2=923.3,DR8
  • Now we will be accessing the internal server console by firstly filling your browser with the IP address (IP of your gateway) where it will bring you to the following page:

Step 4. Use Seeeduino LoRaWAN GPS(RHF76-052AM) to access LoRaWAN server

There are two modes, in this tutorial, we only talk about the ABP Mode(This Mode is free for anyone), for more information about OTAA Mode(This model is commercial, you need to pay for it), you can click here.

  • To use Seeeduino LoRaWAN GPS to access LoRaWAN server, firstly find the “Application” button in the upper right corner of the website above, click it and you will see a new page.
  • Now you need APPEui,DevAddr,DevEui of Seeeduino LoRaWAN to add a new application. In order to get the ID information of Seeeduino LoRaWAN, you need to tap the command below in the serial monitor of Arduino IDE. Click Send, you will get the ID then.
at+id
  • Fill in the blank with the ID info you just got. You can fill in the name and owner as your wish (here we use Seeed and my nick name, use the APPEui you’ve just got. Then click Add button
  • This will bring you into the configure page where you will, choose Personalised Motes.
  • Fill in the DevEUI and DevAddr with ID info. of your Seeeduino LoRaWAN GPS and set the NWKSKEY and  APPSKEY by default. You can refer to the picture below.
    • DevEui: Seeeduino LoRaWAN GPS get through AT+ID command
    • DevAddr: Seeeduino LoRaWAN GPS get through AT+ID command
    • NWKSKEY:Default value 2B7E151628AED2A6ABF7158809CF4F3C
    • APPSKEY:Default value 2B7E151628AED2A6ABF7158809CF4F3C
  • To test whether you add the device successfully, you can use the serial monitor of Arduino IDE tap the command below.
at+mode=lwabp

AT+CMSGHEX="0a 0b 0c 0d 0e"
  • The results should be as follows:
  • Then, return to the website and click Application->Seeed(the name of the Application you just added)->View application data where you will see the data you’ve just sent form the Seeeduino_LoRAWAN.

That’s all! Congratulations! Job done! Lastly you will learn how to connect to Loriot Server which acts as your LoRaWAN network server provider.

Connect to Loriot Server

Step 1: Loriot Server Gateway Registration

  • New users needs register an account first, click registration address . Fill in UserName, Password and email address to register, after registration, an email will be sent to you, please follow the instruction in the email to activate.
  • After successful activation, click here to log in. Default tier is “Community Network”, it supports 1 Gateway (RHF2S001) and 10 nodes.
  • Enter Dashboard -> Gateway, click Add Gateway start to add Gateway.
  • Select Raspberry Pi 3
  • Set as below:
    • Radio front-end -> RHF2S001 868/915 MHz(SX1257)
    • BUS -> SPI
  • Fill in the MAC address of your RHF2S001, should be in format of b8:27:eb:xx:xx:xx. And also input Gateway Location information.
  • Click “Register Raspberry Pi gateway” to finish the registration.
  • Click the registered gateway to enter configuration page, switch “Frquency Plan” manually, your plan here is decided by the type of your RHF2S001 type, available plan are CN470,CN473, CN434,CN780,EU868, after selected please refresh the page to get the exact channel.In this wiki we choose EU868.
  • Run the command in the putty terminal
cd /home/rxhf/loriot/1.0.2
sudo systemctl stop pktfwd
sudo gwrst
wget https://cn1.loriot.io/home/gwsw/loriot-risinghf-rhf2s008-rhf1257-SPI-0-latest.bin -O loriot-gw.bin
chmod +x loriot-gw.bin
./loriot-gw.bin -f -s cn1.loriot.io
  • Finish gateway registration. You will see the gateway is Connected now. Next is to register node.

Step 2. Loriot Server Connect Node device

  • Firstly, Get the available gateway channels from  Dashboard -> Gateway -> Your Gateway , you can see the available channels as the picture below.
  • Next, configure Seeeduino LoRAWAN GPS(RHF3M076) by opening the serial monitor of Arduino IDE and enter the command below.
at+ch
  • To confirm the default channel of the Seeeduino_LoRAWAN GPS, you will get 3 channels. If there is no available channel, you can change the channels of Seeeduino_LoRAWAN by the command below.
at+ch=0,868.1
at+ch=1,868.3
at+ch=2,868.5
  • Then you can use at+ch again to check.
  • Now, we will add Seeeduino_LoRAWAN GPS as an ABP Node by logging in Loriot server , Click Dash Board->Applications->SimpleApp . Click Import ABP ,input below items:
    • DevAddr: Seeeduino_LoRAWAN GPS get through “AT+ID” command (Note: Loriot doesn’t support colon connector, need remove manually)
    • FCntUp:Set to 1
    • FCntDn:Set to 1
    • NWKSKEY:Default value 2B7E151628AED2A6ABF7158809CF4F3C
    • APPSKEY:Default value 2B7E151628AED2A6ABF7158809CF4F3C
    • EUI:DEVEUI, Seeeduino_LoRAWAN GPS get through “AT+ID” command
  • Click Import Device button to finish the device import. Now choose Dashboard -> Applications -> SampleApp , you will see the new ABP Node you’ve just added.
  • Lastly, we will Send data from the Seeeduino_LoRAWAN by entering this following comand:
AT+CMSGHEX="0a 0b 0c 0d 0e"
  • Then go to Dashboard -> Applications -> SampleApp ->Device , click the Node Device EUI or DevAddr, you will find the data you’ve just sent here.

That’s all on how to Build your own LoRa Network using LoRaWAN!

Summary

With LoRa and LoRaWAN, anyone can set up a network and transfer data over long distances with IoT devices that is secure and also low cost! With this blog, we hope that it will help you set up your own LoRaWAN network and understand how does LoRa works with Arduino.

We also offer various LoRa and LoRaWAN products other than those stated above ranging from Gateways, Node, Arduino LoRa modules, Raspberry Pi LoRa and many more here!

Interested in LoRa projects? You can check out our LoRa IoTea project which is an automatic information collection system applied to a tea plantation!

Please follow and like us:
error