BME280 integration (PR #309)
This commit is contained in:
		
							parent
							
								
									8402dc0948
								
							
						
					
					
						commit
						bfa28d4139
					
				| @ -49,7 +49,7 @@ Depending on board hardware following features are supported: | ||||
| - Silicon unique ID | ||||
| - Battery voltage monitoring | ||||
| - GPS (Generic serial NMEA, or Quectel L76 I2C) | ||||
| - Environmental sensor (Bosch BME680 I2C) | ||||
| - Environmental sensor (Bosch BME280/BME680 I2C) | ||||
| - Real Time Clock (Maxim DS3231 I2C) | ||||
| - IF482 (serial) and DCF77 (gpio) time telegram generator | ||||
| 
 | ||||
|  | ||||
| @ -1,17 +0,0 @@ | ||||
| #ifndef _BME280_H | ||||
| #define _BME280_H | ||||
| 
 | ||||
| #include "globals.h" | ||||
| #include <Wire.h> | ||||
| #include <Adafruit_Sensor.h> | ||||
| #include <Adafruit_BME280.h> | ||||
| #include "../lib/Bosch-BSEC/src/bsec.h" | ||||
| 
 | ||||
| extern bmeStatus_t | ||||
|     bme_status; // Make struct for storing gps data globally available
 | ||||
| extern TaskHandle_t Bme280Task; | ||||
| 
 | ||||
| int bme280_init(); | ||||
| void bme280_loop(void *pvParameters); | ||||
| 
 | ||||
| #endif | ||||
| @ -1,9 +1,15 @@ | ||||
| #ifndef _BME680MEMS_H | ||||
| #define _BME680MEMS_H | ||||
| #ifndef _BMESENSOR_H | ||||
| #define _BMESENSOR_H | ||||
| 
 | ||||
| #include "globals.h" | ||||
| #include <Wire.h> | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
| #include "../lib/Bosch-BSEC/src/bsec.h" | ||||
| #elif defined HAS_BME280 | ||||
| #include <Adafruit_Sensor.h> | ||||
| #include <Adafruit_BME280.h> | ||||
| #endif | ||||
| 
 | ||||
| extern bmeStatus_t | ||||
|     bme_status; // Make struct for storing gps data globally available
 | ||||
| @ -7,12 +7,8 @@ | ||||
| #include "spislave.h" | ||||
| #include <lmic.h> | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
| #include "bme680mems.h" | ||||
| #endif | ||||
| 
 | ||||
| #ifdef HAS_BME280 | ||||
| #include "bme280.h" | ||||
| #if (HAS_BME) | ||||
| #include "bmesensor.h" | ||||
| #endif | ||||
| 
 | ||||
| extern Ticker housekeeper; | ||||
|  | ||||
| @ -151,12 +151,8 @@ extern time_t userUTCTime; | ||||
| #include "sensor.h" | ||||
| #endif | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
| #include "bme680mems.h" | ||||
| #endif | ||||
| 
 | ||||
| #ifdef HAS_BME280 | ||||
| #include "bme280.h" | ||||
| #if (HAS_BME) | ||||
| #include "bmesensor.h" | ||||
| #endif | ||||
| 
 | ||||
| #endif | ||||
| @ -6,7 +6,7 @@ | ||||
| 
 | ||||
| ; ---> SELECT TARGET PLATFORM HERE! <--- | ||||
| [platformio] | ||||
| ;env_default = generic | ||||
| env_default = generic | ||||
| ;env_default = ebox | ||||
| ;env_default = eboxtube | ||||
| ;env_default = heltec | ||||
| @ -14,7 +14,7 @@ | ||||
| ;env_default = ttgov1 | ||||
| ;env_default = ttgov2 | ||||
| ;env_default = ttgov21old | ||||
| env_default = ttgov21new | ||||
| ;env_default = ttgov21new | ||||
| ;env_default = ttgobeam | ||||
| ;env_default = ttgofox | ||||
| ;env_default = lopy | ||||
| @ -53,6 +53,9 @@ lib_deps_gps = | ||||
|     TinyGPSPlus@>=1.0.2 | ||||
| lib_deps_rtc = | ||||
|     RTC@^2.3.0 | ||||
| lib_deps_sensors = | ||||
|     Adafruit Unified Sensor@^1.0.3 | ||||
|     Adafruit BME280 Library@1.0.8 | ||||
| lib_deps_basic = | ||||
|     ArduinoJson@^5.13.1 | ||||
|     Timezone@^1.2.2 | ||||
| @ -201,7 +204,7 @@ lib_deps = | ||||
|     ${common.lib_deps_lora} | ||||
|     ${common.lib_deps_display} | ||||
| build_flags = | ||||
|     ${common.build_flags_all} | ||||
|     ${common.build_flags_basic} | ||||
| upload_protocol = ${common.upload_protocol} | ||||
| extra_scripts = ${common.extra_scripts} | ||||
| monitor_speed = ${common.monitor_speed} | ||||
|  | ||||
| @ -1,63 +0,0 @@ | ||||
| #ifdef HAS_BME280 | ||||
| 
 | ||||
| #include "bme280.h" | ||||
| 
 | ||||
| // Local logging tag
 | ||||
| static const char TAG[] = __FILE__; | ||||
| 
 | ||||
| bmeStatus_t bme_status; | ||||
| TaskHandle_t Bme280Task; | ||||
| 
 | ||||
| #define SEALEVELPRESSURE_HPA (1013.25) | ||||
| 
 | ||||
| Adafruit_BME280 bme; // I2C
 | ||||
| //Adafruit_BME280 bme(BME_CS); // hardware SPI
 | ||||
| //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
 | ||||
| 
 | ||||
| // initialize BME280 sensor
 | ||||
| int bme280_init(void) { | ||||
| 
 | ||||
|   bool status; | ||||
|   // return = 0 -> error / return = 1 -> success
 | ||||
| 
 | ||||
|   // block i2c bus access
 | ||||
|   if (I2C_MUTEX_LOCK()) { | ||||
|     status = bme.begin(BME280_ADDR);   | ||||
|     if (!status) { | ||||
|         ESP_LOGE(TAG, "BME280 sensor not found"); | ||||
|         goto error; | ||||
|     } | ||||
|     ESP_LOGI(TAG, "BME280 sensor found and initialized"); | ||||
|   } else { | ||||
|     ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error"); | ||||
|     goto error; | ||||
|   } | ||||
| 
 | ||||
|   I2C_MUTEX_UNLOCK(); // release i2c bus access
 | ||||
|   return 1; | ||||
| 
 | ||||
| error: | ||||
|   I2C_MUTEX_UNLOCK(); // release i2c bus access
 | ||||
|   return 0; | ||||
| 
 | ||||
| } // bme_init()
 | ||||
| 
 | ||||
| // loop function which reads and processes data based on sensor settings
 | ||||
| void bme280_loop(void *pvParameters) { | ||||
| #ifdef HAS_BME280 | ||||
|   while (1) { | ||||
|     if (I2C_MUTEX_LOCK()) { | ||||
|       bme_status.temperature = bme.readTemperature(); | ||||
|       bme_status.pressure = (bme.readPressure() / 100.0); // conversion Pa -> hPa
 | ||||
|       // bme.readAltitude(SEALEVELPRESSURE_HPA);
 | ||||
|       bme_status.humidity = bme.readHumidity(); | ||||
|       I2C_MUTEX_UNLOCK(); | ||||
|     } | ||||
|   } | ||||
| #endif | ||||
|   ESP_LOGE(TAG, "BME task ended"); | ||||
|   vTaskDelete(Bme280Task); // should never be reached
 | ||||
| 
 | ||||
| } // bme_loop()
 | ||||
| 
 | ||||
| #endif // HAS_BME280
 | ||||
| @ -1,6 +1,6 @@ | ||||
| #ifdef HAS_BME680 | ||||
| #if (HAS_BME) | ||||
| 
 | ||||
| #include "bme680mems.h" | ||||
| #include "bmesensor.h" | ||||
| 
 | ||||
| // Local logging tag
 | ||||
| static const char TAG[] = __FILE__; | ||||
| @ -8,6 +8,9 @@ static const char TAG[] = __FILE__; | ||||
| bmeStatus_t bme_status; | ||||
| TaskHandle_t BmeTask; | ||||
| 
 | ||||
| #define SEALEVELPRESSURE_HPA (1013.25) | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
| bsec_virtual_sensor_t sensorList[10] = { | ||||
|     BSEC_OUTPUT_RAW_TEMPERATURE, | ||||
|     BSEC_OUTPUT_RAW_PRESSURE, | ||||
| @ -26,11 +29,20 @@ uint16_t stateUpdateCounter = 0; | ||||
| 
 | ||||
| Bsec iaqSensor; | ||||
| 
 | ||||
| #elif defined HAS_BME280 | ||||
| 
 | ||||
| Adafruit_BME280 bme; // I2C
 | ||||
|                      // Adafruit_BME280 bme(BME_CS); // hardware SPI
 | ||||
| // Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
 | ||||
| 
 | ||||
| #endif | ||||
| 
 | ||||
| // initialize BME680 sensor
 | ||||
| int bme_init(void) { | ||||
| 
 | ||||
|   // return = 0 -> error / return = 1 -> success
 | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
|   // block i2c bus access
 | ||||
|   if (I2C_MUTEX_LOCK()) { | ||||
| 
 | ||||
| @ -66,6 +78,26 @@ int bme_init(void) { | ||||
|     goto error; | ||||
|   } | ||||
| 
 | ||||
| #elif defined HAS_BME280 | ||||
| 
 | ||||
|   bool status; | ||||
|   // return = 0 -> error / return = 1 -> success
 | ||||
| 
 | ||||
|   // block i2c bus access
 | ||||
|   if (I2C_MUTEX_LOCK()) { | ||||
|     status = bme.begin(BME280_ADDR); | ||||
|     if (!status) { | ||||
|       ESP_LOGE(TAG, "BME280 sensor not found"); | ||||
|       goto error; | ||||
|     } | ||||
|     ESP_LOGI(TAG, "BME280 sensor found and initialized"); | ||||
|   } else { | ||||
|     ESP_LOGE(TAG, "I2c bus busy - BME280 initialization error"); | ||||
|     goto error; | ||||
|   } | ||||
| 
 | ||||
| #endif | ||||
| 
 | ||||
|   I2C_MUTEX_UNLOCK(); // release i2c bus access
 | ||||
|   return 1; | ||||
| 
 | ||||
| @ -75,6 +107,8 @@ error: | ||||
| 
 | ||||
| } // bme_init()
 | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
| 
 | ||||
| // Helper function definitions
 | ||||
| int checkIaqSensorStatus(void) { | ||||
|   int rslt = 1; // true = 1 = no error, false = 0 = error
 | ||||
| @ -97,6 +131,7 @@ int checkIaqSensorStatus(void) { | ||||
| 
 | ||||
|   return rslt; | ||||
| } // checkIaqSensorStatus()
 | ||||
| #endif | ||||
| 
 | ||||
| // loop function which reads and processes data based on sensor settings
 | ||||
| void bme_loop(void *pvParameters) { | ||||
| @ -122,12 +157,24 @@ void bme_loop(void *pvParameters) { | ||||
|       I2C_MUTEX_UNLOCK(); | ||||
|     } | ||||
|   } | ||||
| #elif defined HAS_BME280 | ||||
|   while (1) { | ||||
|     if (I2C_MUTEX_LOCK()) { | ||||
|       bme_status.temperature = bme.readTemperature(); | ||||
|       bme_status.pressure = | ||||
|           (bme.readPressure() / 100.0); // conversion Pa -> hPa
 | ||||
|       // bme.readAltitude(SEALEVELPRESSURE_HPA);
 | ||||
|       bme_status.humidity = bme.readHumidity(); | ||||
|       I2C_MUTEX_UNLOCK(); | ||||
|     } | ||||
|   } | ||||
| #endif | ||||
|   ESP_LOGE(TAG, "BME task ended"); | ||||
|   vTaskDelete(BmeTask); // should never be reached
 | ||||
| 
 | ||||
| } // bme_loop()
 | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
| void loadState(void) { | ||||
|   if (cfg.bsecstate[BSEC_MAX_STATE_BLOB_SIZE] == BSEC_MAX_STATE_BLOB_SIZE) { | ||||
|     // Existing state in NVS stored
 | ||||
| @ -167,5 +214,6 @@ void updateState(void) { | ||||
|     saveConfig(); | ||||
|   } | ||||
| } | ||||
| #endif | ||||
| 
 | ||||
| #endif // HAS_BME680
 | ||||
| #endif // HAS_BME
 | ||||
| @ -36,14 +36,10 @@ void doHousekeeping() { | ||||
|   ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d", | ||||
|            uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask)); | ||||
| #endif | ||||
| #ifdef HAS_BME680 | ||||
| #if (HAS_BME) | ||||
|   ESP_LOGD(TAG, "Bmeloop %d bytes left | Taskstate = %d", | ||||
|            uxTaskGetStackHighWaterMark(BmeTask), eTaskGetState(BmeTask)); | ||||
| #endif | ||||
| #ifdef HAS_BME280 | ||||
|   ESP_LOGD(TAG, "Bme280loop %d bytes left | Taskstate = %d", | ||||
|            uxTaskGetStackHighWaterMark(Bme280Task), eTaskGetState(Bme280Task)); | ||||
| #endif | ||||
| #ifdef HAS_DCF77 | ||||
|   ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d", | ||||
|            uxTaskGetStackHighWaterMark(ClockTask), eTaskGetState(ClockTask)); | ||||
|  | ||||
| @ -21,6 +21,9 @@ | ||||
| #define HAS_BME 1 // Enable BME sensors in general
 | ||||
| #define HAS_BME680 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
 | ||||
| #define BME680_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
 | ||||
| // BME280 sensor on I2C bus
 | ||||
| //#define HAS_BME 1 // Enable BME sensors in general
 | ||||
| //#define HAS_BME280 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
 | ||||
| 
 | ||||
| // user defined sensors
 | ||||
| //#define HAS_SENSORS 1 // comment out if device has user defined sensors
 | ||||
|  | ||||
| @ -14,10 +14,9 @@ | ||||
| #define CFG_sx1276_radio 1 // HPD13A LoRa SoC
 | ||||
| 
 | ||||
| // enable only if device has these sensors, otherwise comment these lines
 | ||||
| // BME680 sensor on I2C bus
 | ||||
| #define HAS_BME 1 // Enable BME sensors in general
 | ||||
| #define HAS_BME280 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
 | ||||
| #define BME280_ADDR BME680_I2C_ADDR_PRIMARY // connect SDIO of BME680 to GND
 | ||||
| // BME280 sensor on I2C bus
 | ||||
| //#define HAS_BME 1 // Enable BME sensors in general
 | ||||
| //#define HAS_BME280 GPIO_NUM_21, GPIO_NUM_22 // SDA, SCL
 | ||||
| 
 | ||||
| #define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C | ||||
| #define HAS_LED (25) // green on board LED
 | ||||
|  | ||||
							
								
								
									
										35
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								src/main.cpp
									
									
									
									
									
								
							| @ -121,7 +121,7 @@ void setup() { | ||||
| #endif | ||||
| 
 | ||||
|   // setup debug output or silence device
 | ||||
| #if(VERBOSE) | ||||
| #if (VERBOSE) | ||||
|   Serial.begin(115200); | ||||
|   esp_log_level_set("*", ESP_LOG_VERBOSE); | ||||
| #else | ||||
| @ -132,7 +132,7 @@ void setup() { | ||||
|   ESP_LOGI(TAG, "Starting %s v%s", PRODUCTNAME, PROGVERSION); | ||||
| 
 | ||||
|   // print chip information on startup if in verbose mode
 | ||||
| #if(VERBOSE) | ||||
| #if (VERBOSE) | ||||
|   esp_chip_info_t chip_info; | ||||
|   esp_chip_info(&chip_info); | ||||
|   ESP_LOGI(TAG, | ||||
| @ -227,7 +227,7 @@ void setup() { | ||||
|   batt_voltage = read_voltage(); | ||||
| #endif | ||||
| 
 | ||||
| #if(USE_OTA) | ||||
| #if (USE_OTA) | ||||
|   strcat_P(features, " OTA"); | ||||
|   // reboot to firmware update mode if ota trigger switch is set
 | ||||
|   if (cfg.runmode == 1) { | ||||
| @ -239,7 +239,7 @@ void setup() { | ||||
| 
 | ||||
| // start BLE scan callback if BLE function is enabled in NVRAM configuration
 | ||||
| // or switch off bluetooth, if not compiled
 | ||||
| #if(BLECOUNTER) | ||||
| #if (BLECOUNTER) | ||||
|   strcat_P(features, " BLE"); | ||||
|   if (cfg.blescan) { | ||||
|     ESP_LOGI(TAG, "Starting Bluetooth..."); | ||||
| @ -301,7 +301,7 @@ void setup() { | ||||
|   assert(spi_init() == ESP_OK); | ||||
| #endif | ||||
| 
 | ||||
| #if(VENDORFILTER) | ||||
| #if (VENDORFILTER) | ||||
|   strcat_P(features, " OUIFLT"); | ||||
| #endif | ||||
| 
 | ||||
| @ -358,11 +358,15 @@ void setup() { | ||||
|                           &irqHandlerTask, // task handle
 | ||||
|                           1);              // CPU core
 | ||||
| 
 | ||||
| // initialize bme680
 | ||||
| // initialize BME sensor (BME280/BME680)
 | ||||
| #if (HAS_BME) | ||||
| #ifdef HAS_BME680 | ||||
|   strcat_P(features, " BME680"); | ||||
| #elif defined HAS_BME280 | ||||
|   strcat_P(features, " BME280"); | ||||
| #endif | ||||
|   if (bme_init()) { | ||||
|     ESP_LOGI(TAG, "Starting BME280 sensor..."); | ||||
|     ESP_LOGI(TAG, "Starting BME sensor..."); | ||||
|     xTaskCreatePinnedToCore(bme_loop,  // task function
 | ||||
|                             "bmeloop", // name of task
 | ||||
|                             2048,      // stack size of task
 | ||||
| @ -373,21 +377,6 @@ void setup() { | ||||
|   } | ||||
| #endif | ||||
| 
 | ||||
| // initialize BME280
 | ||||
| #ifdef HAS_BME280 | ||||
|   strcat_P(features, " BME280"); | ||||
|   if (bme280_init()) { | ||||
|     ESP_LOGI(TAG, "Starting BME280 sensor..."); | ||||
|     xTaskCreatePinnedToCore(bme280_loop,  // task function
 | ||||
|                             "bme280loop", // name of task
 | ||||
|                             2048,      // stack size of task
 | ||||
|                             (void *)1, // parameter of the task
 | ||||
|                             1,         // priority of the task
 | ||||
|                             &Bme280Task,  // task handle
 | ||||
|                             1);        // CPU core
 | ||||
|   } | ||||
| #endif | ||||
| 
 | ||||
|   // starting timers and interrupts
 | ||||
|   assert(irqHandlerTask != NULL); // has interrupt handler task started?
 | ||||
|   ESP_LOGI(TAG, "Starting Timers..."); | ||||
| @ -415,7 +404,7 @@ void setup() { | ||||
| #endif | ||||
| #endif // HAS_BUTTON
 | ||||
| 
 | ||||
| #if(TIME_SYNC_INTERVAL) | ||||
| #if (TIME_SYNC_INTERVAL) | ||||
|   // start pps timepulse
 | ||||
|   ESP_LOGI(TAG, "Starting Timekeeper..."); | ||||
|   assert(timepulse_init()); // setup timepulse
 | ||||
|  | ||||
| @ -262,12 +262,12 @@ void get_gps(uint8_t val[]) { | ||||
| 
 | ||||
| void get_bme(uint8_t val[]) { | ||||
|   ESP_LOGI(TAG, "Remote command: get bme680 sensor data"); | ||||
| #ifdef HAS_BME680 | ||||
| #if (HAS_BME) | ||||
|   payload.reset(); | ||||
|   payload.addBME(bme_status); | ||||
|   SendPayload(BMEPORT, prio_high); | ||||
| #else | ||||
|   ESP_LOGW(TAG, "BME680 sensor not supported"); | ||||
|   ESP_LOGW(TAG, "BME sensor not supported"); | ||||
| #endif | ||||
| }; | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user