Advertisement
gyhn

Untitled

Nov 22nd, 2019
604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1.  
  2. // source : https://github.com/BoschSensortec/BSEC-Arduino-library/blob/master/examples/basic/basic.ino
  3. // https://circuits4you.com/2018/01/02/esp8266-timer-ticker-example/
  4. #include "bsec.h"
  5.  
  6. // # ============================================================== ¤ ticker
  7. // kind of multithreading (launch a function when needed, independantly of loop)
  8. #include <Ticker.h> //Ticker Library
  9. Ticker tickerMeasurement;
  10. void takeMeasurement(void);
  11.  
  12. Ticker tickerSendToOSM;
  13. void sendToOSM(void);
  14. // # ============================================================== ¤
  15.  
  16. // Helper functions declarations
  17. void checkIaqSensorStatus(void);
  18. void errLeds(void);
  19.  
  20. // Create an object of the class Bsec
  21. Bsec iaqSensor;
  22. String output;
  23.  
  24. void takeMeasurement()
  25. {
  26.     if (iaqSensor.run())
  27.     {
  28.         Serial.println("takeMeasurement");
  29.     }
  30.     else
  31.     {
  32.         checkIaqSensorStatus();
  33.     }
  34. }
  35.  
  36. void sendToOSM()
  37. {
  38.     // output = String(time_trigger);
  39.     output += ", " + String(iaqSensor.rawTemperature);
  40.     output += ", " + String(iaqSensor.pressure);
  41.     output += ", " + String(iaqSensor.rawHumidity);
  42.     output += ", " + String(iaqSensor.gasResistance);
  43.     output += ", " + String(iaqSensor.iaq);
  44.     output += ", " + String(iaqSensor.iaqAccuracy);
  45.     output += ", " + String(iaqSensor.temperature);
  46.     output += ", " + String(iaqSensor.humidity);
  47.     output += ", " + String(iaqSensor.staticIaq);
  48.     output += ", " + String(iaqSensor.co2Equivalent);
  49.     output += ", " + String(iaqSensor.breathVocEquivalent);
  50.     Serial.println(output);
  51. }
  52.  
  53. // Entry point for the example
  54. void setup(void)
  55. {
  56.     Serial.begin(115200);
  57.     Wire.begin();
  58.     Serial.println("test.ino : based on basic.ino");
  59.     iaqSensor.begin(BME680_I2C_ADDR_PRIMARY, Wire);
  60.     output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
  61.     Serial.println(output);
  62.     checkIaqSensorStatus();
  63.  
  64.     bsec_virtual_sensor_t sensorList[10] = {
  65.         BSEC_OUTPUT_RAW_TEMPERATURE,
  66.         BSEC_OUTPUT_RAW_PRESSURE,
  67.         BSEC_OUTPUT_RAW_HUMIDITY,
  68.         BSEC_OUTPUT_RAW_GAS,
  69.         BSEC_OUTPUT_IAQ,
  70.         BSEC_OUTPUT_STATIC_IAQ,
  71.         BSEC_OUTPUT_CO2_EQUIVALENT,
  72.         BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
  73.         BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
  74.         BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
  75.     };
  76.  
  77.     iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
  78.     checkIaqSensorStatus();
  79.  
  80.     // Print the header
  81.     output = "Timestamp [ms], raw temperature [°C], pressure [hPa], raw relative humidity [%], gas [Ohm], IAQ, IAQ accuracy, temperature [°C], relative humidity [%], Static IAQ, CO2 equivalent, breath VOC equivalent";
  82.     Serial.println(output);
  83.  
  84.     // Initialize Ticker every 3s
  85.     tickerMeasurement.attach(3, takeMeasurement); //Use <strong>attach_ms</strong> if you need time in ms
  86.     tickerSendToOSM.attach(3, sendToOSM);         //Use <strong>attach_ms</strong> if you need time in ms
  87. }
  88.  
  89. // Function that is looped forever
  90. void loop(void)
  91. {
  92.     // unsigned long time_trigger = millis();
  93.     // if (iaqSensor.run())
  94.     // { // If new data is available
  95.     //     output = String(time_trigger);
  96.     //     output += ", " + String(iaqSensor.rawTemperature);
  97.     //     output += ", " + String(iaqSensor.pressure);
  98.     //     output += ", " + String(iaqSensor.rawHumidity);
  99.     //     output += ", " + String(iaqSensor.gasResistance);
  100.     //     output += ", " + String(iaqSensor.iaq);
  101.     //     output += ", " + String(iaqSensor.iaqAccuracy);
  102.     //     output += ", " + String(iaqSensor.temperature);
  103.     //     output += ", " + String(iaqSensor.humidity);
  104.     //     output += ", " + String(iaqSensor.staticIaq);
  105.     //     output += ", " + String(iaqSensor.co2Equivalent);
  106.     //     output += ", " + String(iaqSensor.breathVocEquivalent);
  107.     //     Serial.println(output);
  108.     //     // delay(3000); // added https://community.bosch-sensortec.com/t5/MEMS-sensors-forum/BME680-warning-code-2-and-BME680-error-code-2-on-NodeMcu-esp8266/m-p/10524/highlight/true#M1869
  109.     // }
  110.     // else
  111.     // {
  112.     //     checkIaqSensorStatus();
  113.     // }
  114. }
  115.  
  116. // Helper function definitions
  117. void checkIaqSensorStatus(void)
  118. {
  119.     if (iaqSensor.status != BSEC_OK)
  120.     {
  121.         if (iaqSensor.status < BSEC_OK)
  122.         {
  123.             output = "BSEC error code : " + String(iaqSensor.status);
  124.             Serial.println(output);
  125.             for (;;)
  126.                 errLeds(); /* Halt in case of failure */
  127.         }
  128.         else
  129.         {
  130.             output = "BSEC warning code : " + String(iaqSensor.status);
  131.             Serial.println(output);
  132.         }
  133.     }
  134.  
  135.     if (iaqSensor.bme680Status != BME680_OK)
  136.     {
  137.         if (iaqSensor.bme680Status < BME680_OK)
  138.         {
  139.             output = "BME680 error code : " + String(iaqSensor.bme680Status);
  140.             Serial.println(output);
  141.             for (;;)
  142.                 errLeds(); /* Halt in case of failure */
  143.         }
  144.         else
  145.         {
  146.             output = "BME680 warning code : " + String(iaqSensor.bme680Status);
  147.             Serial.println(output);
  148.         }
  149.     }
  150. }
  151.  
  152. void errLeds(void)
  153. {
  154.     pinMode(LED_BUILTIN, OUTPUT);
  155.     digitalWrite(LED_BUILTIN, HIGH);
  156.     delay(100);
  157.     digitalWrite(LED_BUILTIN, LOW);
  158.     delay(100);
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement