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.
#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();
}