gyhn

Untitled

Nov 18th, 2019
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.95 KB | None | 0 0
  1. // BME680 gas sensor
  2. // https://github.com/BoschSensortec/BSEC-Arduino-library
  3. // arduino 1.8.10, esp8266 library 2.6.1
  4. // midification apply to eagle.app.v6.common.ld and platform.txt as explained in github/readme
  5. // ESP12E-----------BME680
  6. // GND------------>GND
  7. // 3.3V------------>3.3V
  8. // D2------------>SDA
  9. // D1------------>SCL
  10.  
  11. // connection wifi
  12. #include <ESP8266WiFi.h>
  13.  
  14. const char *ssid = "******";
  15. const char *password = "*******";
  16. WiFiClient wifiClient; // needed to call  WiFi.begin, WiFi.status, WL_CONNECTED...
  17.  
  18. // set up for sensor BME860
  19.  
  20. #include <EEPROM.h>
  21. #include <bsec.h>
  22.  
  23. /* Configure the BSEC library with information about the sensor
  24.  *  18v/33v = Voltage at Vdd. 1.8V or 3.3V
  25.  *  3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP
  26.  *  4d/28d = Operating age of the sensor in days
  27.  *  generic_18v_3s_4d
  28.  *  generic_18v_3s_28d
  29.  *  generic_18v_300s_4d
  30.  *  generic_18v_300s_28d
  31.  *  generic_33v_3s_4d
  32.  *  generic_33v_3s_28d
  33.  *  generic_33v_300s_4d
  34.  *  generic_33v_300s_28d
  35.  */
  36. const uint8_t bsec_config_iaq[] = {
  37. #include "config/generic_33v_3s_4d/bsec_iaq.txt"
  38. };
  39.  
  40. #define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // 360 minutes - 4 times a day
  41.  
  42. // Helper functions declarations
  43. void checkIaqSensorStatus(void);
  44. void errLeds(void);
  45. void loadState(void);
  46. void updateState(void);
  47.  
  48. // Create an object of the class Bsec
  49. Bsec iaqSensor;
  50. uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE] = {0};
  51. uint16_t stateUpdateCounter = 0;
  52.  
  53. String output;
  54.  
  55. // setup()
  56. void setup(void)
  57. {
  58.     // initialization serial monitor
  59.     EEPROM.begin(BSEC_MAX_STATE_BLOB_SIZE + 1); // 1st address for the length
  60.     Serial.begin(115200);
  61.  
  62.     Wire.begin();
  63.     // Wire.begin(I2C_PIN_SDA, I2C_PIN_SCL); // not yet test
  64.  
  65.     setup_wifi();
  66.  
  67.     // initialize bme sensor
  68.     iaqSensor.begin(BME680_I2C_ADDR_PRIMARY, Wire);
  69.     output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
  70.     Serial.println(output);
  71.     checkIaqSensorStatus();
  72.  
  73.     iaqSensor.setConfig(bsec_config_iaq);
  74.     checkIaqSensorStatus();
  75.  
  76.     loadState();
  77.  
  78.     bsec_virtual_sensor_t sensorList[7] = {
  79.         BSEC_OUTPUT_RAW_TEMPERATURE,
  80.         BSEC_OUTPUT_RAW_PRESSURE,
  81.         BSEC_OUTPUT_RAW_HUMIDITY,
  82.         BSEC_OUTPUT_RAW_GAS,
  83.         BSEC_OUTPUT_IAQ,
  84.         BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
  85.         BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
  86.     };
  87.  
  88.     iaqSensor.updateSubscription(sensorList, 7, BSEC_SAMPLE_RATE_LP);
  89.     checkIaqSensorStatus();
  90.  
  91.     // Print the header
  92.     output = "Timestamp [ms], raw temperature [°C], pressure [hPa], raw relative humidity [%], gas [Ohm], IAQ, IAQ accuracy, temperature [°C], relative humidity [%]";
  93.     Serial.println(output);
  94. }
  95.  
  96. //   Loop():
  97. // void loop(void)
  98. void loop()
  99. {
  100.     unsigned long time_trigger = millis();
  101.     if (iaqSensor.run())
  102.     { // If new data is available
  103.         output = String(time_trigger);
  104.         output += ", " + String(iaqSensor.rawTemperature);
  105.         output += ", " + String(iaqSensor.pressure);
  106.         output += ", " + String(iaqSensor.rawHumidity);
  107.         output += ", " + String(iaqSensor.gasResistance);
  108.         output += ", " + String(iaqSensor.iaq);
  109.         output += ", " + String(iaqSensor.iaqAccuracy);
  110.         output += ", " + String(iaqSensor.temperature);
  111.         output += ", " + String(iaqSensor.humidity);
  112.  
  113.         Serial.println(output);
  114.  
  115.         // update state
  116.         updateState();
  117.     }
  118.     else
  119.     {
  120.         checkIaqSensorStatus();
  121.     }
  122. }
  123.  
  124. //  Helper function definitions
  125. void checkIaqSensorStatus(void)
  126. {
  127.     if (iaqSensor.status != BSEC_OK)
  128.     {
  129.         if (iaqSensor.status < BSEC_OK)
  130.         {
  131.             output = "BSEC error code : " + String(iaqSensor.status);
  132.             Serial.println(output);
  133.             for (;;)
  134.                 errLeds(); /* Halt in case of failure */
  135.         }
  136.         else
  137.         {
  138.             output = "BSEC warning code : " + String(iaqSensor.status);
  139.             Serial.println(output);
  140.         }
  141.     }
  142.  
  143.     if (iaqSensor.bme680Status != BME680_OK)
  144.     {
  145.         if (iaqSensor.bme680Status < BME680_OK)
  146.         {
  147.             output = "BME680 error code : " + String(iaqSensor.bme680Status);
  148.             Serial.println(output);
  149.             for (;;)
  150.                 errLeds(); /* Halt in case of failure */
  151.         }
  152.         else
  153.         {
  154.             output = "BME680 warning code : " + String(iaqSensor.bme680Status);
  155.             Serial.println(output);
  156.         }
  157.     }
  158. }
  159.  
  160. void errLeds(void)
  161. {
  162.     pinMode(LED_BUILTIN, OUTPUT);
  163.     digitalWrite(LED_BUILTIN, HIGH);
  164.     delay(100);
  165.     digitalWrite(LED_BUILTIN, LOW);
  166.     delay(100);
  167. }
  168.  
  169. void loadState(void)
  170. {
  171.     if (EEPROM.read(0) == BSEC_MAX_STATE_BLOB_SIZE)
  172.     {
  173.         // Existing state in EEPROM
  174.         Serial.println("Reading state from EEPROM");
  175.  
  176.         for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++)
  177.         {
  178.             bsecState[i] = EEPROM.read(i + 1);
  179.             Serial.println(bsecState[i], HEX);
  180.         }
  181.  
  182.         iaqSensor.setState(bsecState);
  183.         checkIaqSensorStatus();
  184.     }
  185.     else
  186.     {
  187.         // Erase the EEPROM with zeroes
  188.         Serial.println("Erasing EEPROM");
  189.  
  190.         for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE + 1; i++)
  191.             EEPROM.write(i, 0);
  192.  
  193.         EEPROM.commit();
  194.     }
  195. }
  196.  
  197. void updateState(void)
  198. {
  199.     bool update = false;
  200.     /* Set a trigger to save the state. Here, the state is saved every STATE_SAVE_PERIOD with the first state being saved once the algorithm achieves full calibration, i.e. iaqAccuracy = 3 */
  201.     if (stateUpdateCounter == 0)
  202.     {
  203.         if (iaqSensor.iaqAccuracy >= 3)
  204.         {
  205.             update = true;
  206.             stateUpdateCounter++;
  207.         }
  208.     }
  209.     else
  210.     {
  211.         /* Update every STATE_SAVE_PERIOD milliseconds */
  212.         if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis())
  213.         {
  214.             update = true;
  215.             stateUpdateCounter++;
  216.         }
  217.     }
  218.  
  219.     if (update)
  220.     {
  221.         iaqSensor.getState(bsecState);
  222.         checkIaqSensorStatus();
  223.  
  224.         Serial.println("Writing state to EEPROM");
  225.  
  226.         for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++)
  227.         {
  228.             EEPROM.write(i + 1, bsecState[i]);
  229.             Serial.println(bsecState[i], HEX);
  230.         }
  231.  
  232.         EEPROM.write(0, BSEC_MAX_STATE_BLOB_SIZE);
  233.         EEPROM.commit();
  234.     }
  235. }
  236.  
  237. // wifi connection
  238.  
  239. void setup_wifi()
  240. {
  241.     delay(10);
  242.     // info display in debug
  243.  
  244.     Serial.println();
  245.     Serial.print("Connecting to ");
  246.     Serial.println(ssid);
  247.     // connect to wifi define above
  248.     WiFi.begin(ssid, password);
  249.  
  250.     // keep trying to connect while not connected
  251.     while (WiFi.status() != WL_CONNECTED)
  252.     {
  253.         delay(500);
  254.  
  255.         Serial.print(".");
  256.     }
  257.  
  258.     WiFi.setAutoReconnect(true);
  259. }
Advertisement
Add Comment
Please, Sign In to add comment