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])
============
How to convert a CCS811 CircuitPython to Micropython for ESP8266
-
- Posts: 1
- Joined: Tue Jul 04, 2017 11:24 pm
- 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
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:
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.
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])
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to convert a CCS811 CircuitPython to Micropython for ESP8266
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.
- 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
OK, my apologies. DuckDuckGo brought up this documentation which lacks this information.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to convert a CCS811 CircuitPython to Micropython for ESP8266
Ah, thanks for the link! We really do need to clean up some of those obsolete docs. Much appreciated.
-
- Posts: 1
- Joined: Thu Jan 25, 2018 9:05 pm
Re: How to convert a CCS811 CircuitPython to Micropython for ESP8266
I just wrote a very basic CCS811 MicroPython driver for ESP8266. Have a look: https://github.com/Notthemarsian/CCS811