upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
Public Member Functions | Protected Member Functions
MHZ16 Class Reference

API support for the Grove CO2 sensor. More...

Detailed Description

This class implements support for the Grove CO2 sensor.

Its CO2 detection range is 0-2,000 ppm. It requires a 2-3 minute warm-up time before reporting valid data.

mhz16.jpg
// Instantiate a MHZ16 serial CO2 sensor on uart 0.
// This example was tested on the Grove CO2 sensor module.
upm::MHZ16* co2 = new upm::MHZ16(0);
// make sure port is initialized properly. 9600 baud is the default.
if (!co2->setupTty(B9600))
{
cerr << "Failed to setup tty port parameters" << endl;
return 1;
}
cout << "Make sure that the sensor has had at least 3 minutes to warm up"
<< endl;
cout << "or you will not get valid results." << endl;
cout << "The temperature reported is not the ambient temperature, " << endl;
cout << "but rather the temperature of the sensor elements." << endl;
sleep(1);
while (shouldRun)
{
if(!co2->getData()){
cerr << "Failed to retrieve data" << endl;
continue;
}
cout << "CO2 concentration: " << co2->getGas() << " PPM, "
<< "Temperature (in C): " << co2->getTemperature() <<endl;
sleep(2);
}

Public Member Functions

 MHZ16 (int uart)
 
 ~MHZ16 ()
 
bool dataAvailable (unsigned int millis)
 
int readData (char *buffer, int len)
 
int writeData (char *buffer, int len)
 
bool setupTty (speed_t baud=B9600)
 
bool verifyPacket (uint8_t *pkt, int len)
 
bool getData ()
 
int getGas ()
 
int getTemperature ()
 
void calibrateZeroPoint ()
 

Protected Member Functions

int ttyFd ()
 

Constructor & Destructor Documentation

MHZ16 ( int  uart)

MHZ16 constructor

Parameters
uartDefault UART to use (0 or 1)
~MHZ16 ( )

MHZ16 destructor

Member Function Documentation

bool dataAvailable ( unsigned int  millis)

Checks to see if there is data available for reading

Parameters
millisNumber of milliseconds to wait; 0 means no waiting.
Returns
True if there is data available for reading
int readData ( char *  buffer,
int  len 
)

Reads any available data in a user-supplied buffer. Note: the call blocks until data is available to be read. Use dataAvailable() to determine whether there is data available beforehand, to avoid blocking.

Parameters
bufferBuffer to hold the data read
lenLength of the buffer
Returns
Number of bytes read
int writeData ( char *  buffer,
int  len 
)

Writes the data in the buffer to the device

Parameters
bufferBuffer to hold the data read
lenLength of the buffer
Returns
Number of bytes written
bool setupTty ( speed_t  baud = B9600)

Sets up proper tty I/O modes and the baud rate. The default baud rate is 9,600 (B9600).

Parameters
baudDesired baud rate.
Returns
True if successful
bool verifyPacket ( uint8_t *  pkt,
int  len 
)

Verifies the packet header and indicates its validity

Parameters
pktPacket to check
Returns
True if the checksum is valid, false otherwise
bool getData ( )

Queries the sensor and gets the gas (CO2) concentration and temperature data. Should be called before other "get" functions.

Returns
True if successful
int getGas ( )

Returns the gas (CO2) concentration data.

Returns
Gas concentration
int getTemperature ( void  )

Returns the temperature data.

Returns
Temperature in Celsius
void calibrateZeroPoint ( )

Sets the zero point of the sensor


The documentation for this class was generated from the following files: