Working with sps30 and esp32 on micropython

Hello!

I am trying to make the sps30 work with the esp32 but with micropython. I found a library online but it tested using raspberri pi: sps30/sps30.py at master · feyzikesim/sps30 · GitHub so I tried converting it to a usable code for esp32.

I am having problems writing on the i2c bus For example, on the original code:


SPS_ADDR = 0x69
…
R_ARTICLE_CD = [0xD0, 0x25]
…

def read_article_code(self):
result =[]
article_code =[]

    write = i2c_msg.write(self.SPS_ADDR, self.R_ARTICLE_CD)
    self.bus.i2c_rdwr(write)

    read = i2c_msg.read(self.SPS_ADDR, 48)
    self.bus.i2c_rdwr(read)

    for i in range(read.len):
        result.append(bytes_to_int(read.buf[i]))

    if checkCRC(result):
        for i in range (2, len(result), 3):
            article_code.append(chr(result[i-2]))
            article_code.append(chr(result[i-1]))
        return str("".join(article_code))
    else:
        return self.ARTICLE_CODE_ERROR

The code I was able to do was:

SPS_ADDR = b’\x69’
…
R_ARTICLE_CD = bytearray([0xD0, 0x25])
…

def read_article_code(self):
    result = []
    article_code = []

    write = bytearray(self.R_ARTICLE_CD)
    self.i2c.writeto(self.SPS_ADDR, write)

    time.sleep(0.1)

    read = bytearray(48)
    self.i2c.readfrom_into(self.SPS_ADDR, read)
    
    for i in range(read.len):
        result.append(bytes_to_int(read.buf[i]))

    if checkCRC(result):
        for i in range (2, len(result), 3):
            article_code.append(chr(result[i-2]))
            article_code.append(chr(result[i-1]))
        return str("".join(article_code))
    else:
        return self.ARTICLE_CODE_ERROR

The terminal outputs TypeError: can’t convert bytes to int on the self.i2c.writeto(self.SPS_ADDR, write) line. I’m guessing this would happen on the read part also.

I am hoping that maybe someone here could help me? Thanks!

I’ve never used microPython, only regular Python, but to me it looks like it’s complaining because you changed what’s passed from an integer (SPS_ADDR = 0x69) to bytes (SPS_ADDR = b’\x69)

when I do that, it outputs: OSError: [Errno 116] ETIMEDOUT on the same line. I checked my wirings and i think they are still good. Any other ideas?

It appears MicroPython does not support the standard Python smbus2 library. Which means MicroPython also does not support this sps30 library.

I think you are supposed to use the MicroPython Machine library for your processor when using I2C.
https://docs.micropython.org/en/latest/library/machine.I2C.html

I searched around and it appears nobody has written a device driver library for the SPS30 sensor that is compatible with MicroPython.

I did find a driver written for CircuitPython.

So if you don’t mind switching the project from MicroPython to CircuitPython, I suspect you’ll have a much easier time getting it to wok.

1 Like