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