Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BME680 gas sensor
- // https://github.com/BoschSensortec/BSEC-Arduino-library
- // arduino 1.8.10, esp8266 library 2.6.1
- // midification apply to eagle.app.v6.common.ld and platform.txt as explained in github/readme
- // ESP12E-----------BME680
- // GND------------>GND
- // 3.3V------------>3.3V
- // D2------------>SDA
- // D1------------>SCL
- // connection wifi
- #include <ESP8266WiFi.h>
- const char *ssid = "******";
- const char *password = "*******";
- WiFiClient wifiClient; // needed to call WiFi.begin, WiFi.status, WL_CONNECTED...
- // set up for sensor BME860
- #include <EEPROM.h>
- #include <bsec.h>
- /* Configure the BSEC library with information about the sensor
- * 18v/33v = Voltage at Vdd. 1.8V or 3.3V
- * 3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP
- * 4d/28d = Operating age of the sensor in days
- * generic_18v_3s_4d
- * generic_18v_3s_28d
- * generic_18v_300s_4d
- * generic_18v_300s_28d
- * generic_33v_3s_4d
- * generic_33v_3s_28d
- * generic_33v_300s_4d
- * generic_33v_300s_28d
- */
- const uint8_t bsec_config_iaq[] = {
- #include "config/generic_33v_3s_4d/bsec_iaq.txt"
- };
- #define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // 360 minutes - 4 times a day
- // Helper functions declarations
- void checkIaqSensorStatus(void);
- void errLeds(void);
- void loadState(void);
- void updateState(void);
- // Create an object of the class Bsec
- Bsec iaqSensor;
- uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE] = {0};
- uint16_t stateUpdateCounter = 0;
- String output;
- // setup()
- void setup(void)
- {
- // initialization serial monitor
- EEPROM.begin(BSEC_MAX_STATE_BLOB_SIZE + 1); // 1st address for the length
- Serial.begin(115200);
- Wire.begin();
- // Wire.begin(I2C_PIN_SDA, I2C_PIN_SCL); // not yet test
- setup_wifi();
- // initialize bme sensor
- iaqSensor.begin(BME680_I2C_ADDR_PRIMARY, Wire);
- output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
- Serial.println(output);
- checkIaqSensorStatus();
- iaqSensor.setConfig(bsec_config_iaq);
- checkIaqSensorStatus();
- loadState();
- bsec_virtual_sensor_t sensorList[7] = {
- BSEC_OUTPUT_RAW_TEMPERATURE,
- BSEC_OUTPUT_RAW_PRESSURE,
- BSEC_OUTPUT_RAW_HUMIDITY,
- BSEC_OUTPUT_RAW_GAS,
- BSEC_OUTPUT_IAQ,
- BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
- BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
- };
- iaqSensor.updateSubscription(sensorList, 7, BSEC_SAMPLE_RATE_LP);
- checkIaqSensorStatus();
- // Print the header
- output = "Timestamp [ms], raw temperature [°C], pressure [hPa], raw relative humidity [%], gas [Ohm], IAQ, IAQ accuracy, temperature [°C], relative humidity [%]";
- Serial.println(output);
- }
- // Loop():
- // void loop(void)
- void loop()
- {
- unsigned long time_trigger = millis();
- if (iaqSensor.run())
- { // If new data is available
- output = String(time_trigger);
- output += ", " + String(iaqSensor.rawTemperature);
- output += ", " + String(iaqSensor.pressure);
- output += ", " + String(iaqSensor.rawHumidity);
- output += ", " + String(iaqSensor.gasResistance);
- output += ", " + String(iaqSensor.iaq);
- output += ", " + String(iaqSensor.iaqAccuracy);
- output += ", " + String(iaqSensor.temperature);
- output += ", " + String(iaqSensor.humidity);
- Serial.println(output);
- // update state
- updateState();
- }
- else
- {
- checkIaqSensorStatus();
- }
- }
- // Helper function definitions
- void checkIaqSensorStatus(void)
- {
- if (iaqSensor.status != BSEC_OK)
- {
- if (iaqSensor.status < BSEC_OK)
- {
- output = "BSEC error code : " + String(iaqSensor.status);
- Serial.println(output);
- for (;;)
- errLeds(); /* Halt in case of failure */
- }
- else
- {
- output = "BSEC warning code : " + String(iaqSensor.status);
- Serial.println(output);
- }
- }
- if (iaqSensor.bme680Status != BME680_OK)
- {
- if (iaqSensor.bme680Status < BME680_OK)
- {
- output = "BME680 error code : " + String(iaqSensor.bme680Status);
- Serial.println(output);
- for (;;)
- errLeds(); /* Halt in case of failure */
- }
- else
- {
- output = "BME680 warning code : " + String(iaqSensor.bme680Status);
- Serial.println(output);
- }
- }
- }
- void errLeds(void)
- {
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, HIGH);
- delay(100);
- digitalWrite(LED_BUILTIN, LOW);
- delay(100);
- }
- void loadState(void)
- {
- if (EEPROM.read(0) == BSEC_MAX_STATE_BLOB_SIZE)
- {
- // Existing state in EEPROM
- Serial.println("Reading state from EEPROM");
- for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++)
- {
- bsecState[i] = EEPROM.read(i + 1);
- Serial.println(bsecState[i], HEX);
- }
- iaqSensor.setState(bsecState);
- checkIaqSensorStatus();
- }
- else
- {
- // Erase the EEPROM with zeroes
- Serial.println("Erasing EEPROM");
- for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE + 1; i++)
- EEPROM.write(i, 0);
- EEPROM.commit();
- }
- }
- void updateState(void)
- {
- bool update = false;
- /* 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 */
- if (stateUpdateCounter == 0)
- {
- if (iaqSensor.iaqAccuracy >= 3)
- {
- update = true;
- stateUpdateCounter++;
- }
- }
- else
- {
- /* Update every STATE_SAVE_PERIOD milliseconds */
- if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis())
- {
- update = true;
- stateUpdateCounter++;
- }
- }
- if (update)
- {
- iaqSensor.getState(bsecState);
- checkIaqSensorStatus();
- Serial.println("Writing state to EEPROM");
- for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++)
- {
- EEPROM.write(i + 1, bsecState[i]);
- Serial.println(bsecState[i], HEX);
- }
- EEPROM.write(0, BSEC_MAX_STATE_BLOB_SIZE);
- EEPROM.commit();
- }
- }
- // wifi connection
- void setup_wifi()
- {
- delay(10);
- // info display in debug
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- // connect to wifi define above
- WiFi.begin(ssid, password);
- // keep trying to connect while not connected
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- WiFi.setAutoReconnect(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment