109 lines
2.8 KiB
C
Raw Normal View History

2021-08-13 13:08:52 +02:00
#pragma once
//
// FILE: AGS02MA.h
2021-08-15 19:35:40 +02:00
// AUTHOR: Rob Tillaart, Viktor Balint
2021-08-13 13:08:52 +02:00
// DATE: 2021-08-12
2021-12-11 18:45:41 +01:00
// VERSION: 0.1.3
2021-08-13 13:08:52 +02:00
// PURPOSE: Arduino library for AGS02MA TVOC
// URL: https://github.com/RobTillaart/AGS02MA
//
#include "Arduino.h"
#include "Wire.h"
2021-12-11 18:45:41 +01:00
#define AGS02MA_LIB_VERSION (F("0.1.3"))
2021-08-13 13:08:52 +02:00
#define AGS02MA_OK 0
#define AGS02MA_ERROR -10
2021-09-24 12:31:48 +02:00
#define AGS02MA_ERROR_CRC -11
#define AGS02MA_ERROR_READ -12
2021-08-13 13:08:52 +02:00
2021-08-15 19:35:40 +02:00
#define AGS02MA_I2C_CLOCK 30000
2021-08-13 13:08:52 +02:00
class AGS02MA
{
public:
2021-08-15 19:35:40 +02:00
// address 26 = 0x1A
2021-08-13 13:08:52 +02:00
explicit AGS02MA(const uint8_t deviceAddress = 26, TwoWire *wire = &Wire);
2021-08-15 19:35:40 +02:00
#if defined (ESP8266) || defined(ESP32)
2021-08-13 13:08:52 +02:00
bool begin(uint8_t sda, uint8_t scl);
#endif
bool begin();
bool isConnected();
2021-09-24 12:31:48 +02:00
void reset();
2021-08-13 13:08:52 +02:00
bool isHeated() { return (millis() - _startTime) > 120000UL; };
2021-08-15 19:35:40 +02:00
// CONFIGURATION
2021-08-13 13:08:52 +02:00
bool setAddress(const uint8_t deviceAddress);
uint8_t getAddress() { return _address; };
uint8_t getSensorVersion();
2021-08-15 19:35:40 +02:00
// to set the speed the I2C bus should return to
2021-09-24 12:31:48 +02:00
// as the device operates at very low bus speed of 30 kHz.
2021-12-11 18:45:41 +01:00
void setI2CResetSpeed(uint32_t speed) { _I2CResetSpeed = speed; };
2021-08-15 19:35:40 +02:00
uint32_t getI2CResetSpeed() { return _I2CResetSpeed; };
// to be called after at least 5 minutes in fresh air.
bool zeroCalibration();
2021-08-13 13:08:52 +02:00
2021-08-15 19:35:40 +02:00
2021-09-24 12:31:48 +02:00
// MODE
2021-08-13 13:08:52 +02:00
bool setPPBMode();
bool setUGM3Mode();
2021-09-24 12:31:48 +02:00
uint8_t getMode() { return _mode; };
2021-08-13 13:08:52 +02:00
2021-09-24 12:31:48 +02:00
// READ functions
uint32_t readPPB(); // parts per billion 10^9
uint32_t readUGM3(); // microgram per cubic meter
// 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
float lastPPM() { return _lastPPB * 0.001; };
uint32_t lastPPB() { return _lastPPB; }; // fetch last PPB measurement
uint32_t lastUGM3() { return _lastUGM3; }; // fetch last UGM3 measurement
// STATUS
uint32_t lastRead() { return _lastRead; }; // timestamp last measurement
2021-08-13 13:08:52 +02:00
int lastError();
uint8_t lastStatus() { return _status; };
2021-09-24 12:31:48 +02:00
uint8_t dataReady() { return _status & 0x01; };
2021-08-13 13:08:52 +02:00
2021-08-15 19:35:40 +02:00
2021-08-13 13:08:52 +02:00
private:
2021-09-24 12:31:48 +02:00
uint32_t _readSensor();
2021-08-13 13:08:52 +02:00
bool _readRegister(uint8_t reg);
bool _writeRegister(uint8_t reg);
2021-08-15 19:35:40 +02:00
uint32_t _I2CResetSpeed = 100000;
2021-08-13 13:08:52 +02:00
uint32_t _startTime = 0;
uint32_t _lastRead = 0;
2021-09-24 12:31:48 +02:00
uint32_t _lastRegTime = 0;
uint32_t _lastPPB = 0;
uint32_t _lastUGM3 = 0;
2021-08-13 13:08:52 +02:00
uint8_t _address = 0;
uint8_t _mode = 255;
uint8_t _status = 0;
uint8_t _buffer[5];
2021-08-15 19:35:40 +02:00
uint8_t _CRC8(uint8_t * buf, uint8_t size);
2021-08-13 13:08:52 +02:00
int _error = AGS02MA_OK;
TwoWire* _wire;
};
// -- END OF FILE --