Daisuke Yanagihara / Mbed 2 deprecated CLTP_MH-Z19_Test1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MH_Z19.h"
00003 
00004 MH_Z19 Sen(p5);
00005 Serial CO2(p9,p10);
00006  
00007 DigitalOut condition(LED1);
00008 Serial gs(USBTX,USBRX); // for ground station
00009 Timer nodetime;
00010 int rcmd=0,cmdflag=0;
00011 int rco2=0,co2flag=0;
00012 int co2val[2] = {0};
00013 int co2_command[9] = {0};
00014 int co2str[9] = {0};
00015 int p = 0;
00016 
00017 void commandget()
00018 {
00019     rcmd=gs.getc();
00020     cmdflag=1;
00021 }
00022 void receive(int rcmd,int cmdflag)
00023 {
00024     gs.attach(commandget,Serial::RxIrq); 
00025 }
00026 void initialize()
00027 {
00028     rcmd = 0;
00029     cmdflag = 0;
00030     condition = 0;
00031 }
00032        
00033 void co2get()
00034 {
00035     rco2=CO2.getc();
00036     co2str[p++] = rco2;
00037     co2flag=1;
00038 }
00039     
00040 void receive_CO2(int rco2,int co2flag)
00041 {
00042     CO2.attach(co2get,Serial::RxIrq);
00043 }
00044 
00045 void initialize_CO2()
00046 {
00047         rco2 = 0;
00048         co2flag = 0;
00049         p = 0;
00050 }
00051 
00052 int main()
00053 {
00054   //gs.printf("From node: Nominal Operation\r\n");
00055   CO2.baud(9600);
00056   nodetime.start();
00057   receive(rcmd,cmdflag);
00058   receive_CO2(rco2,co2flag);
00059   initialize();
00060   while(1){
00061     condition=!condition;
00062     
00063     //gs.printf("CO2NODE::Time=%f[s], CO2bytes= %d %d \r\n",nodetime.read(),co2val[0],co2val[1]);
00064     //wait_ms(1000);
00065     
00066     if(cmdflag==1){
00067         if(rcmd=='a'){
00068         // get CO2 data
00069             //gs.printf("Command a received\r\n ");
00070             // setup MH-Z19C command string
00071             co2_command[0] = 0xff;
00072             co2_command[1] = 0x01;
00073             co2_command[2] = 0x86;
00074             co2_command[3] = 0x00;
00075             co2_command[4] = 0x00;
00076             co2_command[5] = 0x00;
00077             co2_command[6] = 0x00;
00078             co2_command[7] = 0x00;
00079             co2_command[8] = 0x79;
00080             for(int k = 0 ; k<9; k++){
00081                 CO2.putc(co2_command[k]);
00082             }
00083             wait_ms(100);
00084             if(co2flag==1){
00085                 gs.printf("CO2 = %04d [ppm]\r\n",(co2str[2]*256)+co2str[1]);
00086                 //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]);
00087                 /*
00088                 gs.putc(co2str[2]);
00089                 gs.putc(co2str[3]);
00090                 gs.putc(13);
00091                 gs.putc(10);
00092                 */
00093                 initialize_CO2();
00094             }
00095             
00096         }else if(rcmd=='c'){
00097         // do calibration
00098             gs.printf("Command c received\r\n ");
00099         }
00100         initialize();
00101     }
00102   }
00103   nodetime.stop();
00104   gs.printf("From Sat: Endof Operation \r \n");
00105 }
00106