Devices & Components
Arduino Nano
I2C OLED Display 128x64
RFM95W Module
Raspberry power supply
Raspberry Pi 3 Model B
SD Memory Adapter
DHT22 Temperature Sensor
Audio / Video Cable Assembly, Ultra Slim RedMere HDMI to HDMI
Flash Memory Card, MicroSD Card
868Mhz Antenna
Raspberry Pi Keyboard
Project description
Code
Lora device Sketch for Arduino
arduino
1#include <lmic.h> 2#include <hal/hal.h> 3#include "U8glib.h" 4#include <DHT.h> 5 6 7// OLED Display configuration 8U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI 9 10#define DHTTYPE DHT22 // DHT 22 (AM2302) 11#define DHTPIN 8 12 13DHT dht(DHTPIN, DHTTYPE); 14 15 16// LoRaWAN NwkSKey, network session **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ****** 17static const PROGMEM u1_t NWKSKEY[16] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 18 19// LoRaWAN AppSKey, application session key **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ****** 20static const u1_t PROGMEM APPSKEY[16] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 21 22// LoRaWAN end-device address (DevAddr) **** DO NOT USE AS IS. NEEDS TO BE CHANGED AS PER TUTORIAL ****** 23static const u4_t DEVADDR = { 0xFFFFFFFF }; 24 25// These callbacks are only used in over-the-air activation, so they are 26// left empty here (we cannot leave them out completely unless 27// DISABLE_JOIN is set in config.h, otherwise the linker will complain). 28void os_getArtEui (u1_t* buf) { } 29void os_getDevEui (u1_t* buf) { } 30void os_getDevKey (u1_t* buf) { } 31 32//static uint8_t mydata[] = "LoRa Device Test"; 33static osjob_t sendjob; 34 35// Schedule data trasmission in every this many seconds (might become longer due to duty 36// cycle limitations). 37// we set 10 seconds interval 38const unsigned TX_INTERVAL = 10; 39 40// Pin mapping according to Cytron LoRa Shield RFM 41const lmic_pinmap lmic_pins = { 42 .nss = 10, 43 .rxtx = LMIC_UNUSED_PIN, 44 .rst = 7, 45 .dio = {2, 5, 6}, 46}; 47 48void onEvent (ev_t ev) { 49 50 switch(ev) { 51 case EV_TXCOMPLETE: 52 Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)")); 53 // Schedule next transmission 54 os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send); 55 break; 56 default: 57 Serial.println(F("Unknown event")); 58 break; 59 } 60} 61 62void do_send(osjob_t* j){ 63 char ValueT[5]; 64 char ValueH[5]; 65 int Temperature; 66 int Humidity; 67 68 // Check if there is not a current TX/RX job running 69 if (LMIC.opmode & OP_TXRXPEND) { 70 Serial.println(F("OP_TXRXPEND, not sending")); 71 } else { 72 // Get Temperature 73 Temperature=int(dht.readTemperature()*10); 74 // Get humidity 75 Humidity=int(dht.readHumidity()*10); 76 77 // Format the values to be printed on OLED Screen 78 sprintf(ValueT, "%d.%d", int(dht.readTemperature()),int(dht.readTemperature()*10)-(int(dht.readTemperature())*10)); 79 sprintf(ValueH, "%d.%d", int(dht.readHumidity()),int(dht.readHumidity()*10)-(int(dht.readHumidity())*10)); 80 u8g.firstPage(); 81 do { 82 draw("LoRa", 50, 10); 83 draw("Temperature:", 0, 30); 84 draw(ValueT, 85, 30); 85 draw("C", 115, 30); 86 draw("Humidity:", 0, 50); 87 draw(ValueH, 85, 50); 88 draw("%", 115, 50); 89 } while( u8g.nextPage() ); 90 91 // prepare and schedule data for transmission 92 LMIC.frame[0] = Temperature >> 8; 93 LMIC.frame[1] = Temperature; 94 LMIC.frame[2] = Humidity >> 8; 95 LMIC.frame[3] = Humidity; 96 LMIC_setTxData2(1, LMIC.frame, 4, 0); // (port 1, 4 bytes, unconfirmed) 97 98 Serial.println(F("Packet queued")); 99 } 100 // Next TX is scheduled after TX_COMPLETE event. 101} 102 103void setup() { 104 Serial.begin(9600); 105 Serial.println(F("Starting")); 106 u8g.setColorIndex(1); // pixel on for Display 107 108 // Initialize Temp sensor device. 109 dht.begin(); 110 111 // LMIC init 112 os_init(); 113 114 // Reset the MAC state. Session and pending data transfers will be discarded. 115 LMIC_reset(); 116 117 // Set static session parameters. Instead of dynamically establishing a session 118 // by joining the network, precomputed session parameters are be provided. 119 uint8_t appskey[sizeof(APPSKEY)]; 120 uint8_t nwkskey[sizeof(NWKSKEY)]; 121 memcpy_P(appskey, APPSKEY, sizeof(APPSKEY)); 122 memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY)); 123 LMIC_setSession (0x1, DEVADDR, nwkskey, appskey); 124 125 // Disable link check validation 126 LMIC_setLinkCheckMode(0); 127 128 // Disable ADR 129 LMIC_setAdrMode(false); 130 131 // TTN uses SF9 for its RX2 window. 132 LMIC.dn2Dr = DR_SF9; 133 134 // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library) 135 LMIC_setDrTxpow(DR_SF7,14); 136 137 // Start job 138 do_send(&sendjob); 139} 140 141void loop() { 142 os_runloop_once(); 143 144} 145 146void draw(char* parola, int posx, int posy) { 147 // graphic commands to redraw the complete screen should be placed here 148 u8g.setFont(u8g_font_tpssb); 149 u8g.drawStr( posx, posy, parola); 150} 151
Downloadable files
Gateway Circuit Diagram
Gateway Circuit Diagram

LoRa Device Diagram
LoRa Device Diagram

Gateway Circuit Diagram
Gateway Circuit Diagram

LoRa Device Diagram
LoRa Device Diagram

Comments
Only logged in users can leave comments