Skip to content

Instantly share code, notes, and snippets.

@jhiesey
Created May 20, 2020 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhiesey/7f6ec481f1253794eb6e23a5d4dd0ede to your computer and use it in GitHub Desktop.
Save jhiesey/7f6ec481f1253794eb6e23a5d4dd0ede to your computer and use it in GitHub Desktop.
Chinese o2 sensor
#!/usr/bin/env python3
import serial
import struct
import sys
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]}, <serial port>")
sys.exit(1)
ser = serial.Serial(port=sys.argv[1])
while True:
startByte = ser.read(1)
if startByte[0] != 0x16:
continue
message = startByte + ser.read(11)
if len(message) != 12 or message[1] != 0x09 or message[2] != 0x01 or sum(message) % 256 != 0:
continue
# print(message)
[o2, flow, temp] = [float(val) / 10 for val in struct.unpack('>xxxhhhxxx', message)]
print(f"{o2}% oxygen; flow {flow} l/min; temp {temp} degrees c")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment