[SOLVED] Sending Mutiple Variable to Ubidots

hello , 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);
}
1 Like

Greetings, this has been already addressed at this post, it is a limitation in the PubSubclient library, just split your sensor updates in two requests. Also, use industrial.api.ubidots.com instead of things.iot.ubidots.com to send data, as the first one is our official broker url.

All the best

thanks for the reply @jotathebest, i have changed the broker url from things.iot.ubidots.com to industrial.api.ubidots. I have also tried splitting the sensor update in two requests but i am not sure if i did it the right way because i am quite new to programming, is it possible for you to guide me how can it be edited from my code to splitting the sensors?

Hello @sheltonkoh ,

I’ll leave a couple of recommendations for your code:

  1. You don’t need to define multiple topics (topic1, topic2, topic3…) because the topic is unique because you are sending data to the same device “esp32”. In case you are using several devices you should add a topic for each of them. Example:
Topic1 = /v1.6/devices/esp32-1
Topic2 = /v1.6/devices/esp32-2
Topic3 = /v1.6/devices/esp32-3
  1. You don’t need to structure a payload for each variable to be sent. You can send multiple variables within the same payload, as long as it does not exceed the maximum packet size supported by the PubSubClient library. By default the library handles a maximum of 128 bytes, we recommend increasing that maximum to 521 bytes to handle more data within the payload.

Like my colleague Jose, I recommend to divide the data sending in 2 requests since it is handling 6 variables. So you should have something like this:

  sprintf(topic, "%s%s", "/v1.6/devices/", "esp32");
  
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":%s,", "var-1", "120"); // Adds the variable label
  sprintf(payload, "%s \"%s\":%s,", payload, "var-2", "150"); // Adds the variable label
  sprintf(payload, "%s \"%s\":%s}", payload, "var-3", "160"); // Adds the variable label
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic, payload);
  
  sprintf(payload, "%s", ""); // Cleans the payload
  sprintf(payload, "{\"%s\":%s,", "var-4", "123"); // Adds the variable label
  sprintf(payload, "%s \"%s\":%s,", payload, "var-5", "234"); // Adds the variable label
  sprintf(payload, "%s \"%s\":%s}", payload, "var-6", "436"); // Adds the variable label
  Serial.println("Publishing data to Ubidots Cloud");
  Serial.println(payload);
  client.publish(topic, payload);

Resulting in the following:

Publishing data to Ubidots Cloud
{"var-1":120, "var-2":150, "var-3":160}
Publishing data to Ubidots Cloud
{"var-4":123, "var-5":234, "var-6":436}

For more information on how to handle the data, I recommend you visit the MQTT documentation.

I hope this helps you with your project. Welcome to Ubidots. :slight_smile:

Cheers,
Maria H.

omg thank you so so much @mariahernandez !!!, i tried changing according to your recommendation and changes the maximum package from 128 to 512 and it works! thanks for guiding a newcomer like me. Lastly is there anything else that i need to change as i am getting incorrect figures for temperature and battery in the IoT platform compared to the value i gotten on serial monitor as i have formulate an algorithms for both temperature and battery sensor,

Temp%20and%20batt%20comport

nvm i have solved it! thanks

Glad to know you solved it. If you want you can share the functional code, it would be a great help for another person who may have the same doubt as you. :smiley:

Cheers,
Maria H.

Thank you for sharing it with the community! :hugs:

Hi,
i have the same problem with sending more than 3 variables, but this solution didnt work for me?
I also add a post with code there: https://ubidots.com/community/t/arduino-nano-sim800l-evb-working-code-but-how-to-make-code-more-effective/2716/2.

Any idea how to make it work?

Thank you :slight_smile: