ilf

Untitled

ilf
Jun 22nd, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. #include <lmic.h>
  2. #include <hal/hal.h>
  3. #include <SPI.h>
  4.  
  5. #include <PString.h>
  6.  
  7. #include <Base64.h>
  8.  
  9. #include <Arduino.h>
  10. #include <NMEAGPS.h>
  11.  
  12. #include <Wire.h>
  13. #include <Adafruit_LSM303_U.h>
  14. #include <Adafruit_BMP085_U.h>
  15. #include <Adafruit_Sensor.h>
  16. #include <Adafruit_Sensor_Set.h>
  17. #include <Adafruit_Simple_AHRS.h>
  18. #include <Madgwick.h>
  19. #include <Mahony.h>
  20.  
  21. #define USE_SPI 1
  22. SPIClass mySPI(USE_SPI);
  23. extern SPIClass *SPIp;
  24.  
  25. // Accelerometer + Giro + ...
  26.  
  27. // Create sensor instances.
  28. Adafruit_LSM303_Accel_Unified accel(30301);
  29. Adafruit_LSM303_Mag_Unified mag(30302);
  30. Adafruit_BMP085_Unified bmp(18001);
  31.  
  32. // Create simple AHRS algorithm using the above sensors.
  33. Adafruit_Simple_AHRS ahrs(&accel, &mag);
  34.  
  35. // Update this with the correct SLP for accurate altitude measurements
  36. float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
  37.  
  38. // GPS
  39.  
  40. HardwareSerial & gps_port = Serial1;
  41. #define USING_GPS_PORT "Serial1"
  42.  
  43. #include "Streamers.h"
  44. #ifdef NeoHWSerial_h
  45. #define DEBUG_PORT NeoSerial
  46. #else
  47. #define DEBUG_PORT Serial3
  48. #endif
  49.  
  50. // Check configuration
  51.  
  52. // #ifndef NMEAGPS_INTERRUPT_PROCESSING
  53. // #error You must define NMEAGPS_INTERRUPT_PROCESSING in NMEAGPS_cfg.h!
  54. // #endif
  55.  
  56. static NMEAGPS gps;
  57.  
  58. // LMIC
  59.  
  60. // This EUI must be in little-endian format, so least-significant-byte
  61. // first. When copying an EUI from ttnctl output, this means to reverse
  62. // the bytes. For TTN issued EUIs the last bytes should be 0xD5, 0xB3,
  63. // 0x70.
  64. static const u1_t PROGMEM APPEUI[8]={ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  65. void os_getArtEui (u1_t* buf) { memcpy_P(buf, APPEUI, 8);}
  66.  
  67. // This should also be in little endian format, see above.
  68. static const u1_t PROGMEM DEVEUI[8]={ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  69. void os_getDevEui (u1_t* buf) { memcpy_P(buf, DEVEUI, 8);}
  70.  
  71. // This key should be in big endian format (or, since it is not really a
  72. // number but a block of memory, endianness does not really apply). In
  73. // practice, a key taken from ttnctl can be copied as-is.
  74. // The key shown here is the semtech default key.
  75. static const u1_t PROGMEM APPKEY[16] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C };
  76. void os_getDevKey (u1_t* buf) { memcpy_P(buf, APPKEY, 16);}
  77.  
  78. static osjob_t sendjob;
  79. static uint8_t* encodedString;
  80.  
  81. // Schedule TX every this many seconds (might become longer due to duty
  82. // cycle limitations).
  83. const unsigned TX_INTERVAL = 60;
  84.  
  85. // Pin mapping
  86. const lmic_pinmap lmic_pins = {
  87. .nss = PA4,
  88. .rxtx = LMIC_UNUSED_PIN,
  89. .rst = PB0,
  90. .dio = {PA0, PA1, PA8},
  91. };
  92.  
  93. static void GPSisr( uint8_t c )
  94. {
  95. gps.handle( c );
  96. }
  97.  
  98. void onEvent (ev_t ev) {
  99. switch(ev) {
  100. case EV_TXCOMPLETE:
  101. // Schedule next transmission
  102. os_setTimedCallback(&sendjob, os_getTime()+sec2osticks(TX_INTERVAL), do_send);
  103. free(encodedString);
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109.  
  110. void do_send(osjob_t* j){
  111. // Check if there is not a current TX/RX job running
  112. if (LMIC.opmode & OP_TXRXPEND) {
  113. // Serial.println(F("OP_TXRXPEND, not sending"));
  114. } else {
  115. // Prepare upstream data transmission at the next possible time.
  116. LMIC_setTxData2(1, encodedString, sizeof(encodedString)-1, 0);
  117. // Serial.println(F("Packet queued"));
  118. }
  119. // Next TX is scheduled after TX_COMPLETE event.
  120. }
  121.  
  122. void setup()
  123. {
  124. // SPI
  125. SPIp = &mySPI;
  126.  
  127. // Initialize the sensors.
  128. Serial3.begin(115200);
  129. accel.begin();
  130. mag.begin();
  131. bmp.begin();
  132.  
  133. // GPS
  134.  
  135. // Start the UART for the GPS device
  136. // gps_port.attachInterrupt( GPSisr );
  137. gps_port.begin( 9600 );
  138.  
  139. // LMIC init
  140. os_init();
  141. // Reset the MAC state. Session and pending data transfers will be discarded.
  142. LMIC_reset();
  143.  
  144. // Start job
  145. do_send(&sendjob);
  146. }
  147.  
  148. void loop(void)
  149. {
  150. sensors_vec_t orientation;
  151. char buffer_gps[128];
  152. PString result_gps(buffer_gps, sizeof(buffer_gps), "");
  153.  
  154. char buffer_ahrs[16];
  155. PString result_ahrs(buffer_ahrs, sizeof(buffer_ahrs), "");
  156.  
  157. char buffer_bmp[16];
  158. PString result_bmp(buffer_bmp, sizeof(buffer_bmp), "");
  159.  
  160. char buffer[160];
  161. PString result(buffer, sizeof(buffer), "");
  162.  
  163. if (gps.available()) {
  164. // Print all the things!
  165. trace_all( result, gps, gps.read() );
  166. }
  167.  
  168. if (gps.overrun()) {
  169. gps.overrun( false );
  170. //DEBUG_PORT.println( F("DATA OVERRUN: took too long to use gps.read() data!") );
  171. }
  172.  
  173. // Use the simple AHRS function to get the current orientation.
  174. if (ahrs.getOrientation(&orientation))
  175. {
  176. /* 'orientation' should have valid .roll and .pitch fields */
  177. result_ahrs.format("%f, %f, %f", orientation.roll, orientation.pitch, orientation.heading);
  178. }
  179.  
  180. // Calculate the altitude using the barometric pressure sensor
  181. sensors_event_t bmp_event;
  182. bmp.getEvent(&bmp_event);
  183. if (bmp_event.pressure)
  184. {
  185. /* Get ambient temperature in C */
  186. float temperature;
  187. bmp.getTemperature(&temperature);
  188. /* Convert atmospheric pressure, SLP and temp to altitude */
  189. result_bmp.format("%f, %f", bmp.pressureToAltitude(seaLevelPressure,
  190. bmp_event.pressure,
  191. temperature),
  192. result.print(temperature));
  193. }
  194.  
  195. // Collect all the data in ; separated string
  196. result << result_gps << ";" << result_ahrs << ";" << result_bmp;
  197.  
  198. // Encode to Base64
  199. int encodedLength = Base64.encodedLength(result.length());
  200. encodedString = (uint8_t*)malloc(encodedLength);
  201. Base64.encode(encodedString, buffer, result.length());
  202.  
  203. // Serial.print(buffer);
  204.  
  205. // Send over LMIC
  206. os_runloop_once();
  207.  
  208. //delay(100);
  209. }
Advertisement
Add Comment
Please, Sign In to add comment