How to change SDISerial code to SDI12 code?

Hi All,

How to change the code for a SDISerial library to a SDI-12 library?

Following code below are in SDISerial

/* code reads Decagon GS3 sensor output to the serial terminal.
WHITE wire: power supply/excitation, connected to 5v pin
RED wire: data line, connected to Arduino pin 2 (or another interrupt-capable pin)
bare wire: connected to GND
*/
 
#include <SDISerial.h>
 
 
#define INPUT_SIZE 30
#define NUMSAMPLES 5
#define INVERTED 1
#define DATA_PIN 4
 
int sensorDelay = 1000;
// bulk density used to calculate porewater conductivity (g/cm3)
float bulkDens = 0.4;  
// mineral density assumed to be 2.65 Mg/m3 (used 
// in porewater calculation from eq. 3 from GS-3 manual)
float theta = 1 - (bulkDens / 2.65); 
char* samples; 
 
 
SDISerial sdi_serial_connection(DATA_PIN, INVERTED);


void setup() {
  sdi_serial_connection.begin();
  Serial.begin(921600); 
  Serial.println("Start Initialized\n");
  delay(1000);
}
 
 
void loop() {
  
  uint8_t i;
  // arrays to hold reps
  float dielectric[NUMSAMPLES];
  float soilMoist[NUMSAMPLES];
  float degC[NUMSAMPLES];
  float bulkEC[NUMSAMPLES];
  float porewaterEC[NUMSAMPLES];
  float solutionEC[NUMSAMPLES];
   
  // mean values
  float dielMean        = 0.0;
  float soilMoistMean   = 0.0;
  float degCMean        = 0.0;
  float bulkECMean      = 0.0;
  float porewaterECMean = 0.0;
  float solutionECMean  = 0.0;
 
  // take repeated samples
  for (i = 0; i < NUMSAMPLES; i++) {
    //char* response = get_measurement(); // get measurement data
    samples = get_measurement();
    while (strlen(samples) < 5) {
      samples = get_measurement();  
    }
  
    // first term is the sensor address (irrelevant to me)
    char* term1 = strtok(samples, "+");
     
    // second term is the dielectric conductivity & soil moisture
    term1      = strtok(NULL, "+");
    dielectric[i] = atof(term1);
               
    term1 = strtok(NULL, "+");
    degC[i] = atof(term1);
    term1 = strtok(NULL, "+");
    bulkEC[i] = atof(term1);
    // see eqs. 1 + 2 from GS3 manual (dS/m)
    porewaterEC[i] = ((80.3 - 0.37 * (degC[i] - 20)) * bulkEC[i] / 1000) / (dielectric[i] - 6);
   
   if (bulkEC[i] < 5) {
     soilMoist[i]  = (5.89 * pow(10.0, -6.0) * pow(dielectric[i], 3.0)) - (7.62 * pow(10.0, -4.0) * 
     pow(dielectric[i], 2.0)) + (3.67 * pow(10.0, -2.0) * dielectric[i]) - (7.53 * pow(10.0, -2.0));
   } else {
   soilMoist[i]  =  0.118 * sqrt(dielectric[i]) -0.117;
   }  
    // calculate EC of solution removed from a saturated soil paste, 
    // according to Decagon GS3 manual
    // see page 8 of GS3 manual (eq. 4)
    solutionEC[i] =  (bulkEC[i] / 1000 * soilMoist[i]) / theta; 
 
    // sum with each iteration
    dielMean        += dielectric[i];
    soilMoistMean   += soilMoist[i];
    degCMean        += degC[i];
    bulkECMean      += bulkEC[i];
    porewaterECMean += porewaterEC[i];
    solutionECMean  += solutionEC[i];
  }
   
  // Average readings for each parameter
  dielMean        /= NUMSAMPLES;
  soilMoistMean   /= NUMSAMPLES;
  degCMean        /= NUMSAMPLES;
  bulkECMean      /= (NUMSAMPLES / 0.001);
  porewaterECMean /= NUMSAMPLES;
  solutionECMean  /= NUMSAMPLES;
 
//  Serial.print("dielectric: ");
//  Serial.println(dielMean, 3);
  Serial.print("VWC (m3/m3): ");
  Serial.println(soilMoistMean, 3);
  Serial.print("Degree (C): ");
  Serial.println(degCMean, 1);
  Serial.print("bulk EC (dS/m): ");
  Serial.println(bulkECMean, 3);
//  Serial.print("Solution EC (dS/m): ");
//  Serial.println(solutionECMean, 4);
//  Serial.print("porewater EC (dS/m): "); 
//  Serial.println(porewaterECMean, 4);
  delay(sensorDelay);
  Serial.println("\n");
}

char* get_measurement(){
        // function by Joran Beasley: https://github.com/joranbeasley/SDISerial/blob/master/examples/SDISerialExample/SDISerialExample.ino
    char* service_request = sdi_serial_connection.sdi_query("?M!", sensorDelay);
    //you can use the time returned above to wait for the service_request_complete
    char* service_request_complete = sdi_serial_connection.wait_for_response(sensorDelay);
    // 1 second potential wait, but response is returned as soon as it's available
    return sdi_serial_connection.sdi_query("?D0!", sensorDelay);
}
 

Thanks in advance.
Regards,
Storm

The only SDISerial library I found claims to be a SDI-12 library.

So I make no sense of your question.

Please describe the problem you are having.

a7

Hi Alto777,

If I change the SDISerial.h in the code to SDI12.h, then i will get the error as shown below.


GS3_Interfacing:24:1: error: 'SDISerial' does not name a type; did you mean 'Serial'?
 SDISerial sdi_serial_connection(DATA_PIN, INVERTED);
 ^~~~~~~~~
 Serial
C:\Users\matebook\Documents\Sem 7\IEP1\GS3_Interfacing\GS3_Interfacing.ino: In function 'void setup()':
GS3_Interfacing:28:3: error: 'sdi_serial_connection' was not declared in this scope
   sdi_serial_connection.begin();
   ^~~~~~~~~~~~~~~~~~~~~
C:\Users\matebook\Documents\Sem 7\IEP1\GS3_Interfacing\GS3_Interfacing.ino:28:3: note: suggested alternative: 'uhd_ack_connection'
   sdi_serial_connection.begin();
   ^~~~~~~~~~~~~~~~~~~~~
   uhd_ack_connection
C:\Users\matebook\Documents\Sem 7\IEP1\GS3_Interfacing\GS3_Interfacing.ino: In function 'char* get_measurement()':
GS3_Interfacing:122:29: error: 'sdi_serial_connection' was not declared in this scope
     char* service_request = sdi_serial_connection.sdi_query("?M!", sensorDelay);
                             ^~~~~~~~~~~~~~~~~~~~~
C:\Users\matebook\Documents\Sem 7\IEP1\GS3_Interfacing\GS3_Interfacing.ino:122:29: note: suggested alternative: 'uhd_ack_connection'
     char* service_request = sdi_serial_connection.sdi_query("?M!", sensorDelay);
                             ^~~~~~~~~~~~~~~~~~~~~
                             uhd_ack_connection
exit status 1
'SDISerial' does not name a type; did you mean 'Serial'?

Thus, which part of the code that i need to edit if i change to SDI12?
Thanks in advance.
Regards,
Storm

Why are you switching libraries? Let us assume you have some good reason.

Then you will have to study the examp,es from both libraries to see in what subtle and annoying or not-so-subtle and significant ways they differ.

   SDISerial sdi_serial_connection(DATA_PIN, INVERTED);

is how the SDISerial library names its objects,

   SDI12 sdi_serial_connection(…

is the name of the SDI12 object, for example.

The functionality is the same, the names have been messed with and maybe even the organization needed provision of the features.

So why you wanna switch?

a7

The reason why i want to change the code from SDISerial to SDI12 is because SDISerial cannot be upload to ESP32 and SDI12 is possible to be uploaded based on GitHub https://github.com/EnviroDIY/Arduino-SDI-12. As I need to run the code on ESP32.

Even after I change the "SDISerial sdi_serial_connection" to "SDI12 sdi_serial_connection", there are still error on the char* get_measurement ()

:\Users\matebook\Documents\Sem 7\IEP1\GS3_Interfacing\GS3_Interfacing.ino: In function 'char* get_measurement()':
GS3_Interfacing:122:37: error: 'class SDI12' has no member named 'sdi_query'
     char* service_request = mySDI12.sdi_query("?M!", sensorDelay);
                                     ^~~~~~~~~
GS3_Interfacing:124:46: error: 'class SDI12' has no member named 'wait_for_response'
     char* service_request_complete = mySDI12.wait_for_response(sensorDelay);
                                              ^~~~~~~~~~~~~~~~~
GS3_Interfacing:126:20: error: 'class SDI12' has no member named 'sdi_query'
     return mySDI12.sdi_query("?D0!", sensorDelay);
                    ^~~~~~~~~
exit status 1
'class SDI12' has no member named 'sdi_query'

Thanks in advance.
Regards,
Storm

Like I said, it is probable that many differences might need to be found and addressed.

Either simple name changes or not-so-simple changes to functions and calling sequences, maybe even the exact way each goes about doing what is conceptually the same thing.

You'll have to study examples of the usage of both libraries, become fluent in each, in order to translate or supplant the use of one library for another.

Fortunately all the source code is available for you perusal.

a7

Please do not cross post: Any suitable Arduino AVR Board to replace ESP32? - #18 by stormtan99

@stormtan99, do not cross-post. Thread locked.