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
|
2022-01-14 05:39:18 -05:00
|
|
|
// VERSION: 0.1.2
|
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
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
|
|
|
|
2022-01-14 05:39:18 -05:00
|
|
|
#define AM2315_LIB_VERSION (F("0.1.2"))
|
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();
|
2022-01-12 15:04:52 -05:00
|
|
|
// datasheet- wake up is min 800 us max 3000 us
|
|
|
|
bool isConnected(uint16_t timeout = 3000);
|
2022-01-06 06:08:13 -05:00
|
|
|
|
|
|
|
int read();
|
|
|
|
|
|
|
|
// lastRead is in MilliSeconds since start sketch
|
|
|
|
uint32_t lastRead() { return _lastRead; };
|
|
|
|
|
|
|
|
// preferred interface
|
|
|
|
float getHumidity();
|
|
|
|
float getTemperature();
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
float _humOffset = 0.0;
|
|
|
|
float _tempOffset = 0.0;
|
|
|
|
float _humidity = 0.0;
|
|
|
|
float _temperature = 0.0;
|
|
|
|
uint32_t _lastRead = 0;
|
|
|
|
bool _waitForRead = false;
|
|
|
|
bool _suppressError = false;
|
|
|
|
|
2022-01-14 05:39:18 -05:00
|
|
|
uint8_t _bits[8]; // buffer to hold raw data
|
2022-01-06 06:08:13 -05:00
|
|
|
int _read();
|
|
|
|
int _readSensor();
|
|
|
|
uint16_t _crc16(uint8_t *ptr, uint8_t len);
|
|
|
|
|
|
|
|
TwoWire* _wire;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|