2023-07-20 10:06:31 -04:00
|
|
|
//
|
|
|
|
// FILE: SHT31_SWW.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2023-11-22 03:58:07 -05:00
|
|
|
// VERSION: 0.1.2
|
2023-07-20 10:06:31 -04:00
|
|
|
// DATE: 2019-02-08 (base SHT31 lib)
|
|
|
|
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
|
|
|
|
// to be used with the SoftwareWire library instead of (hardware) Wire.
|
|
|
|
// derives from SHT31 0.3.8
|
2023-07-23 08:19:03 -04:00
|
|
|
// URL: https://www.adafruit.com/product/2857
|
|
|
|
// https://github.com/RobTillaart/SHT31_SWW
|
|
|
|
// https://github.com/RobTillaart/SHT31
|
2023-07-20 10:06:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include "SHT31_SWW.h"
|
|
|
|
|
|
|
|
|
|
|
|
// SUPPORTED COMMANDS - single shot mode only
|
|
|
|
#define SHT31_READ_STATUS 0xF32D
|
|
|
|
#define SHT31_CLEAR_STATUS 0x3041
|
|
|
|
|
|
|
|
#define SHT31_SOFT_RESET 0x30A2
|
|
|
|
#define SHT31_HARD_RESET 0x0006
|
|
|
|
|
|
|
|
#define SHT31_MEASUREMENT_FAST 0x2416 // page 10 datasheet
|
|
|
|
#define SHT31_MEASUREMENT_SLOW 0x2400 // no clock stretching
|
|
|
|
|
|
|
|
#define SHT31_HEAT_ON 0x306D
|
|
|
|
#define SHT31_HEAT_OFF 0x3066
|
|
|
|
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds
|
|
|
|
|
|
|
|
|
|
|
|
SHT31_SWW::SHT31_SWW()
|
|
|
|
{
|
2023-07-23 08:19:03 -04:00
|
|
|
_softwareWire = NULL;
|
2023-07-20 10:06:31 -04:00
|
|
|
_address = 0;
|
|
|
|
_lastRead = 0;
|
|
|
|
_rawTemperature = 0;
|
|
|
|
_rawHumidity = 0;
|
|
|
|
_heatTimeout = 0;
|
|
|
|
_heaterStart = 0;
|
|
|
|
_heaterStop = 0;
|
|
|
|
_heaterOn = false;
|
|
|
|
_error = SHT31_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-23 08:19:03 -04:00
|
|
|
bool SHT31_SWW::begin(const uint8_t address, SoftwareWire * wire)
|
2023-07-20 10:06:31 -04:00
|
|
|
{
|
|
|
|
if ((address != 0x44) && (address != 0x45))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_address = address;
|
2023-07-23 08:19:03 -04:00
|
|
|
_softwareWire = wire;
|
|
|
|
_softwareWire->begin();
|
2023-07-20 10:06:31 -04:00
|
|
|
return reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-23 08:19:03 -04:00
|
|
|
bool SHT31_SWW::begin(SoftwareWire * wire)
|
2023-07-20 10:06:31 -04:00
|
|
|
{
|
2023-07-23 08:19:03 -04:00
|
|
|
return begin(SHT_DEFAULT_ADDRESS, wire);
|
2023-07-20 10:06:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SHT31_SWW::isConnected()
|
|
|
|
{
|
2023-07-23 08:19:03 -04:00
|
|
|
_softwareWire->beginTransmission(_address);
|
|
|
|
int rv = _softwareWire->endTransmission();
|
2023-07-20 10:06:31 -04:00
|
|
|
if (rv != 0) _error = SHT31_ERR_NOT_CONNECT;
|
|
|
|
return (rv == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PRIVATE
|
|
|
|
//
|
|
|
|
bool SHT31_SWW::writeCmd(uint16_t cmd)
|
|
|
|
{
|
2023-07-23 08:19:03 -04:00
|
|
|
_softwareWire->beginTransmission(_address);
|
|
|
|
_softwareWire->write(cmd >> 8 );
|
|
|
|
_softwareWire->write(cmd & 0xFF);
|
|
|
|
if (_softwareWire->endTransmission() != 0)
|
2023-07-20 10:06:31 -04:00
|
|
|
{
|
|
|
|
_error = SHT31_ERR_WRITECMD;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SHT31_SWW::readBytes(uint8_t n, uint8_t *val)
|
|
|
|
{
|
2023-07-23 08:19:03 -04:00
|
|
|
int rv = _softwareWire->requestFrom(_address, (uint8_t) n);
|
2023-07-20 10:06:31 -04:00
|
|
|
if (rv == n)
|
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < n; i++)
|
|
|
|
{
|
2023-07-23 08:19:03 -04:00
|
|
|
val[i] = _softwareWire->read();
|
2023-07-20 10:06:31 -04:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_error = SHT31_ERR_READBYTES;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|