forked from letscontrolit/ESPEasyPluginPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P136_TMP117.ino
256 lines (229 loc) · 9.33 KB
/
_P136_TMP117.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
248
249
250
251
252
253
254
255
256
#ifdef PLUGIN_BUILD_TESTING
//#######################################################################################################
//######################## Plugin 136 TMP117 I2C high-accuracy Temperature Sensor ############################################
//#######################################################################################################
// by tonydee ### 20-03-2020 https://microtechitalia.it
#include <TMP117.h>
#define PLUGIN_136
#define PLUGIN_ID_136 136
#define PLUGIN_NAME_136 "Temperature High Precision - TMP117 [TESTING]"
#define PLUGIN_VALUENAME1_136 "Temperature"
// ======================================
// TMP117 I2C high-accuracy Temperature Sensor
// ======================================
boolean Plugin_136_init = false;
// Select the correct address setting
uint8_t ADDR_GND = 0x48; // 1001000
uint8_t ADDR_VCC = 0x49; // 1001001
uint8_t ADDR_SDA = 0x4A; // 1001010
uint8_t ADDR_SCL = 0x4B; // 1001011
uint8_t ADDR = ADDR_GND;
uint8_t ConvTime = 0x00;
uint8_t Averaging = 0x00;
boolean Plugin_136(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_136;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 1; //number of output variables. The value should match the number of keys PLUGIN_VALUENAME1_xxx
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_136);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_136));
// strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_136));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
#define TMP117_ConvTime_OPTION 8
#define TMP117_Averaging_OPTION 4
byte choice = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
String options[TMP117_ConvTime_OPTION];
uint optionValues[TMP117_ConvTime_OPTION];
optionValues[0] = (0x00);
options[0] = F("0 - ConvTime C15mS5");
optionValues[1] = (0x01);
options[1] = F("1 - ConvTime C125mS");
optionValues[2] = (0x02);
options[2] = F("2 - ConvTime C250mS");
optionValues[3] = (0x03);
options[3] = F("3 - ConvTime C500mS");
optionValues[4] = (0x04);
options[4] = F("4 - ConvTime C1S");
optionValues[5] = (0x05);
options[5] = F("5 - ConvTime C4S");
optionValues[6] = (0x06);
options[6] = F("6 - ConvTime C8S");
optionValues[7] = (0x07);
options[7] = F("7 - ConvTime C16S");
string += F("<TR><TD>ConvTime:<TD><select name='plugin_136_ConvTime'>");
for (byte x = 0; x < TMP117_ConvTime_OPTION; x++)
{
string += F("<option value='");
string += optionValues[x];
string += "'";
if (choice == optionValues[x])
string += F(" selected");
string += ">";
string += options[x];
string += F("</option>");
}
string += F("</select>");
byte choice1 = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
String options1[TMP117_Averaging_OPTION];
uint optionValues1[TMP117_Averaging_OPTION];
optionValues1[0] = (0x00);
options1[0] = F("0 - Averaging (0)");
optionValues1[1] = (0x01);
options1[1] = F("1 - Averaging (8)");
optionValues1[2] = (0x02);
options1[2] = F("2 - Averaging (32)");
optionValues1[3] = (0x03);
options1[3] = F("3 - Averaging (64)");
string += F("<TR><TD>Averaging:<TD><select name='plugin_136_Averaging'>");
for (byte x = 0; x < TMP117_Averaging_OPTION; x++)
{
string += F("<option value='");
string += optionValues1[x];
string += "'";
if (choice1 == optionValues1[x])
string += F(" selected");
string += ">";
string += options1[x];
string += F("</option>");
}
string += F("</select>");
string += F("<TR><TD>Sensor Adjustment [°C]:<TD><input type='text' title='calibrate sensor to target temperature' name='");
string += F("plugin_136_Adjust' value='");
string += Settings.TaskDevicePluginConfigFloat[event->TaskIndex][2];
string += F("'>");
string += F("<TR><TD>Offset [°C]:<TD><input type='text' title='set temperature offset like ' name='");
string += F("plugin_136_Offset' value='");
string += Settings.TaskDevicePluginConfigFloat[event->TaskIndex][3];
string += F("'>");
LoadTaskSettings(event->TaskIndex); // we need to restore our original taskvalues!
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg F("plugin_136_ConvTime");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
String plugin2 = WebServer.arg F("plugin_136_Averaging");
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = plugin2.toInt();
String TMP117_adjust = WebServer.arg F("plugin_136_Adjust");
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][2] = TMP117_adjust.toFloat();
String TMP117_offset = WebServer.arg F("plugin_136_Offset");
Settings.TaskDevicePluginConfigFloat[event->TaskIndex][3] = TMP117_offset.toFloat();
Plugin_136_init = false; // Force device setup next time
success = true;
break;
}
case PLUGIN_INIT:
{
LoadTaskSettings(event->TaskIndex); // we need to restore our original taskvalues!
Plugin_136_init = true;
success = true;
break;
}
case PLUGIN_READ:
{
TMP117 TMP117_tmp(ADDR); // I2C address for the sensor
//float TMP117_offset = 0.0;
//float TMP117_adjust = 0.5;
// Get sensor resolution configuration
uint8_t ConvTime = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
uint8_t Averaging = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
float TMP117_adjust = Settings.TaskDevicePluginConfigFloat[event->TaskIndex][2];
float TMP117_offset = Settings.TaskDevicePluginConfigFloat[event->TaskIndex][3];
Wire.begin();
/* The default setup is :
* Conversion mode = CONTINUOUS ---> continuoss
* Conversion time = C125mS -|
* Averaging mode = AVE8 -|-> new data every 125mS
* Alert mode = data ---> alert pin states that new data is available
*
*/
TMP117_tmp.init ( NULL ); // no callback
TMP117_tmp.setConvMode (CONTINUOUS); // contious measurement, also ONESHOT or SHUTWDOWN possible
switch (ConvTime)
{
case (0x00):
TMP117_tmp.setConvTime (C15mS5); // 1. setup C125mS+NOAVE = 15.5 mS measurement time
break;
case (0x01):
TMP117_tmp.setConvTime (C125mS);
break;
case (0x02):
TMP117_tmp.setConvTime (C250mS);
break;
case (0x03):
TMP117_tmp.setConvTime (C500mS);
break;
case (0x04):
TMP117_tmp.setConvTime (C1S);
break;
case (0x05):
TMP117_tmp.setConvTime (C4S);
break;
case (0x06):
TMP117_tmp.setConvTime (C8S);
break;
case (0x07):
TMP117_tmp.setConvTime (C16S);
break;
default:
TMP117_tmp.setConvTime (C15mS5);
break;
}
switch (Averaging)
{
case (0x00):
TMP117_tmp.setAveraging (NOAVE);
break;
case (0x01):
TMP117_tmp.setAveraging (AVE8);
break;
case (0x02):
TMP117_tmp.setAveraging (AVE32);
break;
case (0x03):
TMP117_tmp.setAveraging (AVE64);
break;
default:
TMP117_tmp.setAveraging (NOAVE);
break;
}
TMP117_tmp.setOffsetTemperature ( TMP117_offset ); //set temperature offset
TMP117_tmp.setTargetTemperature ( TMP117_adjust ); //calibrate sensor to target temperature
TMP117_tmp.update();
// double temperature = TMP117_tmp.getTemperature();
UserVar[event->BaseVarIndex] = TMP117_tmp.getTemperature();
String log = F("TMP117: Temperature: ");
log += UserVar[event->BaseVarIndex];
addLog(LOG_LEVEL_INFO, log);
success = true;
break;
}
}
return success;
}
#endif