Ubidots Community

How to publish multiple values on MQTT using ESP8266?

Hello! I am using an ultrasonic sensor with esp8266. I want to publish the sensor values to MQTT but it publishes only one value. Please guide me on how can I publish more than 1 value?
Here is my code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

int respons;
int responss;

//  network setting.

const char* ssid = "-----";
const char* password = "--------";
const char* mqtt_server = "broker.mqtt-dashboard.com";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {

  client.setServer(mqtt_server, 1883);

void setup_wifi() {

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {


  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  for (int i = 0; i < length; i++) {


void reconnect() {

  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    //if you MQTT broker has clientID,username and password
    //please change following line to    if (client.connect(clientId,userName,passWord))
    if (client.connect(clientId.c_str()))
      // Once connected, publish an announcement...
      //char dataArray[10]; //an array to put data in
      //sprintf(dataArray, "%d", responss); //turn the data into a C string
      //client.publish("Topic", dataArray); //publish the data
      //client.publish("Topic", dataArray1);
      // ... and resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void loop() {

  if (!client.connected()) {

  long now = millis();
  //respons = Serial.read();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    String msg = "sensor value:";
    msg = msg + responss;
    char dataArray[58]; //an array to put data in
    msg.toCharArray(dataArray, 58);
    client.publish("Topic", dataArray);
    //sprintf(dataArray, "%d", responss); //turn the data into a C string
    //client.publish("Topic", dataArray);
    //client.publish("Topic", dataArray); //publish the data
    //client.publish("Topic", "sensor");
  // sensor programming
  byte poll[] = {0xAF, 0xFC, 0xFE, 0x40};
  Serial.write(poll, sizeof(poll));
  int x = 6;

  while (x > 0)
    responss = Serial.read();
    Serial.println(responss, HEX);

/*void output()
  Serial.print("the value of responss is ");

Hi @mehakfatima

I noticed that your question isn’t related in any way to the Ubidots Library or The Ubidots Platform itself, I would recommend you to use our library for the ESP8266 and follow our step-by-step guidepost in our help center, if you have any doubts afterward please contact us.

Hi…with such an amount of information you will battle to send every last bit of it in one go. We should expect that your ints are every 4 bytes in size, and you need a byte to isolate the ints from one another in the message you are sending. That is 5 bytes for each worth, with 6,000 qualities, that comes to 30KB. You’d require basically the entirety of the RAM on the ESP8266 to send these in one go. (That is overlooking the way that you’ve put away them off an in cluster which has utilized a comparable measure of information … so you will be unable to store each of the 6,000 qualities in one go either except if you have more modest ints).

Notwithstanding the abovementioned in case you’re utilizing tuanpmt’s mqtt stack you’ll see that it has its own round cradle to store messages in while sending. I fail to remember the default size of this, yet you need to keep it genuinely low for the remainder of your code to have some RAM to play with. I keep this at 4KB in my code.