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

Use a better (more reliable) BME280 library #3

Merged
merged 1 commit into from
Jun 4, 2022
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
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ upload_speed = 921600
monitor_port = /dev/cu.usbserial-0001
monitor_speed = 115200
lib_deps =
enviromonitor/BME280_Light @ 0.0.0-alpha+sha.600667f3a6
adafruit/Adafruit BME280 Library @ ^2.2.2
arduino-libraries/NTPClient @ ^3.2.1
ottowinter/AsyncMqttClient-esphome @ ^0.8.6
arcao/Syslog @ ^2.0.0
53 changes: 48 additions & 5 deletions src/bme_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include "logging.h"

#define PASCALS_IN_HPA 100.0f
#ifndef ALTITUDE
#define ALTITUDE 80.0f
#endif

BMEHomeSensor::BMEHomeSensor(int data_pin, int clk_pin, int pwr_pin) :
data_pin(data_pin),
Expand All @@ -24,19 +26,60 @@ String BMEHomeSensor::name()
return "BME280";
}

uint8_t BMEHomeSensor::getAddress()
{
uint8_t found = 0;
byte error, address;
for (address = 1; address < 127 && found == 0; ++address) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
found = address;
}
else if (error==4)
{
logf("Unknow error scanning I2C at address 0x%x", address);
}
}
return found;
}

void BMEHomeSensor::setup()
{
Wire.begin(data_pin, clk_pin);
status = bme.begin();
uint8_t address = getAddress();
if (address == 0)
{
logln("Unable to find an I2C device on the selected wire");
status = false;
}
else
{
logf("Found an I2C device ID of 0x%x\n", bme.sensorID());
status = bme.begin(address);
if (!status)
{
logln("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
logln("ID of: 0xFF probably means a bad address, a BMP 180 or BMP 085");
logln(" 0x56-0x58 represents a BMP 280,");
logln(" 0x60 represents a BME 280,");
logln(" 0x61 represents a BME 680.");
}
}
}

void BMEHomeSensor::submitReading(ReadingSubmitter &submitter)
{
logln("Getting BME sensors");
bme.refresh();
submitter.sendReading("temperature", bme.temperature);
submitter.sendReading("humidity", bme.humidity);
submitter.sendReading("pressure", bme.seaLevelForAltitude(ALTITUDE) / PASCALS_IN_HPA);

sensors_event_t temp_event, pressure_event, humidity_event;
bme.getTemperatureSensor()->getEvent(&temp_event);
bme.getPressureSensor()->getEvent(&pressure_event);
bme.getHumiditySensor()->getEvent(&humidity_event);

submitter.sendReading("temperature", temp_event.temperature);
submitter.sendReading("humidity", humidity_event.relative_humidity);
submitter.sendReading("pressure", pressure_event.pressure);
data_sent = true;
}

Expand Down
7 changes: 5 additions & 2 deletions src/bme_sensor.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <Wire.h>
#include <BME280_t.h>
#include <Adafruit_BME280.h>
#include "home_sensor.h"

class BMEHomeSensor: public HomeSensor {
Expand All @@ -15,12 +15,15 @@ class BMEHomeSensor: public HomeSensor {
virtual bool isOn();
virtual bool sent();
virtual String name();

private:
int data_pin;
int clk_pin;
int pwr_pin;
bool status;
bool data_sent;
bool is_on;
BME280<> bme;
Adafruit_BME280 bme;

uint8_t getAddress();
};
4 changes: 2 additions & 2 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void logln(const char* line)
#if SYSLOG_ENABLED == 1
if (WiFi.isConnected())
{
Serial.printf("Syslog...");
Serial.printf("Syslog... ");
syslog.log(LOG_INFO, line);
}
#endif
Expand All @@ -67,7 +67,7 @@ void logf(const char *format, ...)
#if SYSLOG_ENABLED == 1
if (WiFi.isConnected())
{
Serial.printf("Syslog...");
Serial.printf("Syslog... ");
va_copy(copy2, arg);
syslog.vlogf(LOG_INFO, format, copy2);
va_end(copy2);
Expand Down