Daisuke Yanagihara / Mbed 2 deprecated CLTP_MH-Z19_Test1

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Yanagihara
Date:
Tue Aug 30 02:52:31 2022 +0000
Parent:
2:9ec8d00db1a9
Commit message:
CO2 test

Changed in this revision

MH_Z19.cpp Show annotated file Show diff for this revision Revisions of this file
MH_Z19.h 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MH_Z19.cpp	Tue Aug 30 02:52:31 2022 +0000
@@ -0,0 +1,47 @@
+/* mbed MH-Z19 Library
+ * Copyright (c) 2016, zeus3110
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "MH_Z19.h"
+
+MH_Z19::MH_Z19(PinName p) : Port(p) {
+    Port.rise(this, &MH_Z19::rise);
+    Port.fall(this, &MH_Z19::fall);
+    OnWidth=2;
+    T.start();
+}
+
+int MH_Z19::ReadCO2PPM() {
+    
+    return ((CO2_MAX_PPM)*(OnWidth-2)/(PWM_CYCLE_MS-4));
+
+}
+    
+
+void MH_Z19::rise() {
+    T.reset();
+}
+
+void MH_Z19::fall() {
+    OnWidth=T.read_ms();
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MH_Z19.h	Tue Aug 30 02:52:31 2022 +0000
@@ -0,0 +1,52 @@
+/* mbed MH-Z19 Library
+ * Copyright (c) 2016, zeus3110
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MH_Z19_H
+#define MH_Z19_H
+
+#include "mbed.h"
+
+#define PWM_CYCLE_MS 1004
+#define CO2_MAX_PPM 5000
+
+class MH_Z19 {
+public:
+    /** Create a MH_Z19
+     *
+     * @param Port The pwm input pin (must support InterruptIn)
+     */ 
+    MH_Z19(PinName Port);
+
+    int ReadCO2PPM();
+
+protected:        
+    void rise();
+    void fall();
+    
+    InterruptIn Port;
+    Timer T;
+    int OnWidth;
+};
+
+#endif
+
+
--- a/main.cpp	Fri Oct 30 05:13:46 2020 +0000
+++ b/main.cpp	Tue Aug 30 02:52:31 2022 +0000
@@ -1,27 +1,106 @@
-#include"mbed.h"
+#include "mbed.h"
+#include "MH_Z19.h"
 
-Serial pc(USBTX,USBRX);
+MH_Z19 Sen(p5);
+Serial CO2(p9,p10);
+ 
+DigitalOut condition(LED1);
+Serial gs(USBTX,USBRX); // for ground station
+Timer nodetime;
+int rcmd=0,cmdflag=0;
+int rco2=0,co2flag=0;
+int co2val[2] = {0};
+int co2_command[9] = {0};
+int co2str[9] = {0};
+int p = 0;
 
-int main(){
-    pc.baud(9600);
-    int i1=7;
-    float i2=7.5;
-    char i3='H';
-    char c;
+void commandget()
+{
+    rcmd=gs.getc();
+    cmdflag=1;
+}
+void receive(int rcmd,int cmdflag)
+{
+    gs.attach(commandget,Serial::RxIrq); 
+}
+void initialize()
+{
+    rcmd = 0;
+    cmdflag = 0;
+    condition = 0;
+}
+       
+void co2get()
+{
+    rco2=CO2.getc();
+    co2str[p++] = rco2;
+    co2flag=1;
+}
     
-    pc.printf("Hello World!\r\n");
-    pc.printf("i1=%d\r\n",i1);
-    pc.printf("i2=%f\r\n",i2);
-    pc.printf("i3=%c\r\n",i3);
-    pc.putc(i3);
-    pc.printf("\r\n");
+void receive_CO2(int rco2,int co2flag)
+{
+    CO2.attach(co2get,Serial::RxIrq);
+}
+
+void initialize_CO2()
+{
+        rco2 = 0;
+        co2flag = 0;
+        p = 0;
+}
+
+int main()
+{
+  //gs.printf("From node: Nominal Operation\r\n");
+  CO2.baud(9600);
+  nodetime.start();
+  receive(rcmd,cmdflag);
+  receive_CO2(rco2,co2flag);
+  initialize();
+  while(1){
+    condition=!condition;
+    
+    //gs.printf("CO2NODE::Time=%f[s], CO2bytes= %d %d \r\n",nodetime.read(),co2val[0],co2val[1]);
+    //wait_ms(1000);
     
-    for(int j=0; j<10; j++){
-        pc.printf("j=%d -- enter any key --> ",j);
-        c = pc.getc();
-        pc.putc(c);
-        pc.printf("\r\n");
+    if(cmdflag==1){
+        if(rcmd=='a'){
+        // get CO2 data
+            //gs.printf("Command a received\r\n ");
+            // setup MH-Z19C command string
+            co2_command[0] = 0xff;
+            co2_command[1] = 0x01;
+            co2_command[2] = 0x86;
+            co2_command[3] = 0x00;
+            co2_command[4] = 0x00;
+            co2_command[5] = 0x00;
+            co2_command[6] = 0x00;
+            co2_command[7] = 0x00;
+            co2_command[8] = 0x79;
+            for(int k = 0 ; k<9; k++){
+                CO2.putc(co2_command[k]);
+            }
+            wait_ms(100);
+            if(co2flag==1){
+                gs.printf("CO2 = %04d [ppm]\r\n",(co2str[2]*256)+co2str[1]);
+                //gs.printf("CO2 received: %x,%x,%x,%x,%x,%x,%x,%x,%x",co2str[0],co2str[1],co2str[2],co2str[3],co2str[4],co2str[5],co2str[6],co2str[7],co2str[8]);
+                /*
+                gs.putc(co2str[2]);
+                gs.putc(co2str[3]);
+                gs.putc(13);
+                gs.putc(10);
+                */
+                initialize_CO2();
+            }
+            
+        }else if(rcmd=='c'){
+        // do calibration
+            gs.printf("Command c received\r\n ");
+        }
+        initialize();
     }
-    
-    pc.printf("finish!!\r\n");
-}
\ No newline at end of file
+  }
+  nodetime.stop();
+  gs.printf("From Sat: Endof Operation \r \n");
+}
+