Advertisement
Guest User

Untitled

a guest
Nov 16th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.09 KB | None | 0 0
  1. #include <lmic.h>
  2. #include <hal/hal.h>
  3. #include <SPI.h>
  4. #include <TinyGPS++.h>
  5. #include <SoftwareSerial.h>
  6. #define LPP_GPS 136  
  7. #include <Adafruit_SleepyDog.h>
  8. //#include <avr/sleep.h>
  9. //#include <avr/wdt.h>
  10. //#include <LowPower.h>
  11. static bool devmode = true; //set to true if you want messages output to serial
  12. static int OnOffPin = 3 ; //Specify the pin you have attached to the ON_OFF pin of the ATGM336H module here
  13. static const int RXPin = 4, TXPin = 5;
  14. static const uint32_t GPSBaud = 9600;
  15.  
  16. static bool first_run = true;
  17.  
  18. TinyGPSPlus gps;
  19. SoftwareSerial ss(RXPin, TXPin); // RX, TX
  20.  
  21. static const PROGMEM u1_t NWKSKEY[16] = { Redacted };
  22. static const u1_t PROGMEM APPSKEY[16] = { Redacted };
  23. static const u4_t DEVADDR = Redacted; // <-- Change this address for every node!
  24. const unsigned TX_INTERVAL = 1;
  25.  
  26. void os_getArtEui (u1_t* buf) { }
  27. void os_getDevEui (u1_t* buf) { }
  28. void os_getDevKey (u1_t* buf) { }
  29.  
  30.   uint8_t coords[11];
  31.   static osjob_t sendjob;
  32.   static osjob_t initjob;
  33.   uint8_t cursor = 0;
  34.   uint8_t channel;
  35.  
  36. // Pin mapping
  37.   const lmic_pinmap lmic_pins = {
  38.   .nss = 10,
  39.   .rxtx = LMIC_UNUSED_PIN,
  40.   .rst = 9,
  41.   .dio = {2, 8, 7},
  42. };
  43.  
  44.   void get_coords () {
  45.  
  46.   if (first_run == false){
  47.   digitalWrite(OnOffPin,LOW);
  48.   if (devmode == true) {
  49.   Serial.println("On/Off pin set to low");
  50.   }
  51.   delay(150);
  52.   if (devmode == true) {
  53.   Serial.println("Going to sleep");
  54.   }
  55.   Serial.flush();
  56.   //Sleep for 1 minute
  57.   int sleepMS1 = Watchdog.sleep(8000);
  58.   int sleepMS2 = Watchdog.sleep(8000);
  59.   int sleepMS3 = Watchdog.sleep(8000);
  60.   int sleepMS4 = Watchdog.sleep(8000);
  61.   int sleepMS5 = Watchdog.sleep(8000);
  62.   int sleepMS6 = Watchdog.sleep(8000);
  63.   int sleepMS7 = Watchdog.sleep(8000);
  64.   int sleepMS8 = Watchdog.sleep(4000);  }
  65.   else {
  66.   first_run = false;
  67.   }
  68.   delay(500);
  69.   digitalWrite(OnOffPin,HIGH);
  70.   if (devmode == true) {
  71.   Serial.println("On/Off pin set to high");
  72.   }
  73.   delay(500); //Give the GPS module a moment to power up
  74.   int state;
  75.   state = digitalRead(OnOffPin);
  76.   Serial.print("ON_OFF Pin state is ");
  77.   Serial.println(state);
  78.   bool newData = false;
  79.   unsigned long chars;
  80.   unsigned short sentences, failed;
  81.   float flat,flon,faltitudeGPS,fhdopGPS;
  82.   int fsat = 0;
  83.   int cycles = 0;
  84.   unsigned long age;
  85.     // For one second we parse GPS data and report some key values
  86.   if (devmode == true) {
  87.   Serial.print("GPS Starting");
  88.   }
  89.     while (fsat < 4 && cycles < 300){ //This prevents the code getting stuck in an infinite loop if the GPS fails to power up properly
  90.   cycles = cycles + 1;
  91.   for (unsigned long start = millis(); millis() - start < 1000;) {
  92.     while (ss.available()) {
  93.       char c = ss.read();
  94.       //if (devmode == true) {
  95.       Serial.write(c); // uncomment this line if you want to see the GPS data flowing
  96.       //Serial.println("Reading data from GPS");
  97.       //}
  98.       if (gps.encode(c)) { // Did a new valid sentence come in?
  99.         newData = true;
  100.         //if (devmode == true) {
  101.        //   Serial.println("Valid GPS data received");
  102.         //}
  103.                
  104.         }
  105.         //else
  106.         //{
  107.         //first_run = true;
  108.         //if (devmode == true) {
  109.         //Serial.println("No data, trying again");
  110.         //}
  111.         //return;
  112.       //}
  113.     }
  114.   }
  115.  
  116. if ( newData ) {
  117.     fsat=gps.satellites.value();
  118.     //fsat = 1;
  119.     flat=gps.location.lat();
  120.     flon=gps.location.lng();
  121.     if (gps.altitude.isValid())
  122.          faltitudeGPS = gps.altitude.meters();
  123.      else
  124.         faltitudeGPS=0;
  125.     fhdopGPS = gps.hdop.value();
  126.     //if (devmode == true) {
  127.     //      Serial.println("No data received, trying again");
  128.     //    }
  129.     //first_run = true;
  130.     //return;
  131.  }
  132.  
  133.  
  134.   //gps.stats(&chars, &sentences, &failed);
  135.  
  136.  
  137.   int32_t lat = flat * 10000;
  138.   int32_t lon = flon * 10000;
  139.   int32_t altitudeGPS = faltitudeGPS  * 100;
  140.   //int16_t altitudeGPS = gps.altitude.meters;
  141.   //int8_t hdopGPS = fhdopGPS;
  142.  
  143.   channel = 0x01;
  144.   coords[0] = channel;
  145.   coords[1] = LPP_GPS;
  146.  
  147.   coords[2] = (lat >> 16);
  148.   coords[3] = (lat >> 8);
  149.   coords[4] = lat;
  150.  
  151.   coords[5] = (lon >> 16);
  152.   coords[6] = (lon >> 8);
  153.   coords[7] = lon;
  154.  
  155.   coords[8] = (altitudeGPS >> 16);
  156.   coords[9] = (altitudeGPS >> 8);
  157.   coords[10] = altitudeGPS;
  158.   //fsat = 1;
  159.   if (devmode == true) {
  160.   Serial.print("Sats = ");
  161.   Serial.println(fsat);
  162.   Serial.print("Cycles = ");
  163.   Serial.println(cycles);
  164.     }
  165.    }
  166.   }
  167.  
  168.  
  169. void do_send(osjob_t* j) {
  170.   // Check if there is not a current TX/RX job running
  171.   if (LMIC.opmode & OP_TXRXPEND) {
  172.     if (devmode == true) {
  173.     Serial.println(F("OP_TXRXPEND, not sending"));
  174.     }
  175.   } else {
  176.     // Prepare upstream data transmission at the next possible time.
  177.  
  178.     get_coords();
  179.     LMIC_setTxData2(1, (uint8_t*) coords, sizeof(coords), 0);
  180.    
  181.   }
  182. }
  183.  
  184.   // Next TX is scheduled after TX_COMPLETE event.
  185.  
  186. void onEvent (ev_t ev) {
  187.   if (devmode == true) {
  188.   Serial.print(os_getTime());
  189.   Serial.print(": ");
  190.   }
  191.   switch(ev) {
  192.     case EV_SCAN_TIMEOUT:
  193.       if (devmode == true) {
  194.       Serial.println(F("EV_SCAN_TIMEOUT"));
  195.       }
  196.       break;
  197.     case EV_BEACON_FOUND:
  198.     if (devmode == true) {
  199.       Serial.println(F("EV_BEACON_FOUND"));
  200.     }
  201.       break;
  202.     case EV_BEACON_MISSED:
  203.     if (devmode == true) {
  204.       Serial.println(F("EV_BEACON_MISSED"));
  205.     }
  206.       break;
  207.     case EV_BEACON_TRACKED:
  208.      if (devmode == true) {
  209.       Serial.println(F("EV_BEACON_TRACKED"));
  210.      }
  211.       break;
  212.     case EV_JOINING:
  213.       if (devmode == true) {
  214.       Serial.println(F("EV_JOINING"));
  215.       }
  216.       break;
  217.     case EV_JOINED:
  218.       if (devmode == true) {
  219.       Serial.println(F("EV_JOINED"));
  220.       }
  221.       // Disable link check validation (automatically enabled
  222.       // during join, but not supported by TTN at this time).
  223.       LMIC_setLinkCheckMode(0);
  224.       break;
  225.     case EV_RFU1:
  226.       if (devmode == true) {
  227.       Serial.println(F("EV_RFU1"));
  228.       }
  229.       break;
  230.     case EV_JOIN_FAILED:
  231.       if (devmode == true) {
  232.       Serial.println(F("EV_JOIN_FAILED"));
  233.       }
  234.       break;
  235.     case EV_REJOIN_FAILED:
  236.       if (devmode == true) {
  237.       Serial.println(F("EV_REJOIN_FAILED"));
  238.       }
  239.       break;
  240.       break;
  241.     case EV_TXCOMPLETE:
  242.       if (devmode == true) {
  243.       Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  244.       }
  245.       if (LMIC.txrxFlags & TXRX_ACK)
  246.         if (devmode == true) {
  247.         Serial.println(F("Received ack"));
  248.         }
  249.       if (LMIC.dataLen) {
  250.         if (devmode == true) {
  251.         Serial.println(F("Received "));
  252.         Serial.println(LMIC.dataLen);
  253.         Serial.println(F(" bytes of payload"));
  254.         }
  255.       }
  256.       // Schedule next transmission
  257.       os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  258.       break;
  259.     case EV_LOST_TSYNC:
  260.       if (devmode == true) {
  261.       Serial.println(F("EV_LOST_TSYNC"));
  262.       }
  263.       break;
  264.     case EV_RESET:
  265.       if (devmode == true) {
  266.       Serial.println(F("EV_RESET"));
  267.       }
  268.       break;
  269.     case EV_RXCOMPLETE:
  270.       // data received in ping slot
  271.       if (devmode == true) {
  272.       Serial.println(F("EV_RXCOMPLETE"));
  273.       }
  274.       break;
  275.     case EV_LINK_DEAD:
  276.       if (devmode == true) {
  277.       Serial.println(F("EV_LINK_DEAD"));
  278.       }
  279.       break;
  280.     case EV_LINK_ALIVE:
  281.       if (devmode == true) {
  282.       Serial.println(F("EV_LINK_ALIVE"));
  283.       }
  284.       break;
  285.     default:
  286.       if (devmode == true) {
  287.       Serial.println(F("Unknown event"));
  288.       }
  289.       break;
  290.  
  291.   }
  292. }
  293.  
  294. void setup()
  295. {
  296.   Serial.begin(115200);
  297.   if (devmode == true) {
  298.   Serial.println(F("Starting"));
  299.   }
  300.   ss.begin(GPSBaud);
  301.   pinMode(OnOffPin, OUTPUT);
  302.   static bool first_run = true;
  303.   // LMIC init
  304.   os_init();
  305.  
  306.   // Reset the MAC state. Session and pending data transfers will be discarded.
  307.   LMIC_reset();
  308.  
  309.     // Set static session parameters. Instead of dynamically establishing a session
  310.     // by joining the network, precomputed session parameters are be provided.
  311.     #ifdef PROGMEM
  312.     // On AVR, these values are stored in flash and only copied to RAM
  313.     // once. Copy them to a temporary buffer here, LMIC_setSession will
  314.     // copy them into a buffer of its own again.
  315.     uint8_t appskey[sizeof(APPSKEY)];
  316.     uint8_t nwkskey[sizeof(NWKSKEY)];
  317.     memcpy_P(appskey, APPSKEY, sizeof(APPSKEY));
  318.     memcpy_P(nwkskey, NWKSKEY, sizeof(NWKSKEY));
  319.     LMIC_setSession (0x1, DEVADDR, nwkskey, appskey);
  320.     #else
  321.     // If not running an AVR with PROGMEM, just use the arrays directly
  322.     LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
  323.     #endif
  324.  
  325.     #if defined(CFG_eu868)
  326.     // Set up the channels used by the Things Network, which corresponds
  327.     // to the defaults of most gateways. Without this, only three base
  328.     // channels from the LoRaWAN specification are used, which certainly
  329.     // works, so it is good for debugging, but can overload those
  330.     // frequencies, so be sure to configure the full frequency range of
  331.     // your network here (unless your network autoconfigures them).
  332.     // Setting up channels should happen after LMIC_setSession, as that
  333.     // configures the minimal channel set.
  334.     // NA-US channels 0-71 are configured automatically
  335.     LMIC_setupChannel(0, 868100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  336.     LMIC_setupChannel(1, 868300000, DR_RANGE_MAP(DR_SF12, DR_SF7B), BAND_CENTI);      // g-band
  337.     LMIC_setupChannel(2, 868500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  338.     // LMIC_setupChannel(3, 867100000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  339.     // LMIC_setupChannel(4, 867300000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  340.     // LMIC_setupChannel(5, 867500000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  341.     // LMIC_setupChannel(6, 867700000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  342.     // LMIC_setupChannel(7, 867900000, DR_RANGE_MAP(DR_SF12, DR_SF7),  BAND_CENTI);      // g-band
  343.     // LMIC_setupChannel(8, 868800000, DR_RANGE_MAP(DR_FSK,  DR_FSK),  BAND_MILLI);      // g2-band
  344.     // TTN defines an additional channel at 869.525Mhz using SF9 for class B
  345.     // devices' ping slots. LMIC does not have an easy way to define set this
  346.     // frequency and support for class B is spotty and untested, so this
  347.     // frequency is not configured here.
  348.     #elif defined(CFG_us915)
  349.     // NA-US channels 0-71 are configured automatically
  350.     // but only one group of 8 should (a subband) should be active
  351.     // TTN recommends the second sub band, 1 in a zero based count.
  352.     // https://github.com/TheThingsNetwork/gateway-conf/blob/master/US-global_conf.json
  353.     LMIC_selectSubBand(1);
  354.     #endif
  355.  
  356.     // Disable link check validation
  357.     LMIC_setLinkCheckMode(0);
  358.  
  359.     // TTN uses SF9 for its RX2 window.
  360.     LMIC.dn2Dr = DR_SF9;
  361.  
  362.     // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
  363.     LMIC_setDrTxpow(DR_SF7,14);
  364.  
  365.     // Start job
  366.     do_send(&sendjob);
  367.      
  368. }
  369.  
  370. void loop() {
  371.   //Serial.println("Looping");
  372.   os_runloop_once();
  373. }
  374.  
  375.  
  376.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement