MQTT problem with TDS sensor

This is the code that I have been using for the tds sensor and it has worked well for me because it is the one provided by the manufacturer. Add MQTT to connect my device to ubidots and I did all the steps. From add token, device and variable. However, I have a problem that when integrating MQTT the values I get in the serial monitor are 0 ppm and the values I get in the ubidots dashboard are also 0. I’m new to MQTT and I’m a bit lost. :frowning:

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

#include <UbidotsEsp32Mqtt.h>

#include <PubSubClient.h>

#define TdsSensorPin 27

#define VREF 3.3 // voltaje de referencia (Volt) del ADC

#define SCOUNT 30 // suma de puntos de muestra

const char *UBIDOTS_TOKEN = “BBFF-2DmXEg1QY8xrd6tA4vSTnDD17H6KiQ”;

const char *WIFI_SSID = “Posis”;

const char *WIFI_PASS = “Posis7979”;

const char *DEVICE_LABEL = “tdsj”;

const char *VARIABLE_LABEL = “tds”;

const int PUBLISH_FREQUENCY = 5000;

unsigned long timer;

//uint8_t analogPin = 27;

Ubidots ubidots(UBIDOTS_TOKEN);

void callback(char *topic, byte *payload, unsigned int length)

{

Serial.print(“Message arrived [”);

Serial.print(topic);

Serial.print("] ");

for (int i = 0; i < length; i++)

{

Serial.print((char)payload[i]);

}

Serial.println();

}

int analogBuffer[SCOUNT]; // almacena el valor analógico en el array, leído del ADC

int analogBufferTemp[SCOUNT];

int analogBufferIndex = 0;

int copyIndex = 0;

float averageVoltage = 0;

float tdsValue = 0;

float temperature = 25; // temperatura actual para la compensación

LiquidCrystal_I2C lcd(0x27, 16, 2); // Dirección I2C de la pantalla LCD: 0x27, 16 columnas, 2 filas

// algoritmo de filtrado de mediana

int getMedianNum(int bArray[], int iFilterLen){

int bTab[iFilterLen];

for (byte i = 0; i<iFilterLen; i++)

bTab[i] = bArray[i];

int i, j, bTemp;

for (j = 0; j < iFilterLen - 1; j++) {

for (i = 0; i < iFilterLen - j - 1; i++) {

  if (bTab[i] > bTab[i + 1]) {

    bTemp = bTab[i];

    bTab[i] = bTab[i + 1];

    bTab[i + 1] = bTemp;

  }

}

}

if ((iFilterLen & 1) > 0){

bTemp = bTab[(iFilterLen - 1) / 2];

}

else {

bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;

}

return bTemp;

}

void setup(){

lcd.init();

lcd.backlight(); // Inicializar la pantalla LCD

// put your setup code here, to run once:

Serial.begin(115200);

// ubidots.setDebug(true); // uncomment this to make debug messages available

Serial.println(“Medicion iniciada”);

ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);

ubidots.setCallback(callback);

ubidots.setup();

ubidots.reconnect();

timer = millis();

}

void loop(){

static unsigned long analogSampleTimepoint = millis();

if(millis()-analogSampleTimepoint > 40U){ // cada 40 milisegundos, leer el valor analógico del ADC

analogSampleTimepoint = millis();

analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin);    // leer el valor analógico y almacenarlo en el buffer

analogBufferIndex++;

if(analogBufferIndex == SCOUNT){

  analogBufferIndex = 0;

}

}

static unsigned long printTimepoint = millis();

if(millis()-printTimepoint > 800U){

printTimepoint = millis();

for(copyIndex=0; copyIndex<SCOUNT; copyIndex++){

  analogBufferTemp[copyIndex] = analogBuffer[copyIndex];

 

  // leer el valor analógico de manera más estable utilizando el algoritmo de filtrado de mediana y convertirlo a voltaje

  averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 4096.0;

 

  // calcular el valor de TDS

  tdsValue = (133.42 * averageVoltage * averageVoltage * averageVoltage - 255.86 * averageVoltage * averageVoltage + 857.39 * averageVoltage) * 0.5;

 

  //lcd.setCursor(7, 1);

  //lcd.print(tdsValue, 0);

  // Serial.print("voltage:");

  // Serial.print(averageVoltage, 2);

  // Serial.print("V   ");

  Serial.print("TDS Value:");

  Serial.print(tdsValue, 0);

  Serial.println("ppm");

  //lcd.setCursor(0, 0);

  //lcd.print("TDS: ");

  //lcd.print(tdsValue,0);

  //lcd.print(" PPM");

}

}

  // put your main code here, to run repeatedly:

if (!ubidots.connected())

{

ubidots.reconnect();

}

if (abs(millis() - timer) > PUBLISH_FREQUENCY) // triggers the routine every 5 seconds

{

float tdsjj = tdsValue;

ubidots.add(VARIABLE_LABEL, tdsjj); // Insert your variable Labels and the value to be sent

ubidots.publish(DEVICE_LABEL);

Serial.println("Enviando datos a Ubidots");

Serial.println("TDS: " + String(tdsjj));

timer = millis();

}

ubidots.loop();

lcd.setCursor(0, 0);

lcd.print("TDS: ");

lcd.print(tdsValue,0);

lcd.print(" PPM");

delay(1000);

lcd.clear();

}

Hello @fernando.josue1301,

Thank you for reaching out. In order for us and our community to help you, please follow the steps below:

  1. Verify MQTT Configuration: Double-check that you have correctly configured the MQTT settings in your code, including the MQTT broker address, port, topic, and credentials.

  2. Debugging MQTT: Uncomment the line //ubidots.setDebug(true); in your code to enable debug messages. This will provide additional information in the serial monitor, which can help identify any potential issues related to MQTT communication.

  3. Check Wi-Fi Connection: Verify that your device is properly connected to the Wi-Fi network. Ensure that the Wi-Fi credentials are accurate, and there are no connectivity issues affecting the MQTT connection.

  4. Review Ubidots Configuration: Double-check the MQTT configuration details provided in Ubidots MQTT Reference.

Following these steps and monitor the serial monitor for any debug information or error messages may help identify the reason for the 0 ppm values from the TDS sensor.

If the issue persists or you need further assistance, please provide any relevant error messages or additional details about your setup. This will help us and the community better understand the problem and provide more specific guidance.