-
Notifications
You must be signed in to change notification settings - Fork 1
/
soil_sensor.ino
108 lines (89 loc) · 3.96 KB
/
soil_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
#include <SoftwareSerial.h>
#include <Wire.h>
#define RE 8
#define DE 7
const byte hum_temp_ec[8] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x03, 0x05, 0xCB};
byte sensorResponse[12] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte sensor_values[11];
SoftwareSerial mod(2, 3);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
mod.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
digitalWrite(RE, LOW);
digitalWrite(DE, LOW);
delay(100);
}
void loop() {
/************** Soil EC Reading *******************/
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
memset(sensor_values, 0, sizeof(sensor_values));
delay(100);
if (mod.write(hum_temp_ec, sizeof(hum_temp_ec)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 12; i++) {
//Serial.print(mod.read(),HEX);
sensorResponse[i] = mod.read();
yield();
Serial.print(sensorResponse[i], HEX);
Serial.print(",");
}
Serial.println();
}
delay(250);
// get sensor response data
float soil_hum = 0.1 * int(sensorResponse[3] << 8 | sensorResponse[4]);
float soil_temp = 0.1 * int(sensorResponse[5] << 8 | sensorResponse[6]);
int soil_ec = int(sensorResponse[7] << 8 | sensorResponse[8]);
/************* Calculations and sensor corrections *************/
/**
* Soil VWC correction. Test and use if necessary.
*/
// change: quadratic aproximation of VWC from CWT sensor to Teros 12 sensor.
// Just in case or for tests. The VWC of Teros and chinese sensor are very close (see reference spreadsheet).
//soil_hum = -0.0134 * soil_hum * soil_hum + 1.6659 * soil_hum - 6.1095;
/**
* Bulk EC correction. Choose one and uncomment.
*/
// CHOOSE ONE: cubic aproximation of BULK EC from CWT sensor to Teros 12 sensor (more precise)
//soil_ec = 0.0000014403 * soil_ec * soil_ec * soil_ec - 0.0036 * soil_ec * soil_ec + 3.7525 * soil_ec - 814.1833;
// CHOOSE ONE: This equation was obtained from calibration using distilled water and a 1.1178mS/cm solution.
// Change by @danielfppps >> https://github.com/kromadg/soil-sensor/issues/3#issuecomment-1383959976
soil_ec = 1.93 * soil_ec - 270.8;
/**
* Bulk EC temperature correction. Test and use if necessary.
*/
// Soil EC temp correction based on the Teros 12 manual. https://github.com/kromadg/soil-sensor/issues/1
soil_ec = soil_ec / (1.0 + 0.019 * (soil_temp - 25));
// soil_temp was left the same because the Teros and chinese sensor values are similar
// quadratic aproximation
// the teros bulk_permittivity was calculated from the teros temperature, teros bulk ec and teros pwec by Hilhorst 2000 model
float soil_apparent_dieletric_constant = 1.3088 + 0.1439 * soil_hum + 0.0076 * soil_hum * soil_hum;
float soil_bulk_permittivity = soil_apparent_dieletric_constant; /// Hamed 2015 (apparent_dieletric_constant is the real part of permittivity)
float soil_pore_permittivity = 80.3 - 0.37 * (soil_temp - 20); /// same as water 80.3 and corrected for temperature
// converting bulk EC to pore water EC
float soil_pw_ec;
if (soil_bulk_permittivity > 4.1)
soil_pw_ec = ((soil_pore_permittivity * soil_ec) / (soil_bulk_permittivity - 4.1) / 1000); /// from Hilhorst 2000.
else
soil_pw_ec = 0;
Serial.print("Humidity: ");
Serial.print(soil_hum);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(soil_temp);
Serial.println(" °C");
Serial.print("EC: ");
Serial.print(soil_ec);
Serial.println(" us/cm");
Serial.print("pwEC: ");
Serial.print(soil_pw_ec);
Serial.println(" dS/m");
Serial.print("soil_bulk_permittivity: ");
Serial.println(soil_bulk_permittivity);
delay(2000);
}