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() {

  Serial.begin(19200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void setup_wifi() {

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

  WiFi.begin(ssid, password);

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

  }

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

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]);
  }

}

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()))
    {
      Serial.println("connected");
      // 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);
      //delay(3000);
      // ... and resubscribe
      //client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {

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

  client.loop();
  long now = millis();
  //respons = Serial.read();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    //++value;
    String msg = "sensor value:";
    msg = msg + responss;
    char dataArray[58]; //an array to put data in
    msg.toCharArray(dataArray, 58);
    Serial.println(dataArray);
    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
  Serial.begin(19200);
  byte poll[] = {0xAF, 0xFC, 0xFE, 0x40};
  Serial.write(poll, sizeof(poll));
  delay(3000);
  int x = 6;

  while (x > 0)
  {
    responss = Serial.read();
    Serial.println(responss, HEX);
    delay(500);
    x--;

  }
  Serial.end();
  //resetFunc();
}
/*void output()
  {
  Serial.print("the value of responss is ");
  Serial.println(responss);
  }*/

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.

pcb fabrication and assembly