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!

Byte is int8 or uint8
Int is int16 or uint16

I think it is the issue.

Here you convert : result.append(bytes_to_int(read.buf[i]))

It seems that I2C want some int array and not a byte array.

do i conver the bytes to int then convert it back when i need it?

I think it is what micropython expects according to the error message.