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

BME280 sensor read Temperature, Humidity, Pressure #353

Closed
xoseperez opened this issue Dec 30, 2017 · 23 comments
Closed

BME280 sensor read Temperature, Humidity, Pressure #353

xoseperez opened this issue Dec 30, 2017 · 23 comments
Milestone

Comments

@xoseperez
Copy link
Owner

Originally reported by: Stefano Cotterli (Bitbucket: faina09, GitHub: faina09)


I have a BME280 sensor linked via wire protocol to GPIO0 (SData) and GPIO2 (SClock) and I tested hardware with the following sample code; after this I can read Pressure, Temp and Humidity values from sensor:

#!arduino

#include <Adafruit_BME280.h>

Adafruit_BME280 bme; // I2C

void bme280Setup() {
    Wire.begin(0, 2);
    Wire.setClock(100000);
delay(2ms)
    bme.begin();
float tc = bme.readTemperature();
....
}
.....

In ESPurna I set the following defines:

#!arduino
#define BMX280_SUPPORT         1
#define I2C_SDA_PIN         0
#define I2C_SCL_PIN         2

the i2c bus starts but searches for a BMP280

#!arduino

[I2C] Using GPIO0 for SDA and GPIO2 for SCL
[I2C] No devices found
[SENSOR] BMP280 @ I2C (0x00)
[SENSOR]  -> ERROR 6

What is wrong or what I missed in my ESPurna configuration?

@xoseperez
Copy link
Owner Author

Apparently, the firmware is not able to locate any I2C device on that bus. It could be that the wiring is wrong (maybe you changed something) or the bus is noisy. In this second case, you can try enabling I2C_CLEAR_BUS in the sensors.h file.

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


The wiring is ok:
I tested back with the stand-alone code, but this code do a recheck every 10 seconds if the board is not found, and often this is the case at the first check.

I enabled I2C_CLEAR_BUS but the issue remains, I get the response

#!arduino

[I2C] Using GPIO0 for SDA and GPIO2 for SCL
[RELAY] Saving mask: 0
[I2C] Clear bus (response: 0)
[I2C] No devices found
[SENSOR] BMP280 @ I2C (0x00)
[SENSOR]  -> ERROR 6

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


I run a i2cScan() in loop() rouitine and bingo!

[I2C] Device found at address 0x77

obviously this is not a solution... I need to wait for a while or do a rescan until all expected devices are found...

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


I temporary fixed avoidig sensorSetup(); in setup() of espurna.ini

and modifying loop() in the same file as follow:

#!arduino

void loop() {
    static unsigned long last_update = 0;
    static bool bme280OK = false;
    if ((millis() - last_update > 5000) || (last_update == 0)) {
        last_update = millis();
        if (!bme280OK) {
            DEBUG_MSG_P(PSTR("[INIT] check i2c again ....%d sec\n"), getUptime());
            unsigned char error = i2cCheck(119);
            if (error == 0) {
                bme280OK = true;
                sensorSetup();
            }
        }
    }

again, it is not a solution, just a workaround

@xoseperez
Copy link
Owner Author

Wow, interesting. The BME280 I have does not show this behaviour. Does this mean it needs a warmup time? How long does it take to show up?

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


Seems to be around 50ms. I will investigate further in the weekend

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


I can confirm: performing a try to read the sensor every 10ms I found that the sensor is detected when the check is done at least 40-60ms after the I2C bus setup

@xoseperez
Copy link
Owner Author

Can you test adding a delay at the end of the i2cSetup() method in the i2c.ino module?

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


In espurna.ini I

  • restored the sensorSetup(); in setup()
  • inserted 500ms delay just before i2cScan()
  • add [INIT] ... logs
    the results are:
#!arduino
[I2C] Using GPIO0 for SDA and GPIO2 for SCL
[INIT] setup i2c end ....at 500 msec
[I2C] No devices found
[SENSOR] BMP280 @ I2C (0x00)
[SENSOR]  -> ERROR 6
(...)
[INIT] setup end ....at 529 msec
[MAIN] Uptime: 0 seconds
[MAIN] Free heap: 24672 bytes
[MAIN] Power: 2780 mV
[MAIN] Time: Not set
[INIT] check i2c again ....540 msec
[INIT] check i2c again ....631 msec
[WIFI] Scanning
[INIT] check i2c again ....642 msec
[I2C] Address 0x77 locked
[SENSOR] BMP280 @ I2C (0x00)
[SENSOR]  -> ERROR 6
[SENSOR] BME280 @ I2C (0x77)
[SENSOR]  -> temperature:0
[SENSOR]  -> pressure:0
[SENSOR]  -> humidity:0
[RELAY] Saving mask: 0
[WIFI]      BSSID 58:2A:F7:3D:96:20 - RSSI -92 - CH 02 - ENC AUTO - SSID InfostradaWiFi-003909
[WIFI] -->  BSSID 98:DE:D0:E8:94:9D - RSSI -46 - CH 12 - ENC WPA2 - SSID TP_LINK_W8968
[WIFI] Connecting to BSSID 98:DE:D0:E8:94:9D - CH 12 - SSID TP_LINK_W8968
[SENSOR] Error reading data from BMP280 @ I2C (0x00) (error: 4)
[SENSOR] BME280 @ I2C (0x77) - temperature: 19.1C
[SENSOR] BME280 @ I2C (0x77) - pressure: 994.61hPa
[SENSOR] BME280 @ I2C (0x77) - humidity: 58%
[MDNS] OK

@xoseperez
Copy link
Owner Author

I don't understand that there is an error 6 (I2C error in the BMX280 sensor) just after the 0x77 locked... But apparently there is something needed before the sensor is ready and it's not the i2c setup...

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


The error comes from the first i2cscan, that 'founds' (sic!) a BMP280 at address 0x00.
If the sensorSetup() call in setup() is commented out, no errors are displayed.
i noticed (may be just a coincidence with no meaning) that the sensor is always detected after a wifi scan:

#!arduino

[INIT] setup end ....at 24 msec
[MAIN] Uptime: 0 seconds
[MAIN] Free heap: 24768 bytes
[MAIN] Power: 2779 mV
[MAIN] Time: Not set
[INIT] check i2c again ....32 msec
[INIT] check i2c again ....49 msec
[WIFI] Scanning
[INIT] check i2c again ....70 msec
[I2C] Address 0x77 locked
[SENSOR] BME280 @ I2C (0x77)

@xoseperez
Copy link
Owner Author

No, i2cScan doesn't find "anything". It just report what addresses look like being used (so there is a device pulling the line at the other end). Then the BMX280 module uses i2cFindFirst to find what address of the possible addresses the sensor may use is actually in use. It finds nothing (maybe the sensor is not yet ready?) and returns address 0 and an error 6 (which means I2C error).

What if you try to hardcode the sensor address in the sensors.h file? Look for the BMX280_ADDREESS and set it to 0x77 (instead of 0x00 which means "discover it").

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


good idea, I'll try it this evening

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


In the dev branch I found no longer

#define BMX280_ADDRESS

but it is used in sensor.ino

#!arduino

    #if BMX280_SUPPORT
    {
        BMX280Sensor * sensor = new BMX280Sensor();
        sensor->setAddress(BMX280_ADDRESS);
        _sensors.push_back(sensor);
    }

I try defining it 0x77 but does not work.
To get my BME280 working I still need the loop test in loop() as described above

@xoseperez
Copy link
Owner Author

BMX280_ADDRESS is defined in sensors.h.

@xoseperez
Copy link
Owner Author

Original comment by Stefano Cotterli (Bitbucket: faina09, GitHub: faina09):


sure, you're rigth my fault.
Found and defined at 0x77, but this way the sensor does not work, seems to be not jet ready

@xoseperez
Copy link
Owner Author

Original comment by Hany Info (Bitbucket: hanyinfo, GitHub: hanyinfo):


Hi guys, Is there any change see from this same sensor BME280 the values for http, mqtt. I try to compile project, it works fine. Is there Xose some "generic" i2c device? I would see the values some way.

Which device to configure? Thank you very much for help.

#!arduino

#define GENERIC_8CH     1

#define BMX280_SUPPORT         1

I have ESP8266 NodeMCU V3 Lua WIFI module + memory 32Mbit Flash

@xoseperez
Copy link
Owner Author

@hanyinfo you might have to configure the GPIOs you are using for I2C. Check the I2C_SDA_PIN and I2C_SCL_PIN in sensors.h

@xoseperez
Copy link
Owner Author

Original comment by Hany Info (Bitbucket: hanyinfo, GitHub: hanyinfo):


@xoseperez Ok, I will try...thank you

@xoseperez
Copy link
Owner Author

@hanyinfo Any update on this?

@hanyinfo
Copy link

yes... I tried few minutes ago and I can see the values on status page together with 8 switches. I can see Sensors configuration with temperature. Thanks for help!

in hardware.h

#elif defined(GENERIC_8CH)

#define I2C_SDA_PIN D3
#define I2C_SCL_PIN D4

and in arguino.h
#define BMX280_SUPPORT 1

@xoseperez
Copy link
Owner Author

Great.

To recap: there is still the issue @fain09 reported. Looks like his BME280 requires some time to warm up before being accessible. I'm receiving another sensor tomorrow. Will try to reproduce the issue.

@xoseperez
Copy link
Owner Author

With the latest changes in the sensors branch the BMX280Sensor has been tested under pressure, hot-unplugging and plugging it, and it always recovers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants