Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. // Library include
  2. #include <WaspSensorGas_v30.h>
  3. #include <WaspFrame.h>
  4. #include <WaspXBee802.h>
  5.  
  6. // Destination MAC address
  7. //////////////////////////////////////////
  8. char RX_ADDRESS[] = "0013A20041678B8C";
  9.  
  10.  
  11. // Define the Waspmote ID
  12. char WASPMOTE_ID[] = "Gas_Board";
  13.  
  14.  
  15.  
  16. // define variable
  17. uint8_t error;
  18.  
  19.  
  20. // PAN (Personal Area Network) Identifier
  21. uint8_t panID[2] = {0x12,0x34};
  22.  
  23.  
  24. // Define Freq Channel to be set:
  25. // Center Frequency = 2.405 + (CH - 11d) * 5 MHz
  26. // Range: 0x0B - 0x1A (XBee)
  27. // Range: 0x0C - 0x17 (XBee-PRO)
  28. uint8_t channel = 0x0F;
  29.  
  30.  
  31.  
  32. // Define the Encryption mode: 1 (enabled) or 0 (disabled)
  33. uint8_t encryptionMode = 0;
  34.  
  35.  
  36. // Define the AES 16-byte Encryption Key
  37. char encryptionKey[] = "WaspmoteLinkKey!";
  38.  
  39.  
  40. // NO2 Sensor must be connected physically in SOCKET_3
  41. NO2SensorClass NO2Sensor;
  42.  
  43.  
  44. // CO Sensor must be connected physically in SOCKET_4
  45. COSensorClass COSensor;
  46.  
  47.  
  48. // VOC Sensor must be connected in SOCKET_5
  49. VOCSensorClass VOCSensor;
  50.  
  51. // Calibration voltages obtained during calibration process (in KOHMs)
  52. #define POINT1_RES_NO2 45.25 // <-- Rs at normal concentration in air
  53. #define POINT2_RES_NO2 25.50
  54. #define POINT3_RES_NO2 3.55
  55.  
  56.  
  57. // Calibration resistances obtained during calibration process
  58. #define POINT1_RES_CO 230.30 // <-- Ro Resistance at 100 ppm. Necessary value.
  59. #define POINT2_RES_CO 40.665 //
  60. #define POINT3_RES_CO 20.300 //
  61. #define POINT1_RES_VOC 230.30 // <-- Ro Resistance at 100 ppm. Necessary value.
  62. #define POINT2_RES_VOC 40.665 //
  63. #define POINT3_RES_VOC 20.300 //
  64.  
  65.  
  66. // Concentrations used in calibration process
  67. #define POINT1_PPM_NO2 10.0 // <-- Normal concentration in air
  68. #define POINT2_PPM_NO2 50.0
  69. #define POINT3_PPM_NO2 100.0
  70.  
  71.  
  72. // Concentratios used in calibration process
  73. #define POINT1_PPM_CO 100.0 // <--- Ro value at this concentration
  74. #define POINT2_PPM_CO 300.0 //
  75. #define POINT3_PPM_CO 1000.0 //
  76. #define POINT1_PPM_VOC 100.0 // <--- Ro value at this concentration
  77. #define POINT2_PPM_VOC 300.0
  78. #define POINT3_PPM_VOC 1000.0
  79.  
  80.  
  81.  
  82. // Define the number of calibration points
  83. #define numPoints 3
  84.  
  85.  
  86. float temperature; // Stores the temperature in ΒΊC
  87. float humidity; // Stores the realitve humidity in %RH
  88. float pressure; // Stores the pressure in Pa
  89.  
  90. float NO2resValues[] = {POINT1_RES_NO2, POINT2_RES_NO2, POINT3_RES_NO2};
  91.  
  92. float resValues[] = {POINT1_RES_CO, POINT2_RES_CO, POINT3_RES_CO};
  93.  
  94. float VOCresValues[] = {POINT1_RES_VOC, POINT2_RES_VOC, POINT3_RES_VOC};
  95.  
  96. float NO2concentrations[] = {POINT1_PPM_NO2, POINT2_PPM_NO2, POINT3_PPM_NO2};
  97.  
  98. float concentrations[] = {POINT1_PPM_CO, POINT2_PPM_CO, POINT3_PPM_CO};
  99.  
  100. float VOCconcentrations[] = {POINT1_PPM_VOC, POINT2_PPM_VOC, POINT3_PPM_VOC};
  101.  
  102.  
  103.  
  104. void setup()
  105.  
  106. {
  107.  
  108. // Configure the USB port
  109. USB.ON();
  110.  
  111. USB.println(F("-------------------------------"));
  112.  
  113. USB.println(F("Configure XBee 802.15.4"));
  114.  
  115. USB.println(F("-------------------------------"));
  116.  
  117.  
  118. // init XBee
  119. xbee802.ON();
  120.  
  121. /////////////////////////////////////
  122. // 1. set channel
  123. /////////////////////////////////////
  124. xbee802.setChannel( channel );
  125.  
  126.  
  127. // check at commmand execution flag
  128. if( xbee802.error_AT == 0 )
  129.  
  130. {
  131.  
  132. USB.print(F("1. Channel set OK to: 0x"));
  133. USB.printHex( xbee802.channel );
  134. USB.println();
  135.  
  136. }
  137.  
  138. else
  139.  
  140. {
  141.  
  142. USB.println(F("1. Error calling 'setChannel()'"));
  143.  
  144. }
  145.  
  146.  
  147.  
  148. /////////////////////////////////////
  149. // 2. set PANID
  150. /////////////////////////////////////
  151. xbee802.setPAN( panID );
  152.  
  153.  
  154. // check the AT commmand execution flag
  155. if( xbee802.error_AT == 0 )
  156.  
  157. {
  158.  
  159. USB.print(F("2. PAN ID set OK to: 0x"));
  160. USB.printHex( xbee802.PAN_ID[0] );
  161. USB.printHex( xbee802.PAN_ID[1] );
  162. USB.println();
  163.  
  164. }
  165.  
  166. else
  167.  
  168. {
  169.  
  170. USB.println(F("2. Error calling 'setPAN()'"));
  171.  
  172. }
  173.  
  174.  
  175.  
  176. /////////////////////////////////////
  177. // 3. set encryption mode (1:enable; 0:disable)
  178. /////////////////////////////////////
  179. xbee802.setEncryptionMode( encryptionMode );
  180.  
  181.  
  182.  
  183. // check the AT commmand execution flag
  184. if( xbee802.error_AT == 0 )
  185.  
  186. {
  187.  
  188. USB.print(F("3. AES encryption configured (1:enabled; 0:disabled):"));
  189. USB.println( xbee802.encryptMode, DEC );
  190.  
  191. }
  192.  
  193. else
  194.  
  195. {
  196.  
  197. USB.println(F("3. Error calling 'setEncryptionMode()'"));
  198.  
  199. }
  200.  
  201.  
  202.  
  203. /////////////////////////////////////
  204. // 4. set encryption key
  205. /////////////////////////////////////
  206. xbee802.setLinkKey( encryptionKey );
  207.  
  208.  
  209.  
  210. // check the AT commmand execution flag
  211. if( xbee802.error_AT == 0 )
  212.  
  213. {
  214.  
  215. USB.println(F("4. AES encryption key set OK"));
  216.  
  217. }
  218.  
  219. else
  220.  
  221. {
  222.  
  223. USB.println(F("4. Error calling 'setLinkKey()'"));
  224.  
  225. }
  226.  
  227.  
  228.  
  229. /////////////////////////////////////
  230. // 5. write values to XBee module memory
  231. /////////////////////////////////////
  232. xbee802.writeValues();
  233.  
  234.  
  235.  
  236. // check the AT commmand execution flag
  237. if( xbee802.error_AT == 0 )
  238.  
  239. {
  240.  
  241. USB.println(F("5. Changes stored OK"));
  242.  
  243. }
  244.  
  245. else
  246.  
  247. {
  248.  
  249. USB.println(F("5. Error calling 'writeValues()'"));
  250.  
  251. }
  252.  
  253.  
  254.  
  255. USB.println(F("-------------------------------"));
  256. USB.println(F("Sending packets example"));
  257.  
  258.  
  259.  
  260. // store Waspmote identifier in EEPROM memory
  261. frame.setID( WASPMOTE_ID );
  262.  
  263.  
  264. // Turn on the sensor board
  265.  
  266. Gases.ON();
  267. delay(100);
  268. NO2Sensor.ON();
  269. COSensor.ON();
  270. VOCSensor.ON();
  271.  
  272.  
  273. // Calculate the slope and the intersection of the logarithmic function
  274. NO2Sensor.setCalibrationPoints(NO2resValues, NO2concentrations, numPoints);
  275.  
  276.  
  277.  
  278. // Calculate the slope and the intersection of the logarithmic function
  279. COSensor.setCalibrationPoints(resValues, concentrations, numPoints);
  280.  
  281.  
  282.  
  283. // Calculate the slope and the intersection of the logarithmic function
  284. VOCSensor.setCalibrationPoints(VOCresValues, VOCconcentrations, numPoints);
  285. }
  286.  
  287.  
  288.  
  289.  
  290. void loop()
  291.  
  292.  
  293.  
  294. {
  295.  
  296. /////////////////////////////////////
  297. // 1. get channel
  298. /////////////////////////////////////
  299. xbee802.getChannel();
  300.  
  301. USB.print(F("channel: "));
  302.  
  303. USB.printHex(xbee802.channel);
  304.  
  305. USB.println();
  306.  
  307.  
  308.  
  309. /////////////////////////////////////
  310. // 2. get PANID
  311. /////////////////////////////////////
  312.  
  313. xbee802.getPAN();
  314.  
  315. USB.print(F("panid: "));
  316.  
  317. USB.printHex(xbee802.PAN_ID[0]);
  318.  
  319. USB.printHex(xbee802.PAN_ID[1]);
  320.  
  321. USB.println();
  322.  
  323.  
  324.  
  325. ////////////////////////////////////
  326. // 3. get encryption mode (1:enable; 0:disable)
  327. /////////////////////////////////////
  328.  
  329. xbee802.getEncryptionMode();
  330.  
  331. USB.print(F("encryption mode: "));
  332.  
  333. USB.printHex(xbee802.encryptMode);
  334.  
  335. USB.println();
  336.  
  337. USB.println(F("-------------------------------"));
  338.  
  339. delay(3000);
  340.  
  341.  
  342.  
  343.  
  344.  
  345. //////////////////////////////////////////
  346. // 2. Read sensors
  347. //////////////////////////////////////////
  348.  
  349. temperature = Gases.getTemperature();
  350. humidity = Gases.getHumidity();
  351. pressure = Gases.getPressure();
  352.  
  353. float NO2Vol = NO2Sensor.readVoltage();
  354. float coVol = COSensor.readVoltage();
  355. float vocVol = VOCSensor.readVoltage();
  356.  
  357. float NO2Res = NO2Sensor.readResistance(NO2Vol); // Resistance of the sensor
  358. float coRes = COSensor.readResistance(coVol);
  359. float vocRes = VOCSensor.readResistance(vocVol); // Resistance of the sensor
  360.  
  361.  
  362. float NO2PPM = NO2Sensor.readConcentration(NO2Res); // PPM value of NO2
  363. float coPPM = COSensor.readConcentration(coRes); // PPM value of CO
  364. float vocPPM = VOCSensor.readConcentration(vocRes); // PPM value of VOC
  365.  
  366.  
  367. char node_ID[] = "TRHP_CO_VOC_NO2_example";
  368.  
  369.  
  370. // Print of the results
  371.  
  372. USB.print(F(" Temperature: "));
  373. USB.print(temperature);
  374.  
  375. USB.println(F(" Celsius Degrees |"));
  376. USB.print(F(" Humidity : "));
  377. USB.print(humidity);
  378. USB.println(F(" %RH"));
  379.  
  380. USB.print(F(" Pressure : "));
  381. USB.print(pressure);
  382. USB.println(F(" Pa"));
  383.  
  384. USB.print(F(" CO Sensor Voltage: "));
  385. USB.print(coVol);
  386. USB.println(F(" mV |"));
  387.  
  388.  
  389. USB.print(F(" VOC Sensor Voltage: "));
  390. USB.print(vocVol);
  391. USB.println(F(" V |"));
  392.  
  393.  
  394. USB.print(F(" NO2 Sensor Voltage: "));
  395. USB.print(NO2Vol);
  396. USB.println(F(" V |"));
  397.  
  398.  
  399. USB.print(F(" CO Sensor Resistance: "));
  400. USB.print(coRes);
  401. USB.println(F(" Ohms |"));
  402.  
  403.  
  404. USB.print(F(" VOC Sensor Resistance: "));
  405. USB.print(vocRes);
  406. USB.println(F(" Ohms |"));
  407.  
  408.  
  409. USB.print(F(" NO2 Sensor Resistance: "));
  410. USB.print(NO2Res);
  411. USB.println(F(" Ohms |"));
  412.  
  413.  
  414. USB.print(F(" CO concentration Estimated: "));
  415. USB.print(coPPM);
  416. USB.println(F(" ppm"));
  417.  
  418.  
  419. USB.print(F(" VOC concentration Estimated: "));
  420. USB.print(vocPPM);
  421. USB.println(F(" ppm"));
  422.  
  423.  
  424. USB.print(F(" NO2 concentration Estimated: "));
  425. USB.print(NO2PPM);
  426. USB.println(F(" ppm"));
  427. USB.println();
  428.  
  429.  
  430.  
  431. ///////////////////////////////////////////
  432. // 3. Create BINARY frame
  433. ///////////////////////////////////////////
  434. frame.createFrame(BINARY);
  435.  
  436. frame.addSensor(SENSOR_GASES_TC, Gases.getTemperature());
  437.  
  438. frame.addSensor(SENSOR_GASES_HUM, Gases.getHumidity());
  439.  
  440. frame.addSensor(SENSOR_GASES_PRES, Gases.getPressure());
  441.  
  442. frame.addSensor(SENSOR_GASES_NO2, NO2Sensor.readConcentration(NO2Res));
  443.  
  444. frame.addSensor(SENSOR_GASES_CO, COSensor.readConcentration(coRes));
  445.  
  446. frame.addSensor(SENSOR_GASES_VOC, VOCSensor.readConcentration(vocRes));
  447.  
  448. delay(5000);
  449.  
  450.  
  451. // Show the frame
  452. //frame.showFrame();
  453.  
  454. // send XBee packet
  455. error = xbee802.send( RX_ADDRESS, frame.buffer, frame.length );
  456.  
  457.  
  458. // check TX flag
  459. if( error == 0 )
  460.  
  461. {
  462.  
  463.  
  464.  
  465. USB.println(F("send ok"));
  466. // blink green LED
  467. Utils.blinkGreenLED();
  468.  
  469. }
  470.  
  471. else
  472.  
  473. {
  474.  
  475.  
  476.  
  477. USB.println(F("send error"));
  478.  
  479. // blink red LED
  480. Utils.blinkRedLED();
  481.  
  482. }
  483.  
  484. delay(2000);
  485.  
  486. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement