Created
May 20, 2020 03:28
-
-
Save jhiesey/7f6ec481f1253794eb6e23a5d4dd0ede to your computer and use it in GitHub Desktop.
Chinese o2 sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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