Hello everyone @majenkotech @diegogmx @majenkotech,@ALL I am trying to read data from a Campbell Scientific sensor by sdi12 with an ESP32, but the sensor does not recognize me or it simply does not detect that the sdi12 object is available.
I share my code in case someone could help me
I'm from Chile, that's why there are some lines in Spanish. If there was someone who spoke my language much better!!!
`//#include <SPI.h>
//#include <UIPEthernet.h>
//#include <SPI.h>
//#include <Ethernet.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include "SDI12.h"
SDI12 mySDI12(2);
String datos = "";
bool isAvailable(int pin){
// SDI12 mySDI12(pin);
// Serial.println("DATAPIN: "+this->DATAPIN);
return mySDI12.available();
}
void printBufferToScreen(int pin){
Serial.println(" en print buffer screen ");
String buffer = "";
buffer +="{"H":"";
byte cont = 0;
byte largo = 0;
byte cont_largo = 0;
mySDI12.read(); // consume address
while(mySDI12.available()){
Serial.println("dentro del while print buffer screeen");
char c = mySDI12.read();
if(c == '+'){
if(cont == 0){
}
if(cont == 1){
buffer += "","CE":"";
}
if(cont == 2){
buffer += "","T":"";
}if(cont == 3){
buffer += "","PDIEF":"";
}
if(cont == 4){
buffer +="}";
}
cont++;
if(c == '-') {
buffer += '-';
}
if(c=='\0'){
}
}
else {
buffer += c;
}
Serial.println("despues del while en print buffer screen");
datos = buffer;
}
void takeMeasurement(char i,int pin){
Serial.println(" take measurement");
String command = "";
command += i;
command += "M3!"; // SDI-12 measurement command format [address]['M'][!]
mySDI12.sendCommand(command);
while(!mySDI12.available()>5); // wait for acknowlegement with format [address][ttt (3 char, seconds)][number of measurments available, 0-9]
delay(100);
mySDI12.read(); //consume address
// find out how long we have to wait (in seconds).
int wait = 0;
wait += 100 * mySDI12.read()-'0';
wait += 10 * mySDI12.read()-'0';
wait += 1 * mySDI12.read()-'0';
mySDI12.read(); // ignore # measurements, for this simple examlpe
mySDI12.read(); // ignore carriage return
mySDI12.read(); // ignore line feed
long timerStart = millis();
while((millis() - timerStart) > (1000 * wait)){
if(mySDI12.available()) break; //sensor can interrupt us to let us know it is done early
}
// in this example we will only take the 'DO' measurement
mySDI12.flush();
command = "";
command += i;
command += "D0!"; // SDI-12 command to get data [address][D][dataOption][!]
mySDI12.sendCommand(command);
while(!mySDI12.available()>1); // wait for acknowlegement
delay(300); // let the data transfer
//Serial.println("antes de printBufferToScreen()");
printBufferToScreen(pin);
mySDI12.flush();
}
String obtenerDatos(int pin){
float temp_c;
float humidity;
temp_c = 0;//sht1x.readTemperatureC();
humidity = 0;//sht1x.readHumidity();
takeMeasurement('0',int(pin));
// datos +="","PH":"","+""TA":"+"""+temp_c+"","+""HA":"+"}";
datos +="","PH":"",";
datos += ""TA":";
datos +=""";
datos += temp_c;
datos +="",";
datos += ""HA":";
datos +=""";
datos += humidity;
datos +=""";
datos += "}";
int largo_datos = datos.length()+1;
char datos_array[largo_datos];
//char nuevo_datos_array[largo_datos];
String datos_limpios = String("");
datos.toCharArray(datos_array,largo_datos);
for(int i=0;i<largo_datos;i++){
if(datos_array[i] != '\0' && datos_array[i] != '\n' && datos_array[i] != '\r'){
datos_limpios+=datos_array[i];
}
}
return String(datos_limpios);
}
String jsonSensor(int id){
Serial.print("id: ");
Serial.println(id);
String data = obtenerDatos(id);
String json = "{";
json += ""pin":";
json += """;
json += id;
json += "",";
json += ""available":";
json += """;
json += isAvailable(id);
json += "",";
json += ""data":";
//json += """;
json += data;
//json += """;
json += "}";
return String(json);
}
void setup() {
Serial.begin(115200);
delay(500);
mySDI12.begin();
}
void loop() {
Serial.println(jsonSensor(2));
delay(1000);
}
`
I hope you can help me
Thank you
Greetings
Matias