working with the esp32 and sps30 on micropython
Posted: 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!
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!