maarten-pennings / CCS811

Arduino library for the CCS811 gas sensor for monitoring indoor air quality.
MIT License
165 stars 46 forks source link

Chinese NodeMCU and CCS811 #6

Closed taunoe closed 5 years ago

taunoe commented 5 years ago

I use chinese clones. Like this: https://www.aliexpress.com/item/CJMCU-811-CCS811-Carbon-Monoxide-CO-VOCs-Air-Quality-Numerical-Gas-Sensors/32762314271.html And this library is in trouble to read data:

Starting CCS811 basic demo init: hardware version: 12 init: bootloader version: 1000 init: application version: 1100 CCS811: errstat=0=--vhxmrwf--ad-ie CCS811: errstat=0=--vhxmrwf--ad-ie CCS811: waiting for (new) data CCS811: waiting for (new) data CCS811: waiting for (new) data CCS811: waiting for (new) data

When i wait 5 min i get some data:

CCS811: waiting for (new) data CCS811: waiting for (new) data CCS811: I2C error CCS811: eco2=459 ppm etvoc=8 ppb
CCS811: waiting for (new) data CCS811: waiting for (new) data

Sensor works when i use Adafruit ccs811 library. But there are other problems ...

maarten-pennings commented 5 years ago

Hmm, not sure what is going on. I do know the CCS811 requires clock stretching, and that not all controllers (I2C masters) support that. For example, I used an ESP8266, and some extra waits are required (built-in in the lib).

What I do see is that your controller (which MCU is that by the way) does see the CCS811. We do get the correct hardware version fro example. So that is good.

But the first status lines are bad. You get errstat=0=--vhxmrwf--ad-ie but you should get something like errstat=90=--vhxmrwF--Ad-ie The "A" flag must be 1, it means that the internal memory contains a valid application. That flag can never be 0 (it would mean you erased the memory, and no operation is possible). The "F" flag must be 1 at this point in time. It means there is a valid application, and the the bootloader started it. A 0 means the chip is still in bootloader mode.

Since the next line is ok, and you even get data sometimes, I think the chip has a valid app A-hi and that app is running F-hi, but the (I2C) communication has errors.

taunoe commented 5 years ago

I have this MCU: https://www.aliexpress.com/item/1PCS-Wireless-module-CH340-NodeMcu-V3-Lua-WIFI-Internet-of-Things-development-board-based-ESP8266/32665100123.html

I do not have i2C pull-ups. I assumed they are on board. All other things are working: i2c display and HTU21D sensor. But i can add them and see does they change something.

I used library default example sketch. Nothing changed.

I can use oscilloscope. If needed.

Adafruit library: https://github.com/adafruit/Adafruit_CCS811/

maarten-pennings commented 5 years ago

That is strange - I have (at least at first sight) exactly the same board as you. The bad news is that ESP8266 is bad a clock stretching - but for me the lib is working on ESP8266.

I believe the ESP8266 has internal pull-ups, but weak ones, and they only become active when wire.begin() is called, which causes some "bus spikes" during startup. Adding pull-ups has helped me in the past in some cases. You might try, but I don't think it is the cause of your problems.

Because you do get some sensible output now and then but nonsens in other cases, I still suspect the I2C communication (hence a bus trace would be nice).

You said the Adafruit lib does work with CCS811, but you have other problems with it. What does that mean precisely? CCS811 is always working with that lib, but you have problems elsewhere (humidity sensor/oled). Or does the Adafruit lib also cause problems with CCS811? I see the Adafruit lib is using Wire.setClockStretchLimit(500); maybe that helps.

taunoe commented 5 years ago

When I use CCS811 Adafruit library it works for some time: 30m or 3h. Then i get eCO2 reading 39040ppm and in next loop i will lost connection with CCS811. I added your I2C bus clear library. All other i2C devices will keep working. CCS811 will not respond and recovers only when i manually restart MCU.

I have now second similar CCS811 + 10K pull-ups. Your library works same way like before. I will test now does pull-ups fix my problem or does one CCS811 is just bad.

btw. From one of your example i learned how to use thingspeak.com. Thank You!

maarten-pennings commented 5 years ago

Sorry for the late reply, last week I was on business trip.

When I use CCS811 Adafruit library it works for some time: 30m or 3h. Then i get eCO2 reading 39040ppm and in next loop i will lost connection with CCS811.

Is the 39040 just an example, or do you always get that number? In hex this is 9880, and the 98 looks a bit like the STATUS value. So this might mean the ESP and 811 are out of sync due to I2C problems. Did you ever try to run this 811 board with an non-ESP8266 arduino, just to make sure you have a working 811 board and it is not ESP related I2C problems?

I added your I2C bus clear library. All other I2C devices will keep working. CCS811 will not respond and recovers only when i manually restart MCU.

That is strange. What is the exit code of the bus clear? When you call bus clear and that routine sees ok bus levels, it does not actually do anything on the bus.

How do you restart the 811 after bus clear? Do you call begin() again? What error does begin() report?

I have now second similar CCS811 + 10K pull-ups. Your library works same way like before. I will test now does pull-ups fix my problem or does one CCS811 is just bad.

So, what is the status of my lib with the new board and the pull-ups?

From one of your example i learned how to use thingspeak.com.

Pheww. At least you got one thing running!

taunoe commented 5 years ago

1.After i added pull-ups it seems to be more stable but not error free. For long time it was always same number (39040) but now i am not sure anymore.

One recorded error: https://imgur.com/a/nRB0H6Y

My reset is now this:

else{ Serial.println("CCS811 reading ERROR!"); I2Cbus_clear(SDA,SCL); Wire.begin(); delay(100); digitalWrite(CCSRESET, LOW); delay(300);
digitalWrite(CCSRESET, HIGH); if(!ccs.begin()){ Serial.println("Failed to restart CCS811!"); while(1) delay(1); } }

After that CCS811 works again. And its resolves the problem for me.

  1. I'm testing now your library with Nano (Atmega328p Old Bootloader). 2.1 There seems to be some typos: file:ccs811.h line 64 and 65 ccs811.cpp line 278 and 289 Should be uint16_t not uint16

2.2 First it worked the same way. Lot of waiting before i get data:

Starting CCS811 basic demo init: hardware version: 12 init: bootloader version: 1000 init: application version: 1100 CCS811: errstat=0=--vhxmrwf--ad-ie CCS811: errstat=0=--vhxmrwf--ad-ie CCS811: waiting for (new) data CCS811: waiting for (new) data

After i added delay(500) on beginning. I get more data. 1000 is waiting free (+1000 at the end).

CCS811: eco2=400 ppm etvoc=0 ppb
CCS811: eco2=403 ppm etvoc=0 ppb
CCS811: waiting for (new) data CCS811: eco2=400 ppm etvoc=0 ppb
CCS811: waiting for (new) data CCS811: eco2=400 ppm etvoc=0 ppb
CCS811: eco2=400 ppm etvoc=0 ppb
CCS811: waiting for (new) data CCS811: eco2=400 ppm etvoc=0 ppb
CCS811: eco2=409 ppm etvoc=1 ppb

My testing is been to short to say does CCS811 error appears or not.

maarten-pennings commented 5 years ago

(1a) The problem of a CCS811 reset, is that you loose the internally maintained baseline. (1b) With Serial.printf("setup: I2C bus: %s\n", I2Cbus_statusstr(I2Cbus_clear(SDA,SCL))) you can see what bus clear actually did. (2) I fixed the typos (and some warnings)

I would like to play with your board myself...

maarten-pennings commented 5 years ago

I would like to play with your board myself...

I ordered one chinese clone

maarten-pennings commented 5 years ago

After quite some research, I found a bug in the ESP8266 core libraries, which caused problems when an I2C devices stretches the clock - just as CCS811 does.

See the last paragraph of the ESP8266 section in the readme. You might want to try that! Solved all my problems in the CCS811 driver.

taunoe commented 5 years ago

Thank You. I will try it.

taunoe commented 5 years ago

I've been testing this for a few days now and I think this will fix the problem with CCS811!

maarten-pennings commented 5 years ago

Sounds good.

Still have to convince the esp8266 library engineers to add this fix to the i2c library...

Groetjes,

Maarten

On Sat, Nov 17, 2018, 16:16 Tauno Erik <notifications@github.com wrote:

I've been testing this for a few days now and I think this will fix the problem with CCS811!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/maarten-pennings/CCS811/issues/6#issuecomment-439624507, or mute the thread https://github.com/notifications/unsubscribe-auth/Ac-yw6k7_wPZdT9HiBHEIYcq9w3m1zQJks5uwChdgaJpZM4YNMij .

maarten-pennings commented 5 years ago

It seems my proposed change for the i2c library is accepted, so when there is a new release of the esp8266 library or should work.

Are you happy, can I close this issue?

taunoe commented 5 years ago

Yes i am happy!

maarten-pennings commented 5 years ago

closing