Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
105
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 <SPI.h>
  4.  
  5.  
  6. #define CFG_eu868
  7.  
  8. // LoRaWAN NwkSKey, network session key
  9. // This is the default Semtech key, which is used by the early prototype TTN
  10. // network.
  11.  
  12. #ifdef CREDENTIALS
  13. static const PROGMEM u1_t NWKSKEY[16] = NWKSKEY1;
  14. static const PROGMEM u1_t APPSKEY[16] = APPSKEY1;
  15. static const PROGMEM u4_t DEVADDR = DEVADDR1;
  16. #else
  17. static const PROGMEM u1_t NWKSKEY[16] = { my_network_key };
  18. static const PROGMEM u1_t APPSKEY[16] = { my_app_key };
  19. static const PROGMEM u4_t DEVADDR = my_dev_address;
  20. #endif
  21.  
  22. unsigned long entry = millis();
  23.  
  24. #define GREENLED 3
  25. #define REDLED 8
  26. #define SFSWITCH 4
  27. #define RUNPIN 5
  28.  
  29. // These callbacks are only used in over-the-air activation, so they are
  30. // left empty here (we cannot leave them out completely unless
  31. // DISABLE_JOIN is set in config.h, otherwise the linker will complain).
  32. void os_getArtEui (u1_t* buf) { }
  33. void os_getDevEui (u1_t* buf) { }
  34. void os_getDevKey (u1_t* buf) { }
  35.  
  36. byte counter = 0;
  37. static osjob_t sendjob;
  38. bool switchState = true, oldSwitchState = false;
  39.  
  40. // Schedule TX every this many seconds (might become longer due to duty
  41. // cycle limitations).
  42. const unsigned TX_INTERVAL = 60;
  43.  
  44. // Pin mapping
  45. const lmic_pinmap lmic_pins = {
  46.   .nss = 10,
  47.   .rxtx = LMIC_UNUSED_PIN,
  48.   .rst = 9,
  49.   .dio = {2, 6, 7},
  50. };
  51.  
  52. void onEvent (ev_t ev) {
  53.   Serial.print(os_getTime());
  54.   Serial.print(": ");
  55.   switch (ev) {
  56.     case EV_SCAN_TIMEOUT:
  57.       Serial.println(F("EV_SCAN_TIMEOUT"));
  58.       break;
  59.     case EV_BEACON_FOUND:
  60.       Serial.println(F("EV_BEACON_FOUND"));
  61.       break;
  62.     case EV_BEACON_MISSED:
  63.       Serial.println(F("EV_BEACON_MISSED"));
  64.       break;
  65.     case EV_BEACON_TRACKED:
  66.       Serial.println(F("EV_BEACON_TRACKED"));
  67.       break;
  68.     case EV_JOINING:
  69.       Serial.println(F("EV_JOINING"));
  70.       break;
  71.     case EV_JOINED:
  72.       Serial.println(F("EV_JOINED"));
  73.       break;
  74.     case EV_RFU1:
  75.       Serial.println(F("EV_RFU1"));
  76.       break;
  77.     case EV_JOIN_FAILED:
  78.       Serial.println(F("EV_JOIN_FAILED"));
  79.       break;
  80.     case EV_REJOIN_FAILED:
  81.       Serial.println(F("EV_REJOIN_FAILED"));
  82.       break;
  83.  
  84.     case EV_TXCOMPLETE:
  85.       Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  86.       Serial.println(LMIC.dataLen);
  87.       if (LMIC.dataLen) {
  88.         // data received in rx slot after tx
  89.         uint8_t downlink[LMIC.dataLen];
  90.         memcpy(&downlink, &(LMIC.frame + LMIC.dataBeg)[0], LMIC.dataLen);
  91.         // Turn on/off fan if we get the magic number
  92.         if ( downlink[0] == 0x31 ) {
  93.           // digitalWrite(FanPin, HIGH);
  94.         }
  95.         else {
  96.           // digitalWrite(FanPin, LOW);
  97.         }
  98.  
  99.         Serial.print(F("Data Received: "));
  100.         Serial.write(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
  101.         Serial.println();
  102.  
  103.       }
  104.       // Schedule next transmission
  105.       os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
  106.       digitalWrite(REDLED, HIGH);
  107.       break;
  108.     /*
  109.       case EV_TXCOMPLETE:
  110.       Serial.print((millis() - entry) / 1000);  Serial.print(" ");
  111.       entry = millis();
  112.       Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  113.       if (LMIC.txrxFlags & TXRX_ACK)
  114.       Serial.println(F("Received ack"));
  115.       if (LMIC.dataLen) {
  116.       Serial.println(F("Received "));
  117.       Serial.println(LMIC.dataLen);
  118.       Serial.println(F(" bytes of payload"));
  119.       }
  120.       // Schedule next transmission
  121.       os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
  122.       digitalWrite(REDLED, HIGH);
  123.       break;
  124.     */
  125.     case EV_LOST_TSYNC:
  126.       Serial.println(F("EV_LOST_TSYNC"));
  127.       break;
  128.     case EV_RESET:
  129.       Serial.println(F("EV_RESET"));
  130.       break;
  131.     case EV_RXCOMPLETE:
  132.       // data received in ping slot
  133.       Serial.println(F("EV_RXCOMPLETE"));
  134.       break;
  135.     case EV_LINK_DEAD:
  136.       Serial.println(F("EV_LINK_DEAD"));
  137.       break;
  138.     case EV_LINK_ALIVE:
  139.       Serial.println(F("EV_LINK_ALIVE"));
  140.       break;
  141.     default:
  142.       Serial.println(F("Unknown event"));
  143.       break;
  144.   }
  145. }
  146.  
  147. void do_send(osjob_t* j) {
  148.   byte buffer[32];
  149.   // Check if there is not a current TX/RX job running
  150.   if (LMIC.opmode & OP_TXRXPEND) {
  151.     Serial.println(F("OP_TXRXPEND, not sending"));
  152.   } else {
  153.     // Prepare upstream data transmission at the next possible time.
  154.     String message = "Hello TTN from arduino uno!";
  155.     message.getBytes(buffer, message.length() + 1);
  156.  
  157.     Serial.println("Sending: " + message);
  158.     LMIC_setTxData2(1, (uint8_t*) buffer, message.length() , 0);
  159.     Serial.println(F(" Packet queued"));
  160.     digitalWrite(REDLED, LOW);
  161.   }
  162.   // Next TX is scheduled after TX_COMPLETE event.
  163. }
  164.  
  165. void setup() {
  166.   Serial.begin(9600);
  167.   Serial.println(F("Starting"));
  168.  
  169.   pinMode(GREENLED, OUTPUT);
  170.   pinMode(REDLED, OUTPUT);
  171.   pinMode(SFSWITCH, INPUT_PULLUP);
  172.   pinMode(RUNPIN, INPUT_PULLUP);
  173.   digitalWrite(REDLED, HIGH);
  174.   digitalWrite(GREENLED, HIGH);
  175.  
  176. #ifdef VCC_ENABLE
  177.   // For Pinoccio Scout boards
  178.   pinMode(VCC_ENABLE, OUTPUT);
  179.   digitalWrite(VCC_ENABLE, HIGH);
  180.   delay(1000);
  181. #endif
  182.  
  183.   // LMIC init
  184.   os_init();
  185.   // Reset the MAC state. Session and pending data transfers will be discarded.
  186.   LMIC_reset();
  187.  
  188.   // Set static session parameters. Instead of dynamically establishing a session
  189.   // by joining the network, precomputed session parameters are be provided.
  190. #ifdef PROGMEM
  191.   // On AVR, these values are stored in flash and only copied to RAM
  192.   // once. Copy them to a temporary buffer here, LMIC_setSession will
  193.   // copy them into a buffer of its own again.
  194.   uint8_t appskey[sizeof(APPSKEY)];
  195.   uint8_t nwkskey[sizeof(NWKSKEY)];
  196.   memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
  197.   memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
  198.   LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
  199. #else
  200.   // If not running an AVR with PROGMEM, just use the arrays directly
  201.   LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
  202. #endif
  203.  
  204. #if defined(CFG_eu868)
  205.   Serial.println("European Channels");
  206.   for (int i = 1; i <= 8; i++) LMIC_disableChannel(i);
  207. #elif defined(CFG_us915)
  208.   // NA-US channels 0-71 are configured automatically
  209.   // but only one group of 8 should (a subband) should be active
  210.   // TTN recommends the second sub band, 1 in a zero based count.
  211.   // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
  212.   LMIC_selectSubBand(1);
  213. #endif
  214.  
  215.   // Disable link check validation
  216.   LMIC_setLinkCheckMode(0);
  217.  
  218.   // TTN uses SF9 for its RX2 window.
  219.   LMIC.dn2Dr = DR_SF9;
  220.  
  221.   // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
  222.   if (digitalRead(SFSWITCH) == HIGH) {
  223.     Serial.println("SF7");
  224.     LMIC_setDrTxpow(DR_SF7, 14);
  225.   }
  226.   else  {
  227.     Serial.println("SF12");
  228.     LMIC_setDrTxpow(DR_SF12, 14);
  229.   }
  230.  
  231.   // Start job
  232.   do_send(&sendjob);
  233. }
  234.  
  235. void loop() {
  236.   while (digitalRead(RUNPIN) == HIGH) {
  237.     digitalWrite(GREENLED, HIGH);
  238.     delay(10);
  239.   }
  240.   switchState = digitalRead(SFSWITCH);
  241.   if (switchState != oldSwitchState) {
  242.     delay(100);
  243.     // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
  244.     if (digitalRead(SFSWITCH) == HIGH) {
  245.       Serial.println("SF7");
  246.       LMIC_setDrTxpow(DR_SF7, 14);
  247.     }
  248.     else  {
  249.       Serial.println("SF12");
  250.       LMIC_setDrTxpow(DR_SF12, 14);
  251.     }
  252.     oldSwitchState = switchState;
  253.   }
  254.   digitalWrite(GREENLED, LOW);
  255.   os_runloop_once();
  256.  
  257. }
Advertisement
RAW Paste Data Copied
Advertisement