SHARE
TWEET

Untitled

a guest Sep 17th, 2019 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************************************************************************
  2.  * Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
  3.  * Copyright (c) 2018 Terry Moore, MCCI
  4.  *
  5.  * Permission is hereby granted, free of charge, to anyone
  6.  * obtaining a copy of this document and accompanying files,
  7.  * to do whatever they want with them without any restriction,
  8.  * including, but not limited to, copying, modification and redistribution.
  9.  * NO WARRANTY OF ANY KIND IS PROVIDED.
  10.  *
  11.  * This example sends a valid LoRaWAN packet with payload "Hello,
  12.  * world!", using frequency and encryption settings matching those of
  13.  * the The Things Network. It's pre-configured for the Adafruit
  14.  * Feather M0 LoRa.
  15.  *
  16.  * This uses OTAA (Over-the-air activation), where where a DevEUI and
  17.  * application key is configured, which are used in an over-the-air
  18.  * activation procedure where a DevAddr and session keys are
  19.  * assigned/generated for use with all further communication.
  20.  *
  21.  * Note: LoRaWAN per sub-band duty-cycle limitation is enforced (1% in
  22.  * g1, 0.1% in g2), but not the TTN fair usage policy (which is probably
  23.  * violated by this sketch when left running for longer)!
  24.  
  25.  * To use this sketch, first register your application and device with
  26.  * the things network, to set or generate an AppEUI, DevEUI and AppKey.
  27.  * Multiple devices can use the same AppEUI, but each device has its own
  28.  * DevEUI and AppKey.
  29.  *
  30.  * Do not forget to define the radio type correctly in
  31.  * arduino-lmic/project_config/lmic_project_config.h or from your BOARDS.txt.
  32.  *
  33.  *******************************************************************************/
  34.  
  35. #include <lmic.h>
  36. #include <hal/hal.h>
  37. #include <SPI.h>
  38. //#include <LoraEncoder.h>
  39. #include <LoraMessage.h>
  40.  
  41. // This EUI must be in little-endian format, so least-significant-byte
  42. // first. When copying an EUI from ttnctl output, this means to reverse
  43. // the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3,
  44. // 0x70.
  45. static const u1_t PROGMEM APPEUI[8]= { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  46. void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}
  47.  
  48. // This should also be in little endian format, see above.
  49. static const u1_t PROGMEM DEVEUI[8]= { 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x04 };
  50. void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}
  51.  
  52. // This key should be in big endian format (or, since it is not really a
  53. // number but a block of memory, endianness does not really apply). In
  54. // practice, a key taken from the TTN console can be copied as-is.
  55. static const u1_t PROGMEM APPKEY[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  56. void os_getDevKey (u1_t* buf) {  memcpy_P(buf, APPKEY, 16);}    
  57.  
  58. //static uint8_t mydata[3];
  59. // seem to need to add one to the number of bytes that the encoder system expects
  60.  
  61. static osjob_t sendjob;
  62.  
  63. // Schedule TX every this many seconds (might become longer due to duty
  64. // cycle limitations).
  65. const unsigned TX_INTERVAL = 10;
  66.  
  67.  
  68. // Pin mapping for Adafruit Feather M4 LoRa, etc.
  69. const lmic_pinmap lmic_pins = {
  70.     .nss = 9,
  71.     .rxtx = LMIC_UNUSED_PIN,
  72.     .rst = 11,
  73.     .dio = {10, 7, LMIC_UNUSED_PIN},
  74.     .rxtx_rx_active = 0,
  75.     .rssi_cal = 8,              // LBT cal for the Adafruit Feather M0 LoRa, in dB
  76.     .spi_freq = 8000000,
  77. };
  78.  
  79. void onEvent (ev_t ev) {
  80.     Serial.print(os_getTime());
  81.     Serial.print(": ");
  82.     switch(ev) {
  83.         case EV_SCAN_TIMEOUT:
  84.             Serial.println(F("EV_SCAN_TIMEOUT"));
  85.             break;
  86.         case EV_BEACON_FOUND:
  87.             Serial.println(F("EV_BEACON_FOUND"));
  88.             break;
  89.         case EV_BEACON_MISSED:
  90.             Serial.println(F("EV_BEACON_MISSED"));
  91.             break;
  92.         case EV_BEACON_TRACKED:
  93.             Serial.println(F("EV_BEACON_TRACKED"));
  94.             break;
  95.         case EV_JOINING:
  96.             Serial.println(F("EV_JOINING"));
  97.             break;
  98.         case EV_JOINED:
  99.             Serial.println(F("EV_JOINED"));
  100.             {
  101.               u4_t netid = 0;
  102.               devaddr_t devaddr = 0;
  103.               u1_t nwkKey[16];
  104.               u1_t artKey[16];
  105.               LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey);
  106.               Serial.print("netid: ");
  107.               Serial.println(netid, DEC);
  108.               Serial.print("devaddr: ");
  109.               Serial.println(devaddr, HEX);
  110.               Serial.print("artKey: ");
  111.               for (int i=0; i<sizeof(artKey); ++i) {
  112.                 if (i != 0)
  113.                   Serial.print("-");
  114.                 Serial.print(artKey[i], HEX);
  115.               }
  116.               Serial.println("");
  117.               Serial.print("nwkKey: ");
  118.               for (int i=0; i<sizeof(nwkKey); ++i) {
  119.                       if (i != 0)
  120.                               Serial.print("-");
  121.                       Serial.print(nwkKey[i], HEX);
  122.               }
  123.               Serial.println("");
  124.             }
  125.             // Disable link check validation (automatically enabled
  126.             // during join, but because slow data rates change max TX
  127.         // size, we don't use it in this example.
  128.             LMIC_setLinkCheckMode(0);
  129.             break;
  130.         /*
  131.         || This event is defined but not used in the code. No
  132.         || point in wasting codespace on it.
  133.         ||
  134.         || case EV_RFU1:
  135.         ||     Serial.println(F("EV_RFU1"));
  136.         ||     break;
  137.         */
  138.         case EV_JOIN_FAILED:
  139.             Serial.println(F("EV_JOIN_FAILED"));
  140.             break;
  141.         case EV_REJOIN_FAILED:
  142.             Serial.println(F("EV_REJOIN_FAILED"));
  143.             break;
  144.             break;
  145.         case EV_TXCOMPLETE:
  146.             Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  147.             if (LMIC.txrxFlags & TXRX_ACK)
  148.               Serial.println(F("Received ack"));
  149.             if (LMIC.dataLen) {
  150.               Serial.println(F("Received "));
  151.               Serial.println(LMIC.dataLen);
  152.               Serial.println(F(" bytes of payload"));
  153.             }
  154.             // Schedule next transmission
  155.             os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  156.             break;
  157.         case EV_LOST_TSYNC:
  158.             Serial.println(F("EV_LOST_TSYNC"));
  159.             break;
  160.         case EV_RESET:
  161.             Serial.println(F("EV_RESET"));
  162.             break;
  163.         case EV_RXCOMPLETE:
  164.             // data received in ping slot
  165.             Serial.println(F("EV_RXCOMPLETE"));
  166.             break;
  167.         case EV_LINK_DEAD:
  168.             Serial.println(F("EV_LINK_DEAD"));
  169.             break;
  170.         case EV_LINK_ALIVE:
  171.             Serial.println(F("EV_LINK_ALIVE"));
  172.             break;
  173.         /*
  174.         || This event is defined but not used in the code. No
  175.         || point in wasting codespace on it.
  176.         ||
  177.         || case EV_SCAN_FOUND:
  178.         ||    Serial.println(F("EV_SCAN_FOUND"));
  179.         ||    break;
  180.         */
  181.         case EV_TXSTART:
  182.             Serial.println(F("EV_TXSTART"));
  183.             break;
  184.         default:
  185.             Serial.print(F("Unknown event: "));
  186.             Serial.println((unsigned) ev);
  187.             break;
  188.     }
  189. }
  190.  
  191. void do_send(osjob_t* j){
  192.     // Check if there is not a current TX/RX job running
  193.     if (LMIC.opmode & OP_TXRXPEND) {
  194.         Serial.println(F("OP_TXRXPEND, not sending"));
  195.     } else {
  196.  
  197.  
  198.         float temp = 24.5;
  199.         float humidity = 33.;
  200.        
  201.         //LoraEncoder encoder(mydata);
  202.         //encoder.writeTemperature(-123.45);
  203.         LoraMessage message;
  204.         message.addTemperature(temp).addHumidity(humidity);
  205.  
  206.        
  207.         // Prepare upstream data transmission at the next possible time.
  208.         //LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
  209.         //Serial.println(sizeof(message.getBytes()));
  210.        
  211.         LMIC_setTxData2(1, message.getBytes(), sizeof(message.getBytes()), 0);
  212.  
  213.         //delete message;
  214.        
  215.         Serial.println(F("Packet queued"));
  216.     }
  217.     // Next TX is scheduled after TX_COMPLETE event.
  218. }
  219.  
  220. void setup() {
  221.     delay(5000);
  222.     while (! Serial)
  223.         ;
  224.     Serial.begin(9600);
  225.     Serial.println(F("Starting"));
  226.  
  227.     #ifdef VCC_ENABLE
  228.     // For Pinoccio Scout boards
  229.     pinMode(VCC_ENABLE, OUTPUT);
  230.     digitalWrite(VCC_ENABLE, HIGH);
  231.     delay(1000);
  232.     #endif
  233.  
  234.     // LMIC init
  235.     os_init();
  236.     // Reset the MAC state. Session and pending data transfers will be discarded.
  237.     LMIC_reset();
  238.  
  239.     LMIC_setLinkCheckMode(0);
  240.     LMIC_setDrTxpow(DR_SF7,14);
  241.     LMIC_selectSubBand(1);
  242.  
  243.     // Start job (sending automatically starts OTAA too)
  244.     do_send(&sendjob);
  245. }
  246.  
  247. void loop() {
  248.     os_runloop_once();
  249. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top