Senseair S8 sensor not transmitting anything, Micropython on the Pi Pico rp2040 #10409
-
Hello there, I have recently aquired a co2 sensor called Senseair S8. A technical sheet on how to use it can be found here. I have been trying to read data from the sensor from a Pi Pico 2040. For the code I tried to follow the senseair-s8 library you can find on github here. import time
import serial
class SenseairS8:
def __init__(self, port="/dev/ttyS0", baudrate=9600, timeout=.5):
self.ser = serial.Serial(port, baudrate=baudrate, timeout=timeout)
self.ser.flushInput()
def co2(self):
try:
self.ser.flushInput()
self.ser.write(b"\xFE\x44\x00\x08\x02\x9F\x25")
time.sleep(1)
response = self.ser.read(7)
high = response[3]
low = response[4]
co2 = (high*256) + low
except Exception as e:
co2 = None
print(e)
return co2 Now, micropython does not have the pyserial library. From what I understand, I instead need to use machine.UART. So I came up with this bit of code : from machine import UART, Pin
from time import sleep
def main():
uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5), timeout=500)
uart1.init(9600, bits=8, parity=None, stop=1)
uart1.flush()
sleep(1)
while True:
uart1.flush()
sequence_to_send = b'\xFE\x44\x00\x08\x02\x9F\x25'
uart1.write(sequence_to_send)
sleep(1)
if uart1.any():
resp = uart1.read(7)
high = ord(resp[3])
low = ord(resp[4])
co2 = hight * 256 + low
print(co2)
else:
print("Nothing to read...")
if __name__ == "__main__":
main() Now here is my issue : I don't seem to be receiving any data at all. 'uart1.any()' always returns '0', which means there never is anything to read. I don't quite understand where my mistake is... Any ideas ? Thank you in advance for your answers ! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Just a few confirmation questions:
Remove the uart.init() statement. It may change values that you have set defore. All parameters you need can be included in the constructor. |
Beta Was this translation helpful? Give feedback.
Just a few confirmation questions:
Remove the uart.init() statement. It may change values that you have set defore. All parameters you need can be included in the constructor.
You could try to swap RX and TX. Sometines the labelling at devices is reversed, showing the expected connection.