forked from RAKWireless/WisBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoil.cpp
350 lines (294 loc) · 6.45 KB
/
soil.cpp
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* @file soil.cpp
* @author Bernd Giesecke ([email protected])
* @brief Soil sensor initialization and readings
* @version 0.1
* @date 2021-08-17
*
* @copyright Copyright (c) 2021
*
*/
#include "app.h"
/** Sensor */
RAK12035 sensor;
soil_data_s g_soil_data;
struct calib_values_s
{
uint16_t dry_cal = 75;
uint16_t wet_cal = 250;
};
calib_values_s calib_values;
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>
using namespace Adafruit_LittleFS_Namespace;
static const char soil_name[] = "SOIL";
File soil_file(InternalFS);
uint8_t read_fail_counter = 0;
bool init_soil(void)
{
// Check if sensors is available
bool found_sensor = false;
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
pinMode(WB_IO5, INPUT);
Wire.begin();
// Initialize the sensor
sensor.begin();
uint8_t data = 0;
uint16_t value = 0;
// Check the sensor version
if (!sensor.get_sensor_version(&data))
{
MYLOG("SOIL", "No sensor found");
}
else
{
MYLOG("SOIL", "Sensor FW version %d", data);
found_sensor = true;
}
// Check the sensor calibration values
if (!sensor.get_dry_cal(&value))
{
MYLOG("SOIL", "No Dry calibration");
}
else
{
MYLOG("SOIL", "Sensor Dry Cal %d", value);
found_sensor = true;
}
// Check the sensor calibration values
if (!sensor.get_wet_cal(&value))
{
MYLOG("SOIL", "No Wet calibration");
}
else
{
MYLOG("SOIL", "Sensor Wet Cal %d", value);
found_sensor = true;
}
// #define CAL_TEST
#ifdef CAL_TEST
for (int i = 0; i < 100; i++)
{
MYLOG("SOIL", "Read cycle %d", i);
// Check the sensor calibration values
uint16_t value = 0;
if (!sensor.get_dry_cal(&value))
{
MYLOG("SOIL", "No Dry calibration");
}
else
{
MYLOG("SOIL", "Sensor Dry Cal %d", value);
}
// Check the sensor calibration values
if (!sensor.get_wet_cal(&value))
{
MYLOG("SOIL", "No Wet calibration");
}
else
{
MYLOG("SOIL", "Sensor Wet Cal %d", value);
}
MYLOG("SOIL", "Powercycle Sensor");
sensor.sensor_sleep();
digitalWrite(WB_IO2, LOW);
delay(500);
digitalWrite(WB_IO2, HIGH);
delay(500);
sensor.reset();
}
#endif
sensor.sensor_sleep();
Wire.end();
return found_sensor;
}
void read_soil(void)
{
uint16_t sensTemp = 0;
uint8_t sensHumid = 0;
uint32_t avgTemp = 0;
uint32_t avgHumid = 0;
uint16_t sensCap = 0;
uint32_t avgCap = 0;
// Wake up the sensor
Wire.begin();
if (!sensor.sensor_on())
{
MYLOG("SOIL", "Can't wake up sensor");
if (g_ble_uart_is_connected)
{
g_ble_uart.println("Can't wake up sensor");
}
g_soil_data.temp_1 = 0xFF;
g_soil_data.temp_2 = 0xFF;
g_soil_data.humid_1 = 0xFF;
g_soil_data.valid = 0;
Wire.end();
read_fail_counter++;
if (read_fail_counter == 5)
{
read_fail_counter = 0;
delay(1000);
NVIC_SystemReset();
}
return;
}
// Get the sensor values
bool got_value = false;
for (int retry = 0; retry < 3; retry++)
{
if (sensor.get_sensor_moisture(&sensHumid) && sensor.get_sensor_temperature(&sensTemp))
{
got_value = true;
retry = 4;
avgTemp = sensTemp;
avgHumid = sensHumid;
sensor.get_sensor_capacitance(&sensCap);
delay(250);
for (int avg = 0; avg < 50; avg++)
{
delay(250);
if (sensor.get_sensor_temperature(&sensTemp))
{
avgTemp += sensTemp;
avgTemp /= 2;
}
if (sensor.get_sensor_moisture(&sensHumid))
{
avgHumid += sensHumid;
avgHumid /= 2;
}
if (sensor.get_sensor_capacitance(&sensCap))
{
avgCap += sensCap;
avgCap /= 2;
}
}
}
}
MYLOG("SOIL", "Sensor reading was %s", got_value ? "success" : "unsuccessful");
MYLOG("SOIL", "T %.2f H %ld C %ld", (double)(avgTemp / 10.0), avgHumid, avgCap);
if (g_ble_uart_is_connected)
{
g_ble_uart.printf("Sensor reading was %s\n", got_value ? "success" : "unsuccessful");
g_ble_uart.printf("T %.2f H %ld C %ld\n", (double)(avgTemp / 10.0), avgHumid, avgCap);
}
avgHumid = avgHumid * 2.0;
g_soil_data.temp_1 = (uint8_t)(avgTemp >> 8);
g_soil_data.temp_2 = (uint8_t)(avgTemp);
g_soil_data.humid_1 = (uint8_t)(avgHumid);
g_soil_data.cap_1 = (uint8_t)(avgCap >> 8);
g_soil_data.cap_2 = (uint8_t)(avgCap);
if (got_value)
{
g_soil_data.valid = 1;
}
else
{
g_soil_data.valid = 0;
}
sensor.sensor_sleep();
Wire.end();
}
uint16_t start_calib(bool is_dry)
{
MYLOG("SOIL", "Starting calibration for %s", is_dry ? "dry" : "wet");
Serial.flush();
uint16_t new_reading = 0;
uint16_t new_value = 0;
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
// Stop app timer while we do calibration
g_task_wakeup_timer.stop();
Wire.begin();
if (!sensor.sensor_on())
{
MYLOG("SOIL", "Can't wake up sensor");
Wire.end();
if (g_lorawan_settings.send_repeat_time != 0)
{
// Calibration finished, restart the timer that will wakeup the loop frequently
g_task_wakeup_timer.stop();
g_task_wakeup_timer.setPeriod(g_lorawan_settings.send_repeat_time);
g_task_wakeup_timer.start();
}
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_GREEN, LOW);
if (is_dry)
{
return 0xFFFF;
}
else
{
return 0xFFFF;
}
}
sensor.get_sensor_capacitance(&new_value);
for (int readings = 0; readings < 100; readings++)
{
sensor.get_sensor_capacitance(&new_reading);
new_value += new_reading;
new_value = new_value / 2;
delay(250);
digitalToggle(LED_GREEN);
digitalToggle(LED_BLUE);
}
// Send calibration value
if (is_dry)
{
MYLOG("SOIL", "Dry calibration value %d", new_value);
sensor.set_dry_cal(new_value);
calib_values.dry_cal = new_value;
}
else
{
MYLOG("SOIL", "Wet calibration value %d", new_value);
sensor.set_wet_cal(new_value);
calib_values.wet_cal = new_value;
}
if (g_lorawan_settings.send_repeat_time != 0)
{
// Calibration finished, restart the timer that will wakeup the loop frequently
g_task_wakeup_timer.stop();
g_task_wakeup_timer.setPeriod(g_lorawan_settings.send_repeat_time);
g_task_wakeup_timer.start();
}
// Return the result
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_GREEN, LOW);
sensor.sensor_sleep();
Wire.end();
return new_value;
}
uint16_t get_calib(bool is_dry)
{
uint16_t value = 0;
Wire.begin();
sensor.sensor_on();
if (is_dry)
{
if (!sensor.get_dry_cal(&value))
{
MYLOG("SOIL", "No Dry calibration");
}
else
{
MYLOG("SOIL", "Sensor Dry Cal %d", value);
}
}
else
{
if (!sensor.get_wet_cal(&value))
{
MYLOG("SOIL", "No Wet calibration");
}
else
{
MYLOG("SOIL", "Sensor Wet Cal %d", value);
}
}
sensor.sensor_sleep();
Wire.end();
return value;
}