Skip to content

Instantly share code, notes, and snippets.

@vkoloss
Forked from UedaTakeyuki/mh_z19.py
Created December 26, 2016 08:58
Show Gist options
  • Save vkoloss/eb0a4ce03f131c16799a4a63912e43eb to your computer and use it in GitHub Desktop.
Save vkoloss/eb0a4ce03f131c16799a4a63912e43eb to your computer and use it in GitHub Desktop.
MH-Z19 CO2 Sensor reading.
# http://eleparts.co.kr/data/design/product_file/SENSOR/gas/MH-Z19_CO2%20Manual%20V2.pdf
# http://qiita.com/UedaTakeyuki/items/c5226960a7328155635f
import serial
import time
def mh_z19():
ser = serial.Serial('/dev/ttyAMA0',
baudrate=9600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1.0)
while 1:
result=ser.write("\xff\x01\x86\x00\x00\x00\x00\x00\x79")
s=ser.read(9)
if s[0] == "\xff" and s[1] == "\x86":
return {'co2': ord(s[2])*256 + ord(s[3])}
break
if __name__ == '__main__':
value = mh_z19()
print "co2=", value["co2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment