2022-01-06 06:08:13 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: AM2315.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// PURPOSE: AM2315 Temperature and Humidity sensor library for Arduino
|
2023-01-21 09:19:08 -05:00
|
|
|
// VERSION: 0.1.7
|
2022-01-06 06:08:13 -05:00
|
|
|
// URL: https://github.com/RobTillaart/AM2315
|
2022-01-14 05:39:18 -05:00
|
|
|
//
|
2022-01-06 06:08:13 -05:00
|
|
|
// AM232X PIN layout AM2315 COLOR
|
|
|
|
// ============================================
|
|
|
|
// bottom view DESCRIPTION COLOR
|
|
|
|
// +---+
|
|
|
|
// |o | VDD RED
|
|
|
|
// |o | SDA YELLOW
|
|
|
|
// |o | GND BLACK
|
|
|
|
// |o | SCL GREY
|
|
|
|
// +---+
|
2022-01-14 05:39:18 -05:00
|
|
|
//
|
2023-01-21 09:19:08 -05:00
|
|
|
// do not forget pull up resistors between SDA, SCL and VDD.
|
2022-01-06 06:08:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
#include "Wire.h"
|
|
|
|
|
|
|
|
|
2023-01-21 09:19:08 -05:00
|
|
|
#define AM2315_LIB_VERSION (F("0.1.7"))
|
2022-01-06 06:08:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
#define AM2315_OK 0
|
|
|
|
#define AM2315_ERROR_CHECKSUM -10
|
|
|
|
#define AM2315_ERROR_CONNECT -11
|
|
|
|
#define AM2315_MISSING_BYTES -12
|
|
|
|
#define AM2315_WAITING_FOR_READ -50
|
|
|
|
|
|
|
|
|
|
|
|
// optionally detect out of range values.
|
|
|
|
// occurs seldom so not enabled by default.
|
|
|
|
// #define AM2315_VALUE_OUT_OF_RANGE
|
|
|
|
#define AM2315_HUMIDITY_OUT_OF_RANGE -100
|
|
|
|
#define AM2315_TEMPERATURE_OUT_OF_RANGE -101
|
|
|
|
|
|
|
|
// allows to overrule AM2315_INVALID_VALUE e.g. to prevent spike in graphs.
|
|
|
|
#ifndef AM2315_INVALID_VALUE
|
|
|
|
#define AM2315_INVALID_VALUE -999
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
class AM2315
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
AM2315(TwoWire *wire = &Wire);
|
|
|
|
|
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
|
|
|
bool begin(const uint8_t dataPin, const uint8_t clockPin);
|
|
|
|
#endif
|
|
|
|
bool begin();
|
2023-01-21 09:19:08 -05:00
|
|
|
// datasheet- wake up is min 800 us max 3000 us
|
2022-01-12 15:04:52 -05:00
|
|
|
bool isConnected(uint16_t timeout = 3000);
|
2022-01-06 06:08:13 -05:00
|
|
|
|
|
|
|
int read();
|
|
|
|
|
2023-01-21 09:19:08 -05:00
|
|
|
// lastRead is in MilliSeconds since start sketch
|
2022-01-06 06:08:13 -05:00
|
|
|
uint32_t lastRead() { return _lastRead; };
|
|
|
|
|
2023-01-21 09:19:08 -05:00
|
|
|
// preferred interface
|
2022-01-06 06:08:13 -05:00
|
|
|
float getHumidity();
|
|
|
|
float getTemperature();
|
|
|
|
|
2023-01-21 09:19:08 -05:00
|
|
|
// adding offsets works well in normal range
|
|
|
|
// might introduce under- or overflow at the ends of the sensor range
|
2022-01-14 05:39:18 -05:00
|
|
|
void setHumOffset(float offset = 0) { _humOffset = offset; };
|
|
|
|
void setTempOffset(float offset = 0) { _tempOffset = offset; };
|
|
|
|
float getHumOffset() { return _humOffset; };
|
|
|
|
float getTempOffset() { return _tempOffset; };
|
2022-01-06 06:08:13 -05:00
|
|
|
|
2022-01-14 05:39:18 -05:00
|
|
|
bool getWaitForReading() { return _waitForRead; };
|
|
|
|
void setWaitForReading(bool b ) { _waitForRead = b; };
|
2022-01-06 06:08:13 -05:00
|
|
|
|
|
|
|
|
2023-01-21 09:19:08 -05:00
|
|
|
// suppress error values of -999 => check return value of read() instead
|
2022-01-14 05:39:18 -05:00
|
|
|
bool getSuppressError() { return _suppressError; };
|
|
|
|
void setSuppressError(bool b) { _suppressError = b; };
|
2022-01-06 06:08:13 -05:00
|
|
|
|
2022-01-12 15:04:52 -05:00
|
|
|
bool wakeUp() { return isConnected(); };
|
2022-01-06 06:08:13 -05:00
|
|
|
|
|
|
|
private:
|
2023-01-21 09:19:08 -05:00
|
|
|
uint8_t _bits[8]; // buffer to hold raw data
|
2022-01-06 06:08:13 -05:00
|
|
|
float _humidity = 0.0;
|
|
|
|
float _temperature = 0.0;
|
2022-06-17 07:17:03 -04:00
|
|
|
float _humOffset = 0.0;
|
|
|
|
float _tempOffset = 0.0;
|
2022-01-06 06:08:13 -05:00
|
|
|
uint32_t _lastRead = 0;
|
2022-06-17 07:17:03 -04:00
|
|
|
|
2022-01-06 06:08:13 -05:00
|
|
|
bool _waitForRead = false;
|
|
|
|
bool _suppressError = false;
|
|
|
|
|
|
|
|
int _read();
|
|
|
|
int _readSensor();
|
2022-06-17 07:17:03 -04:00
|
|
|
|
2022-01-06 06:08:13 -05:00
|
|
|
uint16_t _crc16(uint8_t *ptr, uint8_t len);
|
|
|
|
|
|
|
|
TwoWire* _wire;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|