Skip to content

Commit 62b5078

Browse files
authored
fix #38 support Wire1 for ESP32 (#39)
- fix #38 support for Wire1 for ESP32 - update examples - update readme.md
1 parent ac8fdd4 commit 62b5078

File tree

8 files changed

+85
-60
lines changed

8 files changed

+85
-60
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.4.0] - 2023-09-21
10+
- fix #38 support for Wire1 for ESP32
11+
- update examples
12+
- update readme.md
13+
14+
----
15+
916
## [0.3.8] - 2023-03-23
1017
- prepare for derived class SHT31_SW
1118
- update readme.md
1219
- update GitHub actions
1320
- update license 2023
1421
- minor edits
1522

16-
1723
## [0.3.7] - 2022-11-24
1824
- Add RP2040 support to build-CI.
1925
- Add CHANGELOG.md

README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

2-
[![Arduino CI](https://github.com/robtillaart/SHT31/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3-
[![JSON check](https://github.com/RobTillaart/SHT31/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT31/actions/workflows/jsoncheck.yml)
2+
[![Arduino CI](https://github.com/RobTillaart/SHT31/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
43
[![Arduino-lint](https://github.com/RobTillaart/SHT31/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/SHT31/actions/workflows/arduino-lint.yml)
4+
[![JSON check](https://github.com/RobTillaart/SHT31/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT31/actions/workflows/jsoncheck.yml)
5+
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/SHT31.svg)](https://github.com/RobTillaart/SHT31/issues)
6+
57
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/SHT31/blob/master/LICENSE)
68
[![GitHub release](https://img.shields.io/github/release/RobTillaart/SHT31.svg?maxAge=3600)](https://github.com/RobTillaart/SHT31/releases)
9+
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/SHT31.svg)](https://registry.platformio.org/libraries/robtillaart/SHT31)
710

811

912
# SHT31
@@ -31,20 +34,27 @@ Accuracy table
3134

3235

3336
An elaborated library for the SHT31 sensor can be found here
34-
https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube
37+
- https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube
38+
39+
A derived class for using the SHT31 sensor with SoftWire (soft I2C) can be found here
40+
- https://github.com/RobTillaart/SHT31_SW
3541

3642

3743
## Interface
3844

45+
```cpp
46+
#include "SHT31.h"
47+
```
48+
49+
3950
#### Base interface
4051

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

150160
## Future
151161

152-
- keep in sync with SHT85 library
153-
- check TODO in code
162+
#### Must
163+
164+
- keep in sync with SHT85 library.
165+
- keep derived SHT31_SW builds green
166+
167+
#### Should
168+
169+
- check TODO in code.
170+
- rename MAGIC numbers. e.g. in dataReady()
171+
172+
#### Could
173+
174+
- move code from .h to .cpp
175+
176+
#### Wont
177+
178+
179+
## Support
180+
181+
If you appreciate my libraries, you can support the development and maintenance.
182+
Improve the quality of the libraries by providing issues and Pull Requests, or
183+
donate through PayPal or GitHub sponsors.
184+
185+
Thank you,
154186

155187

SHT31.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT31.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.8
4+
// VERSION: 0.4.0
55
// DATE: 2019-02-08
66
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
77
// https://www.adafruit.com/product/2857
@@ -26,9 +26,9 @@
2626
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds
2727

2828

29-
SHT31::SHT31()
29+
SHT31::SHT31(TwoWire *wire)
3030
{
31-
_wire = NULL;
31+
_wire = wire;
3232
_address = 0;
3333
_lastRead = 0;
3434
_rawTemperature = 0;
@@ -50,7 +50,6 @@ bool SHT31::begin(const uint8_t address, const uint8_t dataPin, const uint8_t cl
5050
}
5151
_address = address;
5252

53-
_wire = &Wire;
5453
if ((dataPin < 255) && (clockPin < 255))
5554
{
5655
_wire->begin(dataPin, clockPin);
@@ -68,25 +67,18 @@ bool SHT31::begin(const uint8_t dataPin, const uint8_t clockPin)
6867
#endif
6968

7069

71-
bool SHT31::begin(const uint8_t address, TwoWire *wire)
70+
bool SHT31::begin(const uint8_t address)
7271
{
7372
if ((address != 0x44) && (address != 0x45))
7473
{
7574
return false;
7675
}
7776
_address = address;
78-
_wire = wire;
7977
_wire->begin();
8078
return reset();
8179
}
8280

8381

84-
bool SHT31::begin(TwoWire *wire)
85-
{
86-
return begin(SHT_DEFAULT_ADDRESS, wire);
87-
}
88-
89-
9082
bool SHT31::read(bool fast)
9183
{
9284
if (writeCmd(fast ? SHT31_MEASUREMENT_FAST : SHT31_MEASUREMENT_SLOW) == false)
@@ -151,7 +143,7 @@ uint16_t SHT31::readStatus()
151143
return 0xFFFF;
152144
}
153145

154-
if (status[2] != crc8(status, 2))
146+
if (status[2] != crc8(status, 2))
155147
{
156148
_error = SHT31_ERR_CRC_STATUS;
157149
return 0xFFFF;
@@ -256,12 +248,12 @@ bool SHT31::readData(bool fast)
256248

257249
if (!fast)
258250
{
259-
if (buffer[2] != crc8(buffer, 2))
251+
if (buffer[2] != crc8(buffer, 2))
260252
{
261253
_error = SHT31_ERR_CRC_TEMP;
262254
return false;
263255
}
264-
if (buffer[5] != crc8(buffer + 3, 2))
256+
if (buffer[5] != crc8(buffer + 3, 2))
265257
{
266258
_error = SHT31_ERR_CRC_HUM;
267259
return false;
@@ -287,17 +279,17 @@ int SHT31::getError()
287279

288280
//////////////////////////////////////////////////////////
289281

290-
uint8_t SHT31::crc8(const uint8_t *data, uint8_t len)
282+
uint8_t SHT31::crc8(const uint8_t *data, uint8_t len)
291283
{
292284
// CRC-8 formula from page 14 of SHT spec pdf
293285
const uint8_t POLY(0x31);
294286
uint8_t crc(0xFF);
295287

296-
for (uint8_t j = len; j; --j)
288+
for (uint8_t j = len; j; --j)
297289
{
298290
crc ^= *data++;
299291

300-
for (uint8_t i = 8; i; --i)
292+
for (uint8_t i = 8; i; --i)
301293
{
302294
crc = (crc & 0x80) ? (crc << 1) ^ POLY : (crc << 1);
303295
}

SHT31.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
//
33
// FILE: SHT31.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.3.8
5+
// VERSION: 0.4.0
66
// DATE: 2019-02-08
77
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
88
// https://www.adafruit.com/product/2857
99
// URL: https://github.com/RobTillaart/SHT31
1010

1111

1212
#include "Arduino.h"
13-
#include "Wire.h"
13+
#include "Wire.h"
1414

1515

16-
#define SHT31_LIB_VERSION (F("0.3.8"))
16+
#define SHT31_LIB_VERSION (F("0.4.0"))
1717

18-
#ifndef SHT_DEFAULT_ADDRESS
18+
#ifndef SHT_DEFAULT_ADDRESS
1919
#define SHT_DEFAULT_ADDRESS 0x44
2020
#endif
2121

@@ -44,16 +44,14 @@
4444
class SHT31
4545
{
4646
public:
47-
SHT31();
47+
SHT31(TwoWire *wire = &Wire);
4848

4949
#if defined(ESP8266) || defined(ESP32)
5050
bool begin(const uint8_t address, uint8_t dataPin, uint8_t clockPin);
5151
// use SHT_DEFAULT_ADDRESS
5252
bool begin(const uint8_t dataPin, const uint8_t clockPin);
5353
#endif
54-
bool begin(const uint8_t address, TwoWire *wire = &Wire);
55-
// use SHT_DEFAULT_ADDRESS
56-
bool begin(TwoWire *wire = &Wire);
54+
bool begin(const uint8_t address = SHT_DEFAULT_ADDRESS);
5755

5856
// blocks 15 milliseconds + actual read + math
5957
bool read(bool fast = true);

examples/SHT31_two_I2C/SHT31_two_I2C.ino

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "SHT31.h"
1313

1414

15-
TwoWire myWire(&sercom5, 0, 1);
16-
// TwoWire myWire = Wire1; // test.
15+
// TwoWire myWire(&sercom5, 0, 1);
16+
TwoWire myWire = Wire1; // test.
1717

1818

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

2525

26-
SHT31 sht_1;
27-
SHT31 sht_2;
28-
SHT31 sht_3;
29-
SHT31 sht_4;
26+
SHT31 sht_1(&Wire);
27+
SHT31 sht_2(&Wire);
28+
SHT31 sht_3(&myWire);
29+
SHT31 sht_4(&myWire);
3030

3131

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

4747
// see datasheet for details
48-
pinPeripheral(0, PIO_SERCOM_ALT);
49-
pinPeripheral(1, PIO_SERCOM_ALT);
48+
// pinPeripheral(0, PIO_SERCOM_ALT);
49+
// pinPeripheral(1, PIO_SERCOM_ALT);
5050

51-
b1 = sht_1.begin(SHT31_ADDRESS_1, &Wire);
52-
b2 = sht_2.begin(SHT31_ADDRESS_2, &Wire);
53-
b3 = sht_3.begin(SHT31_ADDRESS_3, &myWire);
54-
b4 = sht_4.begin(SHT31_ADDRESS_4, &myWire);
51+
b1 = sht_1.begin(SHT31_ADDRESS_1);
52+
b2 = sht_2.begin(SHT31_ADDRESS_2);
53+
b3 = sht_3.begin(SHT31_ADDRESS_3);
54+
b4 = sht_4.begin(SHT31_ADDRESS_4);
5555

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

9898

9999
// -- END OF FILE --
100-

examples/SHT31_two_I2C_array/SHT31_two_I2C_array.ino

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
#include "SHT31.h"
1414

1515

16-
// TwoWire myWire(&sercom5, 0, 1);
17-
TwoWire myWire = Wire1;
16+
TwoWire myWire(&sercom5, 0, 1);
17+
// TwoWire myWire = Wire1;
1818

1919

2020
uint8_t addr[4] = { 0x44, 0x45, 0x44, 0x45 };
21-
TwoWire * wireAr[4] = { &Wire, &Wire, &myWire, &myWire };
22-
SHT31 sht[4];
21+
SHT31 sht[4] = { SHT31(&Wire), SHT31(&Wire), SHT31(&myWire), SHT31(&myWire) };
2322
bool b[4];
2423

2524

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

3837
// see datasheet for details
39-
// pinPeripheral(0, PIO_SERCOM_ALT);
40-
// pinPeripheral(1, PIO_SERCOM_ALT);
38+
pinPeripheral(0, PIO_SERCOM_ALT);
39+
pinPeripheral(1, PIO_SERCOM_ALT);
4140

4241
for (uint8_t i = 0; i < 4; i++)
4342
{
44-
b[i] = sht[i].begin(addr[i], wireAr[i]);
43+
b[i] = sht[i].begin(addr[i]);
4544
}
4645

4746
// see if they are connected
@@ -80,4 +79,3 @@ void loop()
8079

8180

8281
// -- END OF FILE --
83-

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/SHT31"
1717
},
18-
"version": "0.3.8",
18+
"version": "0.4.0",
1919
"license": "MIT",
20-
"frameworks": "arduino",
20+
"frameworks": "*",
2121
"platforms": "*",
2222
"headers": "SHT31.h"
2323
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SHT31
2-
version=0.3.8
2+
version=0.4.0
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for the SHT31 temperature and humidity sensor

0 commit comments

Comments
 (0)