Jump to content
  • 0

OPi PC+ and MH-Z19


Scandinav21
 Share

Question

Armbianmonitor:

Hi everyone.

I almost gave up while reading tons of forums:(

I bought CO2 sensor MH-Z19 from aliexpress.

As I figured out this sensor used by RPi without any problems. Manual I found here:

https://www.circuits.dk/testing-mh-z19-ndir-co2-sensor-module/

I connected module as mentioned:

Vin - 2 pin gpio
GND - 9 pin
Tx - 13 pin
Rx - 11 pin

Module was blinking when I powered on my OPi, but when I tried to send data to UART nothing happened. I got timeout.

I tried to connect Tx and Rx (11 and 13 pin) by cable, then sent bytes and immediatley got answer, UART work, as I understood.

I checked uart in armbian-config menu and all (1,2,3) was enabled.

Orange PI PC Plus
Linux orangepipcplus 5.4.30-sunxi #20.02.9 SMP Mon Apr 6 19:46:24 CEST 2020 armv7l GNU/Linux

Can somebody help me, please?

 

Spoiler

import serial
import time
import subprocess
import getrpimodel

serial_dev = '/dev/ttyS2'
stop_getty = 'sudo systemctl stop serial-getty@ttyS0.service'
start_getty = 'sudo systemctl start serial-getty@ttyS0.service'

def mh_z19():
    ser = serial.Serial(serial_dev,
                        baudrate=9600,
                        bytesize=serial.EIGHTBITS,
                        parity=serial.PARITY_NONE,
                        stopbits=serial.STOPBITS_ONE,
                        timeout=1.0)
    while 1:
        s=ser.read(9)
        result=ser.write("\xff\x01\x86\x00\x00\x00\x00\x00\x79")
        s=ser.read(9)
        print(s)
        if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86":
            return {'co2': ord(s[2])*256 + ord(s[3])}
        break

def read():
    p = subprocess.call(stop_getty, stdout=subprocess.PIPE, shell=True)
    result = mh_z19()
    p = subprocess.call(start_getty, stdout=subprocess.PIPE, shell=True)
    if result is not None:
        return {'CO2': result["co2"]}

if __name__ == '__main__':
#  value = mh_z19()
#  print "co2=", value["co2"]
    value = read()
    if value is not None:
        print "co2=", value["CO2"]
    else:
        print "None"

 

 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Donate and support the project!

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...