janw-cz / JWA_BME280

Arduino library for BME280 sensor based on reference driver
15 stars 9 forks source link

Allow custom i2c pins #13

Open meltdown03 opened 4 years ago

meltdown03 commented 4 years ago

I needed to do this to use different I2C pins on my ESP32S2 Kaluga and Saola. It changes the beginI2C() method and defaults to the SDA and SCL set by the Arduino pins_arduino.h file if not set. Another approach would be to overload the method.

hasenradball commented 2 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: grafik

Then you set the I2C directly by the contsuctor...

hasenradball commented 2 years ago

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);
}
hasenradball commented 2 years ago

Hi @meltdown03,

you can take a look at this repository: Bosch_BME280_Arduino