SOLVED: Two SDI-12 sensors to Arduino Uno

Hi
Sorry, it turned out I had made a stupid mistake in the variable naming. The approach works, however I don’t know if it is the most elegant sollution :slight_smile:


Hi everyone

I am putting together a small data logger based on an Arduino Uno R3. I want to connect two SDI-12 sensors to the logger and have therefore downloaded the helpful “Arduino-SDI-12” library. With this I can get data from one sensor, but not two (not if they are connected to the same input pin nor to two different pins). I have given the sensors two different addresses. Below is my code: if I comment out everything that is related to either the incoming or the outgoing sensor (below I have commented the part that deals with the incoming sensor), it reads the data just fine from one sensor. However if both are uncommented I get zeros from one sensor and constant high values from the other.
My approach in the code below has been to copy the section that works for one sensor and then repeat it for the other. Perhaps this is completely wrong? Any suggestions are most welcome to solve this.
All the best
Andreas

/*
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();
}