EnviroDIY / Arduino-SDI-12

An Arduino library for SDI-12 communication with a wide variety of environmental sensors. This library provides a general software solution, without requiring any additional hardware.
https://github.com/EnviroDIY/Arduino-SDI-12/wiki
BSD 3-Clause "New" or "Revised" License
158 stars 100 forks source link

SDI12 - ESP32 NOT OPEN BUS #85

Open matiasAS opened 2 years ago

matiasAS commented 2 years ago

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 //#include //#include //#include

include

include

include

include

include

include

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;
}

delay(100);   
}

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

matiasAS commented 2 years ago

I can get data with arduino menga and old version sdi-12 library (incompatible with esp32), but i need use a esp32

matiasAS commented 2 years ago

Hello @ALL @majenkotech @diegogmx @aufdenkampe @Kevin-M-Smith , could you help me please, I've been waiting 20 days for an answer

matiasAS commented 2 years ago

@SRGDamia1

diegogmx commented 2 years ago

Hola Matías, perdón, no vi el mensaje, tampoco estoy particularmente involucrado en el desarrollo del proyecto, los problemas que he tenido con estos sensores generalmente se deben a temas de timing, particularmente con el pulso inicial de sdi12 y con el tiempo de espera de respuesta, si tenes un osciloscópio a mano podrías hacer unas capturas y vemos que sucede ahí

On Wed, 9 Feb 2022, 05:16 matiasAS, @.***> wrote:

Hello everyone @majenkotech https://github.com/majenkotech @diegogmx https://github.com/diegogmx @majenkotech https://github.com/majenkotech 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 //#include //#include //#include

include

include

include

include

include

include

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; }

delay(100); }

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

— Reply to this email directly, view it on GitHub https://github.com/EnviroDIY/Arduino-SDI-12/issues/85, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGCAQQ7YW3R45RUBCSK2HLU2HTDPANCNFSM5NQVAG2Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

matiasAS commented 2 years ago

Hola @diegogmx gracias por tu respuesta, y que bueno poder hablar en español, jajaja, soy de chile. He estado preguntando en grupos de electrónica en facebook y me dicen que debo pasar la señal de sensor por un conversor para bajar de 5v a 3.3v que es a lo que trabaja el ESP32.

La razón de usar un ESP32 primero por su precio y segundo porque se puede conectar a wifi y es super estable y confiable en ese sentido. Ya tengo un plan B que sería un arduino mega con el shied YUN, con eso estaría super!!.

Sobre lo del osciloscopio....tengo uno que compré en aliexpress, es uno mini, pero la verdad que en los 7 años que estuve en la U, una de las cosas que más de una vez vi y que nunca aprendí a usar es....un osciloscopio.....

Saludos

matiasAS commented 2 years ago

ahora @diegogmx mi duda es si en el readme de este repo aseguran compatibilidad con ESP32, en caso de tener que usar algun tipo de conversor lo deberia decir o estoy equivocado?

diegogmx commented 2 years ago

Yo lo use con un samd21, realmente no se lo de la compatibilidad del esp, lo de la señal de 3v3 podría llegar a ser pero me inclino a decir que no, yo lo use con 3v3 directo y sin problema, depende de los niveles efectivos, lo que si es que perdés inmunidad a ruido, lo que te sugeriría es, si vas a seguir en electrónica aprende ayer a usar un osciloscópio, te puede ahorrar horas y horas de debug, después, si mal no me acuerdo, la biblioteca tiene un modo que te permite hacer un dump en el puerto serie, así ves que es lo que está pasando por el bus, a falta de osciloscópio proba eso, otro detalle, tenes que usar un pin con funcionalidad de pwm para este coso, verifica eso también

On Fri, 25 Feb 2022, 18:55 matiasAS, @.***> wrote:

ahora @diegogmx https://github.com/diegogmx mi duda es si en el readme de este repo aseguran compatibilidad con ESP32, en caso de tener que usar algun tipo de conversor lo deberia decir o estoy equivocado?

— Reply to this email directly, view it on GitHub https://github.com/EnviroDIY/Arduino-SDI-12/issues/85#issuecomment-1051069597, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGCAQU7EKK5THIISP3O4LLU46677ANCNFSM5NQVAG2Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

matiasAS commented 2 years ago

@diegogmx sinceramente nosé de lo que me hablas....y no, no soy electrónico. Lo que tengo implementado es obtener datos con arduino, transmitir por mqtt a una raspberry y ahi con python/django hacer todo lo de transmitir los datos a un server central, almacenar datos y mostrar en gráficos la data

diegogmx commented 2 years ago

mira, no quiero sonar feo pero si queres hacer cosas como esta hay que leer, y bastante si podes mandame un esquema de lo que tenes armado, asi como ahora no tengo suficiente informacion para decir que puede estar sucediendo, puede haber un tema de hardware, no algo de la libreria

El vie, 25 feb 2022 a las 19:34, matiasAS @.***>) escribió:

@diegogmx https://github.com/diegogmx sinceramente nosé de lo que me hablas....y no, no soy electrónico. Lo que tengo implementado es obtener datos con arduino, transmitir por mqtt a una raspberry y ahi con python/django hacer todo lo de transmitir los datos a un server central, almacenar datos y mostrar en gráficos la data

— Reply to this email directly, view it on GitHub https://github.com/EnviroDIY/Arduino-SDI-12/issues/85#issuecomment-1051097414, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGCAQSCWSJQBFAIHZ3AYADU47DUTANCNFSM5NQVAG2Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>