2021-08-13 07:08:52 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: AGS02MA.h
|
2022-04-25 08:17:43 -04:00
|
|
|
// AUTHOR: Rob Tillaart, Viktor Balint, Beanow
|
2021-08-13 07:08:52 -04:00
|
|
|
// DATE: 2021-08-12
|
2022-10-27 04:47:11 -04:00
|
|
|
// VERSION: 0.3.2
|
2021-08-13 07:08:52 -04:00
|
|
|
// PURPOSE: Arduino library for AGS02MA TVOC
|
|
|
|
// URL: https://github.com/RobTillaart/AGS02MA
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Wire.h"
|
|
|
|
|
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
#define AGS02MA_LIB_VERSION (F("0.3.2"))
|
2021-08-13 07:08:52 -04:00
|
|
|
|
|
|
|
#define AGS02MA_OK 0
|
|
|
|
#define AGS02MA_ERROR -10
|
2021-09-24 06:31:48 -04:00
|
|
|
#define AGS02MA_ERROR_CRC -11
|
|
|
|
#define AGS02MA_ERROR_READ -12
|
2022-01-22 04:17:50 -05:00
|
|
|
#define AGS02MA_ERROR_NOT_READY -13
|
2021-08-13 07:08:52 -04:00
|
|
|
|
|
|
|
|
2022-08-12 06:15:08 -04:00
|
|
|
#define AGS02MA_I2C_CLOCK 25000 // max 30000
|
2021-08-13 07:08:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class AGS02MA
|
|
|
|
{
|
|
|
|
public:
|
2022-05-02 07:50:27 -04:00
|
|
|
struct RegisterData
|
|
|
|
{
|
|
|
|
uint8_t data[4];
|
|
|
|
uint8_t crc;
|
|
|
|
bool crcValid;
|
|
|
|
};
|
|
|
|
|
2022-04-25 08:17:43 -04:00
|
|
|
struct ZeroCalibrationData
|
|
|
|
{
|
|
|
|
/**
|
2022-10-27 04:47:11 -04:00
|
|
|
* Warning, the exact meaning of this status is not fully documented.
|
|
|
|
* It seems like it's a bit mask:
|
|
|
|
* 0000 1100 | 0x0C | 12 | Typical value
|
|
|
|
* 0000 1101 | 0x0D | 13 | Sometimes seen on v117
|
|
|
|
* 0111 1101 | 0x7D | 125 | Seen on v118, after power-off (gives different data than 12!)
|
2022-04-25 08:17:43 -04:00
|
|
|
*/
|
|
|
|
uint16_t status;
|
|
|
|
uint16_t value;
|
|
|
|
};
|
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// address 26 = 0x1A
|
2021-08-13 07:08:52 -04:00
|
|
|
explicit AGS02MA(const uint8_t deviceAddress = 26, TwoWire *wire = &Wire);
|
|
|
|
|
2021-08-15 13:35:40 -04:00
|
|
|
#if defined (ESP8266) || defined(ESP32)
|
2021-08-13 07:08:52 -04:00
|
|
|
bool begin(uint8_t sda, uint8_t scl);
|
|
|
|
#endif
|
|
|
|
bool begin();
|
|
|
|
bool isConnected();
|
2021-09-24 06:31:48 -04:00
|
|
|
void reset();
|
2021-08-13 07:08:52 -04:00
|
|
|
|
|
|
|
bool isHeated() { return (millis() - _startTime) > 120000UL; };
|
|
|
|
|
2021-08-15 13:35:40 -04:00
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// CONFIGURATION
|
2021-08-13 07:08:52 -04:00
|
|
|
bool setAddress(const uint8_t deviceAddress);
|
|
|
|
uint8_t getAddress() { return _address; };
|
|
|
|
uint8_t getSensorVersion();
|
2022-01-22 04:17:50 -05:00
|
|
|
uint32_t getSensorDate();
|
2021-08-13 07:08:52 -04:00
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// to set the speed the I2C bus should return to
|
|
|
|
// as the device operates at very low bus speed of 30 kHz.
|
2021-12-11 12:45:41 -05:00
|
|
|
void setI2CResetSpeed(uint32_t speed) { _I2CResetSpeed = speed; };
|
2021-08-15 13:35:40 -04:00
|
|
|
uint32_t getI2CResetSpeed() { return _I2CResetSpeed; };
|
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// to be called after at least 5 minutes in fresh air.
|
2022-04-25 08:17:43 -04:00
|
|
|
bool zeroCalibration() { return manualZeroCalibration(0); };
|
|
|
|
|
|
|
|
/**
|
2022-10-27 04:47:11 -04:00
|
|
|
* Set the zero calibration value manually.
|
|
|
|
* To be called after at least 5 minutes in fresh air.
|
|
|
|
* For v117: 0-65535 = automatic calibration.
|
|
|
|
* For v118: 0 = automatic calibration, 1-65535 manual calibration.
|
2022-04-25 08:17:43 -04:00
|
|
|
*/
|
|
|
|
bool manualZeroCalibration(uint16_t value = 0);
|
|
|
|
bool getZeroCalibrationData(ZeroCalibrationData &data);
|
2021-08-13 07:08:52 -04:00
|
|
|
|
2021-08-15 13:35:40 -04:00
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// MODE
|
2021-08-13 07:08:52 -04:00
|
|
|
bool setPPBMode();
|
|
|
|
bool setUGM3Mode();
|
2021-09-24 06:31:48 -04:00
|
|
|
uint8_t getMode() { return _mode; };
|
2021-08-13 07:08:52 -04:00
|
|
|
|
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// READ functions
|
|
|
|
uint32_t readPPB(); // parts per billion 10^9
|
|
|
|
uint32_t readUGM3(); // microgram per cubic meter
|
2021-09-24 06:31:48 -04:00
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// derived read functions
|
|
|
|
float readPPM() { return readPPB() * 0.001; }; // parts per million
|
|
|
|
float readMGM3() { return readUGM3() * 0.001; }; // milligram per cubic meter
|
|
|
|
float readUGF3() { return readUGM3() * 0.0283168466; }; // microgram per cubic feet
|
2021-09-24 06:31:48 -04:00
|
|
|
|
|
|
|
float lastPPM() { return _lastPPB * 0.001; };
|
2022-10-27 04:47:11 -04:00
|
|
|
uint32_t lastPPB() { return _lastPPB; }; // fetch last PPB measurement
|
2021-09-24 06:31:48 -04:00
|
|
|
uint32_t lastUGM3() { return _lastUGM3; }; // fetch last UGM3 measurement
|
|
|
|
|
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// STATUS
|
|
|
|
uint32_t lastRead() { return _lastRead; }; // timestamp last measurement
|
2021-08-13 07:08:52 -04:00
|
|
|
int lastError();
|
|
|
|
uint8_t lastStatus() { return _status; };
|
2021-09-24 06:31:48 -04:00
|
|
|
uint8_t dataReady() { return _status & 0x01; };
|
2021-08-13 07:08:52 -04:00
|
|
|
|
2022-10-27 04:47:11 -04:00
|
|
|
// Reading registers
|
2022-05-02 07:50:27 -04:00
|
|
|
bool readRegister(uint8_t address, RegisterData ®);
|
|
|
|
|
2021-08-15 13:35:40 -04:00
|
|
|
|
2021-08-13 07:08:52 -04:00
|
|
|
private:
|
2021-09-24 06:31:48 -04:00
|
|
|
uint32_t _readSensor();
|
2021-08-13 07:08:52 -04:00
|
|
|
bool _readRegister(uint8_t reg);
|
|
|
|
bool _writeRegister(uint8_t reg);
|
|
|
|
|
2021-08-15 13:35:40 -04:00
|
|
|
uint32_t _I2CResetSpeed = 100000;
|
2021-08-13 07:08:52 -04:00
|
|
|
uint32_t _startTime = 0;
|
|
|
|
uint32_t _lastRead = 0;
|
2021-09-24 06:31:48 -04:00
|
|
|
uint32_t _lastRegTime = 0;
|
|
|
|
uint32_t _lastPPB = 0;
|
|
|
|
uint32_t _lastUGM3 = 0;
|
2021-08-13 07:08:52 -04:00
|
|
|
uint8_t _address = 0;
|
|
|
|
uint8_t _mode = 255;
|
|
|
|
uint8_t _status = 0;
|
|
|
|
uint8_t _buffer[5];
|
|
|
|
|
2022-04-25 08:17:43 -04:00
|
|
|
uint16_t _getDataMSB();
|
|
|
|
uint16_t _getDataLSB();
|
2021-08-15 13:35:40 -04:00
|
|
|
uint8_t _CRC8(uint8_t * buf, uint8_t size);
|
2022-01-22 04:17:50 -05:00
|
|
|
uint8_t _bin2bcd(uint8_t val);
|
2021-08-15 13:35:40 -04:00
|
|
|
|
2021-08-13 07:08:52 -04:00
|
|
|
int _error = AGS02MA_OK;
|
|
|
|
|
|
|
|
TwoWire* _wire;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|