Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
cstram
Published © GPL3+

Arduino TTN LoRa node device using PCB Way

You want to have a cool TTN LoRa device using Arduino? Come and see my new project.

IntermediateFull instructions provided952
Arduino TTN LoRa node device using PCB Way

Things used in this project

Hardware components

Arduino Pro Mini 328 - 5V/16MHz
SparkFun Arduino Pro Mini 328 - 5V/16MHz
×1
Arduino Mini USB serial adapter
Arduino Mini USB serial adapter
×1
RFM95W Module
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Antenna 868Mhz
×1
PCBWay Custom PCB
PCBWay Custom PCB
×1
Antenna PCB Connector
×1

Hand tools and fabrication machines

Drill / Driver, Cordless
Drill / Driver, Cordless
Heater, Replacement/Spare for Weller DS800 Desoldering Station
Heater, Replacement/Spare for Weller DS800 Desoldering Station

Story

Read more

Schematics

Arduino TTN LoRa node device

Code

Arduino TTN LoRa node device using PCB Way

Arduino
#include <lmic.h>
#include <hal/hal.h>
#include <DHT.h>


#define DHTTYPE    DHT22     // DHT 22 (AM2302)
#define DHTPIN 3

DHT dht(DHTPIN, DHTTYPE);


// LoRaWAN NwkSKey, network session **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ******
static const PROGMEM u1_t NWKSKEY[16] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x34, 0x13, 0x72, 0x62, 0xFF, 0x6F, 0x7E };

// LoRaWAN AppSKey, application session key **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ******
static const u1_t PROGMEM APPSKEY[16] = { 0xFF, 0xFF, 0x23, 0x73, 0x0C, 0xD1, 0xED, 0x3A, 0x69, 0x63, 0x75, 0xC9, 0x95, 0x90, 0x59, 0x43 };

// LoRaWAN end-device address (DevAddr) **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ******
static const u4_t DEVADDR = { 0xFF0BAFFF };

// These callbacks are only used in over-the-air activation, so they are
// left empty here (we cannot leave them out completely unless
// DISABLE_JOIN is set in config.h, otherwise the linker will complain).
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }

static osjob_t sendjob;

// Schedule data trasmission in every this many seconds (might become longer due to duty
// cycle limitations).
// we set 10 seconds interval
const unsigned TX_INTERVAL = 10;

// Pin mapping according to Cytron LoRa Shield RFM
const lmic_pinmap lmic_pins = {
  .nss = 10,
  .rxtx = LMIC_UNUSED_PIN,
  .rst = 7,
  .dio = {4, 5, 6},
};

void onEvent (ev_t ev) {

  switch(ev) {
    case EV_TXCOMPLETE:
      Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
      // Schedule next transmission
      os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
      break;  
    default:
      Serial.println(F("Unknown event"));
      break;
  }
}

void do_send(osjob_t* j){
  int Temperature;
  int Humidity;
  
  // Check if there is not a current TX/RX job running
  if (LMIC.opmode & OP_TXRXPEND) {
    Serial.println(F("OP_TXRXPEND, not sending"));
  } else {
  // Get Temperature
  Temperature=int(dht.readTemperature()*10);
  // Get humidity 
  Humidity=int(dht.readHumidity()*10);

    // prepare and schedule data for transmission 
    LMIC.frame[0] = Temperature >> 8; 
    LMIC.frame[1] = Temperature;
    LMIC.frame[2] = Humidity >> 8;
    LMIC.frame[3] = Humidity; 
    LMIC_setTxData2(1, LMIC.frame, 4, 0); // (port 1, 4 bytes, unconfirmed)

    Serial.println(F("Packet queued"));
  }
  // Next TX is scheduled after TX_COMPLETE event.
}

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

  // Initialize Temp sensor device.
  dht.begin();

  // LMIC init
  os_init();

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

  // Set static session parameters. Instead of dynamically establishing a session
  // by joining the network, precomputed session parameters are be provided.
  uint8_t appskey[sizeof(APPSKEY)];
  uint8_t nwkskey[sizeof(NWKSKEY)];
  memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
  memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
  LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);

  // Disable link check validation
  LMIC_setLinkCheckMode(0);

  // Disable ADR
  LMIC_setAdrMode(false);

  // TTN uses SF9 for its RX2 window.
  LMIC.dn2Dr = DR_SF9;

  // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
  LMIC_setDrTxpow(DR_SF7,14);

  // Start job
  do_send(&sendjob);
}

void loop() {
  os_runloop_once();

}

Credits

cstram
16 projects • 23 followers
Passionate about IT, Electronics and DIY. Strong believer in Raspberry and Arduino devices. Experience in digital television and security.
Contact

Comments

Please log in or sign up to comment.