Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grove_co2_lib.py not compatible with python3 #505

Closed
danaelg opened this issue Sep 5, 2021 · 1 comment
Closed

grove_co2_lib.py not compatible with python3 #505

danaelg opened this issue Sep 5, 2021 · 1 comment

Comments

@danaelg
Copy link

danaelg commented Sep 5, 2021

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/)           
#
########################################################################

import serial, time

class CO2:
#inspired from c code of http://www.seeedstudio.com/wiki/Grove_-_CO2_Sensor
#Gas concentration= high level *256+low level
	inp = []
	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 port
                ser = serial.Serial('/dev/serial0', 9600, timeout = 1)	#Open the serial port at 9600 baud

		#init serial
		ser.flush()
		
	def read(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, temp
			conc = high_level*256+low_level
			return [conc,temp_co2]

		except IOError:
			return [-1,-1]
			
if __name__ == "__main__":		
	c = CO2()
	while True:
		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.

@CleoQc
Copy link
Member

CleoQc commented Sep 7, 2021

Thank you for your contribution!

@CleoQc CleoQc closed this as completed Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants