I found code bellow. In this code the user conect two sensor I want to connect just one of them in port TX1. My sensor has adress as 2. Could you give me an example of code of this.
Thanks in advance
/*
Written by Kevin M. Smith in 2013.
Contact: SDI12@ethosengineering.org
Extended by Ruben Kertesz in 2016
*/
#include <SDI12.h>
#define DATAPIN 2 // change to the 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 from outgoing sensor
//first command to take a measurement
mySDI12.sendCommand("1M!"); // change "1" to the correct address of the sensor
delay(30); // wait a while for a 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 and requesting data
srsIn = ""; // clear the response string
// next command to request data from last measurement
mySDI12.sendCommand("1D0!"); // CHANGE "1" to appropriate address
delay(30); // wait a while for a response
while (mySDI12.available()) { // build string from response
char c = mySDI12.read();
if ((c != '\n') && (c != '\r')) {
srsOut += c;
delay(5); //former val = 5
}
}
//-----------------------------------------------------------------------------------
//Grab data from Incoming sensor
//first command to take a measurement
// myCommand = "0M!"; // change "0" to the correct address of the sensor
// mySDI12.sendCommand(myCommand);
// delay(30); // wait a while for a 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 and requesting data
// srsOut = ""; // clear the response string
//
//// next command to request data from last measurement
// myCommand = "0D0!"; // CHANGE first zero to appropriate address
//// Serial.println(myCommand); // echo command to terminal
//
// mySDI12.sendCommand(myCommand);
// delay(30); // wait a while for a response
//
// while (mySDI12.available()) { // build string from response
// char c = mySDI12.read();
// if ((c != '\n') && (c != '\r')) {
// srsIn += c;
// delay(5); //former val = 5
// }
// }
//
//-----------------------------------------------------------------------------------
//write the measurement to the screen
redOutString = srsOut.substring(4,9); //pick part of string
nirOutString = srsOut.substring(11,16);
redOut = redOutString.toFloat(); //string to float
nirOut = nirOutString.toFloat();
redInString = srsIn.substring(4,9); //pick part of string
nirInString = srsIn.substring(11,16);
redIn = redInString.toFloat(); //string to 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();
}