Skip to content

Commit

Permalink
fix #38 support Wire1 for ESP32 (#39)
Browse files Browse the repository at this point in the history
- fix #38 support for Wire1 for ESP32
- update examples
- update readme.md
  • Loading branch information
RobTillaart committed Sep 21, 2023
1 parent ac8fdd4 commit 62b5078
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 60 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.4.0] - 2023-09-21
- fix #38 support for Wire1 for ESP32
- update examples
- update readme.md

----

## [0.3.8] - 2023-03-23
- prepare for derived class SHT31_SW
- update readme.md
- update GitHub actions
- update license 2023
- minor edits


## [0.3.7] - 2022-11-24
- Add RP2040 support to build-CI.
- Add CHANGELOG.md
Expand Down
50 changes: 41 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

[![Arduino CI](https://github.com/robtillaart/SHT31/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![JSON check](https://github.com/RobTillaart/SHT31/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT31/actions/workflows/jsoncheck.yml)
[![Arduino CI](https://github.com/RobTillaart/SHT31/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/SHT31/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/SHT31/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/SHT31/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT31/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/SHT31.svg)](https://github.com/RobTillaart/SHT31/issues)

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/SHT31/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/SHT31.svg?maxAge=3600)](https://github.com/RobTillaart/SHT31/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/SHT31.svg)](https://registry.platformio.org/libraries/robtillaart/SHT31)


# SHT31
Expand Down Expand Up @@ -31,20 +34,27 @@ Accuracy table


An elaborated library for the SHT31 sensor can be found here
https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube
- https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube

A derived class for using the SHT31 sensor with SoftWire (soft I2C) can be found here
- https://github.com/RobTillaart/SHT31_SW


## Interface

```cpp
#include "SHT31.h"
```


#### Base interface

- **SHT31()** constructor.
- **SHT31(TwoWire \*wire = &Wire)** constructor. Optional select the I2C bus (Wire, Wire1 etc).
- **bool begin(uint8_t address, uint8_t dataPin, uint8_t clockPin)** begin function for ESP8266 & ESP32;
returns false if device address is incorrect or device cannot be reset.
- **bool begin(uint8_t dataPin, uint8_t clockPin)** same as above. With default SHT_DEFAULT_ADDRESS.
- **bool begin(uint8_t address, TwoWire \*wire = &Wire)** for platforms with multiple I2C buses.
- **bool begin(TwoWire \*wire = &Wire)** same as above.
With default SHT_DEFAULT_ADDRESS.
- **bool begin(uint8_t address = SHT_DEFAULT_ADDRESS)**
Returns false if device address is incorrect or device cannot be reset.
- **bool read(bool fast = true)** blocks 4 (fast) or 15 (slow) milliseconds + actual read + math.
Does read both the temperature and humidity.
- **bool isConnected()** check sensor is reachable over I2C. Returns false if not connected.
Expand Down Expand Up @@ -149,7 +159,29 @@ See examples.

## Future

- keep in sync with SHT85 library
- check TODO in code
#### Must

- keep in sync with SHT85 library.
- keep derived SHT31_SW builds green

#### Should

- check TODO in code.
- rename MAGIC numbers. e.g. in dataReady()

#### Could

- move code from .h to .cpp

#### Wont


## Support

If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.

Thank you,


28 changes: 10 additions & 18 deletions SHT31.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: SHT31.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.8
// VERSION: 0.4.0
// DATE: 2019-02-08
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
// https://www.adafruit.com/product/2857
Expand All @@ -26,9 +26,9 @@
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds


SHT31::SHT31()
SHT31::SHT31(TwoWire *wire)
{
_wire = NULL;
_wire = wire;
_address = 0;
_lastRead = 0;
_rawTemperature = 0;
Expand All @@ -50,7 +50,6 @@ bool SHT31::begin(const uint8_t address, const uint8_t dataPin, const uint8_t cl
}
_address = address;

_wire = &Wire;
if ((dataPin < 255) && (clockPin < 255))
{
_wire->begin(dataPin, clockPin);
Expand All @@ -68,25 +67,18 @@ bool SHT31::begin(const uint8_t dataPin, const uint8_t clockPin)
#endif


bool SHT31::begin(const uint8_t address, TwoWire *wire)
bool SHT31::begin(const uint8_t address)
{
if ((address != 0x44) && (address != 0x45))
{
return false;
}
_address = address;
_wire = wire;
_wire->begin();
return reset();
}


bool SHT31::begin(TwoWire *wire)
{
return begin(SHT_DEFAULT_ADDRESS, wire);
}


bool SHT31::read(bool fast)
{
if (writeCmd(fast ? SHT31_MEASUREMENT_FAST : SHT31_MEASUREMENT_SLOW) == false)
Expand Down Expand Up @@ -151,7 +143,7 @@ uint16_t SHT31::readStatus()
return 0xFFFF;
}

if (status[2] != crc8(status, 2))
if (status[2] != crc8(status, 2))
{
_error = SHT31_ERR_CRC_STATUS;
return 0xFFFF;
Expand Down Expand Up @@ -256,12 +248,12 @@ bool SHT31::readData(bool fast)

if (!fast)
{
if (buffer[2] != crc8(buffer, 2))
if (buffer[2] != crc8(buffer, 2))
{
_error = SHT31_ERR_CRC_TEMP;
return false;
}
if (buffer[5] != crc8(buffer + 3, 2))
if (buffer[5] != crc8(buffer + 3, 2))
{
_error = SHT31_ERR_CRC_HUM;
return false;
Expand All @@ -287,17 +279,17 @@ int SHT31::getError()

//////////////////////////////////////////////////////////

uint8_t SHT31::crc8(const uint8_t *data, uint8_t len)
uint8_t SHT31::crc8(const uint8_t *data, uint8_t len)
{
// CRC-8 formula from page 14 of SHT spec pdf
const uint8_t POLY(0x31);
uint8_t crc(0xFF);

for (uint8_t j = len; j; --j)
for (uint8_t j = len; j; --j)
{
crc ^= *data++;

for (uint8_t i = 8; i; --i)
for (uint8_t i = 8; i; --i)
{
crc = (crc & 0x80) ? (crc << 1) ^ POLY : (crc << 1);
}
Expand Down
14 changes: 6 additions & 8 deletions SHT31.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
//
// FILE: SHT31.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.8
// VERSION: 0.4.0
// DATE: 2019-02-08
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
// https://www.adafruit.com/product/2857
// URL: https://github.com/RobTillaart/SHT31


#include "Arduino.h"
#include "Wire.h"
#include "Wire.h"


#define SHT31_LIB_VERSION (F("0.3.8"))
#define SHT31_LIB_VERSION (F("0.4.0"))

#ifndef SHT_DEFAULT_ADDRESS
#ifndef SHT_DEFAULT_ADDRESS
#define SHT_DEFAULT_ADDRESS 0x44
#endif

Expand Down Expand Up @@ -44,16 +44,14 @@
class SHT31
{
public:
SHT31();
SHT31(TwoWire *wire = &Wire);

#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
// use SHT_DEFAULT_ADDRESS
bool begin(const uint8_t dataPin, const uint8_t clockPin);
#endif
bool begin(const uint8_t address, TwoWire *wire = &Wire);
// use SHT_DEFAULT_ADDRESS
bool begin(TwoWire *wire = &Wire);
bool begin(const uint8_t address = SHT_DEFAULT_ADDRESS);

// blocks 15 milliseconds + actual read + math
bool read(bool fast = true);
Expand Down
25 changes: 12 additions & 13 deletions examples/SHT31_two_I2C/SHT31_two_I2C.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "SHT31.h"


TwoWire myWire(&sercom5, 0, 1);
// TwoWire myWire = Wire1; // test.
// TwoWire myWire(&sercom5, 0, 1);
TwoWire myWire = Wire1; // test.


// note: address reuse on second I2C bus
Expand All @@ -23,10 +23,10 @@ TwoWire myWire(&sercom5, 0, 1);
#define SHT31_ADDRESS_4 0x45


SHT31 sht_1;
SHT31 sht_2;
SHT31 sht_3;
SHT31 sht_4;
SHT31 sht_1(&Wire);
SHT31 sht_2(&Wire);
SHT31 sht_3(&myWire);
SHT31 sht_4(&myWire);


bool b1, b2, b3, b4;
Expand All @@ -45,13 +45,13 @@ void setup()
myWire.setClock(100000);

// see datasheet for details
pinPeripheral(0, PIO_SERCOM_ALT);
pinPeripheral(1, PIO_SERCOM_ALT);
// pinPeripheral(0, PIO_SERCOM_ALT);
// pinPeripheral(1, PIO_SERCOM_ALT);

b1 = sht_1.begin(SHT31_ADDRESS_1, &Wire);
b2 = sht_2.begin(SHT31_ADDRESS_2, &Wire);
b3 = sht_3.begin(SHT31_ADDRESS_3, &myWire);
b4 = sht_4.begin(SHT31_ADDRESS_4, &myWire);
b1 = sht_1.begin(SHT31_ADDRESS_1);
b2 = sht_2.begin(SHT31_ADDRESS_2);
b3 = sht_3.begin(SHT31_ADDRESS_3);
b4 = sht_4.begin(SHT31_ADDRESS_4);

// see if they are connected
Serial.print("BEGIN:\t");
Expand Down Expand Up @@ -97,4 +97,3 @@ void loop()


// -- END OF FILE --

14 changes: 6 additions & 8 deletions examples/SHT31_two_I2C_array/SHT31_two_I2C_array.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
#include "SHT31.h"


// TwoWire myWire(&sercom5, 0, 1);
TwoWire myWire = Wire1;
TwoWire myWire(&sercom5, 0, 1);
// TwoWire myWire = Wire1;


uint8_t addr[4] = { 0x44, 0x45, 0x44, 0x45 };
TwoWire * wireAr[4] = { &Wire, &Wire, &myWire, &myWire };
SHT31 sht[4];
SHT31 sht[4] = { SHT31(&Wire), SHT31(&Wire), SHT31(&myWire), SHT31(&myWire) };
bool b[4];


Expand All @@ -36,12 +35,12 @@ void setup()
myWire.setClock(100000);

// see datasheet for details
// pinPeripheral(0, PIO_SERCOM_ALT);
// pinPeripheral(1, PIO_SERCOM_ALT);
pinPeripheral(0, PIO_SERCOM_ALT);
pinPeripheral(1, PIO_SERCOM_ALT);

for (uint8_t i = 0; i < 4; i++)
{
b[i] = sht[i].begin(addr[i], wireAr[i]);
b[i] = sht[i].begin(addr[i]);
}

// see if they are connected
Expand Down Expand Up @@ -80,4 +79,3 @@ void loop()


// -- END OF FILE --

4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/SHT31"
},
"version": "0.3.8",
"version": "0.4.0",
"license": "MIT",
"frameworks": "arduino",
"frameworks": "*",
"platforms": "*",
"headers": "SHT31.h"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SHT31
version=0.3.8
version=0.4.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for the SHT31 temperature and humidity sensor
Expand Down

0 comments on commit 62b5078

Please sign in to comment.