Outputs data from a AM2320 on stdout.
#include <iostream>
#include <thread>
#include <iomanip>
#include <assert.h>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/program_options.hpp>
constexpr int valw = 8;
bool quit = false;
try {
int succ = 0, fail = 0;
std::cout << std::fixed << std::setprecision(1);
do {
std::this_thread::sleep_for(std::chrono::seconds(delay));
bool skip = true;
try {
++succ;
skip = false;
throw;
throw;
} catch (...) {
std::cerr << "Failed sample attempt " << ++fail << ":\n" <<
boost::current_exception_diagnostic_information() << std::endl;
}
if (!skip) {
std::cout << "Temp: " << std::setw(6) <<
temp.
value <<
"K " << std::setw(5) <<
temp.
value - 273.15 <<
"C " << std::setw(5) <<
temp.
value * 1.8 - 459.67 <<
"F Rel humid: " <<
std::setw(5) << humid.
value <<
'%' << std::endl;
}
} while (!quit);
std::cout << "Read " << succ << " samples successfully, and failed to read "
<< fail << " samples.\n" << std::setprecision(2) <<
100.0f * (float)fail / (float)(succ + fail) << "% failed." << std::endl;
} catch (...) {
std::cerr << "Program failed in runtest(): " <<
boost::current_exception_diagnostic_information() << std::endl;
}
int main(int argc, char *argv[])
try {
std::string i2cpath;
int delay;
{
boost::program_options::options_description optdesc(
"Options for AM2320 test"
);
optdesc.add_options()
(
"help,h",
"Show this help message"
)
(
"i2cdev,i",
boost::program_options::value<std::string>(&i2cpath)->
default_value("/dev/i2c-1"),
"Specify I2C device file"
)
(
"delay,d",
boost::program_options::value<int>(&delay)->
default_value(8),
"Time in seconds bewteen samples"
)
;
boost::program_options::variables_map vm;
boost::program_options::store(
boost::program_options::parse_command_line(argc, argv, optdesc),
vm
);
boost::program_options::notify(vm);
if (vm.count("help")) {
std::cout << "Test program for temperature and relative humidity "
"sensor AM2320\n" << argv[0] << " [options]\n" << optdesc << std::endl;
return 0;
}
}
std::unique_ptr<duds::hardware::interface::I2c> i2c(
);
std::this_thread::sleep_for(std::chrono::milliseconds(2));
std::thread doit(runtest, std::ref(meter), delay);
std::cin.get();
quit = true;
doit.join();
} catch (...) {
std::cerr << "Program failed in main(): " <<
boost::current_exception_diagnostic_information() << std::endl;
return 1;
}