
read from mh-z19 CO2 sensor with LPC1114
Revision 1:c7a36fa2c772, committed 2017-09-29
- Comitter:
- kohacraft
- Date:
- Fri Sep 29 03:15:40 2017 +0000
- Parent:
- 0:963c0badad70
- Commit message:
- bug fixed
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Sep 27 06:51:22 2017 +0000 +++ b/main.cpp Fri Sep 29 03:15:40 2017 +0000 @@ -1,42 +1,79 @@ -#include "mbed.h" -#include "SoftSerial.h" +/* + Read from MH-Z19 CO2 Sensor by serial + and show data to PC throw the softserial + for LPC1114FN28 + + MH-Z19B Datasheet http://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf + Also reference https://github.com/nara256/mhz19_uart + + please see the blog in detail + http://kohacraft.com/archives/1068007971.html + +*/ -SoftSerial softSerial(dp18,dp17); //Tx,Rx -Serial pc(USBTX, USBRX); //Tx,Rx +#include "mbed.h" +#include "SoftSerial.h" // by Erick Olieman https://os.mbed.com/users/Sissors/code/SoftSerial/ + +SoftSerial pc(dp18,dp17); //Tx,Rx to USB serial (mbed cat) +Serial co2sensor(USBTX, USBRX); //Tx,Rx to mh-z19 CO2 sensor int main() { - softSerial.baud(9600); //to mbed cat - pc.baud(9600); // to mh-z19 CO2 sensor + pc.baud(9600); + co2sensor.baud(9600); char command[] = { 0xff , 0x01 , 0x86 , 0 , 0 , 0 , 0 , 0 , 0x79 }; //command of Read CO2 concentration char responds[9]; - unsigned char c; - if( pc.readable() ) // throw received data away - responds[0] = pc.getc( ); - + while( co2sensor.readable() ) // throw received data away + responds[0] = co2sensor.getc(); + while( pc.readable() ) // throw received data away + responds[0] = pc.getc(); + + long time=0; + while(1) { //send command for( int i=0 ; i<9 ; i++ ) { - while( pc.writeable() == 0 ); - pc.putc( command[i] ); + while( co2sensor.writeable() == 0 ); //wait for writable a data + co2sensor.putc( command[i] ); } //receve responds - c=0; //calc check sum - softSerial.printf(" responds:"); - for( int i=0 ; i<9 ; i++ ) { - responds[i] = pc.getc( ); - softSerial.printf("%x ",responds[i]); - c += responds[i]; + unsigned char csm=0; //clear check sum + for( int i=0 ; i<9 ; i++ ) //clear responds variable + responds[i] = 0; + pc.printf("responds:"); + int i=0; + for( int timeout = 0 ; timeout < 10000 ; timeout++ ) { + + if( co2sensor.readable() ) { + responds[i] = co2sensor.getc( ); + pc.printf("%x ",responds[i]); + + if( responds[0] == 0xff ) { // wether the first data is 0xff + // sdd check sum + csm += responds[i]; + timeout = 0; + i++; + if( i>8 ) + break; // received all data + } + } } - c -= responds[8]; - c = ( 0xff - c ); - softSerial.printf(" csm:%x CO2:%dppm temp:%dC stat:%d\r" , c , responds[2]*256 + responds[3] , responds[4]-40 , responds[5]); + + //check the check sum and show to PC + csm -= responds[8]; + csm = ( 0xff - csm ); + if( responds[8] == csm ) { // compare with check sum + pc.printf("csm:%x CO2:%dppm temp:%dC stat:%d," , csm , responds[2]*256 + responds[3] , responds[4]-40 , responds[5]); + pc.printf("%d,%d,%d,%d\r" , time , responds[2]*256 + responds[3] , responds[4]-40 , responds[5]); + } + wait(1); + time++; } }