read from mh-z19 CO2 sensor with LPC1114

Dependencies:   SoftSerial mbed

Files at this revision

API Documentation at this revision

Comitter:
kohacraft
Date:
Wed Sep 27 06:51:22 2017 +0000
Child:
1:c7a36fa2c772
Commit message:
read from mh-z19 CO2 sensor with LPC1114

Changed in this revision

SoftSerial.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftSerial.lib	Wed Sep 27 06:51:22 2017 +0000
@@ -0,0 +1,1 @@
+http://os.mbed.com/users/Sissors/code/SoftSerial/#a0029614de72
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 27 06:51:22 2017 +0000
@@ -0,0 +1,42 @@
+#include "mbed.h"
+#include "SoftSerial.h"
+
+SoftSerial softSerial(dp18,dp17); //Tx,Rx
+Serial pc(USBTX, USBRX); //Tx,Rx
+
+int main() {
+    
+    softSerial.baud(9600);  //to mbed cat
+    pc.baud(9600);  // to mh-z19 CO2 sensor
+    
+    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(1) {
+        
+        //send command
+        for( int i=0 ; i<9 ; i++ ) {
+            while( pc.writeable() == 0 );
+            pc.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];
+        }
+        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]);
+        wait(1);
+
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Sep 27 06:51:22 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/f9eeca106725
\ No newline at end of file