B4R Question ccs811 help

Discussion in 'B4R Questions' started by giggetto71, Oct 18, 2019.

  1. giggetto71

    giggetto71 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!!


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

    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.

    Code:
    #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
     
  3. giggetto71

    giggetto71 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.
     
  4. thetahsk

    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 ?
     
  5. thetahsk

    thetahsk Active Member Licensed User

    giggetto71 likes this.
  6. giggetto71

    giggetto71 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!!

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

    thetahsk Active Member Licensed User

    try this, not testet because i have only esp32 MCU's

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

    giggetto71 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..
     
  9. thetahsk

    thetahsk Active Member Licensed User

    Update your ESP with Arduino's Board Manager.
    SetClockStretchLimit is defined in
    https://github.com/esp8266/Arduino/blob/master/libraries/Wire/Wire.h
     
  10. giggetto71

    giggetto71 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

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

    Any clue?
     
  11. thetahsk

    thetahsk Active Member Licensed User

    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.

    Code:
    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: Oct 20, 2019
    giggetto71 likes this.
  12. giggetto71

    giggetto71 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!!!
     
  13. thetahsk

    thetahsk Active Member Licensed User

    ..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.
     
  14. giggetto71

    giggetto71 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!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice