working with the esp32 and sps30 on micropython

senruuu
Posts: 1
Joined: Mon May 08, 2023 12:23 am

working with the esp32 and sps30 on micropython

Postby senruuu » Mon May 08, 2023 12:26 am

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](https://github.com/feyzikesim/sps30/blo ... 0/sps30.py) 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))

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))

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!

bidrohini
Posts: 202
Joined: Thu Oct 27, 2022 12:55 pm

Re: working with the esp32 and sps30 on micropython

Postby bidrohini » Mon May 08, 2023 8:24 am

The issue with your code is that you are passing a bytes object (b'\x69') instead of an integer value (0x69) as the address parameter to the writeto() method of the I2C bus. The writeto() method expects an integer value representing the I2C device address.

You can fix this by changing the line where you define SPS_ADDR to:

Code: Select all

  SPS_ADDR = 0x69   
Alternatively, you can convert the bytes object to an integer using the int.from_bytes() method:

Code: Select all

 SPS_ADDR = int.from_bytes(b'\x69', byteorder='big') 
This should resolve the TypeError issue you are facing.

Additionally, I noticed that in the original code, the read.len property is used to get the length of the received data. However, in your code, you are trying to access read.len, which is not a valid attribute. Instead, you should use the len() function to get the length of the received data:

Code: Select all

  for i in range(len(read)):
    result.append(read[i]) 
Similarly, you should use len() to get the length of result list:

Code: Select all

    for i in range (2, len(result), 3):  
Once you make these changes, your code should work correctly.

Who is online

Users browsing this forum: No registered users and 123 guests