Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brzo I2C support. #33

Merged
merged 2 commits into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions examples/BME_280_BRZO_I2C_Test/BME_280_BRZO_I2C_Test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
BME280 BRZO I2C Test.ino
This code shows how to record data from the BME280 environmental sensor
using I2C interface and https://github.com/pasko-zh/brzo_i2c library
on ESP8266.
This file is an example file, part of the Arduino BME280 library.

Copyright (C) 2016 Tyler Glenn
Forked by Alex Shavlovsky
to support https://github.com/pasko-zh/brzo_i2c library on ESP8266.

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 <http:https://www.gnu.org/licenses/>.

Written: Dec 30 2015.
Last Updated: Sep 19 2016.

Connecting the BME280 Sensor:
Sensor -> Board
-----------------------------
Vin (Voltage In) -> 3.3V
Gnd (Ground) -> Gnd
SDA (Serial Data) -> D2 on ESP8266
SCK (Serial Clock) -> D1 on ESP8266

*/

/* ==== Includes ==== */
#include <BME280BRZO_I2C.h>
/* ==== END Includes ==== */

/* ==== Defines ==== */
#define SERIAL_BAUD 115200
/* ==== END Defines ==== */

/* ==== Global Variables ==== */
BME280BRZO_I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
bool metric = true;
/* ==== END Global Variables ==== */


/* ==== Prototypes ==== */
/* === Print a message to stream with the temp, humidity and pressure. === */
void printBME280Data(Stream * client);
/* === Print a message to stream with the altitude, and dew point. === */
void printBME280CalculatedData(Stream* client);
/* ==== END Prototypes ==== */

/* ==== Setup ==== */
void setup() {
Serial.begin(SERIAL_BAUD);
while(!Serial) {} // Wait
while(!bme.begin()){
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
}
/* ==== END Setup ==== */

/* ==== Loop ==== */
void loop() {
printBME280Data(&Serial);
printBME280CalculatedData(&Serial);
delay(500);
}
/* ==== End Loop ==== */

/* ==== Functions ==== */
void printBME280Data(Stream* client){
float temp(NAN), hum(NAN), pres(NAN);
uint8_t pressureUnit(3); // unit: B000 = Pa, B001 = hPa, B010 = Hg, B011 = atm, B100 = bar, B101 = torr, B110 = N/m^2, B111 = psi
bme.read(pres, temp, hum, metric, pressureUnit); // Parameters: (float& pressure, float& temp, float& humidity, bool celsius = false, uint8_t pressureUnit = 0x0)
/* Alternatives to ReadData():
float temp(bool celsius = false);
float pres(uint8_t unit = 0);
float hum();

Keep in mind the temperature is used for humidity and
pressure calculations. So it is more effcient to read
temperature, humidity and pressure all together.
*/
client->print("Temp: ");
client->print(temp);
client->print("°"+ String(metric ? 'C' :'F'));
client->print("\t\tHumidity: ");
client->print(hum);
client->print("% RH");
client->print("\t\tPressure: ");
client->print(pres);
client->print(" atm");
}
void printBME280CalculatedData(Stream* client){
float altitude = bme.alt(metric);
float dewPoint = bme.dew(metric);
client->print("\t\tAltitude: ");
client->print(altitude);
client->print((metric ? "m" : "ft"));
client->print("\t\tDew point: ");
client->print(dewPoint);
client->println("°"+ String(metric ? 'C' :'F'));

}
/* ==== END Functions ==== */
112 changes: 112 additions & 0 deletions src/BME280BRZO_I2C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
BME280BRZO_I2C.cpp
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
Forked by Alex Shavlovsky
to support https://github.com/pasko-zh/brzo_i2c library on ESP8266.

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 <http:https://www.gnu.org/licenses/>.

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 BME280BRZO_I2C 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:https://andrew.rsmas.miami.edu.
*/

#include "brzo_i2c.h"
#include "BME280BRZO_I2C.h"

/****************************************************************/
BME280BRZO_I2C::BME280BRZO_I2C
(
uint8_t tosr,
uint8_t hosr,
uint8_t posr,
uint8_t mode,
uint8_t st,
uint8_t filter,
bool spiEnable,
uint8_t bme_280_addr
):BME280(tosr, hosr, posr, mode, st, filter, spiEnable),
bme_280_addr(bme_280_addr)
{
}


/****************************************************************/
bool BME280BRZO_I2C::WriteRegister
(
uint8_t addr,
uint8_t data
)
{
uint8_t bf[2];
bf[0] = addr;
bf[1] = data;
brzo_i2c_start_transaction(bme_280_addr, I2C_CLOCK_RATE);
brzo_i2c_write(bf, 2, false);
return (brzo_i2c_end_transaction()==0);
}

/****************************************************************/
bool BME280BRZO_I2C::ReadRegister
(
uint8_t addr,
uint8_t data[],
uint8_t length
)
{
brzo_i2c_start_transaction(bme_280_addr, I2C_CLOCK_RATE);
brzo_i2c_write(&addr, 1, true);
brzo_i2c_read(data, length, false);
brzo_i2c_end_transaction();
return (brzo_i2c_end_transaction()==0);
}


/****************************************************************/
#if defined(ARDUINO_ARCH_ESP8266)
bool BME280BRZO_I2C::begin
(
int SDA,
int SCL
)
{
// allow config of pins
brzo_i2c_setup(SDA,SCL,I2C_ACK_TIMEOUT);
return BME280::Initialize();
}


/****************************************************************/
bool BME280BRZO_I2C::begin()
{
return Initialize();
}
#endif


/****************************************************************/
bool BME280BRZO_I2C::Initialize()
{
brzo_i2c_setup(D2,D1,I2C_ACK_TIMEOUT);
return BME280::Initialize();
}
101 changes: 101 additions & 0 deletions src/BME280BRZO_I2C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
BME280BRZO_I2C.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
Forked by Alex Shavlovsky
to support https://github.com/pasko-zh/brzo_i2c library on ESP8266.

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 <http:https://www.gnu.org/licenses/>.

Written: Sep 19 2016.
Last Updated: Sep 19 2016. - Happy Fall! <5
This code is licensed under the GNU LGPL and is open for ditrbution
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 BME280BRZO_I2C_H
#define BME280BRZO_I2C_H

#ifndef I2C_CLOCK_RATE
#define I2C_CLOCK_RATE 400
#endif

#ifndef I2C_ACK_TIMEOUT
#define I2C_ACK_TIMEOUT 2000
#endif

#include "BME280.h"

//////////////////////////////////////////////////////////////////
/// BME280BRZO_I2C - I2C Implementation of BME280.
class BME280BRZO_I2C: public BME280
{

public:

///////////////////////////////////////////////////////////////
/// Constructor used to create the class. All parameters have
/// default values.
BME280BRZO_I2C(
uint8_t tosr = 0x1,
uint8_t hosr = 0x1,
uint8_t posr = 0x1,
uint8_t mode = 0x1,
uint8_t st = 0x5,
uint8_t filter = 0x0,
bool spiEnable = false,
uint8_t bme_280_addr = 0x76
); // Oversampling = 1, mode = forced, standby time = 1000ms, filter = none.

#if defined(ARDUINO_ARCH_ESP8266)
///////////////////////////////////////////////////////////////
/// On esp8266 it is possible to define I2C pins
virtual bool begin(
int SDA,
int SCL);

virtual bool begin();
#endif

protected:

///////////////////////////////////////////////////////////////
/// Method used at start up to initialize the class. Starts the
/// I2C interface.
virtual bool Initialize();

private:

uint8_t bme_280_addr;

//////////////////////////////////////////////////////////////////
/// 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 // BME280BRZO_I2C_H