sef

Dependencies:   SoftSerial mbed

Fork of kc_mh-z19_CO2sensor by kohacraft Lab

Committer:
jw17594
Date:
Fri Sep 14 09:25:37 2018 +0000
Revision:
2:efdada46c319
Parent:
1:c7a36fa2c772
def

Who changed what in which revision?

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