-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch_KIT_PLS_ADS_2.0
152 lines (129 loc) · 3.74 KB
/
sketch_KIT_PLS_ADS_2.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
149
150
151
152
#include <SDI12.h>
#include <Wire.h>
#include <DFRobot_ADS1115.h>
DFRobot_ADS1115 ads(&Wire);
#define SERIAL_BAUD 115200
#define DATA_PIN 12
SDI12 mySDI12(DATA_PIN);
//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);
Serial.println("Temperature:" +Temperature);
delay(50);
}
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 setup(void)
{
Serial.begin(115200);
ads.setAddr_ADS1115(ADS1115_IIC_ADDRESS1); // 0x48
ads.setGain(eGAIN_TWOTHIRDS); // 2/3x gain
ads.setMode(eMODE_SINGLE); // single-shot mode
ads.setRate(eRATE_128); // 128SPS (default)
ads.setOSMode(eOSMODE_SINGLE); // Set to start a single-conversion
ads.init();
}
void loop(void)
{
if (ads.checkADS1115())
{
int16_t adc0, adc1, adc2, adc3;
adc0 = ads.readVoltage(0);
Serial.print("A0:");
Serial.print(adc0);
Serial.print("mV, ");
adc1 = ads.readVoltage(1);
Serial.print("A1:");
Serial.print(adc1);
Serial.print("mV, ");
adc2 = ads.readVoltage(2);
Serial.print("A2:");
Serial.print(adc2);
Serial.print("mV, ");
adc3 = ads.readVoltage(3);
Serial.print("A3:");
Serial.print(adc3);
Serial.println("mV");
}
else
{
Serial.println("ADS1115 Disconnected!");
}
delay(1000);
}
void loop() {
for (int i = 0; i <= 20; i++){
measREFsensor();
delay(100);
measKitSensor();
delay(100);
}
while(1){}
}