Hello Lora with Arduino Pro mini and Microchip RN2483

Thomas Amberg edited this page Feb 3, 2017 · 19 revisions

Prerequisites

Sign up for an account, create an application and add a device with ABP to get a device id, network key and app key at https://console.thethingsnetwork.org/applications

Caution

Make sure the antenna is screwed to the module before applying power

Wiring

Arduino <--> RN2483
 Pin 2  <--> RST
 Pin 7  <--> TX
 Pin 8  <--> RX
 VCC    <--> 3V3
 GND    <--> GND

Library

Install https://github.com/jpmeijers/RN2483-Arduino-Library

Uplink Example

Sending data from the Arduino / sensor to the TTN back-end. by @gnz

#include <rn2xx3.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 8); // RX, TX
#define RST  2

// Copy the following lines from TTN Console -> Devices -> Overview tab -> "EXAMPLE CODE"
const char *devAddr = "00000000";
const char *nwkSKey = "00000000000000000000000000000000";
const char *appSKey = "00000000000000000000000000000000";

rn2xx3 myLora(mySerial);

// Setup routine runs once when you press reset
void setup() {
  pinMode(13, OUTPUT);
  led_on();

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Startup");

  // Reset rn2483
  pinMode(RST, OUTPUT);
  digitalWrite(RST, HIGH);
  digitalWrite(RST, LOW);
  delay(500);
  digitalWrite(RST, HIGH);

  // Initialise the rn2483 module
  myLora.autobaud();

  Serial.println("When using OTAA, register this DevEUI: ");
  Serial.println(myLora.hweui());
  Serial.print("RN2483 version number: ");
  Serial.println(myLora.sysver());

  myLora.initABP(devAddr, appSKey, nwkSKey);

  led_off();
  delay(2000);
}

// the loop routine runs over and over again forever:
void loop() {
  led_on();
  Serial.println("TXing");

  myLora.txUncnf("X");
  
  led_off();

  delay(20000);
}

void led_on()
{
  digitalWrite(13, 1);
}

void led_off()
{
  digitalWrite(13, 0);
}

Downlink Example

Sending data from the TTN back-end to the Arduino / sensor.

/*
  Author: Dennis Ruigrok and JP Meijers
  Date: 2017-01-16

  This program is meant to be used with an Arduino UNO or NANO, conencted to an RNxx3 radio module.
  It will most likely also work on other compatible Arduino or Arduino compatible boards,
  like The Things Uno, but might need some slight modifications.

  Transmit a one byte packet via TTN, using confirmed messages,
  waiting for an acknowledgement or a downlink message.

  CHECK THE RULES BEFORE USING THIS PROGRAM!

  CHANGE ADDRESS!
  Change the device address, network (session) key, and app (session) key to the values
  that are registered via the TTN dashboard.
  The appropriate line is "myLora.initABP(XXX);" or "myLora.initOTAA(XXX);"
  When using ABP, it is advised to enable "relax frame count".

  If you use an Arduino with a free hardware serial port, you can replace
  the line "rn2xx3 myLora(mySerial);"
  with     "rn2xx3 myLora(SerialX);"
  where the parameter is the serial port the RN2xx3 is connected to.
  Remember that the serial port should be initialised before calling initTTN().
  For best performance the serial port should be set to 57600 baud, which is impossible with a software serial port.
  If you use 57600 baud, you can remove the line "myLora.autobaud();".

*/
#include <rn2xx3.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 8); // RX, TX

// Copy the following lines from TTN Console -> Devices -> Overview tab -> "EXAMPLE CODE"
const char *devAddr = "00000000";
const char *nwkSKey = "00000000000000000000000000000000";
const char *appSKey = "00000000000000000000000000000000";

rn2xx3 myLora(mySerial);

void setup() {
 //output LED pin
 pinMode(13, OUTPUT);
 led_on();

 // Open serial communications and wait for port to open:
 Serial.begin(9600); //serial port to computer
 mySerial.begin(9600); //serial port to radio
 Serial.println("\r\n\r\nStartup RN2483 downlink");

 initialize_radio();

 //transmit a startup message
 //myLora.tx("TTN Mapper on TTN Enschede node");
 led_off();
 delay(2000);
}

void initialize_radio()
{
 //reset rn2483
 pinMode(2, OUTPUT);
 digitalWrite(2, LOW);
 delay(500);
 digitalWrite(2, HIGH);

 delay(100); //wait for the RN2xx3's startup message
 mySerial.flush();

 //Autobaud the rn2483 module to 9600. The default would otherwise be 57600.
 myLora.autobaud();

 //check communication with radio
 String hweui = myLora.hweui();
 while (hweui.length() != 16)
 {
   Serial.println("Communication with RN2xx3 unsuccesful. Power cycle the board.");
   Serial.println(hweui);
   delay(10000);
   hweui = myLora.hweui();
 }

 //print out the HWEUI so that we can register it via ttnctl
 Serial.println("When using OTAA, register this DevEUI: ");
 Serial.println(myLora.hweui());
 Serial.println("RN2xx3 firmware version:");
 Serial.println(myLora.sysver());

 //configure your keys and join the network
 Serial.println("Trying to join TTN");
 bool join_result = false;

 join_result = myLora.initABP(devAddr, appSKey, nwkSKey);

 while (!join_result)
 {
   Serial.println("Unable to join. Are your keys correct, and do you have TTN coverage?");
   delay(60000); //delay a minute before retry
   join_result = myLora.initABP(devAddr, appSKey, nwkSKey);
 }
 send();
}

void loop() {
 yield();
}

void send() {
 int j = 5;
 String rec;
 for (int i = 1; i <= j; i++) {
   led_on();

   Serial.print("TXing ");
   Serial.print(i);
   Serial.print("/");
   Serial.print(j);
    Serial.print(" ");
  // myLora.txCnf("!"); //one byte, blocking function

   switch (myLora.txCnf("BRM2")) //one byte, blocking function
   {
     case TX_FAIL:
       Serial.println("TX faild or not acknowledged");
       break;

     case TX_SUCCESS:
       Serial.println("TX successful and acknowledged");
       break;

     case TX_WITH_RX:
        rec=myLora.base16decode(myLora.getRx());
       Serial.print("Received downlink, length=");
       Serial.println(rec.length());
       Serial.print(", data=" );
       Serial.println(rec);
       break;

     default:
       Serial.println("Unknown response from TX function");
   }

   led_off();
   delay(10000);
 }
}

void led_on() {
 digitalWrite(13, 1);
}

void led_off() {
 digitalWrite(13, 0);
}

Datasheets

Links

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.