You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
3.2 KiB
118 lines
3.2 KiB
#include "BME280.h"
|
|
|
|
#include <Wire.h>
|
|
#include <SPI.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <Adafruit_BME280.h>
|
|
|
|
BME280::BME280() {
|
|
valid = false;
|
|
}
|
|
|
|
bool BME280::begin(void) {
|
|
SPI.begin(BME_SCK, BME_MISO, BME_MOSI, BME_CS);
|
|
pinMode(18, OUTPUT);
|
|
digitalWrite(18, HIGH); // disable LoRa_CS
|
|
pinMode(BME_CS, OUTPUT);
|
|
digitalWrite(BME_CS, LOW); // enable bme280 CS
|
|
delay(100);
|
|
// bme = new Adafruit_BME280(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
|
|
bme = new Adafruit_BME280(BME_CS); // hardware SPI
|
|
|
|
bme->setSampling(Adafruit_BME280::sensor_mode::MODE_NORMAL,
|
|
Adafruit_BME280::sensor_sampling::SAMPLING_X8,
|
|
Adafruit_BME280::sensor_sampling::SAMPLING_X8,
|
|
Adafruit_BME280::sensor_sampling::SAMPLING_X8,
|
|
Adafruit_BME280::sensor_filter::FILTER_OFF,
|
|
Adafruit_BME280::standby_duration::STANDBY_MS_250);
|
|
|
|
valid = bme->begin();
|
|
if (!valid) bme->init();
|
|
|
|
if (!valid) return false;
|
|
return true;
|
|
}
|
|
|
|
void BME280::sleep() {
|
|
bme->setSampling(Adafruit_BME280::sensor_mode::MODE_SLEEP,
|
|
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
|
|
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
|
|
Adafruit_BME280::sensor_sampling::SAMPLING_X16,
|
|
Adafruit_BME280::sensor_filter::FILTER_OFF,
|
|
Adafruit_BME280::standby_duration::STANDBY_MS_1000);
|
|
}
|
|
|
|
bool BME280::reinit() {
|
|
pinMode(18, OUTPUT);
|
|
digitalWrite(18, HIGH); // disable LoRa_CS
|
|
pinMode(BME_CS, OUTPUT);
|
|
// digitalWrite(BME_CS, LOW); // enable bme280 CS
|
|
|
|
valid = false;
|
|
for (int retries=0; !valid && retries<5; retries++) {
|
|
/* reset chip */
|
|
Serial.println("resetting BME280");
|
|
SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
|
|
digitalWrite(BME_CS, LOW);
|
|
constexpr byte BME280_REGISTER_SOFTRESET = 0xE0;
|
|
SPI.transfer(BME280_REGISTER_SOFTRESET & ~0x80);
|
|
SPI.transfer(0xB6);
|
|
digitalWrite(BME_CS, HIGH);
|
|
SPI.endTransaction(); // release the SPI bus
|
|
delay(300); // wait for chip to wake up
|
|
|
|
valid = bme->init();
|
|
retries++;
|
|
|
|
checkValid();
|
|
}
|
|
if (!valid) {
|
|
Serial.println("Could not find a valid BME280 sensor, check wiring!");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
void BME280::printValues() {
|
|
Serial.print("Temperature = ");
|
|
Serial.print(bme->readTemperature());
|
|
Serial.println(" *C");
|
|
|
|
Serial.print("Pressure = ");
|
|
|
|
Serial.print(bme->readPressure() / 100.0F);
|
|
Serial.println(" hPa");
|
|
|
|
Serial.print("Approx. Altitude = ");
|
|
Serial.print(bme->readAltitude(SEALEVELPRESSURE_HPA));
|
|
Serial.println(" m");
|
|
|
|
Serial.print("Humidity = ");
|
|
Serial.print(bme->readHumidity());
|
|
Serial.println(" %");
|
|
|
|
Serial.println();
|
|
}
|
|
|
|
float BME280::readTemperature(void) {
|
|
float temp = bme->readTemperature();
|
|
if (temp < -40 || temp > 85) valid = false;
|
|
else valid = true;
|
|
return temp;
|
|
}
|
|
float BME280::readPressure(void) {
|
|
float p = bme->readPressure() / 100.0F;
|
|
if (p < 300 || p > 1100) valid = false;
|
|
else valid = true;
|
|
return p;
|
|
}
|
|
float BME280::readHumidity(void) {
|
|
float h = bme->readHumidity();
|
|
if (h < 0 || h > 100) valid = false;
|
|
else valid = true;
|
|
return h;
|
|
}
|
|
|
|
bool BME280::checkValid(void) {
|
|
readPressure();
|
|
return valid;
|
|
}
|
|
|