read from mh-z19 CO2 sensor with LPC1114

Dependencies:   SoftSerial mbed

Committer:
kohacraft
Date:
Fri Sep 29 03:15:40 2017 +0000
Revision:
1:c7a36fa2c772
Parent:
0:963c0badad70
bug fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kohacraft 1:c7a36fa2c772 1 /*
kohacraft 1:c7a36fa2c772 2 Read from MH-Z19 CO2 Sensor by serial
kohacraft 1:c7a36fa2c772 3 and show data to PC throw the softserial
kohacraft 1:c7a36fa2c772 4 for LPC1114FN28
kohacraft 1:c7a36fa2c772 5
kohacraft 1:c7a36fa2c772 6 MH-Z19B Datasheet http://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf
kohacraft 1:c7a36fa2c772 7 Also reference https://github.com/nara256/mhz19_uart
kohacraft 1:c7a36fa2c772 8
kohacraft 1:c7a36fa2c772 9 please see the blog in detail
kohacraft 1:c7a36fa2c772 10 http://kohacraft.com/archives/1068007971.html
kohacraft 1:c7a36fa2c772 11
kohacraft 1:c7a36fa2c772 12 */
kohacraft 0:963c0badad70 13
kohacraft 1:c7a36fa2c772 14 #include "mbed.h"
kohacraft 1:c7a36fa2c772 15 #include "SoftSerial.h" // by Erick Olieman https://os.mbed.com/users/Sissors/code/SoftSerial/
kohacraft 1:c7a36fa2c772 16
kohacraft 1:c7a36fa2c772 17 SoftSerial pc(dp18,dp17); //Tx,Rx to USB serial (mbed cat)
kohacraft 1:c7a36fa2c772 18 Serial co2sensor(USBTX, USBRX); //Tx,Rx to mh-z19 CO2 sensor
kohacraft 0:963c0badad70 19
kohacraft 0:963c0badad70 20 int main() {
kohacraft 0:963c0badad70 21
kohacraft 1:c7a36fa2c772 22 pc.baud(9600);
kohacraft 1:c7a36fa2c772 23 co2sensor.baud(9600);
kohacraft 0:963c0badad70 24
kohacraft 0:963c0badad70 25 char command[] = { 0xff , 0x01 , 0x86 , 0 , 0 , 0 , 0 , 0 , 0x79 }; //command of Read CO2 concentration
kohacraft 0:963c0badad70 26 char responds[9];
kohacraft 0:963c0badad70 27
kohacraft 0:963c0badad70 28
kohacraft 1:c7a36fa2c772 29 while( co2sensor.readable() ) // throw received data away
kohacraft 1:c7a36fa2c772 30 responds[0] = co2sensor.getc();
kohacraft 1:c7a36fa2c772 31 while( pc.readable() ) // throw received data away
kohacraft 1:c7a36fa2c772 32 responds[0] = pc.getc();
kohacraft 1:c7a36fa2c772 33
kohacraft 1:c7a36fa2c772 34 long time=0;
kohacraft 1:c7a36fa2c772 35
kohacraft 0:963c0badad70 36 while(1) {
kohacraft 0:963c0badad70 37
kohacraft 0:963c0badad70 38 //send command
kohacraft 0:963c0badad70 39 for( int i=0 ; i<9 ; i++ ) {
kohacraft 1:c7a36fa2c772 40 while( co2sensor.writeable() == 0 ); //wait for writable a data
kohacraft 1:c7a36fa2c772 41 co2sensor.putc( command[i] );
kohacraft 0:963c0badad70 42 }
kohacraft 0:963c0badad70 43
kohacraft 0:963c0badad70 44 //receve responds
kohacraft 1:c7a36fa2c772 45 unsigned char csm=0; //clear check sum
kohacraft 1:c7a36fa2c772 46 for( int i=0 ; i<9 ; i++ ) //clear responds variable
kohacraft 1:c7a36fa2c772 47 responds[i] = 0;
kohacraft 1:c7a36fa2c772 48 pc.printf("responds:");
kohacraft 1:c7a36fa2c772 49 int i=0;
kohacraft 1:c7a36fa2c772 50 for( int timeout = 0 ; timeout < 10000 ; timeout++ ) {
kohacraft 1:c7a36fa2c772 51
kohacraft 1:c7a36fa2c772 52 if( co2sensor.readable() ) {
kohacraft 1:c7a36fa2c772 53 responds[i] = co2sensor.getc( );
kohacraft 1:c7a36fa2c772 54 pc.printf("%x ",responds[i]);
kohacraft 1:c7a36fa2c772 55
kohacraft 1:c7a36fa2c772 56 if( responds[0] == 0xff ) { // wether the first data is 0xff
kohacraft 1:c7a36fa2c772 57 // sdd check sum
kohacraft 1:c7a36fa2c772 58 csm += responds[i];
kohacraft 1:c7a36fa2c772 59 timeout = 0;
kohacraft 1:c7a36fa2c772 60 i++;
kohacraft 1:c7a36fa2c772 61 if( i>8 )
kohacraft 1:c7a36fa2c772 62 break; // received all data
kohacraft 1:c7a36fa2c772 63 }
kohacraft 1:c7a36fa2c772 64 }
kohacraft 0:963c0badad70 65 }
kohacraft 1:c7a36fa2c772 66
kohacraft 1:c7a36fa2c772 67 //check the check sum and show to PC
kohacraft 1:c7a36fa2c772 68 csm -= responds[8];
kohacraft 1:c7a36fa2c772 69 csm = ( 0xff - csm );
kohacraft 1:c7a36fa2c772 70 if( responds[8] == csm ) { // compare with check sum
kohacraft 1:c7a36fa2c772 71 pc.printf("csm:%x CO2:%dppm temp:%dC stat:%d," , csm , responds[2]*256 + responds[3] , responds[4]-40 , responds[5]);
kohacraft 1:c7a36fa2c772 72 pc.printf("%d,%d,%d,%d\r" , time , responds[2]*256 + responds[3] , responds[4]-40 , responds[5]);
kohacraft 1:c7a36fa2c772 73 }
kohacraft 1:c7a36fa2c772 74
kohacraft 0:963c0badad70 75 wait(1);
kohacraft 1:c7a36fa2c772 76 time++;
kohacraft 0:963c0badad70 77
kohacraft 0:963c0badad70 78 }
kohacraft 0:963c0badad70 79 }