You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using the MH-Z16 CO2 sensor with grove_co2_lib.py and we discovered that it is not compatible with Python 3.
We updated the code as follow to make it compatible.
#!/usr/bin/env python######################################################################## # GrovePi Library for using the Grove - CO2 Sensor(http://www.seeedstudio.com/depot/Grove-CO2-Sensor-p-1863.html)## The GrovePi connects the Raspberry Pi and Grove sensors. You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi## Have a question about this library? Ask on the forums here: http://forum.dexterindustries.com/c/grovepi# ## NOTES: # * Calibration and read of the CO2 sensor MH-Z16 according to the datasheet : http://www.seeedstudio.com/wiki/images/c/ca/MH-Z16_CO2_datasheet_EN.pdf# * output value directly in ppm# * Library derived from the inital controbution by Doms Genoud (@domsgen) here: http://www.dexterindustries.com/topic/how-to-use-co2-grove-sensor-with-serial-grovepi/# # History# ------------------------------------------------# Author Date Comments# Doms Genoud 13 Apr 15 Initial Authoring# Karan 07 Jan 16 Code cleanup and added to main Github repo# # These files have been made available online through a Creative Commons Attribution-ShareAlike 3.0 license.# (http://creativecommons.org/licenses/by-sa/3.0/) #########################################################################importserial, timeclassCO2:
#inspired from c code of http://www.seeedstudio.com/wiki/Grove_-_CO2_Sensor#Gas concentration= high level *256+low levelinp= []
cmd_zero_sensor=b'\xff\x87\x87\x00\x00\x00\x00\x00\xf2'cmd_span_sensor=b'\xff\x87\x87\x00\x00\x00\x00\x00\xf2'cmd_get_sensor=b'\xff\x01\x86\x00\x00\x00\x00\x00\x79'def__init__(self):
#To open the raspberry serial portser=serial.Serial('/dev/serial0', 9600, timeout=1) #Open the serial port at 9600 baud#init serialser.flush()
defread(self):
try:
ser.write(self.cmd_get_sensor)
self.inp=ser.read(9)
high_level=self.inp[2]
low_level=self.inp[3]
temp_co2=self.inp[4] -40#output in ppm, tempconc=high_level*256+low_levelreturn [conc,temp_co2]
exceptIOError:
return [-1,-1]
if__name__=="__main__":
c=CO2()
whileTrue:
print(c.read())
time.sleep(1)
I've never contributed to a public github project so I preferred to create an issue instead of making a PR.
The text was updated successfully, but these errors were encountered:
I'm using the MH-Z16 CO2 sensor with grove_co2_lib.py and we discovered that it is not compatible with Python 3.
We updated the code as follow to make it compatible.
I've never contributed to a public github project so I preferred to create an issue instead of making a PR.
The text was updated successfully, but these errors were encountered: