Connection To MQTT

I have a problem when trying to send a variable via MQTT protocol to the thinger platform. The device manages to connect successfully, I configure the data buckets with the topic “v2/your_user/devices/your_device/data/cycle_time” which is the one that is published, but on the platform, the value of this variable is not reflected. This is the code I use:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Define the sensor pins
#define sensorPin1 D5 // Pin of the first sensor
#define sensorPin2 D7 // Second sensor pin
#define Connectivity Indicator D4 // LED_BUILTIN MICRO

// Define global variables to store the time passed by each sensor
unsigned long SensorTime1 = 0;
unsigned long SensorTime2 = 0;

// Define Global Variables to store the cycle time calculation
unsigned long cycleTime;
floatcalculatedTime;

const char* ssid = “ssid”;
const char* password = “password”;
const char* thingerServer = “backend.thinger.io”;
const int thingerPort = 1883;
const char* userName = “franklintorres06”;
const char* deviceName = “MICRO_TEST2”;
const char* deviceCredential = “MICRO_TEST”;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
// Initialize serial port
Serial.begin(9600);
WiFi.begin(ssid, password);

// LED_BUILTIN MICRO TO INDICATE CONNECTIVITY
pinMode(ConnectivityIndicator, OUTPUT);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(“Connecting to WiFi…”);
}
Serial.println(“Connected to WiFi network”);

client.setServer(thingerServer, thingerPort);

while (!client.connected()) {
Serial.println(“Connecting to MQTT server…”);
if (client.connect(deviceName, userName, deviceCredential)) {
Serial.println(“Connected to MQTT server”);
} else {
Serial.printf(“MQTT connection failed, rc=%d. Retrying in 5 seconds…\n”, client.state());
delay(5000);
}
}

// add variable

}

void loop() {

// Define local variables to control wait and read times during program operation
static unsigned long waitTime = 15000; //s (15 seconds)
static unsigned long StartWaitTime = 0;
static boolean sensor1On = false;

//Print message every 2 seconds if we are waiting for the activation of sensor 1
static unsigned longLastMessageTime = 0;
if (!sensor1Activated && millis() - lastMessageTime > 2000) {
Serial.println(“”);
Serial.println(“Waiting for activation of sensor 1…”);
LastMessageTime = millis();
}

// If sensor 1 is activated, print message and start timeout
if (digitalRead(sensorPin1) == HIGH && Sensor1Time == 0 && !sensor1Activated) {
timeSensor1 = millis(); // Store the current time
Serial.println(“”);
Serial.println(“Sensor 1 has been activated!!! and the timeout for sensor 2 has started…”);

 // Configure read pins as digital outputs to command a low step and configure it again as input, due to hardware problems
 pinMode(sensorPin2, OUTPUT);
 digitalWrite(sensorPin2, LOW);
 pinMode(sensorPin2, INPUT);

 StartWaitTime = millis(); // Start timeout for sensor 2
 sensor1On = true; // Mark that sensor 1 has been activated

}

// Don’t show any more messages while the timeout is running out
if (sensor1Activated && millis() - StartWaitTime <= WaitTime) {

 // Check if sensor 2 is activated during the waiting time
 if (digitalRead(sensorPin2) == HIGH && sensorTime2 == 0) {
   timeSensor2 = millis(); // Store the current time
   delay(500);

   CycleTime = SensorTime2 - SensorTime1;
   calculatedTime = cycleTime / 1000.0; // Convert from milliseconds to seconds
 

   Serial.println("");
   Serial.print("Calculated cycle time: ");
   Serial.print(calculatedtime, 2); // Show up to 2 decimal places
   Serial.println(" Seconds ");

   // Reboot to wait for sensor 1 again
   timeSensor1 = 0;
   timeSensor2 = 0;
   sensor1On = false; // Reset sensor 1 detection

  // Configure read pins as digital outputs to command a low step and configure it again as input, due to hardware problems
   pinMode(sensorPin1, OUTPUT);
   digitalWrite(sensorPin1, LOW);
   pinMode(sensorPin1, INPUT);
 }
 return; // Exit the function if the timeout has not yet expired

}

// Print if the timeout has expired or display the exact result in seconds
if (sensor1Activated) {
Serial.println(“”);
if (digitalRead(sensorPin2) == HIGH && sensorTime2 == 0) {

   // If sensor 2 is activated after the timeout has expired
   timeSensor2 = millis(); // Store the current time
 delay(500);

  tiempoCiclo = tiempoSensor2 - tiempoSensor1;
  tiempoCalculado = tiempoCiclo / 1000.0; // Convertir de milisegundos a segundos


  Serial.print("Tiempo de ciclo Calculado: ");
  Serial.print(tiempoCalculado, 2); // Mostrar hasta 3 decimales
  Serial.println(" Segundos ");

  // Reiniciar para esperar el sensor 1 nuevamente
  tiempoSensor1 = 0;
  tiempoSensor2 = 0;
  sensor1Activado = false;  // Reiniciar la detección del sensor 1

  pinMode(sensorPin1, OUTPUT);
  digitalWrite(sensorPin1, LOW);
  pinMode(sensorPin1, INPUT);
} else {
  Serial.println("¡Tiempo de espera ha expirado! Sensor 2 no detectado.");
  tiempoSensor1 = 0;  // Reiniciar para esperar el sensor 1 nuevamente
  sensor1Activado = false;  // Reiniciar la detección del sensor 1

  pinMode(sensorPin1, OUTPUT);
  digitalWrite(sensorPin1, LOW);
  pinMode(sensorPin1, INPUT);
}

}

// Publicar datos en MQTT
char payload[50];
sprintf(payload, “{"tiempo_ciclo": %.2f}”, tiempoCalculado);
client.publish(“v2/tu_usuario/devices/tu_dispositivo/data/tiempo_ciclo”, payload);
Serial.println(“Enviando datos a MQTT…”);
Serial.println(payload);

delay(5000); // Agregar un retardo para no enviar datos constantemente
}

Hi, in your example there are two different topics:

v2/your_user/devices/your_device/data/cycle_time

and

v2/tu_usuario/devices/tu_dispositivo/data/tiempo_ciclo

Please, ensure that both topics are the same both in your code and in the bucket configuration. You can also go to the device inspector to see if the messages are being received correctly by the platform.

Thank you very much, I was guided by the device inspector and I was receiving the information, it seemed to be a typo when adding it to the data buckets. I already solved it. Now I am trying to perform the reverse function, that is, I want to subscribe to a widget with MQTT to turn an LED on and off. The microcontroller connects very well but I don’t know how to configure the topic, or the message I would have to receive from the widget to turn the LED on and off.