Guest User

Untitled

a guest
Sep 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.02 KB | None | 0 0
  1.  
  2.  
  3. #include <lmic.h>
  4. #include <hal/hal.h>
  5. #include <SPI.h>
  6.  
  7. // LoRaWAN variables
  8. static const PROGMEM u1_t NWKSKEY[16] = { <Numbers> };
  9. static const u1_t PROGMEM APPSKEY[16] = { <numbers> };
  10. static const u4_t DEVADDR = <Number>;; // <-- Change this address for every node!
  11. // These callbacks are only used in over-the-air activation, so they are
  12. // left empty here (we cannot leave them out completely unless
  13. // DISABLE_JOIN is set in config.h, otherwise the linker will complain).
  14. void os_getArtEui (u1_t* buf) { }
  15. void os_getDevEui (u1_t* buf) { }
  16. void os_getDevKey (u1_t* buf) { }
  17.  
  18. // Moisture Settings
  19. int soilMoistureValue1 = 0;
  20.  
  21.  
  22. static uint8_t mydata[] = "Hello, world!";
  23. static osjob_t sendjob;
  24.  
  25. // Schedule TX every this many seconds (might become longer due to duty
  26. // cycle limitations).
  27. const unsigned TX_INTERVAL = 60;
  28.  
  29. //For Heltec Wifi LoRa 32, TTGO LoRa and TTGO LoRa32 V1 use:
  30. const lmic_pinmap lmic_pins = {
  31.   .nss = 18,
  32.   .rxtx = LMIC_UNUSED_PIN,
  33.   .rst = 14,
  34.   .dio = {/*dio0*/ 26, /*dio1*/ 33, /*dio2*/ 32}
  35. };
  36.  
  37.  
  38. void onEvent (ev_t ev) {
  39.     Serial.print(os_getTime());
  40.     Serial.print(": ");
  41.     switch(ev) {
  42.         case EV_SCAN_TIMEOUT:
  43.             Serial.println(F("EV_SCAN_TIMEOUT"));
  44.             break;
  45.         case EV_BEACON_FOUND:
  46.             Serial.println(F("EV_BEACON_FOUND"));
  47.             break;
  48.         case EV_BEACON_MISSED:
  49.             Serial.println(F("EV_BEACON_MISSED"));
  50.             break;
  51.         case EV_BEACON_TRACKED:
  52.             Serial.println(F("EV_BEACON_TRACKED"));
  53.             break;
  54.         case EV_JOINING:
  55.             Serial.println(F("EV_JOINING"));
  56.             break;
  57.         case EV_JOINED:
  58.             Serial.println(F("EV_JOINED"));
  59.             break;
  60.         case EV_JOIN_FAILED:
  61.             Serial.println(F("EV_JOIN_FAILED"));
  62.             break;
  63.         case EV_REJOIN_FAILED:
  64.             Serial.println(F("EV_REJOIN_FAILED"));
  65.             break;
  66.         case EV_TXCOMPLETE:
  67.             Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  68.             if (LMIC.txrxFlags & TXRX_ACK)
  69.               Serial.println(F("Received ack"));
  70.             if (LMIC.dataLen) {
  71.               Serial.println(F("Received "));
  72.               Serial.println(LMIC.dataLen);
  73.               Serial.println(F(" bytes of payload"));
  74.             }
  75.             // Schedule next transmission
  76.             os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  77.             break;
  78.         case EV_LOST_TSYNC:
  79.             Serial.println(F("EV_LOST_TSYNC"));
  80.             break;
  81.         case EV_RESET:
  82.             Serial.println(F("EV_RESET"));
  83.             break;
  84.         case EV_RXCOMPLETE:
  85.             // data received in ping slot
  86.             Serial.println(F("EV_RXCOMPLETE"));
  87.             break;
  88.         case EV_LINK_DEAD:
  89.             Serial.println(F("EV_LINK_DEAD"));
  90.             break;
  91.         case EV_LINK_ALIVE:
  92.             Serial.println(F("EV_LINK_ALIVE"));
  93.             break;
  94.         case EV_TXSTART:
  95.             Serial.println(F("EV_TXSTART"));
  96.             break;
  97.         default:
  98.             Serial.print(F("Unknown event: "));
  99.             Serial.println((unsigned) ev);
  100.             break;
  101.     }
  102. }
  103.  
  104. void do_send(osjob_t* j){
  105.     // Check if there is not a current TX/RX job running
  106.     if (LMIC.opmode & OP_TXRXPEND) {
  107.         Serial.println(F("OP_TXRXPEND, not sending"));
  108.     } else {
  109.         // Prepare upstream data transmission at the next possible time.
  110.        
  111.         LMIC_setTxData2(1, soilMoistureValue1, sizeof(soilMoistureValue1)-1, 0);
  112.         //LMIC_setTxData2(1, mydata, sizeof(mydata)-1, 0);
  113.         Serial.println(F("Packet queued"));
  114.         Serial.print(F("Sending packet on frequency: "));
  115.         Serial.println(LMIC.freq);
  116.     }
  117.     // Next TX is scheduled after TX_COMPLETE event.
  118. }
  119.  
  120. void setup() {
  121. //    pinMode(13, OUTPUT);
  122.     while (!Serial); // wait for Serial to be initialized
  123.     Serial.begin(115200);
  124.     delay(100);     // per sample code on RF_95 test
  125.     Serial.println(F("Starting"));
  126.  
  127.     #ifdef VCC_ENABLE
  128.     // For Pinoccio Scout boards
  129.     pinMode(VCC_ENABLE, OUTPUT);
  130.     digitalWrite(VCC_ENABLE, HIGH);
  131.     delay(1000);
  132.     #endif
  133.  
  134.     // LMIC init
  135.     os_init();
  136.     // Reset the MAC state. Session and pending data transfers will be discarded.
  137.     LMIC_reset();
  138.  
  139.     // Set static session parameters. Instead of dynamically establishing a session
  140.     // by joining the network, precomputed session parameters are be provided.
  141.     #ifdef PROGMEM
  142.     // On AVR, these values are stored in flash and only copied to RAM
  143.     // once. Copy them to a temporary buffer here, LMIC_setSession will
  144.     // copy them into a buffer of its own again.
  145.     uint8_t appskey[sizeof(APPSKEY)];
  146.     uint8_t nwkskey[sizeof(NWKSKEY)];
  147.     memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
  148.     memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
  149.     LMIC_setSession (0x13, DEVADDR, nwkskey, appskey);
  150.     #else
  151.     // If not running an AVR with PROGMEM, just use the arrays directly
  152.     LMIC_setSession (0x13, DEVADDR, NWKSKEY, APPSKEY);
  153.     #endif
  154.  
  155.     #if defined(CFG_eu868)
  156.     // Set up the channels used by the Things Network, which corresponds
  157.     // to the defaults of most gateways. Without this, only three base
  158.     // channels from the LoRaWAN specification are used, which certainly
  159.     // works, so it is good for debugging, but can overload those
  160.     // frequencies, so be sure to configure the full frequency range of
  161.     // your network here (unless your network autoconfigures them).
  162.     // Setting up channels should happen after LMIC_setSession, as that
  163.     // configures the minimal channel set.
  164.     LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  165.     LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI);      // g-band
  166.     LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  167.     LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  168.     LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  169.     LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  170.     LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  171.     LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  172.     LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK,  DR_FSK),  BAND_MILLI);      // g2-band
  173.     // TTN defines an additional channel at 869.525Mhz using SF9 for class B
  174.     // devices' ping slots. LMIC does not have an easy way to define set this
  175.     // frequency and support for class B is spotty and untested, so this
  176.     // frequency is not configured here.
  177.     #elif defined(CFG_us915)
  178.     // NA-US channels 0-71 are configured automatically
  179.     // but only one group of 8 should (a subband) should be active
  180.     // TTN recommends the second sub band, 1 in a zero based count.
  181.     // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
  182.     LMIC_selectSubBand(1);
  183.     // Specify to operate on AU915 sub-band 2
  184.     #elif defined(CFG_au921)
  185.     Serial.println(F("Loading AU915/AU921 Configuration..."));
  186.     // Set to AU915 sub-band 2
  187.     LMIC_selectSubBand(1);
  188.     #endif
  189.  
  190.     // Disable link check validation
  191.     LMIC_setLinkCheckMode(0);
  192.  
  193.     // TTN uses SF9 for its RX2 window.
  194.     LMIC.dn2Dr = DR_SF9;
  195.  
  196.     // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
  197.     LMIC_setDrTxpow(DR_SF7,14);
  198.  
  199.     // Start job
  200.     readMoisture();
  201.     do_send(&sendjob);
  202. }
  203.  
  204.  
  205. void readMoisture() {
  206.     soilMoistureValue1 = analogRead(36);  //put Sensor insert into soil
  207.     Serial.println(soilMoistureValue1);
  208.     }
  209.  
  210.  
  211. void loop() {
  212.     unsigned long now;
  213.     now = millis();
  214.     if ((now & 512) != 0) {
  215.       digitalWrite(13, HIGH);
  216.     }
  217.     else {
  218.       digitalWrite(13, LOW);
  219.     }
  220.    readMoisture();
  221.    delay(1000);  
  222.     os_runloop_once();
  223.    
  224. }
Add Comment
Please, Sign In to add comment