forked from letscontrolit/ESPEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P031_SHT1X.ino
247 lines (210 loc) · 7.03 KB
/
_P031_SHT1X.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//#######################################################################################################
//#################### Plugin 031: SHT10/SHT11/SHT15 Temp/Humidity Sensor ###############################
//#######################################################################################################
#define PLUGIN_031
#define PLUGIN_ID_031 31
#define PLUGIN_NAME_031 "Temperature & Humidity - SHT1X"
#define PLUGIN_VALUENAME1_031 "Temperature"
#define PLUGIN_VALUENAME2_031 "Humidity"
boolean Plugin_031_init = false;
byte Plugin_031_DATA_Pin = 0;
byte Plugin_031_CLOCK_Pin = 0;
int input_mode;
enum {
SHT1X_CMD_MEASURE_TEMP = B00000011,
SHT1X_CMD_MEASURE_RH = B00000101,
SHT1X_CMD_READ_STATUS = B00000111,
SHT1X_CMD_SOFT_RESET = B00011110
};
boolean Plugin_031(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_031;
Device[deviceCount].Type = DEVICE_TYPE_DUAL;
Device[deviceCount].VType = SENSOR_TYPE_TEMP_HUM;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = true;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 2;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_031);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_031));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_031));
break;
}
case PLUGIN_INIT:
{
Plugin_031_init = true;
Plugin_031_DATA_Pin = Settings.TaskDevicePin1[event->TaskIndex];
Plugin_031_CLOCK_Pin = Settings.TaskDevicePin2[event->TaskIndex];
if (Settings.TaskDevicePin1PullUp[event->TaskIndex]) {
input_mode = INPUT_PULLUP;
String log = F("SHT1X: Setting PullUp on pin ");
log += String(Plugin_031_DATA_Pin);
addLog(LOG_LEVEL_DEBUG, log);
}
else {
input_mode = INPUT;
}
pinMode(Plugin_031_DATA_Pin, input_mode); /* Keep Hi-Z except when sending data */
pinMode(Plugin_031_CLOCK_Pin, OUTPUT);
Plugin_031_reset();
byte status = Plugin_031_readStatus();
String log = F("SHT1X : Status byte: ");
log += String(status, HEX);
log += F(" - resolution: ");
log += (status & 1 ? "low" : "high");
log += F(" reload from OTP: ");
log += ((status >> 1) & 1 ? "yes" : "no");
log += F(", heater: ");
log += ((status >> 2) & 1 ? "on" : "off");
addLog(LOG_LEVEL_DEBUG, log);
success = true;
break;
}
case PLUGIN_READ:
{
if (!Plugin_031_init) {
String log = F("SHT1X : not yet initialized!");
addLog(LOG_LEVEL_ERROR, log);
break;
}
UserVar[event->BaseVarIndex] = Plugin_031_readTemperature();
UserVar[event->BaseVarIndex+1] = Plugin_031_readRelHumidity(UserVar[event->BaseVarIndex]);
success = true;
break;
}
}
return success;
}
float Plugin_031_readTemperature()
{
float tempRaw, tempC;
Plugin_031_sendCommand(SHT1X_CMD_MEASURE_TEMP);
Plugin_031_awaitResult();
tempRaw = Plugin_031_readData(16);
// Temperature conversion coefficients from SHT1X datasheet for version 4
const float d1 = -39.7; // 3.5V
const float d2 = 0.01; // 14-bit
tempC = d1 + (tempRaw * d2);
String log = F("SHT1X : Read temperature (raw): ");
log += String(tempRaw);
log += " (Celcius): ";
log += String(tempC);
addLog(LOG_LEVEL_DEBUG, log);
return tempC;
}
float Plugin_031_readRelHumidity(float tempC)
{
float raw, rhLinear, rhTrue;
Plugin_031_sendCommand(SHT1X_CMD_MEASURE_RH);
Plugin_031_awaitResult();
raw = Plugin_031_readData(16);
// Temperature conversion coefficients from SHT1X datasheet for version 4
const float c1 = -2.0468;
const float c2 = 0.0367;
const float c3 = -1.5955E-6;
const float t1 = 0.01;
const float t2 = 0.00008;
rhLinear = c1 + c2 * raw + c3 * raw * raw;
rhTrue = (tempC - 25) * (t1 + t2 * raw) + rhLinear;
String log = F("SHT1X : Read humidity (raw): ");
log += String(raw);
log += " (Linear): ";
log += String(rhLinear);
log += " (True): ";
log += String(rhTrue);
addLog(LOG_LEVEL_DEBUG, log);
return rhTrue;
}
void Plugin_031_reset()
{
delay(11);
for (int i=0; i<9; i++) {
digitalWrite(Plugin_031_CLOCK_Pin, HIGH);
digitalWrite(Plugin_031_CLOCK_Pin, LOW);
}
Plugin_031_sendCommand(SHT1X_CMD_SOFT_RESET);
delay(11);
}
byte Plugin_031_readStatus()
{
Plugin_031_sendCommand(SHT1X_CMD_READ_STATUS);
return Plugin_031_readData(8);
}
void Plugin_031_sendCommand(const byte cmd)
{
pinMode(Plugin_031_DATA_Pin, OUTPUT);
// Transmission Start sequence
digitalWrite(Plugin_031_DATA_Pin, HIGH);
digitalWrite(Plugin_031_CLOCK_Pin, HIGH);
digitalWrite(Plugin_031_DATA_Pin, LOW);
digitalWrite(Plugin_031_CLOCK_Pin, LOW);
digitalWrite(Plugin_031_CLOCK_Pin, HIGH);
digitalWrite(Plugin_031_DATA_Pin, HIGH);
digitalWrite(Plugin_031_CLOCK_Pin, LOW);
// Send the command (address must be 000b)
shiftOut(Plugin_031_DATA_Pin, Plugin_031_CLOCK_Pin, MSBFIRST, cmd);
// Wait for ACK
bool ackerror = false;
digitalWrite(Plugin_031_CLOCK_Pin, HIGH);
pinMode(Plugin_031_DATA_Pin, input_mode);
if (digitalRead(Plugin_031_DATA_Pin) != LOW) ackerror = true;
digitalWrite(Plugin_031_CLOCK_Pin, LOW);
if (cmd == SHT1X_CMD_MEASURE_TEMP || cmd == SHT1X_CMD_MEASURE_RH) {
delayMicroseconds(1); /* Give the sensor time to release the data line */
if (digitalRead(Plugin_031_DATA_Pin) != HIGH) ackerror = true;
}
if (ackerror) {
String log = F("SHT1X : Sensor did not ACK command");
addLog(LOG_LEVEL_ERROR, log);
}
}
void Plugin_031_awaitResult()
{
// Maximum 320ms for 14 bit measurement
for (int i=0; i<16; i++) {
if (digitalRead(Plugin_031_DATA_Pin) == LOW) return;
delay(20);
}
if (digitalRead(Plugin_031_DATA_Pin) != LOW) {
String log = F("SHT1X : Data not ready");
addLog(LOG_LEVEL_ERROR, log);
}
}
int Plugin_031_readData(const int bits)
{
int val = 0;
if (bits == 16) {
// Read most significant byte
val = shiftIn(Plugin_031_DATA_Pin, Plugin_031_CLOCK_Pin, 8);
val <<= 8;
// Send ACK
pinMode(Plugin_031_DATA_Pin, OUTPUT);
digitalWrite(Plugin_031_DATA_Pin, LOW);
digitalWrite(Plugin_031_CLOCK_Pin, HIGH);
digitalWrite(Plugin_031_CLOCK_Pin, LOW);
pinMode(Plugin_031_DATA_Pin, input_mode);
}
// Read least significant byte
val |= shiftIn(Plugin_031_DATA_Pin, Plugin_031_CLOCK_Pin, 8);
// Keep DATA pin high to skip CRC
digitalWrite(Plugin_031_CLOCK_Pin, HIGH);
digitalWrite(Plugin_031_CLOCK_Pin, LOW);
return val;
}