Devices & Components
PCBWay Custom PCB
Antenna PCB Connector
Arduino Pro Mini 328 - 5V/16MHz
Arduino Mini USB serial adapter
RFM95W Module
Antenna 868Mhz
DHT22 Temperature Sensor
Hardware & Tools
Drill / Driver, Cordless
Heater, Replacement/Spare for Weller DS800 Desoldering Station
Project description
Code
Arduino TTN LoRa node device using PCB Way
arduino
1#include <lmic.h> 2#include <hal/hal.h> 3#include <DHT.h> 4 5 6#define DHTTYPE DHT22 // DHT 22 (AM2302) 7#define DHTPIN 3 8 9DHT dht(DHTPIN, DHTTYPE); 10 11 12// LoRaWAN NwkSKey, network session **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ****** 13static const PROGMEM u1_t NWKSKEY[16] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0x34, 0x13, 0x72, 0x62, 0xFF, 0x6F, 0x7E }; 14 15// LoRaWAN AppSKey, application session key **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ****** 16static const u1_t PROGMEM APPSKEY[16] = { 0xFF, 0xFF, 0x23, 0x73, 0x0C, 0xD1, 0xED, 0x3A, 0x69, 0x63, 0x75, 0xC9, 0x95, 0x90, 0x59, 0x43 }; 17 18// LoRaWAN end-device address (DevAddr) **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ****** 19static const u4_t DEVADDR = { 0xFF0BAFFF }; 20 21// These callbacks are only used in over-the-air activation, so they are 22// left empty here (we cannot leave them out completely unless 23// DISABLE_JOIN is set in config.h, otherwise the linker will complain). 24void os_getArtEui (u1_t* buf) { } 25void os_getDevEui (u1_t* buf) { } 26void os_getDevKey (u1_t* buf) { } 27 28static osjob_t sendjob; 29 30// Schedule data trasmission in every this many seconds (might become longer due to duty 31// cycle limitations). 32// we set 10 seconds interval 33const unsigned TX_INTERVAL = 10; 34 35// Pin mapping according to Cytron LoRa Shield RFM 36const lmic_pinmap lmic_pins = { 37 .nss = 10, 38 .rxtx = LMIC_UNUSED_PIN, 39 .rst = 7, 40 .dio = {4, 5, 6}, 41}; 42 43void onEvent (ev_t ev) { 44 45 switch(ev) { 46 case EV_TXCOMPLETE: 47 Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)")); 48 // Schedule next transmission 49 os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); 50 break; 51 default: 52 Serial.println(F("Unknown event")); 53 break; 54 } 55} 56 57void do_send(osjob_t* j){ 58 int Temperature; 59 int Humidity; 60 61 // Check if there is not a current TX/RX job running 62 if (LMIC.opmode & OP_TXRXPEND) { 63 Serial.println(F("OP_TXRXPEND, not sending")); 64 } else { 65 // Get Temperature 66 Temperature=int(dht.readTemperature()*10); 67 // Get humidity 68 Humidity=int(dht.readHumidity()*10); 69 70 // prepare and schedule data for transmission 71 LMIC.frame[0] = Temperature >> 8; 72 LMIC.frame[1] = Temperature; 73 LMIC.frame[2] = Humidity >> 8; 74 LMIC.frame[3] = Humidity; 75 LMIC_setTxData2(1, LMIC.frame, 4, 0); // (port 1, 4 bytes, unconfirmed) 76 77 Serial.println(F("Packet queued")); 78 } 79 // Next TX is scheduled after TX_COMPLETE event. 80} 81 82void setup() { 83 Serial.begin(9600); 84 Serial.println(F("Starting")); 85 86 // Initialize Temp sensor device. 87 dht.begin(); 88 89 // LMIC init 90 os_init(); 91 92 // Reset the MAC state. Session and pending data transfers will be discarded. 93 LMIC_reset(); 94 95 // Set static session parameters. Instead of dynamically establishing a session 96 // by joining the network, precomputed session parameters are be provided. 97 uint8_t appskey[sizeof(APPSKEY)]; 98 uint8_t nwkskey[sizeof(NWKSKEY)]; 99 memcpy_P(appskey, APPSKEY, sizeof(APPSKEY)); 100 memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY)); 101 LMIC_setSession (0x1, DEVADDR, nwkskey, appskey); 102 103 // Disable link check validation 104 LMIC_setLinkCheckMode(0); 105 106 // Disable ADR 107 LMIC_setAdrMode(false); 108 109 // TTN uses SF9 for its RX2 window. 110 LMIC.dn2Dr = DR_SF9; 111 112 // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library) 113 LMIC_setDrTxpow(DR_SF7,14); 114 115 // Start job 116 do_send(&sendjob); 117} 118 119void loop() { 120 os_runloop_once(); 121 122} 123
Downloadable files
Arduino TTN LoRa node device
Arduino TTN LoRa node device
Arduino TTN LoRa node device
Arduino TTN LoRa node device
Comments
Only logged in users can leave comments