Advertisement
shinvoid69

TTNMapper

Nov 2nd, 2018
49
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <lmic.h>
  2. #include <hal/hal.h>
  3. #include <WiFi.h>
  4.  
  5. // UPDATE the config.h file in the same folder WITH YOUR TTN KEYS AND ADDR.
  6. #include "config.h"
  7. #include "gps.h"
  8.  
  9. // T-Beam specific hardware
  10. #define BUILTIN_LED 21
  11.  
  12. char s[32]; // used to sprintf for Serial output
  13. uint8_t txBuffer[9];
  14. gps gps;
  15.  
  16. // These callbacks are only used in over-the-air activation, so they are
  17. // left empty here (we cannot leave them out completely unless
  18. // DISABLE_JOIN is set in config.h, otherwise the linker will complain).
  19. void os_getArtEui (u1_t* buf) { }
  20. void os_getDevEui (u1_t* buf) { }
  21. void os_getDevKey (u1_t* buf) { }
  22.  
  23. static osjob_t sendjob;
  24. // Schedule TX every this many seconds (might become longer due to duty cycle limitations).
  25. const unsigned TX_INTERVAL = 30;
  26.  
  27. // Pin mapping
  28. const lmic_pinmap lmic_pins = {
  29.   .nss = 18,
  30.   .rxtx = LMIC_UNUSED_PIN,
  31.   .rst = LMIC_UNUSED_PIN, // was "14,"
  32.   .dio = {26, 33, 32},
  33. };
  34.  
  35. void onEvent (ev_t ev) {
  36.   switch (ev) {
  37.     case EV_SCAN_TIMEOUT:
  38.       Serial.println(F("EV_SCAN_TIMEOUT"));
  39.       break;
  40.     case EV_BEACON_FOUND:
  41.       Serial.println(F("EV_BEACON_FOUND"));
  42.       break;
  43.     case EV_BEACON_MISSED:
  44.       Serial.println(F("EV_BEACON_MISSED"));
  45.       break;
  46.     case EV_BEACON_TRACKED:
  47.       Serial.println(F("EV_BEACON_TRACKED"));
  48.       break;
  49.     case EV_JOINING:
  50.       Serial.println(F("EV_JOINING"));
  51.       break;
  52.     case EV_JOINED:
  53.       Serial.println(F("EV_JOINED"));
  54.       // Disable link check validation (automatically enabled
  55.       // during join, but not supported by TTN at this time).
  56.       LMIC_setLinkCheckMode(0);
  57.       break;
  58.     case EV_RFU1:
  59.       Serial.println(F("EV_RFU1"));
  60.       break;
  61.     case EV_JOIN_FAILED:
  62.       Serial.println(F("EV_JOIN_FAILED"));
  63.       break;
  64.     case EV_REJOIN_FAILED:
  65.       Serial.println(F("EV_REJOIN_FAILED"));
  66.       break;
  67.     case EV_TXCOMPLETE:
  68.       Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  69.       digitalWrite(BUILTIN_LED, LOW);
  70.       if (LMIC.txrxFlags & TXRX_ACK) {
  71.         Serial.println(F("Received Ack"));
  72.       }
  73.       if (LMIC.dataLen) {
  74.         sprintf(s, "Received %i bytes of payload", LMIC.dataLen);
  75.         Serial.println(s);
  76.         sprintf(s, "RSSI %d SNR %.1d", LMIC.rssi, LMIC.snr);
  77.         Serial.println(s);
  78.       }
  79.       // Schedule next transmission
  80.       os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
  81.       break;
  82.     case EV_LOST_TSYNC:
  83.       Serial.println(F("EV_LOST_TSYNC"));
  84.       break;
  85.     case EV_RESET:
  86.       Serial.println(F("EV_RESET"));
  87.       break;
  88.     case EV_RXCOMPLETE:
  89.       // data received in ping slot
  90.       Serial.println(F("EV_RXCOMPLETE"));
  91.       break;
  92.     case EV_LINK_DEAD:
  93.       Serial.println(F("EV_LINK_DEAD"));
  94.       break;
  95.     case EV_LINK_ALIVE:
  96.       Serial.println(F("EV_LINK_ALIVE"));
  97.       break;
  98.     default:
  99.       Serial.println(F("Unknown event"));
  100.       break;
  101.   }
  102. }
  103.  
  104. void do_send(osjob_t* j) {  
  105.  
  106.   // Check if there is not a current TX/RX job running
  107.   if (LMIC.opmode & OP_TXRXPEND)
  108.   {
  109.     Serial.println(F("OP_TXRXPEND, not sending"));
  110.   }
  111.   else
  112.   {
  113.     if (gps.checkGpsFix())
  114.     {
  115.       // Prepare upstream data transmission at the next possible time.
  116.       gps.buildPacket(txBuffer);
  117.       LMIC_setTxData2(1, txBuffer, sizeof(txBuffer), 0);
  118.       Serial.println(F("Packet queued"));
  119.       digitalWrite(BUILTIN_LED, HIGH);
  120.     }
  121.     else
  122.     {
  123.       //try again in 3 seconds
  124.       os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(3), do_send);
  125.     }
  126.   }
  127.   // Next TX is scheduled after TX_COMPLETE event.
  128. }
  129.  
  130. void setup() {
  131.   Serial.begin(115200);
  132.   Serial.println(F("TTN Mapper"));
  133.  
  134.   //Turn off WiFi and Bluetooth
  135.   WiFi.mode(WIFI_OFF);
  136.   btStop();
  137.   gps.init();
  138.  
  139.   // LMIC init
  140.   os_init();
  141.   // Reset the MAC state. Session and pending data transfers will be discarded.
  142.   LMIC_reset();
  143.   LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100);
  144.  
  145.   LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
  146.  
  147.   LMIC_selectSubBand(1);
  148.  
  149.   // Disable link check validation
  150.   LMIC_setLinkCheckMode(0);
  151.  
  152.   // TTN uses SF9 for its RX2 window.
  153.   LMIC.dn2Dr = DR_SF9;
  154.  
  155.   // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
  156.   LMIC_setDrTxpow(DR_SF7,14);
  157.  
  158.   do_send(&sendjob);
  159.   pinMode(BUILTIN_LED, OUTPUT);
  160.   digitalWrite(BUILTIN_LED, LOW);
  161.  
  162. }
  163.  
  164. void loop() {
  165.     os_runloop_once();
  166. }
Advertisement
RAW Paste Data Copied
Advertisement