LoRaWAN

 
 
LoRaWAN (Long Range Wide Area Network) is a specification for a telecommunication network suitable for long-distance communication with little power. The technology is used for machine-to-machine communication (Internet of Things).

The architecture consists of gateways, network servers and application servers. There are RF chips from Semtech used to transmit a spread spectrum.

More information about LoRaWAN can be found at:
https://www.lora-alliance.org/
https://www.thethingsnetwork.org/







Build LoRa node using Arduino Pro Mini and HopeRF RFM95 LoRa transceiver module (V3)



Information
none

Operating system used
macOS Mojave

Hardware used
  • Arduino Pro Mini (ATmega328P / 3.3V / 8 MHz)
    The Arduino Pro Mini: ATmega168 will not work because there is not enough space.
    I am not using the Arduino Pro Mini ATmega328P / 5V / 16 MHz because the HopeRF RFM95 LoRa transceiver module uses 3.3V.

    Top view.
    Arduino Pro Mini ATmega328P / 3.3V / 8 MHz top

    Bottom view.
    Arduino Pro Mini ATmega328P / 3.3V / 8 MHz bottom

    With the male pin headers soldered. Arduino Pro Mini with male pin headers soldered

    With the male pin headers soldered, bottom view. Arduino Pro Mini with male pin headers soldered, bottom view

  • LoRa module with dipole antenna.
    How this is made see tutorial:
    Modify ESP8266 module adapter plate for HopeRF RFM95 LoRa transceiver module with RP SMA female edge mount connector

    ESP8266 module adapter plate with RFM95 LoRa module and dipole antenna

    LoRa module with RP SMA female edge connector

  • FTDI FT232RL 3.3/5V

    The Arduino Pro Mini does not have an USB connector. To communicate with the Arduino Pro Mini with your computer via an USB port an USB to TTL serial adapter is needed. In this tutorial I will be using the FTDI FT232RL adapter.

    FTDI FT232RL

    Top view.
    Make sure the jumper is set to 3.3V.
    FTDI FT232RL top view

    Bottom view.
    FTDI FT232RL bottom view

  • Breadboard MB-102

    Solderless breadboard MB-102

  • USB 2.0 cable, USB-A > Mini USB-B (length: 80 cm)
    This cable transfers the sketch from computer to the FTDI adapter.

    USB 2.0 cable type A/B

  • Jumper wires 15 cm long (6x female-female)
    To connect the FTDI adapter to the Arduino Pro Mini.

    FTDI cable

    Connect Arduino Pro Mini and FTDI adapter with the FTDI cable.

    Connect Arduino Pro Mini and FTDI adapter with the FTDI cable

    Arduino Pro Mini FTDI adapter (set jumper 3.3V)
    DTR DTR
    TXO RX
    RXI TX
    VCC VCC
    GND CTS
    GND GND

  • Jumper wires 20 cm long (19x male-male)

    Jumper wire male - male

  • Female pin headers 8 pins (4 pcs)

    Female pin headers

  • Battery holder 3x1.5=4.5V (3x AA)

    Battery holder

    Battery holder open.

    Battery holder open

Software prerequisites
none

Procedure
  1. The HopeRF RFM95 LoRa transceiver module.

    HopeRF RFM95 LoRa transceiver module top view

    Make the following jumper wire connections between the HopeRF RFM95 LoRa transceiver module and the Arduino Pro Mini.

    HopeRF RFM95 LoRa transceiver module Arduino Pro Mini Pin    HopeRF RFM95 LoRa transceiver module Arduino Pro Mini Pin
    ANT -    GND -
    GND GND    DIO5 -
    DIO3 -    RESET 5
    DIO4 -    NSS 10
    3.3V VCC    SCK 13
    DIO0 2    MOSI 11
    DIO1 3    MISO 12
    DIO2 -    GND -

    ANT = Antenna
    GND = Ground
    DIO = Digital Input/Output
    NSS = Slave Select
    SCK = Serial Clock (output from master)
    MOSI = Master Out Slave In (data output from master)
    MISO = Master In Slave Out (data output from slave)

    Note:
    • Always attach the dipole antenna to the SMA connector BEFORE powering up the LoRa module otherwise you may damage the HopeRF RFM95 LoRa transceiver module.
    • The HopeRF RFM95 LoRa transceiver module has 3 GND pins.
      Use the GND pin below the Antenna pin.
    • Do not use DIO2, DIO3, DIO4 and DIO5.
    • The Arduino Pro Mini uses SPI (Serial Peripheral Interface) to communicate with the HopeRF RFM95 LoRa transceiver module. The Arduino Pro Mini is the Master and the HopeRF RFM95 LoRa transceiver module is the Slave.

    Wiring HopeRF RFM95 LoRa transceiver module and Arduino Pro Mini.
    Click on the image for a larger image.

    Wiring HopeRF RFM95 LoRa transceiver module and Arduino Pro Mini



    Wiring LoRa module and Arduino Pro Mini.
    Arduino Pro Mini and LoRa module wiring 1

    Stack two female pin headers (2x) and place them on the breadboard with the LoRa module on top.
    Arduino Pro Mini and LoRa module wiring 2

    Attach dipole antenna.
    Arduino Pro Mini and LoRa module wiring 3

    Side view.
    Arduino Pro Mini and LoRa module wiring 3

  2. Make sure a LoRa gateway is in your area and the LoRa node can send messages to that gateway.
    Use this map: https://www.thethingsnetwork.org/map

  3. Install the latest open source Arduino IDE.
    https://www.arduino.cc/en/Main/Software

  4. Install the Adafruit Unified Sensor library.
    - In the Arduino IDE, select menu Sketch | Include Library | Manage Libraries
    - In the search box enter: adafruit unified
    - Click the Adafruit Unified Sensor library by Adafruit.
      More info: https://github.com/adafruit/Adafruit_Sensor
    - Select the latest version and press the Install button.
      For this tutorial I installed version 1.0.2

    Adafruit Unified Sensor library

  5. Install the DHT11 library.
    - In the Arduino IDE, select menu Sketch | Include Library | Manage Libraries
    - In the search box enter: dht11
    - Click the DHT sensor library by Adafruit.
      More info: https://github.com/adafruit/DHT-sensor-library
    - Select the latest version and press the Install button.
      For this tutorial I installed version 1.3.0

    Adafruit Unified Sensor library

  6. Install the MCCI LoRaWAN LMIC library.
    - In the Arduino IDE, select menu Sketch | Include Library | Manage Libraries
    - In the search box enter: MCCI
    - Click the MCCI LoRaWAN LMIC library by IBM, Matthijs Kooijman, Terry Moore, ChaeHee Won, Frank Rose.
      More info: https://github.com/mcci-catena/arduino-lmic
    - Select the latest version and press the Install button.
      For this tutorial I installed version 4.0.0

    MCCI LoRaWAN LMIC Library

  7. Configure the MCCI LoRaWAN LMIC Library according to your situation.
    Edit file lmic_project_config.h. This file can be found at:
    ../libraries/MCCI_LoRaWAN_LMIC_library/project_config

    I made the following changes to MY lmic_project_config.h file.
    Make changes according to YOUR situation.

    // project-specific definitions
    #define CFG_eu868 1
    //#define CFG_us915 1
    //#define CFG_au915 1
    //#define CFG_as923 1
    //#define LMIC_COUNTRY_CODE LMIC_COUNTRY_CODE_JP /* for as923-JP */
    //#define CFG_kr920 1
    //#define CFG_in866 1
    #define CFG_sx1276_radio 1
    //#define LMIC_USE_INTERRUPTS
    #define LMIC_LORAWAN_SPEC_VERSION LMIC_LORAWAN_SPEC_VERSION_1_0_2
    #define DISABLE_PING
    #define DISABLE_BEACONS
    #define LMIC_ENABLE_DeviceTimeReq 0


  8. Create an account on The Things Stack Community Edition (TTS CE).

  9. Login The Things Stack Community Edition console.
    Make sure you select a cluster (Europe, North America, Australia) near your location.

    Select a cluster

  10. Add an application.
    For example:
    Application ID: youtube-demo-app2 (must be unique)
    Application name: youtube-demo-app2 (can be anything)

    Add an application

  11. The HopeRF RFM95 LoRa transceiver module does not have a built-in DevEUI or AppEUI.
    In such case you should let the TTSCE console generate the required DevEUI or AppEUI.

    Herebelow is an example of generated AppEUI, DevEUI and AppKey in the TTSCE console.

    Add an application

    The AppEUI, DevEUI and AppKey are used in the Arduino sketch ttsce-otaa-pro-mini-sensors.ino.
    More about this sketch later.
    In this Arduino sketch the DevEUI or AppEUI must be converted to an array of 16 bytes in LSB order.
    The AppKey must converted to an array of 32 bytes in MSB order.

    I have created an online tool which converts these values to an bytes array in its correct order (LSB / MSB): https://www.mobilefish.com/download/lora/eui_key_converter.html

    Convert AppEUI, DevEUI and AppKey in bytes array

    As of July 2021, you can now generate the required AppEUI, DevEUI and AppKey in the TTSCE console.
    Do NOT use my lorawan_device.html online tool!

    This tutorial uses the AppEUI, DevEUI and AppKey generated by my online tool lorawan_device.html because this tutorial was written before July 2021.

    For the AppEUI (8 bytes) you can use a value with all zeros.
    For the DevEUI (8 bytes) you can generate your own local adminstered MAC address.
    You can use this online tool:

    https://www.mobilefish.com/download/lora/lorawan_device.html (DO NOT USE THIS TOOL)

    The online tool generated these values:

    AppEUI:
    TTS Community Edition Console (AppEUI):
    0000000000000000
    Arduino Sketch (APPEUI LSB):
    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

    DevEUI (Local administered MAC address)
    TTS Community Edition Console (DevEUI):
    6672029985F6FC00
    Arduino Sketch (DEVEUI LSB):
    {0x00, 0xFC, 0xF6, 0x85, 0x99, 0x02, 0x72, 0x66}
    Note: The DevEUI second hex value from the left should be 2, 6, A, or E

    AppKey
    TTS Community Edition Console (AppKey):
    52E54E35B91D36AAEA4498DBFB8BB901
    Arduino Sketch (APPKEY MSB):
    {0x52, 0xE5, 0x4E, 0x35, 0xB9, 0x1D, 0x36, 0xAA, 0xEA, 0x44, 0x98, 0xDB, 0xFB, 0x8B, 0xB9, 0x01}

    Generate AppEUI, DevEUI and AppKey

  12. Register end device (Preparation).
    Activation mode: Over the air activation (OTAA)
    LoRaWAN version: MAC V1.0.2
    Network Server address: eu1.cloud.thethings.network (Cluster: Europe)
    Application Server address: eu1.cloud.thethings.network (Cluster: Europe)
    Join Server address: eu1.cloud.thethings.network (Cluster: Europe)

    Note: Why use MAC V1.0.2?
    Because the MCCI LoRaWAN LMIC library has only been tested with LoRaWAN 1.0.2/1.0.3 networks.

    Register end device (Preparation)

  13. Register end device (Step 1).
    End device ID: youtube-demo-device2 (must be unique)
    AppEUI: 00 00 00 00 00 00 00 00 (If end device has no AppEUI)
    DevEUI: 66 72 02 99 85 F6 FC 00 (If end device has no DevEUI, local adminstered MAC address)
    End device name: youtube-demo-device2 (can be anything)

    Register end device (Step 1)

  14. Register end device (Step 2).
    Frequency plan: Europe 863-870 MHz (SF9 for RX2 - recommended)
    LoRaWAN version: MAC V1.0.2
    Regional Parameters version: PHY V1.0.2 REV B

    Note: If you selected MAC V1.0.2 you can choose between PHY V1.0.2 REV A and PHY V1.0.2 REV B.
    What the difference is between these 2 revisions can be found in LoRaWAN 1.0.2 Regional Parameters revision B document (chapter 3)

    Register end device (Step 2)

  15. Register end device (Step 3).
    AppKey: 52 E5 4E 35 B9 1D 36 AA EA 44 98 DB FB 8B B9 01

    Register end device (Step 3)

  16. In the Arduino IDE, select menu File | Examples | MCC LoRaWAN LMIC library and select the ttn-otaa sketch.
    Re-save the ttn-otaa sketch and call it ttsce-otaa-helloworld.
    From https://www.mobilefish.com/download/lora/lorawan_device.html copy DevEUI, AppEUI and AppKey to the ttsce-otaa-helloworld sketch.
    Watch out:
    The DevEUI must be in little-endian format.
    The AppKey must be in big endian format.

  17. Modify the ttsce-otaa-helloworld sketch.

    // Pin mapping
    const lmic_pinmap lmic_pins = {
       .nss = 10,
       .rxtx = LMIC_UNUSED_PIN,
       .rst = 5,
       .dio = {2, 3, LMIC_UNUSED_PIN},
    };


  18. The Arduino Pro mini is running at 8MHz and is relatively slow for LMIC and needs an adjustment by relaxing the timing. Add the line: LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100);
    in the setup function, between the LMIC_Reset() and the do_send(&sendjob).
    It makes the receive windows bigger, in case the clock is 1% faster or slower.

    In the ttn-otaa-helloworld sketch, I added the above mentioned line in the setup function.
    See the red marked lines:

    void setup() {
       Serial.begin(9600);
       Serial.println(F("Starting"));

       #ifdef VCC_ENABLE
       // For Pinoccio Scout boards
       pinMode(VCC_ENABLE, OUTPUT);
       digitalWrite(VCC_ENABLE, HIGH);
       delay(1000);
       #endif

       // LMIC init
       os_init();
       // Reset the MAC state. Session and pending data transfers will be discarded.
       LMIC_reset();

       // Use with Arduino Pro Mini ATmega328P 3.3V 8 MHz
       // Let LMIC compensate for +/- 1% clock error
       LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100);


       // Start job (sending automatically starts OTAA too)
       do_send(&sendjob);
    }


    Here is the final sketch: ttsce-otaa-helloworld.ino.

  19. In this sketch the message "Hello, world!” will be transmitted every 60 seconds, see variables: mydata[] and TX_INTERVAL.

  20. Connect the Arduino Pro Mini to your computer using the USB cable and FTDI adapter.

    Connect LoRa module to computer

  21. In the Arduino IDE, select menu Tools | Board and select: Arduino Pro or Pro Mini
    In the Arduino IDE, select menu Tools | Processor: ATmega328P (3.3V, 8MHz)
    In the Arduino IDE, select menu Tools | Port: your port

  22. Compile ttsce-otaa-helloworld sketch.
    You should not see any errors (but there are warnings).

    Compile sketch

  23. Upload the ttsce-otaa-mydemo sketch to the Arduino Pro Mini.
    You should not see any errors.

  24. In The Things Stack Community Edition console, the "Hello, world!" message (in hex) is received every 60 seconds.

    Transmit the message every 60 seconds

  25. If you want to convert the payload into a readable text:
    - Select your application in the "Application Overview" screen.
    - Select "Payload formatters".
    - Select "Uplink".
    - Select "Formatter type: Javascript".

    Paste the decodeuplink payload formatter in the textbox.
    On the bottom of the page press the "Save changes" button.

    Uplink payload formatter

    If you select "Live data" you will see the payload in human readable text.

    Payload decoded

  26. The Arduino Pro Mini can be powered by 3x AA (1.5V) batteries.
    Disconnect the FTDI jumper wires from the Arduino Pro Mini and connect the battery holder.

    Arduino Pro Mini battery powered

    Arduino Pro Mini battery powered connection

    Arduino Pro Mini Pin Battery holder
    RAW + (red wire)
    GND - (black wire)

  27. Optionally a push button, 2 LEDs (yellow, green) and a DHT11 sensor can be added.

    A simple push button and 2 LEDs (yellow, green)

    Push button and 2 LEDs

    DHT11 sensor module (DHT = Digital Humidity Temperature)

    This particular DHT11 sensor comes with a 10 kΩ pull up resistor
 from the data pin to Vcc.

    • Pin VCC connected to Arduino Pro Mini 3.3V

    • Pin DATA connected to Arduino Pro Mini pin 7
    • Pin GND connected to Arduino Pro Mini GND

    A DHT11 sensor is cheap, less precise and less accurate.
 For better accuracy and precision use a DHT22.

    DHT11 sensor with pull up resistor

    Specification:
    Supply voltage: 3.3 ~ 5.5V DC
    Output: single-bus digital signal
    Measuring range: humidity 20-90% RH, temperature 0 ~ 50°C
    Accuracy: humidity ±5% RH, temperature ±2°C
    Resolution: Humidity 1% RH, temperature 1°C
    Long-term stability: < ±1% RH / Year

    DHT11 sensor

    Click on the image for a larger image.

    Wiring HopeRF RFM95 LoRa transceiver module, Arduino Pro Mini and sensors

    Wiring HopeRF RFM95 LoRa transceiver module, Arduino Pro Mini and sensors 2

    Wiring HopeRF RFM95 LoRa transceiver module, Arduino Pro Mini and sensors 3

    The sketch for this circuit diagram: ttsce-otaa-pro-mini-sensors.ino.
    For this sketch, to make the payload data more human readable in the TTS CE console, select "Payload formatters", select "Uplink", select "Javascript" and copy and paste the decodeUplink function from this link ttsce-otaa-pro-mini-sensors-decodeuplink.txt and paste it in this field.

    Receiving DHT11 sensor data and button pressed in TTS CE.

    Receiving DHT11 sensor data and button pressed

    Arduino IDE serial monitor output.

    Arduino IDE serial monitor output

  28. You can also send data from the TTS CE console to the end node.
    The LEDs can be switched ON or OFF.
    In the Arduino sketch ttsce-otaa-pro-mini-sensors.ino the fPort plays an important role.
    Make sure fPort 3 is used!

    Hex Value Yellow LED Green LED
    00 OFF OFF
    01 ON OFF
    02 OFF ON
    03 ON ON


  29. In the TTS CE console, select the application youtube-demo-app2, select the end device youtube-demo-device2, select tab Messaging and select Downlink.

    TTS CE messaging

  30. To switch the yellow LED on:

    - Insert Mode: Push to downlink queue (append)
    - FPort = 3
    - Payload = 01

    Press the Schedule downlink button.

    TTS CE messaging 2

  31. In the Live data screen you will see that a downlink message is sent.

    TTS CE downlink message is sent

  32. On the end device the yellow LED is switch on (it make take several seconds).

    In the images below I have removed the battery holder and I have re-ordered the components on the breadboard to make it less cluttered.

    Yellow LED switch on

    Yellow LED switch on

    The AppSKey, NwkSKey and timestamp in the image below are different compared to previous image because the end device has been stopped and started several times and the images were made at different days and times.

    The Arduino Serial Monitor displays the following:

    Arduino Serial Monitor

  33. With the hex values mentioned in the table above you can now switch the LEDs on or off.