-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch_KIT_PLS_ADS_4.0
148 lines (127 loc) · 4.59 KB
/
sketch_KIT_PLS_ADS_4.0
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
#include <SDI12.h> //library for OTT PLS
#define SERIAL_BAUD 115200
#define DATA_PIN 12
SDI12 mySDI12(DATA_PIN);
#include "ADS1X15.h" //https://github.com/RobTillaart/ADS1X15
ADS1115 ADS(0x48); // ADS1115 physically defined at address 0x48
//SDI 12 commands
#define myComAdress "?!" //ask the adress of the probe
#define myComId "0I!" //ask the identification
#define myComMeasure "0M!" //ask to start a measurement
#define myComSend "0D0!" //ask to send the measure
#define myComSetUnitCM "0OSU+0!" //set the unit
#define myComUnit "0OSU!" //ask for the unit of the measure, +0=m, +1=cm, +2=ft, +3=mbar, +4=psi
String readsensor(){
String sdiResponse = "";
delay(30);
while(mySDI12.available()){
char c = mySDI12.read();
if ((c != '\n') && (c != '\r')) { sdiResponse += c; delay(5); }
}
return sdiResponse;
}
void measREFsensor(){
delay(300);
mySDI12.sendCommand(myComMeasure);
while(mySDI12.available()){ mySDI12.read();}
delay(1000);
mySDI12.clearBuffer();
delay(500);
mySDI12.sendCommand(myComSend);
delay(50);
String rawdata = readsensor();
mySDI12.clearBuffer();
delay(50);
//Decoding string sent from probe
int p=0;
int pos[] = {0,0};
for (int z=0 ; z<rawdata.length() ; z++) {
char u = rawdata.charAt(z);
if (u == '+' || u == '-') {
pos[p] = z ;
p++;
}
delay (50);
}
float RealRefLevel = (rawdata.substring(pos[0],pos[1])).toFloat()-1; //Water level from the reference sensor (without correction)
String Temperature = rawdata.substring(pos[1],rawdata.length()); //Water temperature from the reference sensor
Serial.print("PLS Raw Data:");
Serial.println(rawdata);
delay(50);
}
void measADS1115(){
// Request for voltage measurement at the ADC (results between -32768 and +32767)
int16_t count_A0 = ADS.readADC(0); // Voltage measurement of pin A0, relative to ground
int16_t count_A1 = ADS.readADC(1); // Voltage measurement of pin A1, relative to ground
int16_t count_A2 = ADS.readADC(2); // Voltage measurement of pin A2, relative to ground
int16_t count_A3 = ADS.readADC(3); // Voltage measurement of pin A3, relative to ground
float f = ADS.toVoltage(1); // voltage factor
Serial.print("\tAnalog0: "); Serial.print(count_A0); Serial.print('\t'); Serial.println(count_A0 * f, 30);
Serial.print("\tAnalog1: "); Serial.print(count_A1); Serial.print('\t'); Serial.println(count_A1 * f, 30);
Serial.print("\tAnalog2: "); Serial.print(count_A2); Serial.print('\t'); Serial.println(count_A2 * f, 30);
Serial.print("\tAnalog3: "); Serial.print(count_A3); Serial.print('\t'); Serial.println(count_A3 * f, 30);
Serial.println();
delay(500);
}
void StartRefSensor(){
mySDI12.begin();
delay(2000);
Serial.print(F("'**** Identification of the sensor: "));
mySDI12.sendCommand(myComId);
delay(300);
Serial.println(readsensor());
delay(500);
mySDI12.clearBuffer();
Serial.print(F("'**** Adress of the sensor: "));
mySDI12.sendCommand(myComAdress);
delay(300);
Serial.println(readsensor());
delay(500);
mySDI12.clearBuffer();
Serial.print(F("'**** Set the unit of the sensor to be m: "));
mySDI12.sendCommand(myComSetUnitCM);
delay(300);
Serial.println(readsensor());
delay(500);
mySDI12.clearBuffer();
Serial.print(F("'**** Unit 0+(0=m, 1=cm, 2=ft, 3=mbar, 4=psi): "));
mySDI12.sendCommand(myComUnit);
delay(300);
Serial.println(readsensor());
delay(500);
mySDI12.clearBuffer();
}
void StartADSSensor() {
Serial.begin(115200);
ADS.begin();
ADS.setGain(1); // We take the lowest gain (index 1), to have the measurement range 4.096 volt
ADS.setMode(1); // The ADC is told that measurements will be taken on demand, not continuously (0 = CONTINUOUS, 1 = SINGLE)
ADS.setDataRate(7); // We specify the speed of voltage measurement we want, from 0 to 7 (7 being the fastest, that is 860 samples per second)
ADS.readADC(0); // And we do an empty reading, to send all these parameters
}
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
StartRefSensor();
StartADSSensor();
}
void loop() {
measREFsensor();
measREFsensor();
measREFsensor();
measREFsensor();
measREFsensor();
for (int i = 0; i <= 100; i++){
delay(100);
measADS1115();
delay(100);
}
measREFsensor();
measREFsensor();
measREFsensor();
measREFsensor();
measREFsensor();
while(1){}
}