Working with the sps30 and esp32 in 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 hing that maybe someone here could help me? Thanks!

I think the conversions you are doing are unnecessary. The ESP32 I2C example uses software I2C via a bit-banging method through the machine.SoftI2C class.

Here is the MicroPython Quick Reference for the ESP32 which includes an I2C example

hi snes,
did you manage to get the sps30 working with micropython? would be interested on how you did it, as i want to get it working too - cheers!
thanks!

1 Like

I think he got his answer here. Working with sps30 and esp32 on micropython - Forum Sensor.Community

For a comparative result, you can try Nova PM Sensor SDS011.

1 Like