How to convert a CCS811 CircuitPython to Micropython for ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
markserrano
Posts: 1
Joined: Tue Jul 04, 2017 11:24 pm

How to convert a CCS811 CircuitPython to Micropython for ESP8266

Post by markserrano » Sun Jan 07, 2018 1:41 am

I'm trying to read data from a CCS811 sensor. However, the closest existing implementation is written in CircuitPython. I'm trying to convert it to Micropython for ESP8266, but I'm having difficulty translating it.

This is the i2C reference I am using: http://docs.micropython.org/en/latest/e ... achine-i2c

This is the CircuitPython code:

============
buf[0] = CCS811_ALG_RESULT_DATA
with self.i2c_device as i2c:
i2c.write(buf, end=1, stop=False)
i2c.readinto(buf, start=1)

self._eCO2 = (buf[1] << 8) | (buf[2])
self._TVOC = (buf[3] << 8) | (buf[4])
============

My problem is how to specify an end=1 or a start=1 parameter in write and readinto commands since the Micropython version for ESP8266 doesn't have an equivalent parameter.


I asked Adafruit forums initially, and I got the following response:

============
The CircuitPython code is here. It's writing the one byte `buffer[0]` as the command (due to `end=1`, and reading data into `buffer[1]` through `buffer[4]` (due to `start=1`).

buf[0] = CCS811_ALG_RESULT_DATA
with self.i2c_device as i2c:
i2c.write(buf, end=1, stop=False)
i2c.readinto(buf, start=1)

self._eCO2 = (buf[1] << 8) | (buf[2])
self._TVOC = (buf[3] << 8) | (buf[4])

Your MicroPython code is writing the entire 9-byte `buf` out (because `write()` writes out all the bytes in the buf), and then trying to read data back in, starting at `buf[0]`, which doesn't correspond with your choice of 1 through 4 in the value assembly code.

buf = bytearray(9)
buf[0] = self.ALG_RESULT_DATA_REGISTER

self.i2c.write(buf)
self.i2c.readinto(buf)

self.eCO2 = (buf[1] << 8) | (buf[2])
self.TVOC = (buf[3] << 8) | (buf[4])
============

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to convert a CCS811 CircuitPython to Micropython for ESP8266

Post by pythoncoder » Mon Jan 08, 2018 5:56 am

Adafruit don't help by failing to document the start, stop and end args. However I think the intention is to write a single byte and to read back 8 bytes. So I'd try declaring two buffers, a single byte write buffer and an 8 byte read buffer. Something like:

Code: Select all

wbuf = bytearray(1)
buf = bytearray(8)
wbuf[0] = CCS811_ALG_RESULT_DATA
with self.i2c_device as i2c:
    i2c.write(wbuf)
    i2c.readinto(buf)

    self._eCO2 = (buf[0] << 8) | (buf[1])  # Note the index changes
    self._TVOC = (buf[2] << 8) | (buf[3])
I can't test this as I lack the hardware and the woeful Adafruit documentation means I'm guessing here but it's worth a try.
Peter Hinch
Index to my micropython libraries.

tannewt
Posts: 51
Joined: Thu Aug 25, 2016 2:43 am

Re: How to convert a CCS811 CircuitPython to Micropython for ESP8266

Post by tannewt » Mon Jan 08, 2018 5:29 pm

FYI, it is documented here: https://circuitpython.readthedocs.io/pr ... e.readinto
If start or end is provided, then the buffer will be sliced as if buf[start:end]. This will not cause an allocation like buf[start:end] will so it saves memory.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to convert a CCS811 CircuitPython to Micropython for ESP8266

Post by pythoncoder » Tue Jan 09, 2018 6:24 am

OK, my apologies. DuckDuckGo brought up this documentation which lacks this information.
Peter Hinch
Index to my micropython libraries.

tannewt
Posts: 51
Joined: Thu Aug 25, 2016 2:43 am

Re: How to convert a CCS811 CircuitPython to Micropython for ESP8266

Post by tannewt » Wed Jan 10, 2018 6:11 pm

Ah, thanks for the link! We really do need to clean up some of those obsolete docs. Much appreciated.

Patrick Müller
Posts: 1
Joined: Thu Jan 25, 2018 9:05 pm

Re: How to convert a CCS811 CircuitPython to Micropython for ESP8266

Post by Patrick Müller » Thu Jan 25, 2018 9:44 pm

I just wrote a very basic CCS811 MicroPython driver for ESP8266. Have a look: https://github.com/Notthemarsian/CCS811

Post Reply