B4R Question [RESOLVED] ccs811 help

giggetto71

Active Member
Licensed User
All,
I am getting a very strange behavior from a CCS811 gas sensor used with a Wemos D1.
I have followed the recommendations from the below case

https://www.b4x.com/android/forum/threads/normal-behavior.81417/#post-516437

and actually the I2C readings/writings from/to the sensor work just fine for a while but after some time (I was not able to identify any root cause) upon the status request (see below code) for some reasons the sensor does not reply with the status byte and (I believe) does not reply at all because the "readbyte.lenght" is NOT 1 and from that moment on the sensor stops responding to I2C requests..
the only way to restore it is via HW reset pin which of course takes it back to the default 400 reading for CO2 even if before the rest the sensor was sensing lets' say 1000 (just an example..).
I have tried everything I could think..like retrying..(the sensor actually never recovers until you reset it..so rethy won't work), re-initializing the I2C bus (no luck)...

Does anybody have a clue?
THANKS!!


B4X:
    Private readbyte(1) As Byte
    Private writebyte(1) As Byte
  
    writebyte(0) = STATUS
    
     master.WriteTo2(CSS811_ADDRESS,True,writebyte)
     Delay (50)
     readbyte = master.RequestFrom(CSS811_ADDRESS,1)
        
     If readbyte.Length=1 Then
....
 

thetahsk

Active Member
Licensed User
It would be best if you include the SparkFun CCS811 library.
https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library

If you look at the source code, you can see that there are some time-critical routines.
e.g.

B4X:
#if defined(ARDUINO_ARCH_ESP8266)
    _i2cPort->setClockStretchLimit(200000);// was default 230 uS, now 200ms
#endif
For accurate readings, the values must also be temperature compensated.

What firmware do you have on your CCS811 chip ?
https://ams.com/ccs811
https://github.com/maarten-pennings/CCS811/tree/master/examples/ccs811flash

Read this discussion:
https://github.com/maarten-pennings/CCS811/issues/8

I hope that I could help
 

giggetto71

Active Member
Licensed User
Thanks. Is there a b4r wrapper for that lib? How do I include that in my b4r project? Right now I am using rWire .
As for FW I have 1.1. Hw version 18.
 

thetahsk

Active Member
Licensed User
I think there is no wrapper, but it's easy to include it with inline c. Do you have an original one from Sparkfun or an chinese clone ?
 

giggetto71

Active Member
Licensed User
thanks! I have got the one from Adafruit. As for inline C I have already used it to call the
SetClock method which was not exposed out of the Wire lib, but I could not find in that library the "SetClockStretchLimit" method which I also found in some forum as a good thing..
Could you show me how to include that method and library with inline C please?
I would really appreciate it.
Thanks!!

B4X:
#if C
void SetWireClock(B4R::Object* o) {
   Wire.setClock (o->toULong());
}
#end if
 

thetahsk

Active Member
Licensed User
try this, not testet because i have only esp32 MCU's

B4X:
#if C
#include <Wire.h>
void SetWireClock(B4R::Object* o) 
 {
   Wire.setClockStretchLimit(o->toULong());
 }
#end if
 

giggetto71

Active Member
Licensed User
Nope..the wire.h and wire.cpp versions I have under avr\libraries don't have the method SetClockStretchLimit so B4R won't compile. I am looking for a wire.h and wire.cpp with that method but had not much luck so far..
 

giggetto71

Active Member
Licensed User
I think I need some guidance here..I have update all I could boards, arduino,etc. but still if I try to use the SetClockStretchLimit is defined inWire.h my B4R won't complile.
after the upgrade I found the wire.h which included the SetClockStretchLimit under:

C:\Users\105028248.LOGON\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\libraries\Wire


but the one under

C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src

was still showing the version of Wire.h without the SetClockStretchLimit (probably an old version). I have also tried to delete the old ones in avr\libraries\wire\src and replace with the wire.h and wire.cpp from esp8266 but still B4R fails to compile if I leave

B4X:
#if C
#include <Wire.h>
void setClockStretchLimit(B4R::Object* o)
 {
   Wire.setClockStretchLimit(o->toULong());
 }
#end if

Any clue?
 

thetahsk

Active Member
Licensed User
I think I need some guidance here..I have update all I could boards, arduino,etc. but still if I try to use the SetClockStretchLimit is defined inWire.h my B4R won't complile.
after the upgrade I found the wire.h which included the SetClockStretchLimit under:
Try to reinstall Arduino Version 1.8.9 to make sure that all libs are untouched. Every hardware platform has it's own different header files.
I have installed for testing the esp8266 libs on my machine and tried this snipped with checked rWire Version 1.10.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    RunNative("set_ClockStretchLimit",200000)
End Sub

#if C
#include <Wire.h>
void set_ClockStretchLimit(B4R::Object* o)
 {
   Wire.setClockStretchLimit(o->toULong());
 }
#end if
No compiling error but an upload error.
So it should work.
 
Last edited:

giggetto71

Active Member
Licensed User
UPDATE: got the sensor update to FW version 2.0 and I was able thans to thetahsk to set the stretch limit. now it's working withhout issues from more than 2 hrs...maybe not enogh for the champagne but I am getting optimistic!!!
THANKS!!!
 

thetahsk

Active Member
Licensed User
UPDATE: got the sensor update to FW version 2.0 and I was able thans to thetahsk to set the stretch limit. now it's working withhout issues from more than 2 hrs...maybe not enogh for the champagne but I am getting optimistic!!!
THANKS!!!
..and don't forget to update the firmware to 2.0.1 after the sensors burn-in time. Furthermore you have to solve the temperature problem with an external sensor, because AMS firmware V2 has switched off the NTC function.
 

giggetto71

Active Member
Licensed User
ok. thanks but I am about to give up..after several hrs of test the sensor keeps stops respongin from time to time (way better than with version 1.1 but still..)
I will make a one more attempt with 2.1 and then trash it.
thanks again!
 

giggetto71

Active Member
Licensed User
All, I just wanted to give a quick update on this as I finally found the solution to avoid the sensor to stop responding randolmly. At the end I had to avoid to send the stop bit in the Write2. Basically now it runs without issue. thanks

B4X:
Private readbyte(1) As Byte
    Private writebyte(1) As Byte
 
    writebyte(0) = STATUS
    
     master.WriteTo2(CSS811_ADDRESS,False,writebyte)
     Delay (50)
     readbyte = master.RequestFrom(CSS811_ADDRESS,1)
        
     If readbyte.Length=1 Then
 
Top