Gravity UART CO2 (0 - 50000 ppm) - Connecting to Jetson Nano
I am trying to read out concentrations for the Jetson Nano instead of the Arduino environment, and am unsuccessful.
I know that the query frame sent to the sensor should be:
0xFF 0x01 0x86 0x00 0x00 0x00 0x00 0x00 0x79
The sensor will then send an 8 byte reply frame.
The third byte is the high byte of the CO2 concentration.
The fourth byte is the low byte of the CO2 concentration.
However, my sensor script in python3 sends the query frame and reads the response as: b'', indicating no data transmitted back. I have ensured the correct baudrate and serial port, as well as the physical GPIO connection. Can someone please take a look at my script to see what I can fix?
import serial
import time
serial_port = '/dev/tty0'
baudrate = 9600
timeout = 10
def read_co2_concentration(ser):
while True:
if not ser.is_open:
print("Serial port is not open.")
break # Exit the loop if the serial port is closed
command = [0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79]
ser.flushInput()
ser.flushOutput()
ser.write(bytes(command))
print("Waiting for response...")
time.sleep(2) # Adjust the sleep time as needed for your sensor
response = ser.read(8)
print(f"Raw response: {response}")
if len(response) == 8:
print("Reading new data")
hi = response[2]
lo = response[3]
co2_concentration = (hi << 8) | lo
print(f"CO2 concentration: {co2_concentration}ppm")
else:
print("No data or incomplete data received")
time.sleep(1)
try:
ser = serial.Serial(serial_port, baudrate, timeout=timeout)
print(f"Opened serial port {serial_port} at {baudrate} baud.")
print("Waiting for sensor preheating.")
time.sleep(5) # Wait for sensor to preheat and stabilize
read_co2_concentration(ser)
except serial.SerialException as e:
print(f"Error opening serial port {serial_port}: {e}")
except KeyboardInterrupt:
print("Program terminated by user.")
finally:
if 'ser' in locals() or 'ser' in globals() and ser.is_open:
ser.close()
print("Serial port closed.")