[SOLVED] Only one variable will publish using ESP32 and revised sample sketch

Hello, adding upon the sample MQTT sketch I have the below sketch to publish 2 sensors and subscribe 2 relays. Only the one sensor publishes and creates a variable.

Can anyone tell me what I am missing?

#define VARIABLE_LABEL_PUBLISH "sensor" 
#define VARIABLE_LABEL_SUBSCRIBE "relay" 
#define VARIABLE_LABEL_PUBLISH "sensor2" 
#define VARIABLE_LABEL_SUBSCRIBE "relay2" 
#define DEVICE_LABEL "esp32" 

#define SENSOR 14 
#define RELAY 16 
#define SENSOR2 13 
#define RELAY2 15 

char mqttBroker[]  = "things.ubidots.com";
char payload[100];
char payload2[100];
char topic[150];
char topic2[150];
// Space to store values to send 
char str_sensor[10];
char str_sensor2[10];

/****************************************
 * Auxiliar Functions
 ****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);

void callback(char* topic, byte* payload, unsigned int length) {
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  String message(p);
  if (message == "0") {
    digitalWrite(RELAY, LOW);
  } else {
    digitalWrite(RELAY, HIGH);
  }
  Serial.write(payload, length);
  Serial.println();
}

void callback2(char* topic2, byte* payload2, unsigned int length) {
  char q[length + 1];
  memcpy(q, payload2, length);
  q[length] = NULL;
  String message(q);
  if (message == "0") {
    digitalWrite(RELAY2, LOW);
  } else {
    digitalWrite(RELAY2, HIGH);
  }
  Serial.write(payload2, length);
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    
    // Attemp to connect
    if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
      Serial.println("Connected");
      client.subscribe("/v1.6/devices/"DEVICE_LABEL"/"VARIABLE_LABEL_SUBSCRIBE"/lv");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 2 seconds");
      // Wait 2 seconds before retrying
      delay(2000);
    }
  }
}

/****************************************
 * Main Functions
 ****************************************/
void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFISSID, PASSWORD);
  // Assign the pins as INPUT/OUTPUT 
  pinMode(SENSOR, INPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(SENSOR2, INPUT);
  pinMode(RELAY2, OUTPUT);

  Serial.println();
  Serial.print("Wait for WiFi...");
  
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  
  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  client.setServer(mqttBroker, 1883);
  client.setCallback(callback);
  client.setServer(mqttBroker, 1883);
  client.setCallback(callback2);
  
  client.subscribe("/v1.6/devices/"DEVICE_LABEL"/"VARIABLE_LABEL_SUBSCRIBE"/lv");
}

void loop() {
  if (!client.connected()) {
    reconnect();
    client.subscribe("/v1.6/devices/"DEVICE_LABEL"/"VARIABLE_LABEL_SUBSCRIBE"/lv");   
  }
  sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL); 
  sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);  
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload2, "%s", ""); // Cleans the payload2
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL_PUBLISH); // Adds the variable label
  sprintf(payload2, "{\"%s\":", VARIABLE_LABEL_PUBLISH); // Adds the variable label
  
  float sensor = analogRead(SENSOR);
  float sensor2 = analogRead(SENSOR2); 
  
  /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
  dtostrf(sensor, 4, 2, str_sensor);
  dtostrf(sensor2, 4, 2, str_sensor2);
  
  sprintf(payload, "%s {\"value\": %s}}", payload, str_sensor); // Adds the value
  sprintf(payload2, "%s {\"value\": %s}}", payload2, str_sensor2); // Adds the value
  Serial.println("Publishing data to Ubidots Cloud");
  client.publish(topic, payload);
  client.publish(topic2, payload2);
  client.loop();
  delay(1000);
}

Greetings @richrich36,

Looking into your code I see some errors:

  1. You are trying to publish to two different variables but you have defined only one labeled as ‘VARIABLE_LABEL_PUBLISH’, you should use two different variables if you wish to publish to two different topics.

  2. You are using two callback functions, I am not sure if the PubSubclient library supports this so I advise you to code only one of them.

I advise you too to refer to this article to know how to create properly your MQTT requests.

Also, the code below may serve you as reference too to reach your goal:

/****************************************
 * Include Libraries
 ****************************************/
 
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <stdio.h>  


/****************************************
 * Define Constants
 ****************************************/

#define WIFISSID "...." // Put your WifiSSID here
#define PASSWORD "...." // Put your wifi password here
#define TOKEN "...." // Put your Ubidots' TOKEN
#define VARIABLE_LABEL "..." // Assing the variable label
#define VARIABLE_LABEL_2 "...." // Assing the variable label
#define DEVICE_LABEL "...." // Assing the device label
#define MQTT_CLIENT_NAME "...." // MQTT client Name

char mqttBroker[] = "things.ubidots.com";
char payload[700];
char topic[150];
char topic2[150];

// Space to store values to send
char str_temp[6];
char str_lat[6];
char str_lng[6];


/****************************************
 * Initializate constructors for objects
 ****************************************/

ESP8266WiFiMulti WiFiMulti;
WiFiClient ubidots;
PubSubClient client(ubidots);


/****************************************
 * Auxiliar Functions
 ****************************************/
 
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();
} 

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    
    // Attempt to connect
    if (client.connect(MQTT_CLIENT_NAME, TOKEN,"")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 2 seconds");
      // Wait 2 seconds before retrying
      delay(2000);
    }
  }
}


/****************************************
 * Main Functions
 ****************************************/
 
void setup() {
    Serial.begin(115200);
    pinMode(A0, INPUT);
    WiFiMulti.addAP(WIFISSID, PASSWORD);
    Serial.println();
    Serial.println();
    Serial.print("Wait for WiFi... ");

    while(WiFiMulti.run() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }
    
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    client.setServer(mqttBroker, 1883);
    client.setCallback(callback);
}

void loop() {

    if (!client.connected()) {
      reconnect();

      // Subscribes for getting the value of the control variable in the temperature-box device
      char topicToSubscribe[200];
      sprintf(topicToSubscribe, "%s", ""); // Cleans the content of the char
      sprintf(topicToSubscribe, "%s%s", "/v1.6/devices/", "temperature-box");
      sprintf(topicToSubscribe, "%s/%s/lv", topicToSubscribe, "control");
      client.subscribe(topicToSubscribe);
    }
    
    // Values to send
    float temperature = random(0, 9);
    float lat = 6.101;
    float lng= -1.293;
    
    /* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
    dtostrf(temperature, 4, 2, str_temp);
    dtostrf(lat, 4, 2, str_lat);
    dtostrf(lng, 4, 2, str_lng);

    sprintf(topic, "%s", ""); // Cleans the topic content
    sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);

    sprintf(payload, "%s", ""); // Cleans the payload content
    sprintf(payload, "{\"%s\":", VARIABLE_LABEL); // Adds the variable label   
    sprintf(payload, "%s {\"value\": %s", payload, str_temp); // Adds the value
    sprintf(payload, "%s, \"context\":{\"lat\": %s, \"lng\": %s}", payload, str_lat, str_lng); // Adds coordinates
    sprintf(payload, "%s } }", payload); // Closes the dictionary brackets
    client.publish(topic, payload);
    
    sprintf(topic2, "%s", ""); // Cleans the topic content
    sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);

    sprintf(payload, "%s", ""); // Cleans the payload content
    sprintf(payload, "{\"%s\":", VARIABLE_LABEL_2); // Adds the variable label   
    sprintf(payload, "%s {\"value\": %s", payload, str_temp); // Adds the value
    sprintf(payload, "%s, \"context\":{\"lat\": %s, \"lng\": %s}", payload, str_lat, str_lng); // Adds coordinates
    sprintf(payload, "%s } }", payload); // Closes the dictionary brackets
    
    client.publish(topic2, payload);
    client.loop();
    delay(1000);
}

Regards

Hello Mam,
Im also trying to publish 7 variables in ubidots devices dashboard. I set MQTT packet size to 512 but still only 6 variables are displayed in dashboard. But my serial monitor displaying all seven variables. Attaching Screenshot of serial monitor and ubidots screenshots.

Greetings, Ubidots implements a throttling of 4 requests per second, and it seems that you are sending variables faster than it. I would advice you to add a small delay to fit this throttling and to follow the instructions written by me previously.

All the best

Thanks for your reply. I increased delay to 2s but still only 6 variables are published in ubidots. Also as seen in screenshot of ubidots dashboard, only three variables are updated frequently, remaining three variables takes time for updation… how this can be solved???

Im attaching my code here… can you tell me whats wrong in this??

/****************************************
 * Include Libraries
 ****************************************/
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>;
/****************************************
 * Define Constants
 ****************************************/
#define WIFISSID " " // Put your WifiSSID here
#define PASSWORD " " // Put your wifi password here
#define TOKEN " " // Put your Ubidots' TOKEN
#define MQTT_CLIENT_NAME " " // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string; 
                                           //it should be a random and unique ascii string and different from all other devices

#define VARIABLE_LABEL1 "tmp36-1" // Assing the variable label
#define VARIABLE_LABEL2 "tmp36-2"
#define VARIABLE_LABEL3 "tmp36-3"
#define VARIABLE_LABEL4 "tmp36-4"
#define VARIABLE_LABEL5 "obj sensor"
#define VARIABLE_LABEL6 "dht22-temp"
#define VARIABLE_LABEL7 "humidity"
#define DEVICE_LABEL "esp32" // Assig the device label

#define DHTPIN 23
#define DHTTYPE DHT22
//#define SENSOR 12 // Set the GPIO12 as SENSOR

char mqttBroker[]  = "things.ubidots.com";
char payload[2000]; //The maximum payload size supported by Ubidots is of 10000 bytes.
char topic[150];
char topic2[150];
char topic3[150];
char topic4[150];
char topic5[150];
char topic6[150];
char topic7[150];



// Space to store values to send
char str_sensor1[60];
char str_sensor2[60];
char str_sensor3[60];
char str_sensor4[60];
char str_sensor5[60];
char str_sensor6[60];
char str_sensor7[60];
//char str_lat[6];
//char str_lng[6];

DHT dht(DHTPIN, DHTTYPE);
/****************************************
 * Auxiliar Functions
 ****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);

void callback(char* topic, byte* payload, unsigned int length) {
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  String message(p);
  Serial.write(payload, length);
  Serial.println(topic);
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    
    // Attemp to connect
    if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
      Serial.println("Connected");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 2 seconds");
      // Wait 2 seconds before retrying
      delay(2000);
    }
  }
}

/****************************************
 * Main Functions
 ****************************************/
void setup() {
  Serial.begin(115200);
  dht.begin();
  WiFi.begin(WIFISSID, PASSWORD);
  // Assign the pin as INPUT 
  //pinMode(SENSOR, INPUT);

  Serial.println();
  Serial.print("Wait for WiFi...");
  
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  
  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  client.setServer(mqttBroker, 1883);
  client.setCallback(callback);  
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
 
  
 int sensor1 = analogRead(36); 
 int sensor2 = analogRead(39); //temp 2
 int sensor3 = analogRead(34); //temp 3
 int sensor4 = analogRead(35); //temp 4
 int sensor5 = analogRead(32); //IR Object sensor
  
  float voltage1 = sensor1 / 1023.0;
  float voltage2 = sensor2 / 1023.0;
  float voltage3 = sensor3 / 1023.0;
  float voltage4 = sensor4 / 1023.0;
  float voltage5 = sensor5 * (3.3 / 4095.0);
  
  float temp1 = (voltage1 - 0.5) * 100.0;
  float temp2 = (voltage2 - 0.5) * 100.0;
  float temp3 = (voltage3 - 0.5) * 100.0;
  float temp4 = (voltage4 - 0.5) * 100.0;
  float hum = dht.readHumidity();
  float temp = dht.readTemperature();
 

  /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
  dtostrf(temp1, 4, 2, str_sensor1);
  dtostrf(temp2, 4, 2, str_sensor2);
  dtostrf(temp3, 4, 2, str_sensor3);
  dtostrf(temp4, 4, 2, str_sensor4);
  dtostrf(voltage5, 4, 2, str_sensor5);
  dtostrf(temp, 4, 2, str_sensor6);
  dtostrf(hum, 4, 2, str_sensor7);
   
 
  sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor1); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary brackets
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic, payload);
   
  sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor2); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic2, payload);
 
  sprintf(topic3, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL3); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor3); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic3, payload);
  
  sprintf(topic4, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL4); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor4); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic4, payload);
  
  sprintf(topic5, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL5); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor5); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic5, payload);
  
  sprintf(topic6, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL6); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor6); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic6, payload);
  
  
  sprintf(topic7, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL7); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor7); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic7, payload);
  client.loop();
  delay(2000);
}

The delay should wait for EVERY publish, actually your code is not respecting the throttling because it is sending more than 4 dots at once, you should add the delay between every publish, something like this:

//Builds payload

//publishes
client.publish(topic, payload);
delay(500);

//Builds new payload

//publishes
client.publish(topic2, payload);

All the best

Thanks for your reply. It works great!!!:grinning: and all the variables are updating, but still only 6 variables are displayed. Still one more variable has to be published but that is not happening…

How can I overcome this? Also by changing MQTT packet size to 512 how many maximum variables can be published in ubidots???

Thanks in advance…

The limit is not at Ubidots but at the PubSubclient library, I would advice you to make sure that both hum and temp are getting read as valid float numbers, and, to print the payload of those variables to check if you are sending a valid number.

All the best

Thanks for your reply mam, but the variable to be published is “ir obj” whose value is 0.49 as shown in attached serial monitor screenshot. its a valid floating point number .but still it is not been sent to ubidots dashboard
Pls help…

I have tested this script, which is based on yours and my previous comments and I can publish all the variables properly

/****************************************
 * Include Libraries
 ****************************************/

#include <ESP8266WiFi.h>

#include <PubSubClient.h>

/****************************************
 * Define Constants
 ****************************************/
#define WIFISSID "  " // Put your WifiSSID here
#define PASSWORD "  " // Put your wifi password here
#define TOKEN "BBFF-" // Put your Ubidots' TOKEN
#define MQTT_CLIENT_NAME "asdhuihs789fy9c7y0809dsyhunvxcinv8" // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string; 
                                           //it should be a random and unique ascii string and different from all other devices

#define VARIABLE_LABEL1 "tmp36-1" // Assing the variable label
#define VARIABLE_LABEL2 "tmp36-2"
#define VARIABLE_LABEL3 "tmp36-3"
#define VARIABLE_LABEL4 "tmp36-4"
#define VARIABLE_LABEL5 "obj-sensor"
#define VARIABLE_LABEL6 "dht22-temp"
#define VARIABLE_LABEL7 "humidity"
#define DEVICE_LABEL "esp32" // Assig the device label


char mqttBroker[]  = "things.ubidots.com";
char payload[2000]; //The maximum payload size supported by Ubidots is of 10000 bytes.
char topic[150];
char topic2[150];
char topic3[150];
char topic4[150];
char topic5[150];
char topic6[150];
char topic7[150];



// Space to store values to send
char str_sensor1[60];
char str_sensor2[60];
char str_sensor3[60];
char str_sensor4[60];
char str_sensor5[60];
char str_sensor6[60];
char str_sensor7[60];



/****************************************
 * Auxiliar Functions
 ****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);

void callback(char* topic, byte* payload, unsigned int length) {
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  String message(p);
  Serial.write(payload, length);
  Serial.println(topic);
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    
    // Attemp to connect
    if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
      Serial.println("Connected");
    } else {
      Serial.print("Failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 2 seconds");
      // Wait 2 seconds before retrying
      delay(2000);
    }
  }
}

/****************************************
 * Main Functions
 ****************************************/
void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFISSID, PASSWORD);
  // Assign the pin as INPUT 
  //pinMode(SENSOR, INPUT);

  Serial.println();
  Serial.print("Wait for WiFi...");
  
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  
  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  client.setServer(mqttBroker, 1883);
  client.setCallback(callback);  
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
 
  
 int sensor1 = analogRead(36); 
 int sensor2 = analogRead(39); //temp 2
 int sensor3 = analogRead(34); //temp 3
 int sensor4 = analogRead(35); //temp 4
 int sensor5 = analogRead(32); //IR Object sensor
  
  float voltage1 = random(0, 1000)*1.0;
  float voltage2 = random(0, 1000)*1.0;
  float voltage3 = random(0, 1000)*1.0;
  float voltage4 = random(0, 1000)*1.0;
  float voltage5 = random(0, 1000)*1.0;
  
  float temp1 = (voltage1 - 0.5) * 100.0;
  float temp2 = (voltage2 - 0.5) * 100.0;
  float temp3 = (voltage3 - 0.5) * 100.0;
  float temp4 = (voltage4 - 0.5) * 100.0;
  float hum = random(0, 1000)*1.0;
  float temp = random(0, 1000)*1.0;
 

  /* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
  dtostrf(temp1, 4, 2, str_sensor1);
  dtostrf(temp2, 4, 2, str_sensor2);
  dtostrf(temp3, 4, 2, str_sensor3);
  dtostrf(temp4, 4, 2, str_sensor4);
  dtostrf(voltage5, 4, 2, str_sensor5);
  dtostrf(temp, 4, 2, str_sensor6);
  dtostrf(hum, 4, 2, str_sensor7);
   
 
  sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor1); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary brackets
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic, payload);
  delay(500);
   
  sprintf(topic2, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor2); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic2, payload);
  delay(500);
 
  sprintf(topic3, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL3); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor3); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic3, payload);
  delay(500);
  
  sprintf(topic4, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL4); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor4); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic4, payload);
  delay(500);
  
  sprintf(topic5, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL5); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor5); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic5, payload);
  delay(500);
  
  sprintf(topic6, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL6); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor6); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic6, payload);
  delay(500);
  
  sprintf(topic7, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":", VARIABLE_LABEL7); // Adds the variable label   
  sprintf(payload, "%s {\"value\": %s", payload, str_sensor7); // Adds the value
  sprintf(payload, "%s } }", payload); // Closes the dictionary bracket
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic7, payload);
  client.loop();
  delay(2000);
}

I have just sent random numbers as I do not have any DHT sensor so I advise you to check the values obtained from your sensor as your issue seems to be outside of our scope.

All the best

Thanks for your reply mam.
Previously we tested ESP8266 board which can publish upto 10 variables. But currently we are using ESP32 board, which is unable to publish all the variables in ubidots dashboard. please look into this issue and update us.

As your code includes ESP8266WiFi.h where as code uploaded by me includes WiFi.h, Pls test the code once again with the WiFi.h header file and let us know the results.

Thanks & Regards,

Greetings, both Esp32 and Esp8266 uses the same library from the example, PubSubclient, so it should not be difference, the only difference is the header that you should use to declare the wifi client. If the issue remains in your Esp32 and not in the Esp8266, probably the problem is at the PubSubclient side and is unfortunately outside of our scope.

All the best

hello @jotathebest , i tried posting 6 variables on ubidots using ESP32 but in the platform only 3 variables are working and the other 3 which are pressure4, temperature and battery-1 are not working. However i am able to see all the data on serial monitor.

#include <PubSubClient.h>
#include <WiFi.h>

/****************************************

Define Constants
****************************************/
#define WIFISSID “” // Put your WifiSSID here
#define PASSWORD “” // Put your wifi password here
#define TOKEN “” // Put your Ubidots’ TOKEN
#define MQTT_CLIENT_NAME “” // MQTT client Name, please enter your own 8-12 alphanumeric character ASCII string;
//it should be a random and unique ascii string and different from all other devices
#define VARIABLE_LABEL1 “pressure1” // Assing the variable label
#define VARIABLE_LABEL2 “pressure2”
#define VARIABLE_LABEL3 “pressure3”
#define VARIABLE_LABEL4 “pressure4”
#define VARIABLE_LABEL5 “temperature”
#define VARIABLE_LABEL6 “battery-1”
#define DEVICE_LABEL “esp32” // Assig the device label

int pressure1pin = 36;
int pressure2pin = 39;
int pressure3pin = 34;
int pressure4pin = 35;
int temp_pin=15;
int bat_pin = 33;
int bat = 0;

char mqttBroker[] = “things.iot.ubidots.com”;
char payload[2000]; //The maximum payload size supported by Ubidots is of 10000 bytes.
char topic[150];
char topic2[150];
char topic3[150];
char topic4[150];
char topic5[150];
char topic6[150];
char topic7[150];

// Space to store values to send
char str_sensor1[60];
char str_sensor2[60];
char str_sensor3[60];
char str_sensor4[60];
char str_sensor5[60];
char str_sensor6[60];
char str_sensor7[60];

/****************************************

Auxiliar Functions
**************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);
void callback(char topic, byte payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message§;
Serial.write(payload, length);
Serial.println(topic);
}

void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.println(“Attempting MQTT connection…”);

// Attemp to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, “”)) {
Serial.println(“Connected”);
} else {
Serial.print(“Failed, rc=”);
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}

/****************************************

Main Functions
****************************************/
void setup() {
Serial.begin(115200);
WiFi.begin(WIFISSID, PASSWORD);
// Assign the pin as INPUT
//pinMode(SENSOR, INPUT);
Serial.println();
Serial.print(“Wait for WiFi…”);

while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}

Serial.println("");
Serial.println(“WiFi Connected”);
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
}

void loop() {
if (!client.connected()) {
reconnect();
}

float pressure1= analogRead(pressure1pin);
Serial.print(pressure1);

float pressure2= analogRead(pressure2pin);
Serial.print(pressure2);

float pressure3= analogRead(pressure3pin);
Serial.print(pressure3);

float pressure4= analogRead(pressure4pin);
Serial.print(pressure4);

float temp = analogRead(temp_pin);
Serial.println(temp);

bat = analogRead(bat_pin);
Serial.println(bat);

/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(pressure1, 4, 2, str_sensor1);
dtostrf(pressure2, 4, 2, str_sensor2);
dtostrf(pressure3, 4, 2, str_sensor3);
dtostrf(pressure4, 4, 2, str_sensor4);
dtostrf(temp, 4, 2, str_sensor5);
dtostrf(bat, 4, 2, str_sensor6);

sprintf(topic, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s":", VARIABLE_LABEL1); // Adds the variable label
sprintf(payload, “%s {“value”: %s”, payload, str_sensor1); // Adds the value
sprintf(payload, “%s } }”, payload); // Closes the dictionary brackets
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
client.publish(topic, payload);
//delay(500);

sprintf(topic2, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s":", VARIABLE_LABEL2); // Adds the variable label
sprintf(payload, “%s {“value”: %s”, payload, str_sensor2); // Adds the value
sprintf(payload, “%s } }”, payload); // Closes the dictionary bracket
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
client.publish(topic2, payload);
//delay(500);

sprintf(topic3, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s":", VARIABLE_LABEL3); // Adds the variable label
sprintf(payload, “%s {“value”: %s”, payload, str_sensor3); // Adds the value
sprintf(payload, “%s } }”, payload); // Closes the dictionary bracket
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
client.publish(topic3, payload);
//delay(500);

sprintf(topic4, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s":", VARIABLE_LABEL4); // Adds the variable label
sprintf(payload, “%s {“value”: %s”, payload, str_sensor4); // Adds the value
sprintf(payload, “%s } }”, payload); // Closes the dictionary bracket
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
client.publish(topic4, payload);
//delay(500);

sprintf(topic5, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s":", VARIABLE_LABEL5); // Adds the variable label
sprintf(payload, “%s {“value”: %s”, payload, str_sensor5); // Adds the value
sprintf(payload, “%s } }”, payload); // Closes the dictionary bracket
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
client.publish(topic5, payload);
//client.loop();
//delay(1000);

sprintf(topic6, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);
sprintf(payload, “%s”, “”); // Cleans the payload
sprintf(payload, “{”%s":", VARIABLE_LABEL6); // Adds the variable label
sprintf(payload, “%s {“value”: %s”, payload, str_sensor6); // Adds the value
sprintf(payload, “%s } }”, payload); // Closes the dictionary bracket
Serial.println(“Publishing data to Ubidots Cloud”);
Serial.println(payload);
client.publish(topic6, payload);
client.loop();
delay(1000);
}

To be addressed at this post.