LoRa/LoRaWAN Communication

Objective

To implement LoRa/LoRaWAN communication using Arduino and the RFM95 LoRa module, enabling long-range wireless sensor data transmission to The Things Network (TTN).

Hardware Requirements

  • Arduino Uno/Nano
  • RFM95 LoRa Module
  • DHT11/DHT22 Sensor (Optional)
  • Jumper Wires

Wiring

  • VCC3.3V
  • GNDGND
  • NSSD10
  • MOSID11
  • MISOD12
  • SCKD13
  • RSTD9
  • DIO0D2

The RFM95 module must use 3.3V logic levels, so use a logic level shifter if using a 5V Arduino.

Libraries

  • RadioHead: Install from Library Manager
  • LMIC: IBM LMIC Framework (for LoRaWAN)

Transmitter Code

1#include <SPI.h>
2  #include <RH_RF95.h>
3  
4  #define RFM95_CS 10
5  #define RFM95_RST 9
6  #define RFM95_INT 2
7  
8  RH_RF95 rf95(RFM95_CS, RFM95_INT);
9  
10  void setup() {
11      Serial.begin(115200);
12      pinMode(RFM95_RST, OUTPUT);
13      digitalWrite(RFM95_RST, HIGH); delay(10);
14      digitalWrite(RFM95_RST, LOW); delay(10);
15      digitalWrite(RFM95_RST, HIGH); delay(10);
16  
17      if (!rf95.init()) {
18          Serial.println("LoRa init failed!");
19          while (1);
20      }
21  
22      rf95.setFrequency(915.0);
23      rf95.setTxPower(23, false);
24  
25      Serial.println("LoRa Sender Ready.");
26  }
27  
28  void loop() {
29      const char *msg = "Hello, LoRa!";
30      rf95.send((uint8_t *)msg, strlen(msg));
31      rf95.waitPacketSent();
32      Serial.println("Message Sent: Hello, LoRa!");
33      delay(5000);
34  }

Receiver Code

1#include <SPI.h>
2  #include <RH_RF95.h>
3  
4  #define RFM95_CS 10
5  #define RFM95_RST 9
6  #define RFM95_INT 2
7  
8  RH_RF95 rf95(RFM95_CS, RFM95_INT);
9  
10  void setup() {
11      Serial.begin(115200);
12      pinMode(RFM95_RST, OUTPUT);
13      digitalWrite(RFM95_RST, HIGH); delay(10);
14      digitalWrite(RFM95_RST, LOW); delay(10);
15      digitalWrite(RFM95_RST, HIGH); delay(10);
16  
17      if (!rf95.init()) {
18          Serial.println("LoRa init failed!");
19          while (1);
20      }
21  
22      rf95.setFrequency(915.0);
23      Serial.println("LoRa Receiver Ready.");
24  }
25  
26  void loop() {
27      if (rf95.available()) {
28          uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
29          uint8_t len = sizeof(buf);
30          if (rf95.recv(buf, &len)) {
31              Serial.print("Received: ");
32              Serial.println((char*)buf);
33          }
34      }
35  }

TTN Integration

  • Create a TTN account and register your device
  • Note your Device EUI, App EUI, and App Key
  • Install LMIC library

LMIC Code

1#include <lmic.h>
2  #include <hal/hal.h>
3  #include <SPI.h>
4  
5  static const u1_t PROGMEM APPEUI[8] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
6  static const u1_t PROGMEM DEVEUI[8] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
7  static const u1_t PROGMEM APPKEY[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
8  
9  void os_getArtEui (u1_t* buf) { memcpy(buf, APPEUI, 8); }
10  void os_getDevEui (u1_t* buf) { memcpy(buf, DEVEUI, 8); }
11  void os_getDevKey (u1_t* buf) { memcpy(buf, APPKEY, 16); }
12  
13  static osjob_t sendjob;
14  
15  void setup() {
16      Serial.begin(115200);
17      os_init();
18      LMIC_reset();
19  }
20  
21  void loop() {
22      static uint8_t mydata[] = "Hello TTN!";
23      LMIC_setTxData2(1, mydata, sizeof(mydata) - 1, 0);
24      Serial.println("Packet sent to TTN!");
25      delay(60000);
26  }

View data on: TTN Console → Applications → Your Device → Live Data

LoRa vs LoRaWAN

ProtocolGateway RequiredInternet ConnectivitySecurity
Point-to-Point❌ No❌ No❌ No Encryption
Uses Gateway & Cloud✅ Yes✅ Yes✅ AES-128 Encryption