SOLVED: Two SDI-12 sensors to Arduino Uno
hi
sorry, turned out had made stupid mistake in variable naming. approach works, don't know if elegant sollution :-)
_________________________________________________________
hi everyone
i putting small data logger based on arduino uno r3. want connect 2 sdi-12 sensors logger , have therefore downloaded helpful "arduino-sdi-12" library. can data 1 sensor, not 2 (not if connected same input pin nor 2 different pins). have given sensors 2 different addresses. below code: if comment out related either incoming or outgoing sensor (below have commented part deals incoming sensor), reads data fine 1 sensor. if both uncommented zeros 1 sensor , constant high values other.
my approach in code below has been copy section works 1 sensor , repeat other. perhaps wrong? suggestions welcome solve this.
all best
andreas
sorry, turned out had made stupid mistake in variable naming. approach works, don't know if elegant sollution :-)
_________________________________________________________
hi everyone
i putting small data logger based on arduino uno r3. want connect 2 sdi-12 sensors logger , have therefore downloaded helpful "arduino-sdi-12" library. can data 1 sensor, not 2 (not if connected same input pin nor 2 different pins). have given sensors 2 different addresses. below code: if comment out related either incoming or outgoing sensor (below have commented part deals incoming sensor), reads data fine 1 sensor. if both uncommented zeros 1 sensor , constant high values other.
my approach in code below has been copy section works 1 sensor , repeat other. perhaps wrong? suggestions welcome solve this.
all best
andreas
code: [select]
/*
written kevin m. smith in 2013.
contact: sdi12@ethosengineering.org
extended ruben kertesz in 2016
*/
#include <sdi12.h>
#define datapin 2 // change proper pin
sdi12 mysdi12(datapin);
string srsout = "";
string srsin = "";
string mycommand = "";
string redoutstring = "";
string niroutstring = "";
string redinstring = "";
string nirinstring = "";
float ndvi = 0;
float redin = 0;
float nirin = 0;
float redout = 0;
float nirout = 0;
void setup() {
serial.begin(9600);
mysdi12.begin();
}
void loop() {
//-----------------------------------------------------------------------------------
//grab data outgoing sensor
//first command take measurement
mysdi12.sendcommand("1m!"); // change "1" correct address of sensor
delay(30); // wait while response
while (mysdi12.available()) { // build response string
char c = mysdi12.read();
if ((c != '\n') && (c != '\r')) {
srsout += c;
delay(5);
}
}
if (srsin.length() > 1)
mysdi12.flush();
delay(5000); // delay between taking reading , requesting data
srsin = ""; // clear response string
// next command request data last measurement
mysdi12.sendcommand("1d0!"); // change "1" appropriate address
delay(30); // wait while response
while (mysdi12.available()) { // build string response
char c = mysdi12.read();
if ((c != '\n') && (c != '\r')) {
srsout += c;
delay(5); //former val = 5
}
}
//-----------------------------------------------------------------------------------
//grab data incoming sensor
//first command take measurement
// mycommand = "0m!"; // change "0" correct address of sensor
// mysdi12.sendcommand(mycommand);
// delay(30); // wait while response
//
// while (mysdi12.available()) { // build response string
// char c = mysdi12.read();
// if ((c != '\n') && (c != '\r')) {
// srsin += c;
// delay(5);
// }
// }
// if (srsout.length() > 1)
// mysdi12.flush();
//
// delay(5000); // delay between taking reading , requesting data
// srsout = ""; // clear response string
//
//// next command request data last measurement
// mycommand = "0d0!"; // change first 0 appropriate address
//// serial.println(mycommand); // echo command terminal
//
// mysdi12.sendcommand(mycommand);
// delay(30); // wait while response
//
// while (mysdi12.available()) { // build string response
// char c = mysdi12.read();
// if ((c != '\n') && (c != '\r')) {
// srsin += c;
// delay(5); //former val = 5
// }
// }
//
//-----------------------------------------------------------------------------------
//write measurement screen
redoutstring = srsout.substring(4,9); //pick part of string
niroutstring = srsout.substring(11,16);
redout = redoutstring.tofloat(); //string float
nirout = niroutstring.tofloat();
redinstring = srsin.substring(4,9); //pick part of string
nirinstring = srsin.substring(11,16);
redin = redinstring.tofloat(); //string float
nirin = nirinstring.tofloat();
ndvi = (((nirout/nirin) - (redout/redin)) / ((nirout/nirin) + (redout/redin)));
serial.print("redout: ");
serial.print(redout, 4);
serial.print(" nirout: ");
serial.print(nirout, 4);
serial.print(" redin: ");
serial.print(redin, 4);
serial.print(" nirin: ");
serial.print(nirin, 4);
serial.println();
serial.print("ndvi: ");
serial.print(ndvi);
serial.println();
mysdi12.flush();
}
Arduino Forum > Using Arduino > Sensors > SOLVED: Two SDI-12 sensors to Arduino Uno
arduino
Comments
Post a Comment