Skip to content
This repository was archived by the owner on Jan 30, 2019. It is now read-only.

Commit 5eb9e79

Browse files
committed
Initial import
0 parents  commit 5eb9e79

File tree

11 files changed

+547
-0
lines changed

11 files changed

+547
-0
lines changed

examples/sht3x/sht3x.ino

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <Wire.h>
2+
3+
#include "sht3x.h"
4+
5+
SHT3X sht3x;
6+
7+
void setup() {
8+
// put your setup code here, to run once:
9+
10+
sht3x.setAddress(SHT3X::I2C_ADDRESS_45);
11+
sht3x.setAccuracy(SHT3X::ACCURACY_MEDIUM);
12+
Wire.begin();
13+
Serial.begin(9600);
14+
15+
delay(1000); // let serial console settle
16+
}
17+
18+
void loop() {
19+
// put your main code here, to run repeatedly:
20+
21+
sht3x.readSample();
22+
Serial.print("SHT3x:\n");
23+
Serial.print(" RH: ");
24+
Serial.print(sht3x.getHumidity(), 2);
25+
Serial.print("\n");
26+
Serial.print(" T: ");
27+
Serial.print(sht3x.getTemperature(), 2);
28+
Serial.print("\n");
29+
30+
delay(1000);
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <Wire.h>
2+
3+
#include "sht3xanalog.h"
4+
5+
SHT3XAnalog sht3xAnalog(A0, A1);
6+
7+
void setup() {
8+
// put your setup code here, to run once:
9+
10+
Wire.begin();
11+
Serial.begin(9600);
12+
13+
delay(1000); // let serial console settle
14+
}
15+
16+
void loop() {
17+
Serial.print("SHT3x Analog:\n");
18+
Serial.print(" RH: ");
19+
Serial.print(sht3xAnalog.readHumidity(), 2);
20+
Serial.print("\n");
21+
Serial.print(" T: ");
22+
Serial.print(sht3xAnalog.readTemperature(), 2);
23+
Serial.print("\n");
24+
25+
delay(1000);
26+
}

examples/shtc1/shtc1.ino

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <Wire.h>
2+
3+
#include "shtc1.h"
4+
5+
SHTC1 shtc1;
6+
7+
void setup() {
8+
// put your setup code here, to run once:
9+
10+
Wire.begin();
11+
Serial.begin(9600);
12+
13+
delay(1000); // let serial console settle
14+
}
15+
16+
void loop() {
17+
// put your main code here, to run repeatedly:
18+
19+
shtc1.readSample();
20+
Serial.print("SHTC1:\n");
21+
Serial.print(" RH: ");
22+
Serial.print(shtc1.getHumidity(), 2);
23+
Serial.print("\n");
24+
Serial.print(" T: ");
25+
Serial.print(shtc1.getTemperature(), 2);
26+
Serial.print("\n");
27+
28+
delay(1000);
29+
}

sht3x.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2015, Johannes Winkelmann <jw@smts.ch>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the <organization> nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#ifndef SHT3X_H
29+
#define SHT3X_H
30+
31+
#include "shtbase.h"
32+
33+
class SHT3X : public ShtBase
34+
{
35+
public:
36+
enum SHT3X_I2C_ADDRESS {
37+
I2C_ADDRESS_44 = 0x44,
38+
I2C_ADDRESS_45 = 0x45
39+
};
40+
41+
enum SHT3X_ACCURACY {
42+
ACCURACY_HIGH = 0x2C06,
43+
ACCURACY_MEDIUM = 0x2C0D,
44+
ACCURACY_LOW = 0x2C10
45+
};
46+
47+
SHT3X() : ShtBase(I2C_ADDRESS_44, ACCURACY_HIGH,
48+
-45, 175, 65535,
49+
100, 65535)
50+
{}
51+
52+
void setAccuracy(SHT3X_ACCURACY newAccuracy)
53+
{ mI2CCommand = newAccuracy; }
54+
void setAddress(SHT3X_I2C_ADDRESS newAddress)
55+
{ mI2CAddress = newAddress; }
56+
};
57+
58+
59+
#endif /* #ifdef SHT3X_H */

sht3xanalog.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2015, Johannes Winkelmann <jw@smts.ch>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the <organization> nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#ifndef SHT3XANALOG_H
29+
#define SHT3XANALOG_H
30+
31+
#include <Arduino.h>
32+
#include <math.h>
33+
#include <inttypes.h>
34+
35+
class SHT3XAnalog
36+
{
37+
public:
38+
SHT3XAnalog(uint8_t humidityPin, uint8_t temperaturePin)
39+
: mHumidityAdcPin(humidityPin), mTemperatureAdcPin(temperaturePin)
40+
{
41+
}
42+
43+
double readHumidity()
44+
{ return -12.5 + 125 * (analogRead(mHumidityAdcPin) / (double)MAX_ADC); }
45+
double readTemperature()
46+
{ return -66.875 + 218.75 * (analogRead(mTemperatureAdcPin) / (double)MAX_ADC); }
47+
48+
private:
49+
uint8_t mHumidityAdcPin;
50+
uint8_t mTemperatureAdcPin;
51+
52+
static const uint16_t MAX_ADC = 1023; // for the 10-bit default
53+
};
54+
55+
#endif /* SHT3XANALOG_H */

shtbase.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2015, Johannes Winkelmann <jw@smts.ch>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the <organization> nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#include <Wire.h>
29+
#include <Arduino.h>
30+
31+
#include "shtbase.h"
32+
#include "shti2chelper.h"
33+
34+
ShtBase::ShtBase(uint8_t i2cAddress,
35+
uint16_t i2cCommand,
36+
float a, float b, float c,
37+
float x, float y)
38+
: mI2CAddress(i2cAddress),
39+
mI2CCommand(i2cCommand),
40+
mHumidity(HUMIDITY_INVALID),
41+
mTemperature(TEMPERATURE_INVALID),
42+
mA(a), mB(b), mC(c),
43+
mX(x), mY(y)
44+
{
45+
}
46+
47+
float ShtBase::getHumidity() const
48+
{
49+
return mHumidity;
50+
}
51+
52+
float ShtBase::getTemperature() const
53+
{
54+
return mTemperature;
55+
}
56+
57+
bool ShtBase::readSample()
58+
{
59+
uint8_t data[EXPECTED_DATA_SIZE];
60+
uint8_t cmd[CMD_SIZE];
61+
62+
cmd[0] = mI2CCommand >> 8;
63+
cmd[1] = mI2CCommand & 0x00ff;
64+
65+
if (!ShtI2CHelper::readFromI2C(mI2CAddress, cmd, CMD_SIZE, data, EXPECTED_DATA_SIZE)) {
66+
return false;
67+
}
68+
69+
// Important: this is assuming 2-byte data, 1-byte
70+
uint16_t val;
71+
val = (data[0] << 8) + data[1];
72+
mTemperature = mA + mB * (val / mC);
73+
// TODO: verify CRC: data[2]
74+
75+
val = (data[3] << 8) + data[4];
76+
mHumidity = mX * (val / mY);
77+
// TODO: verify CRC: data[5]
78+
79+
return true;
80+
}

shtbase.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2015, Johannes Winkelmann <jw@smts.ch>
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the <organization> nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
#ifndef SHTBASE_H
29+
#define SHTBASE_H
30+
31+
#include <inttypes.h>
32+
33+
/*
34+
* Base class for SHTC1, SHTW1 and SHT3x sensors
35+
*
36+
* this is assuming 2-byte data, 1-byte CRC for each RH and T
37+
* Temperature first, RH second
38+
*/
39+
class ShtBase
40+
{
41+
public:
42+
/*
43+
* Note: subclasses should never expose the i2c command
44+
*/
45+
ShtBase(uint8_t i2cAddress,
46+
uint16_t i2cCommand,
47+
float a, float b, float c,
48+
float x, float y);
49+
50+
float getHumidity() const;
51+
float getTemperature() const;
52+
53+
bool readSample();
54+
55+
protected:
56+
uint8_t mI2CAddress;
57+
uint16_t mI2CCommand;
58+
59+
private:
60+
float mHumidity;
61+
float mTemperature;
62+
63+
// -- conversion parameters
64+
// T_OUT = A + B * (T_IN / C)
65+
float mA = 0;
66+
float mB = 0;
67+
float mC = 1;
68+
// RH_OUT = X * (RH_IN / Y)
69+
float mX = 0;
70+
float mY = 1;
71+
72+
static const uint8_t MAX_READ_TRIES = 5;
73+
static const uint8_t CMD_SIZE = 2;
74+
static const uint8_t EXPECTED_DATA_SIZE = 6;
75+
static const int16_t HUMIDITY_INVALID = -1;
76+
static const int16_t TEMPERATURE_INVALID = -999;
77+
};
78+
79+
#endif /* SHTBASE_H */

0 commit comments

Comments
 (0)