Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1.  
  2.  
  3. // MIT License
  4. // https://github.com/gonzalocasas/arduino-uno-dragino-lorawan/blob/master/LICENSE
  5. // Based on examples from https://github.com/matthijskooijman/arduino-lmic
  6. // Copyright (c) 2015 Thomas Telkamp and Matthijs Kooijman
  7.  
  8. // Adaptions: Andreas Spiess
  9.  
  10. #include <lmic.h>
  11. #include <hal/hal.h>
  12. //#include <credentials.h>
  13. #include <math.h>
  14. #include <stdlib.h>
  15.  
  16. #ifdef CREDENTIALS
  17. static const u1_t NWKSKEY[16] = NWKSKEY1;
  18. static const u1_t APPSKEY[16] = APPSKEY1;
  19. static const u4_t DEVADDR = DEVADDR1;
  20. #else
  21. static const u1_t NWKSKEY[16] = { key };
  22. static const u1_t APPSKEY[16] = { key };
  23. static const u4_t DEVADDR = 0xthingymcjigger;
  24. #endif
  25.  
  26. // These callbacks are only used in over-the-air activation, so they are
  27. // left empty here (we cannot leave them out completely unless
  28. // DISABLE_JOIN is set in config.h, otherwise the linker will complain).
  29. void os_getArtEui (u1_t* buf) { }
  30. void os_getDevEui (u1_t* buf) { }
  31. void os_getDevKey (u1_t* buf) { }
  32.  
  33. static osjob_t sendjob;
  34.  
  35. // Schedule TX every this many seconds (might become longer due to duty
  36. // cycle limitations).
  37. const unsigned TX_INTERVAL = 20;
  38.  
  39. // Pin mapping Dragino Shield
  40. const lmic_pinmap lmic_pins = {
  41.     .nss = 10,
  42.     .rxtx = LMIC_UNUSED_PIN,
  43.     .rst = 9,
  44.     .dio = {2, 6, 7},
  45. };
  46.  
  47. int sensorPin = A5; // select the input pin for the potentiometer
  48.  
  49. void onEvent (ev_t ev) {
  50.     if (ev == EV_TXCOMPLETE) {
  51.         Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
  52.         // Schedule next transmission
  53.         os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  54.     }
  55. }
  56.  
  57.  
  58. double read_thermistor(int RawADC) {
  59.   double Temp;
  60.   Temp = log(10000.0*((1024.0/RawADC-1)));
  61.   Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  62.   Temp = Temp - 273.15;            // Convert Kelvin to Celcius
  63.    //Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  64.    return Temp;
  65. }
  66.  
  67. void convert_to_string(char * outStr, int length, double val){
  68.     Serial.println(F("Converting..."));
  69.     dtostrf(val,2,0,outStr);
  70.     Serial.println(outStr);
  71. }
  72.  
  73.  
  74. void do_send(osjob_t* j){
  75.     // Payload to send (uplink)
  76.     uint8_t message[8];
  77.     int A = analogRead(sensorPin);
  78.     double temp = read_thermistor(A);
  79.     convert_to_string(message,8,A);
  80.    
  81.     // Check if there is not a current TX/RX job running
  82.     if (LMIC.opmode & OP_TXRXPEND) {
  83.         Serial.println(F("OP_TXRXPEND, not sending"));
  84.     } else {
  85.      
  86.         // Prepare upstream data transmission at the next possible time.
  87.         LMIC_setTxData2(1, message,2, 0);
  88.  
  89. //        LMIC_setTxData2(1, message, sizeof(message)-1, 0);
  90.         Serial.println(F("Sending uplink packet..."));
  91. //        Serial.println(temp);
  92.     }
  93.     // Next TX is scheduled after TX_COMPLETE event.
  94. }
  95.  
  96. void setup() {
  97.     Serial.begin(115200);
  98.     Serial.println(F("Starting..."));
  99.  
  100.     // LMIC init
  101.     os_init();
  102.  
  103.     // Reset the MAC state. Session and pending data transfers will be discarded.
  104.     LMIC_reset();
  105.  
  106.     // Set static session parameters.
  107.     LMIC_setSession (0x1, DEVADDR, NWKSKEY, APPSKEY);
  108.  
  109.     // Disable link check validation
  110.     LMIC_setLinkCheckMode(0);
  111.  
  112.     // TTN uses SF9 for its RX2 window.
  113.     LMIC.dn2Dr = DR_SF9;
  114.  
  115.     // Set data rate and transmit power for uplink (note: txpow seems to be ignored by the library)
  116.     LMIC_setDrTxpow(DR_SF12,14);
  117.  
  118.     // Start job
  119.     do_send(&sendjob);
  120. }
  121.  
  122. void loop() {
  123.     os_runloop_once();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement