2020-05-07 03:53:25 -04:00
|
|
|
#pragma once
|
2017-12-12 07:16:58 -05:00
|
|
|
//
|
|
|
|
// FILE: AM232X.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-02-07 11:43:28 -05:00
|
|
|
// PURPOSE: AM232X Temperature and Humidity sensor library for Arduino
|
2024-04-05 04:40:16 -04:00
|
|
|
// VERSION: 0.5.1
|
2020-05-07 03:53:25 -04:00
|
|
|
// URL: https://github.com/RobTillaart/AM232X
|
2017-12-12 07:16:58 -05:00
|
|
|
//
|
2022-01-06 08:05:38 -05:00
|
|
|
// AM232X PIN layout AM2315 COLOR
|
|
|
|
// ============================================
|
|
|
|
// bottom view DESCRIPTION COLOR
|
2021-01-29 06:31:58 -05:00
|
|
|
// +---+
|
2022-01-06 08:05:38 -05:00
|
|
|
// |o | VDD RED
|
|
|
|
// |o | SDA YELLOW
|
|
|
|
// |o | GND BLACK
|
|
|
|
// |o | SCL GREY
|
2021-01-29 06:31:58 -05:00
|
|
|
// +---+
|
2022-01-06 08:05:38 -05:00
|
|
|
//
|
2024-04-05 04:40:16 -04:00
|
|
|
// do not forget pull up resistors between SDA, SCL and VDD.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2017-12-12 07:16:58 -05:00
|
|
|
#include "Arduino.h"
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "Wire.h"
|
|
|
|
|
|
|
|
|
2024-04-05 04:40:16 -04:00
|
|
|
#define AM232X_LIB_VERSION (F("0.5.1"))
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
#define AM232X_OK 0
|
|
|
|
#define AM232X_ERROR_UNKNOWN -10
|
|
|
|
#define AM232X_ERROR_CONNECT -11
|
|
|
|
#define AM232X_ERROR_FUNCTION -12
|
|
|
|
#define AM232X_ERROR_ADDRESS -13
|
|
|
|
#define AM232X_ERROR_REGISTER -14
|
|
|
|
#define AM232X_ERROR_CRC_1 -15
|
|
|
|
#define AM232X_ERROR_CRC_2 -16
|
|
|
|
#define AM232X_ERROR_WRITE_DISABLED -17
|
|
|
|
#define AM232X_ERROR_WRITE_COUNT -18
|
|
|
|
#define AM232X_MISSING_BYTES -19
|
2022-01-06 08:05:38 -05:00
|
|
|
#define AM232X_READ_TOO_FAST -20
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2017-12-12 07:16:58 -05:00
|
|
|
|
|
|
|
/*
|
2021-02-03 11:20:20 -05:00
|
|
|
from datasheet
|
|
|
|
0x80: not support function code
|
|
|
|
0x81: Read an illegal address
|
|
|
|
0x82: write data beyond the scope
|
|
|
|
0x83: CRC checksum error
|
|
|
|
0x84: Write disabled
|
2017-12-12 07:16:58 -05:00
|
|
|
*/
|
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
|
|
|
|
// optionally detect out of range values.
|
|
|
|
// occurs seldom so not enabled by default.
|
|
|
|
// #define AM232X_VALUE_OUT_OF_RANGE
|
|
|
|
#define AM232X_HUMIDITY_OUT_OF_RANGE -100
|
|
|
|
#define AM232X_TEMPERATURE_OUT_OF_RANGE -101
|
|
|
|
|
|
|
|
|
|
|
|
// allows to overrule AM232X_INVALID_VALUE e.g. to prevent spike in graphs.
|
|
|
|
#ifndef AM232X_INVALID_VALUE
|
|
|
|
#define AM232X_INVALID_VALUE -999
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-12-12 07:16:58 -05:00
|
|
|
class AM232X
|
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
public:
|
2022-06-17 06:53:44 -04:00
|
|
|
explicit AM232X(TwoWire *wire = &Wire);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
bool begin();
|
|
|
|
// datasheet 8.2 - wake up is min 800 us max 3000 us
|
|
|
|
bool isConnected(uint16_t timeout = 3000);
|
|
|
|
|
|
|
|
int read();
|
|
|
|
// lastRead is in MilliSeconds since start sketch
|
|
|
|
uint32_t lastRead() { return _lastRead; };
|
|
|
|
|
|
|
|
// set readDelay to 0 will reset to datasheet values
|
|
|
|
uint16_t getReadDelay() { return _readDelay; };
|
2023-10-16 09:42:56 -04:00
|
|
|
void setReadDelay(uint16_t readDelay = 0);
|
2022-06-17 06:53:44 -04:00
|
|
|
|
|
|
|
// negative return values are errors
|
|
|
|
int getModel();
|
|
|
|
int getVersion();
|
|
|
|
uint32_t getDeviceID();
|
|
|
|
|
|
|
|
int getStatus();
|
|
|
|
int getUserRegisterA();
|
|
|
|
int getUserRegisterB();
|
|
|
|
|
|
|
|
int setStatus(uint8_t value);
|
|
|
|
int setUserRegisterA(int value);
|
|
|
|
int setUserRegisterB(int value);
|
|
|
|
|
|
|
|
float getHumidity();
|
|
|
|
float getTemperature();
|
|
|
|
|
|
|
|
// adding offsets works well in normal range
|
|
|
|
// might introduce under- or overflow at the ends of the sensor range
|
|
|
|
void setHumOffset(float offset = 0) { _humOffset = offset; };
|
|
|
|
void setTempOffset(float offset = 0) { _tempOffset = offset; };
|
2024-04-05 04:40:16 -04:00
|
|
|
float getHumOffset() { return _humOffset; };
|
|
|
|
float getTempOffset() { return _tempOffset; };
|
2022-06-17 06:53:44 -04:00
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
// suppress error values of -999 => check return value of read() instead
|
|
|
|
bool getSuppressError() { return _suppressError; };
|
|
|
|
void setSuppressError(bool b) { _suppressError = b; };
|
2022-10-27 11:22:29 -04:00
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
bool wakeUp() { return isConnected(); };
|
|
|
|
|
2024-04-05 04:40:16 -04:00
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
protected:
|
2024-04-05 04:40:16 -04:00
|
|
|
uint8_t _bits[8]; // buffer to hold raw data
|
2022-06-17 06:53:44 -04:00
|
|
|
float _humidity = 0.0;
|
|
|
|
float _temperature = 0.0;
|
|
|
|
float _humOffset = 0.0;
|
|
|
|
float _tempOffset = 0.0;
|
|
|
|
uint32_t _lastRead = 0;
|
|
|
|
uint16_t _readDelay = 2000;
|
|
|
|
|
2022-06-18 03:51:07 -04:00
|
|
|
bool _suppressError = false;
|
2022-10-27 11:22:29 -04:00
|
|
|
|
2024-04-05 04:40:16 -04:00
|
|
|
int _readRegister(uint8_t reg, uint8_t count);
|
|
|
|
int _writeRegister(uint8_t reg, uint8_t count, int16_t value);
|
2022-06-17 06:53:44 -04:00
|
|
|
int _getData(uint8_t length);
|
|
|
|
|
2024-04-05 04:40:16 -04:00
|
|
|
uint16_t _crc16(uint8_t *ptr, uint8_t length);
|
2022-06-17 06:53:44 -04:00
|
|
|
|
|
|
|
TwoWire* _wire;
|
2017-12-12 07:16:58 -05:00
|
|
|
};
|
|
|
|
|
2022-01-06 08:05:38 -05:00
|
|
|
|
2022-06-17 06:53:44 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2024-04-05 04:40:16 -04:00
|
|
|
// AM232X derived classes
|
2022-06-17 06:53:44 -04:00
|
|
|
//
|
|
|
|
class AM2320 : public AM232X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AM2320(TwoWire *wire = &Wire);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class AM2321 : public AM232X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AM2321(TwoWire *wire = &Wire);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class AM2322 : public AM232X
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AM2322(TwoWire *wire = &Wire);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
// -- END OF FILE --
|
2022-01-06 08:05:38 -05:00
|
|
|
|