Open meltdown03 opened 4 years ago
Hi, Question, why not provide the I2C Parameters with the constructor? And let set the value to a private member?
Like for example:
Then you set the I2C directly by the contsuctor...
Here ist the corresponding cpp-file. As example :-).
#include <cSensor_BME280.h>
BME::Sensor_BME280::Sensor_BME280(uint8_t sda_pin, uint8_t scl_pin, uint8_t addr, float altitute) :
_sda_pin {sda_pin},
_scl_pin {scl_pin},
_addr {addr},
_altitute {altitute},
sensor_available {false},
send_data2DB {false},
Data {0, 0, 0, 0, 0}
{}
bool BME::Sensor_BME280::setup() {
// SDA, SCL
Wire.begin(_sda_pin, _scl_pin);
delay(100);
sensor_available = bme.begin(_addr);
DBG__PRINT("\n >>> BME280-Sensor - init(): ", sensor_available, "\n\n");
return sensor_available;
}
void BME::Sensor_BME280::measure() {
Data.Messwert2 = bme.readTemperature();
Data.Messwert3 = bme.readHumidity();
Data.Messwert4 = (bme.seaLevelForAltitude(_altitute, bme.readPressure()) / 100.0F);
}
Hi @meltdown03,
you can take a look at this repository: Bosch_BME280_Arduino
I needed to do this to use different I2C pins on my ESP32S2 Kaluga and Saola. It changes the
beginI2C()
method and defaults to theSDA
andSCL
set by the Arduinopins_arduino.h
file if not set. Another approach would be to overload the method.