Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created March 17, 2018 08:57
Show Gist options
  • Save yoggy/c0e1bb116124039cf04cd817b89ae6c8 to your computer and use it in GitHub Desktop.
Save yoggy/c0e1bb116124039cf04cd817b89ae6c8 to your computer and use it in GitHub Desktop.
//
// mh-z14-calibration-test.pde
//
// see also... https://www.openhacks.com/uploadsproductos/mh-z14_co2.pdf
//
import processing.serial.*;
byte [] buf = new byte[9];
Serial s = new Serial(this, "COM9", 9600);
// calibrate zero point
s.write(0xff); // header
s.write(0x01); // sensor no
s.write(0x87); // command
s.write(0x00);
s.write(0x00);
s.write(0x00);
s.write(0x00);
s.write(0x00);
s.write(0x78); // check value
delay(500);
// read return value (9byte)
print("read value : ");
for (int i = 0; i < 9; ++i) {
buf[i] = (byte)s.read();
print(hex(buf[i]));
print(", ");
}
println();
// read gas soncentration
s.write(0xff); // header
s.write(0x01); // sensor no
s.write(0x86); // command
s.write(0x00);
s.write(0x00);
s.write(0x00);
s.write(0x00);
s.write(0x00);
s.write(0x79); // check value
delay(500);
// read return value (9byte)
print("read value : ");
for (int i = 0; i < 9; ++i) {
buf[i] = (byte)s.read();
print(hex(buf[i]));
print(", ");
}
println();
int val = Byte.toUnsignedInt(buf[2]) * 256 + Byte.toUnsignedInt(buf[3]);
println("co2=" + val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment