diff --git a/examples/BME_280_I2C_SoftwareWire_Test/BME_280_I2C_SoftwareWire_Test.ino b/examples/BME_280_I2C_SoftwareWire_Test/BME_280_I2C_SoftwareWire_Test.ino new file mode 100644 index 0000000..2ed7403 --- /dev/null +++ b/examples/BME_280_I2C_SoftwareWire_Test/BME_280_I2C_SoftwareWire_Test.ino @@ -0,0 +1,96 @@ +/* +BME280_I2C_SoftwareWire_Test.ino + +This code shows how to record data from the BME280 environmental sensor +using I2C SoftwareWire interface. This file is an example file, part of the Arduino +BME280 library. + +GNU General Public License + +Written: Dec 30 2015. +Last Updated: Oct 07 2017. + +Connecting the BME280 Sensor: +Sensor -> Board +----------------------------- +Vin (Voltage In) -> 3.3V +Gnd (Ground) -> Gnd +SDA (Serial Data) -> 2 +SCK (Serial Clock) -> 3 + + */ + +#include +#include + +#define SERIAL_BAUD 115200 + +#define SDA 2 +#define SCK 3 + +SoftwareWire softwareWire(SDA, SCK); + + +BME280I2CSoftwareWire bme(softwareWire); // Default : forced mode, standby time = 1000 ms + // Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off, + +////////////////////////////////////////////////////////////////// +void setup() +{ + Serial.begin(SERIAL_BAUD); + + while(!Serial) {} // Wait + + softwareWire.begin(); + + while(!bme.begin()) + { + Serial.println("Could not find BME280 sensor!"); + delay(1000); + } + + switch(bme.chipModel()) + { + case BME280::ChipModel_BME280: + Serial.println("Found BME280 sensor! Success."); + break; + case BME280::ChipModel_BMP280: + Serial.println("Found BMP280 sensor! No Humidity available."); + break; + default: + Serial.println("Found UNKNOWN sensor! Error!"); + } +} + +////////////////////////////////////////////////////////////////// +void loop() +{ + printBME280Data(&Serial); + delay(500); +} + +////////////////////////////////////////////////////////////////// +void printBME280Data +( + Stream* client +) +{ + float temp(NAN), hum(NAN), pres(NAN); + + BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); + BME280::PresUnit presUnit(BME280::PresUnit_Pa); + + bme.read(pres, temp, hum, tempUnit, presUnit); + + client->print("Temp: "); + client->print(temp); + client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); + client->print("\t\tHumidity: "); + client->print(hum); + client->print("% RH"); + client->print("\t\tPressure: "); + client->print(pres); + client->println("Pa"); + + delay(1000); +} diff --git a/src/BME280.cpp b/src/BME280.cpp index 10e2384..431e220 100644 --- a/src/BME280.cpp +++ b/src/BME280.cpp @@ -28,8 +28,6 @@ of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. */ -#include - #include "BME280.h" diff --git a/src/BME280.h b/src/BME280.h index 08b1247..fb45110 100644 --- a/src/BME280.h +++ b/src/BME280.h @@ -22,7 +22,7 @@ along with this program. If not, see . Written: Dec 30 2015. Last Updated: Oct 07 2017. -This code is licensed under the GNU LGPL and is open for ditrbution +This code is licensed under the GNU LGPL and is open for distrbution and copying in accordance with the license. This header must be included in any derived code or copies of the code. diff --git a/src/BME280I2C.cpp b/src/BME280I2C.cpp index 67f684d..3a46239 100644 --- a/src/BME280I2C.cpp +++ b/src/BME280I2C.cpp @@ -28,10 +28,13 @@ of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. */ -#include - #include "BME280I2C.h" +#include "Config.h" + +#ifdef USING_WIRE +#include +#endif /****************************************************************/ BME280I2C::BME280I2C @@ -61,6 +64,7 @@ const BME280I2C::Settings& BME280I2C::getSettings() const } +#ifdef USING_WIRE /****************************************************************/ bool BME280I2C::WriteRegister ( @@ -100,3 +104,5 @@ bool BME280I2C::ReadRegister return ord == length; } + +#endif diff --git a/src/BME280I2C.h b/src/BME280I2C.h index 32cd3ac..89e8e79 100644 --- a/src/BME280I2C.h +++ b/src/BME280I2C.h @@ -20,7 +20,7 @@ along with this program. If not, see . Written: Sep 19 2016. Last Updated: Oct 07 2017. -This code is licensed under the GNU LGPL and is open for ditrbution +This code is licensed under the GNU LGPL and is open for distrbution and copying in accordance with the license. This header must be included in any derived code or copies of the code. diff --git a/src/BME280I2CSoftwareWire.cpp b/src/BME280I2CSoftwareWire.cpp new file mode 100644 index 0000000..b756e51 --- /dev/null +++ b/src/BME280I2CSoftwareWire.cpp @@ -0,0 +1,90 @@ +/* +BME280I2CSoftwareWireI2C.cpp +This code records data from the BME280I2CSoftwareWire sensor and provides an API. +This file is part of the Arduino BME280I2CSoftwareWire library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Dec 30 2015. +Last Updated: Jan 1 2016. - Happy New year! + +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the BME280I2CSoftwareWire environmental sensor, +calibration code based on algorithms providedBosch, some unit conversations courtesy +of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation +courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. + */ + +#include "Config.h" + +#ifdef USING_SOFTWARE_WIRE + +#include "SoftwareWire.h" + +#include "BME280I2CSoftwareWire.h" + + +/****************************************************************/ +BME280I2CSoftwareWire::BME280I2CSoftwareWire +( + SoftwareWire& softwareWire, + const Settings& settings +):BME280I2C(settings), + m_softwareWire(softwareWire) +{ +} + +/****************************************************************/ +bool BME280I2CSoftwareWire::WriteRegister +( + uint8_t addr, + uint8_t data +) +{ + m_softwareWire.beginTransmission(getSettings().bme280Addr); + m_softwareWire.write(addr); + m_softwareWire.write(data); + m_softwareWire.endTransmission(); + + return true; // TODO: Chech return values from wire calls. +} + + +/****************************************************************/ +bool BME280I2CSoftwareWire::ReadRegister +( + uint8_t addr, + uint8_t data[], + uint8_t length +) +{ + uint8_t ord(0); + + m_softwareWire.beginTransmission(getSettings().bme280Addr); + m_softwareWire.write(addr); + m_softwareWire.endTransmission(); + + m_softwareWire.requestFrom(static_cast(getSettings().bme280Addr), length); + + while(m_softwareWire.available()) + { + data[ord++] = m_softwareWire.read(); + } + + return ord == length; +} + +#endif diff --git a/src/BME280I2CSoftwareWire.h b/src/BME280I2CSoftwareWire.h new file mode 100644 index 0000000..44790d9 --- /dev/null +++ b/src/BME280I2CSoftwareWire.h @@ -0,0 +1,69 @@ +/* +BME280I2CSoftwareWire.h +This code records data from the BME280 sensor and provides an API. +This file is part of the Arduino BME280 library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Mar 13 2018. + +This code is licensed under the GNU LGPL and is open for distrbution +and copying in accordance with the license. +This header must be included in any derived code or copies of the code. + +Based on the data sheet provided by Bosch for the Bme280 environmental sensor. + */ + +#ifndef TG_BME_280_I2C_SOFTWARE_WIRE_H +#define TG_BME_280_I2C_SOFTWARE_WIRE_H + +#include "BME280I2C.h" + +class SoftwareWire; + +////////////////////////////////////////////////////////////////// +/// BME280I2CSoftwareWire - SoftwareWire I2C Implementation of BME280. +class BME280I2CSoftwareWire: public BME280I2C +{ + +public: + + /////////////////////////////////////////////////////////////// + /// Constructor used to create the class. All parameters have + /// default values. + BME280I2CSoftwareWire( + SoftwareWire& softwareWire, + const BME280I2C::Settings& settings = BME280I2C::Settings()); + + +protected: +private: + SoftwareWire& m_softwareWire; + + ////////////////////////////////////////////////////////////////// + /// Write values to BME280 registers. + virtual bool WriteRegister( + uint8_t addr, + uint8_t data); + + ///////////////////////////////////////////////////////////////// + /// Read values from BME280 registers. + virtual bool ReadRegister( + uint8_t addr, + uint8_t data[], + uint8_t length); + +}; +#endif // TG_BME_280_I2C_H diff --git a/src/BME280I2C_BRZO.cpp b/src/BME280I2C_BRZO.cpp index 451cc59..dc11948 100644 --- a/src/BME280I2C_BRZO.cpp +++ b/src/BME280I2C_BRZO.cpp @@ -33,6 +33,8 @@ courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. #include "BME280I2C_BRZO.h" +#include "Config.h" + #ifdef USING_BRZO #include "brzo_i2c.h" diff --git a/src/BME280I2C_BRZO.h b/src/BME280I2C_BRZO.h index ca16532..2fb0d2c 100644 --- a/src/BME280I2C_BRZO.h +++ b/src/BME280I2C_BRZO.h @@ -23,7 +23,7 @@ along with this program. If not, see . Written: Sep 19 2016. Last Updated: Oct 07 2017. -This code is licensed under the GNU LGPL and is open for ditrbution +This code is licensed under the GNU LGPL and is open for distrbution and copying in accordance with the license. This header must be included in any derived code or copies of the code. diff --git a/src/BME280Spi.cpp b/src/BME280Spi.cpp index fe18236..ee6dd2a 100644 --- a/src/BME280Spi.cpp +++ b/src/BME280Spi.cpp @@ -28,10 +28,14 @@ of www.endmemo.com, altitude equation courtesy of NOAA, and dew point equation courtesy of Brian McNoldy at http://andrew.rsmas.miami.edu. */ -#include "Arduino.h" #include "BME280Spi.h" +#include "Config.h" + +#ifdef USING_SPI + #include +#include "Arduino.h" /****************************************************************/ BME280Spi::BME280Spi @@ -132,3 +136,5 @@ bool BME280Spi::WriteRegister return true; } + +#endif diff --git a/src/BME280SpiSw.h b/src/BME280SpiSw.h index 50b5ca0..0a434a6 100644 --- a/src/BME280SpiSw.h +++ b/src/BME280SpiSw.h @@ -20,7 +20,7 @@ along with this program. If not, see . Written: Dec 18 2016. - Happy Holidays! Last Updated: Oct 07 2017. -This code is licensed under the GNU LGPL and is open for ditrbution +This code is licensed under the GNU LGPL and is open for distrbution and copying in accordance with the license. This header must be included in any derived code or copies of the code. diff --git a/src/Config.h b/src/Config.h new file mode 100644 index 0000000..1d94e76 --- /dev/null +++ b/src/Config.h @@ -0,0 +1,37 @@ +/* + +config.h + +This sets the configuration for the Arduino BME280 library. +Copyright (C) 2016 Tyler Glenn + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Written: Mar 13 2018. + +This code is licensed under the GNU LGPL and is open for distrbution +and copying in accordance with the license. +This header must be included in any derived code or copies of the code. + + */ + +#ifndef TG_BME_280_CONFIG_H +#define TG_BME_280_CONFIG_H + +//#define USING_WIRE 1 +//#define USING_SOFTWARE_WIRE 1 +//#define USING_SPI +//#define USING_BRZO 1 + +#endif