Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. /*
  2. * Sigfox
  3. * Company: Dryp
  4. * Model: Janaury 2019
  5. * Hardware revision:
  6. */
  7.  
  8. // Includes
  9. #include "Maxbotix.h"
  10. #include "LowPower.h"
  11. #include "Akeru.h"
  12. #include <Wire.h>
  13. #include "Adafruit_ADXL345_U.h"
  14. #include "Adafruit_Sensor.h"
  15.  
  16. // Definitions
  17. #define TX 9 // TD1208R Serial port
  18. #define RX 6 // TD1208R Serial port
  19. #define sensorPin 5 // MaxBotix MB7360 PWM pin
  20. #define ENABLE_US 2 // MOSFET pin
  21. #define TRANSMISSION_INTERVAL 75 // Transmission interval X * 8 seconds
  22. #define DEBUG true // Turn on debug messages
  23.  
  24. // Variables
  25. int temperature = 0; // TD1208R internal temperature
  26. int roll = 0, pitch = 0; //Roll & Pitch are the angles which rotate by the axis X and y
  27. double x_Buff = 0.00;
  28. double y_Buff = 0.00;
  29. double z_Buff = 0.00;
  30. unsigned long distance = 0; // MaxBotix MB7360 distance variable
  31.  
  32. // Sigfox instance management
  33. Akeru akeru(RX, TX);
  34.  
  35. // Instantiate sensors
  36. Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(345);
  37. Maxbotix rangeSensorPW(5, Maxbotix::PW, Maxbotix::HRLV,Maxbotix::MEDIAN, 10); // Sample size last
  38.  
  39. // Setup - runs once during bootup
  40. void setup() {
  41. if(DEBUG) {
  42. // initialize serial communication at 9600 bits per second:
  43. Serial.begin(9600);
  44. Serial.println("[INFO] Dryp Unit Starting...");
  45. }
  46.  
  47.  
  48. // Set MOSFET pinMode to OUTPUT
  49. pinMode(ENABLE_US, OUTPUT);
  50.  
  51. // Start communication with ADXL345
  52. if (!accel.begin()) {
  53. if(DEBUG) {
  54. Serial.println("[WARNING] Could not find ADXL345");
  55. }
  56. } else {
  57. if(DEBUG) {
  58. Serial.println("[INFO] ADXL345 started");
  59. }
  60. // Set the range to whatever is appropriate for your project
  61. accel.setRange(ADXL345_RANGE_16_G);
  62.  
  63. // Set output update rate to 6.25Hz with 3.125Hz bandwidth (40 μA consumption) TODO: Check if ADXL345_DATARATE_0_78_HZ exists and works
  64. accel.writeRegister(ADXL345_REG_BW_RATE, 0x06);
  65. }
  66.  
  67. // Display additional sensor information
  68. if(DEBUG) {
  69. /* Display some basic information on this sensor */
  70. displaySensorDetails();
  71.  
  72. /* Display additional settings (outside the scope of sensor_t) */
  73. displayDataRate();
  74. displayRange();
  75. }
  76. }
  77.  
  78. // Loop runs continuously as long as system is up
  79. void loop() {
  80. unsigned long start;
  81. // If debugging, examine how long it takes to do 1 measurement prior to transmission
  82. if (DEBUG) {
  83. start = millis();
  84. akeru.echoOn();
  85. }
  86.  
  87. // Start communication with TD1208R module
  88. if (!akeru.begin()) {
  89. if(DEBUG) {
  90. Serial.println("[WARNING] Could not find TD1208R");
  91. }
  92. } else if(DEBUG) {
  93. Serial.println("[INFO] TD1208R started");
  94. }
  95.  
  96. /*
  97. * Set accelerometer in standby mode prior to execution of begin(), which sets the ADXL345 into measurement mode.
  98. * Per datasheet, it is recommends to put the unit into standby-mode after sleep, prior to going into measurement mode.
  99. */
  100. accel.writeRegister(ADXL345_REG_POWER_CTL, 0); // standby mode
  101.  
  102. // Start communication with ADXL345
  103. if (!accel.begin()) {
  104. if(DEBUG) {
  105. Serial.println("[WARNING] Could not find ADXL345");
  106. }
  107. } else {
  108. if(DEBUG) {
  109. Serial.println("[INFO] ADXL345 started");
  110. }
  111. // Set the range to whatever is appropriate for your project
  112. accel.setRange(ADXL345_RANGE_16_G);
  113. }
  114.  
  115. // Set output power of TD1208R
  116. //akeru.setPower(14);
  117.  
  118. // Enable distance sensor using MOSFET
  119. digitalWrite(ENABLE_US, LOW);
  120.  
  121. // Get new ADXL345 sensor event while ultrasonic is warming up
  122. ADXL_measure();
  123.  
  124. // Set ADXL345 sensor into sleep mode
  125. ADXL_sleep();
  126.  
  127. // Get Ultra Sonic Sensor readings
  128. distance = (rangeSensorPW.getRange())*10;
  129.  
  130. // Shut distance sensor off again
  131. digitalWrite(ENABLE_US, HIGH);
  132.  
  133. // Get SigFox internal temparature
  134. SigFox_getTemperature();
  135.  
  136. temperature = -5;
  137. // Prepare payload by hexing current variables
  138. String t = akeru.toHex(temperature);
  139. String d = akeru.toHex(distance);
  140. String r = akeru.toHex(roll);
  141. String p = akeru.toHex(pitch);
  142.  
  143. // Concatenate all data into one single package
  144. String msg = t+d+r+p; // Put everything together
  145.  
  146. // Show current sensor values, measurement time and hex'ed packages
  147. if(DEBUG) {
  148. Serial.print("Roll: "); Serial.println(roll);
  149. Serial.print("Pitch: "); Serial.println(pitch);
  150. Serial.print("Distance: "); Serial.println(distance);
  151. Serial.println ("Sigfox message");
  152. Serial.println (d);
  153. Serial.println (r);
  154. Serial.println (p);
  155. Serial.println (t);
  156. Serial.println("Measurement time: ");
  157. Serial.print(millis() - start);
  158. Serial.println("ms");
  159. }
  160. if (akeru.sendPayload(msg)) {
  161. if(DEBUG) {
  162. Serial.println("[INFO] message was sent");
  163. }
  164. } else if (DEBUG) {
  165. Serial.println("[ERROR] Message not sent");
  166. }
  167.  
  168.  
  169. delay(100);
  170.  
  171. for (int second = 0; second < TRANSMISSION_INTERVAL; second++)
  172. {
  173. LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  174. }
  175.  
  176. }
  177.  
  178.  
  179. void displaySensorDetails(void)
  180. {
  181. sensor_t sensor;
  182. accel.getSensor(&sensor);
  183. Serial.println("------------------------------------");
  184. Serial.print ("Sensor: "); Serial.println(sensor.name);
  185. Serial.print ("Driver Ver: "); Serial.println(sensor.version);
  186. Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
  187. Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" m/s^2");
  188. Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" m/s^2");
  189. Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" m/s^2");
  190. Serial.println("------------------------------------");
  191. Serial.println("");
  192. }
  193.  
  194. void displayDataRate(void)
  195. {
  196. Serial.print ("Data Rate: ");
  197.  
  198. switch(accel.getDataRate())
  199. {
  200. case ADXL345_DATARATE_3200_HZ:
  201. Serial.print ("3200 ");
  202. break;
  203. case ADXL345_DATARATE_1600_HZ:
  204. Serial.print ("1600 ");
  205. break;
  206. case ADXL345_DATARATE_800_HZ:
  207. Serial.print ("800 ");
  208. break;
  209. case ADXL345_DATARATE_400_HZ:
  210. Serial.print ("400 ");
  211. break;
  212. case ADXL345_DATARATE_200_HZ:
  213. Serial.print ("200 ");
  214. break;
  215. case ADXL345_DATARATE_100_HZ:
  216. Serial.print ("100 ");
  217. break;
  218. case ADXL345_DATARATE_50_HZ:
  219. Serial.print ("50 ");
  220. break;
  221. case ADXL345_DATARATE_25_HZ:
  222. Serial.print ("25 ");
  223. break;
  224. case ADXL345_DATARATE_12_5_HZ:
  225. Serial.print ("12.5 ");
  226. break;
  227. case ADXL345_DATARATE_6_25HZ:
  228. Serial.print ("6.25 ");
  229. break;
  230. case ADXL345_DATARATE_3_13_HZ:
  231. Serial.print ("3.13 ");
  232. break;
  233. case ADXL345_DATARATE_1_56_HZ:
  234. Serial.print ("1.56 ");
  235. break;
  236. case ADXL345_DATARATE_0_78_HZ:
  237. Serial.print ("0.78 ");
  238. break;
  239. case ADXL345_DATARATE_0_39_HZ:
  240. Serial.print ("0.39 ");
  241. break;
  242. case ADXL345_DATARATE_0_20_HZ:
  243. Serial.print ("0.20 ");
  244. break;
  245. case ADXL345_DATARATE_0_10_HZ:
  246. Serial.print ("0.10 ");
  247. break;
  248. default:
  249. Serial.print ("???? ");
  250. break;
  251. }
  252. Serial.println(" Hz");
  253. }
  254.  
  255. void displayRange(void)
  256. {
  257. Serial.print ("Range: +/- ");
  258.  
  259. switch(accel.getRange())
  260. {
  261. case ADXL345_RANGE_16_G:
  262. Serial.print ("16 ");
  263. break;
  264. case ADXL345_RANGE_8_G:
  265. Serial.print ("8 ");
  266. break;
  267. case ADXL345_RANGE_4_G:
  268. Serial.print ("4 ");
  269. break;
  270. case ADXL345_RANGE_2_G:
  271. Serial.print ("2 ");
  272. break;
  273. default:
  274. Serial.print ("?? ");
  275. break;
  276. }
  277. Serial.println(" g");
  278. }
  279.  
  280. void ADXL_measure(void) {
  281.  
  282. delay(1000);
  283. sensors_event_t event;
  284. accel.getEvent(&event);
  285.  
  286. if(DEBUG) {
  287. Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" ");
  288. Serial.print("Y: "); Serial.print(event.acceleration.y); Serial.print(" ");
  289. Serial.print("Z: "); Serial.print(event.acceleration.z); Serial.print(" ");Serial.println("m/s^2 ");
  290. }
  291.  
  292. x_Buff = event.acceleration.x;
  293. y_Buff = event.acceleration.y;
  294. z_Buff = event.acceleration.z;
  295.  
  296. roll = atan2(y_Buff , z_Buff) * 57.3;
  297. pitch = atan2((- x_Buff) , sqrt(y_Buff * y_Buff + z_Buff * z_Buff)) * 57.3;
  298. }
  299.  
  300. void ADXL_sleep(void) {
  301. accel.writeRegister(ADXL345_REG_POWER_CTL, 0);
  302. }
  303.  
  304. void SigFox_getTemperature(void)
  305. {
  306.  
  307.  
  308. if (akeru.getTemperature(&temperature))
  309. {
  310. if(DEBUG)
  311. {
  312. Serial.print("Temperature = ");
  313. Serial.print(temperature);
  314. Serial.println(" C");
  315. }
  316. }
  317. else
  318. {
  319. temperature=99;
  320. Serial.println("Temperature KO");
  321. }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement