-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
_P047_i2c-soil-moisture-sensor.ino
290 lines (243 loc) · 9.81 KB
/
_P047_i2c-soil-moisture-sensor.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "_Plugin_Helper.h"
#ifdef USES_P047
// #######################################################################################################
// #################### Plugin 047 Moisture & Temperature & Light I2C Soil moisture sensor ##############
// #######################################################################################################
//
// Capacitive soil moisture sensor
// like this one: https://www.tindie.com/products/miceuz/i2c-soil-moisture-sensor/
// based on this library: https://github.com/Apollon77/I2CSoilMoistureSensor
// this code is based on version 1.1.2 of the above library
//
/** Changelog:
* 2024-05-09 tonhuisman: Add support for BeFlE v3.x (low power) Moisture sensor
* Code improvements
* ** Fix bug in setting a new I2C address for BeFlE sensors (needs a left-shift by 1)
* 2024-04-05 tonhuisman: Complete implementation for Afafruit I2C Capacitive Moisture sensor.
* Log sensor name and version (or 0 when not available) at plugin startup.
* 2024-03-23 tonhuisman: Start implementation of Adafruit I2C Capacitive Moisture Sensor (product ID 4026)
* From a forum request: https://www.letscontrolit.com/forum/viewtopic.php?t=10107
* 2023-04-07 tonhuisman: Correct typo BelFlE to BeFlE
* 2023-04-01 tonhuisman: Implement staged reading instead of a fixed delay during PLUGIN_READ
* Add range-check on save for I2C address inputs (0x01..0x7F)
* 2023-03-31 tonhuisman: Add support for BelFlE I2C Moisture sensor,
* from a forum request: https://www.letscontrolit.com/forum/viewtopic.php?t=9581
* Move some code to PluginStruct files
* 2023-03-31 tonhuisman: Start of changelog, older changes not logged
*/
# define PLUGIN_047
# define PLUGIN_ID_047 47
# define PLUGIN_NAME_047 "Environment - Soil moisture sensor"
# define PLUGIN_VALUENAME1_047 "Temperature"
# define PLUGIN_VALUENAME2_047 "Moisture"
# define PLUGIN_VALUENAME3_047 "Light"
# include "src/PluginStructs/P047_data_struct.h"
boolean Plugin_047(uint8_t function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_047;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_TRIPLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = true;
Device[deviceCount].ValueCount = 3;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
Device[deviceCount].PluginStats = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_047);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_047));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_047));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_047));
break;
}
case PLUGIN_GET_DEVICEVALUECOUNT:
{
event->Par1 = P047_MODEL_CATNIP == static_cast<P047_SensorModels>(P047_MODEL) ? 3 : 2;
success = true;
break;
}
case PLUGIN_GET_DEVICEVTYPE:
{
event->sensorType = static_cast<Sensor_VType>(P047_MODEL_CATNIP == static_cast<P047_SensorModels>(P047_MODEL) ? 3 : 2);
event->idx = P047_MODEL_CATNIP == static_cast<P047_SensorModels>(P047_MODEL) ? 3 : 2;
success = true;
break;
}
case PLUGIN_SET_DEFAULTS:
{
P047_I2C_ADDR = P047_CATNIP_DEFAULT_ADDR;
break;
}
case PLUGIN_WEBFORM_SHOW_I2C_PARAMS:
{
# if P047_FEATURE_ADAFRUIT
if (P047_MODEL_ADAFRUIT == static_cast<P047_SensorModels>(P047_MODEL)) {
const uint8_t i2cAddressValues[] = { P047_ADAFRUIT_DEFAULT_ADDR, 0x37, 0x38, 0x39 };
addFormSelectorI2C(F("i2c_addr"), 4, i2cAddressValues, P047_I2C_ADDR);
} else
# endif // if P047_FEATURE_ADAFRUIT
{
addFormTextBox(F("I2C Address (Hex)"), F("i2c_addr"),
formatToHex_decimal(P047_I2C_ADDR), 4);
addUnit(F("0x01..0x7F"));
}
break;
}
case PLUGIN_I2C_HAS_ADDRESS:
{
success = event->Par1 == P047_I2C_ADDR; // Show for currently configured address
break;
}
# if FEATURE_I2C_GET_ADDRESS
case PLUGIN_I2C_GET_ADDRESS:
{
event->Par1 = P047_I2C_ADDR;
success = true;
break;
}
# endif // if FEATURE_I2C_GET_ADDRESS
case PLUGIN_WEBFORM_LOAD:
{
{
const __FlashStringHelper *SensorModels[] = {
toString(P047_MODEL_CATNIP),
toString(P047_MODEL_BEFLE),
# if P047_FEATURE_BEFLE_V3
toString(P047_MODEL_BEFLE_V3),
# endif // if P047_FEATURE_BEFLE_V3
# if P047_FEATURE_ADAFRUIT
toString(P047_MODEL_ADAFRUIT),
# endif // if P047_FEATURE_ADAFRUIT
};
const int SensorModelIds[] = {
static_cast<int>(P047_MODEL_CATNIP),
static_cast<int>(P047_MODEL_BEFLE),
# if P047_FEATURE_BEFLE_V3
static_cast<int>(P047_MODEL_BEFLE_V3),
# endif // if P047_FEATURE_BEFLE_V3
# if P047_FEATURE_ADAFRUIT
static_cast<int>(P047_MODEL_ADAFRUIT),
# endif // if P047_FEATURE_ADAFRUIT
};
constexpr size_t P047_MODEL_OPTIONS = NR_ELEMENTS(SensorModelIds);
addFormSelector(F("Sensor model"), F("model"), P047_MODEL_OPTIONS, SensorModels, SensorModelIds, P047_MODEL, true);
addFormNote(F("Changing the Sensor model will reload the page."));
}
if ((P047_MODEL_CATNIP == static_cast<P047_SensorModels>(P047_MODEL))
# if P047_FEATURE_BEFLE_V3
|| (P047_MODEL_BEFLE_V3 == static_cast<P047_SensorModels>(P047_MODEL))
# endif // if P047_FEATURE_BEFLE_V3
) {
addFormSeparator(2);
addFormCheckBox(F("Send sensor to sleep"), F("sleep"), P047_SENSOR_SLEEP);
# if P047_FEATURE_BEFLE_V3
if (P047_MODEL_CATNIP == static_cast<P047_SensorModels>(P047_MODEL))
# endif // if !P047_FEATURE_BEFLE_V3
{
addFormCheckBox(F("Check sensor version"), F("version"), P047_CHECK_VERSION);
}
}
# if P047_FEATURE_ADAFRUIT
if (P047_MODEL_ADAFRUIT != static_cast<P047_SensorModels>(P047_MODEL))
# endif // if P047_FEATURE_ADAFRUIT
{
addFormSeparator(2);
addFormCheckBox(F("Change Sensor address"), F("changeAddr"), false);
addFormTextBox(F("Change I2C Addr. to (Hex)"), F("newAddr"),
formatToHex_decimal(P047_I2C_ADDR), 4);
addUnit(F("0x01..0x7F"));
}
success = true;
break;
}
case PLUGIN_WEBFORM_SAVE:
{
success = true;
String webarg;
int addr;
# if P047_FEATURE_ADAFRUIT
if (P047_MODEL_ADAFRUIT == static_cast<P047_SensorModels>(P047_MODEL)) {
P047_I2C_ADDR = getFormItemInt(F("i2c_addr"));
} else
# endif // if P047_FEATURE_ADAFRUIT
{
webarg = webArg(F("i2c_addr"));
addr = static_cast<int>(strtol(webarg.c_str(), 0, 16));
if ((addr > 0x00) && (addr < 0x80)) {
P047_I2C_ADDR = addr;
} else {
addHtmlError(F("I2C Address (Hex) error, range: 0x01..0x7F"));
success = false;
}
}
uint8_t model = getFormItemInt(F("model"));
if (model != P047_MODEL) {
P047_MODEL = model;
if (P047_MODEL_CATNIP == static_cast<P047_SensorModels>(model)) {
P047_I2C_ADDR = P047_CATNIP_DEFAULT_ADDR;
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_047)); // Gets wiped when switching nr. of values
} else if (P047_MODEL_BEFLE == static_cast<P047_SensorModels>(model)) {
P047_I2C_ADDR = P047_BEFLE_DEFAULT_ADDR;
# if P047_FEATURE_ADAFRUIT
} else if (P047_MODEL_ADAFRUIT == static_cast<P047_SensorModels>(model)) {
P047_I2C_ADDR = P047_ADAFRUIT_DEFAULT_ADDR;
# endif // if P047_FEATURE_ADAFRUIT
# if P047_FEATURE_BEFLE_V3
} else if (P047_MODEL_BEFLE_V3 == static_cast<P047_SensorModels>(model)) {
P047_I2C_ADDR = P047_BEFLE_V3_DEFAULT_ADDR;
# endif // if P047_FEATURE_BEFLE_V3
}
}
P047_SENSOR_SLEEP = isFormItemChecked(F("sleep"));
P047_CHECK_VERSION = isFormItemChecked(F("version"));
# if P047_FEATURE_ADAFRUIT
if (P047_MODEL_ADAFRUIT != static_cast<P047_SensorModels>(P047_MODEL))
# endif // if P047_FEATURE_ADAFRUIT
{
webarg = webArg(F("newAddr"));
if (!webarg.isEmpty()) {
addr = static_cast<int>(strtol(webarg.c_str(), 0, 16));
if ((addr > 0x00) && (addr < 0x80)) {
P047_NEW_ADDR = addr;
P047_CHANGE_ADDR = isFormItemChecked(F("changeAddr"));
} else {
addHtmlError(F("Change I2C Addr. to (Hex) error, range: 0x01..0x7F"));
success = false;
}
}
}
break;
}
case PLUGIN_INIT:
{
success = initPluginTaskData(event->TaskIndex, new (std::nothrow) P047_data_struct(P047_I2C_ADDR, P047_MODEL));
break;
}
case PLUGIN_READ:
{
P047_data_struct *P047_data =
static_cast<P047_data_struct *>(getPluginTaskData(event->TaskIndex));
if (nullptr != P047_data) {
success = P047_data->plugin_read(event);
}
break;
}
}
return success;
}
#endif // USES_P047