// program: co2UART - modyfied dec 2017 by eSJeKo - no support :-) // compile with: g++ co2UART.cpp -o co2UART -lwiringPi // run with: sudo ./co2UART or (better) as <root> ./co2UART // Be sure to disable serial login by running <raspi-config> but don't disable the serial port #include <iostream> #include <wiringPi.h> //only needed for the one sec delay #include <wiringSerial.h> //only needed for serialOpen #include <unistd.h> // needed for read and write #include <ctime> #include <fstream> // std::fstream #include <string> #include <stdio.h> /* printf */ using namespace std; int main() { //600000=10 min // 10 Minutes seems a nice interval int wait; wait = 1000; // setting to wait for netx measurement (use 1000 for testing) system("clear"); time_t now = time(0); char* dt = ctime(&now); time_t t = time(NULL); tm* timePtr = localtime(&t); printf ("co2UART version 2.00 "); // Creating logfilename string logfile;int s_len; // Year/Month/Day/Hour/Minute int year;year = timePtr->tm_year;year = year + 1900;std::string s_year = to_string(year); s_len=0;int month;month = timePtr->tm_mon;month = month +1;std::string s_month = to_string(month);s_len=s_month.length();if( s_len < 2 ) {s_month="0"+s_month;} s_len=0;int day;day = timePtr->tm_mday;std::string s_day = to_string(day);s_len=s_day.length();if( s_len < 2 ) {s_day="0"+s_day;} s_len=0;int hour;hour = timePtr->tm_hour;hour = hour ;std::string s_hour = to_string(hour);s_len=s_hour.length();if( s_len < 2 ) {s_hour="0"+s_hour;} s_len=0;int min;min = timePtr->tm_min;std::string s_min = to_string(min);s_len=s_min.length();if( s_len < 2 ) {s_min="0"+s_min;} // cout << "\n\n\n\n\n"; // cout << s_len; // cout << "\n\n\n\n\n"; // logfilename <CO2log_YYYYMMDD_HH.MM.log> //Change the format if you please string logdir="/special/co2log/"; logfile=logdir+"CO2log_" + s_year + s_month + s_day + "_" + s_hour + "." + s_min + ".log" ; cout << "Logfile: "; cout << logfile; cout << "\n"; // Open logfile for header output std::fstream fs; fs.open (logfile, std::fstream::out);; fs << "- CO2-datlogging starting at: "; fs << s_day;fs << "-";fs << s_month;fs << "-";fs << s_year;fs << " / ";fs << s_hour;fs << ":";fs << s_min;fs << "\n\n"; fs.close(); // Get data from serial port int fd; // Handler ID if ((fd = serialOpen ("/dev/serial0", 9600)) < 0) // uses wiringSerial { cout<< "Unable to open device"; return 1 ; } else { cout <<"ttyAMA0 initialised [ok]"; } char cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; char cmd2[9]; cout << " >CO2uart is running\n\n"; while(1) { write (fd,cmd,9); //write 9 bytes in cmd to UART read(fd,cmd2,9); //read 9 bytes to cmd2 from UART cout << "-["; cout << timePtr->tm_mday; cout << "-"; cout << timePtr->tm_mon + 1; cout << "-"; cout << timePtr->tm_year + 1900; cout << " / "; fs << timePtr->tm_hour + 1; cout << ":"; cout << timePtr->tm_min; cout << "]-----------------------------------------------------------------\n"; cout << "DEC: "; for (int loop=0;loop<=8;loop++) cout << (int)cmd2[loop] << "\t "; // print all 9 bytes with decimal output cout << "\nHEX: "; for (int loop=0;loop<=8;loop++) cout << std::hex << (int)cmd2[loop] << std::dec << "\t "; //hex valures for all 9 bytes read cout << "\n"; int High = (int) cmd2[2]; int Low = (int) cmd2[3]; int ppm = (256*High)+Low; //ppm = (256*byte2)+byte3 remember bytes numbered 0-8 cout << "high " << High << "\t low " << Low << "\tppm = " << ppm << "\n\n"; // Append data to logfile fs.open (logfile, std::fstream::in | std::fstream::out | std::fstream::app); time_t t = time(NULL); tm* timePtr = localtime(&t); fs << timePtr->tm_mday; fs << "-"; fs << timePtr->tm_mon + 1; fs << "-"; fs << timePtr->tm_year + 1900; fs << " / "; s_len=0;int hour;hour = timePtr->tm_hour;hour = hour ;std::string s_hour = to_string(hour);s_len=s_hour.length();if( s_len < 2 ) {s_hour="0"+s_hour;} fs << s_hour; fs << ":"; s_len=0;int min;min = timePtr->tm_min;std::string s_min = to_string(min);s_len=s_min.length();if( s_len < 2 ) {s_min="0"+s_min;} fs << s_min; fs << ","; fs << ppm; fs << "\n"; fs.close(); delay(wait); } return 0; }