Cannot purplish 3 variables

Hi,

I hope you can help me with my project. I want to publish the values of three variables. All of them appear of the dash board. However, two of them take long period of time to change. I would like them to change one every 2 seconds.

I included my could if you could just help me, I would be very greatful.


#include <Arduino.h>

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

  • Include Libraries

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

#include <WiFi.h>

#include <PubSubClient.h>

// the values that I want to sent to the cloud, but they are as strings.

String a;

String T;

String I;

String V;

String t;

float v;

float r;

float i;

// port and pins on 2ed bord

#define RXp2 16

#define TXp2 17

#define WIFISSID “maree123” // Put your WifiSSID here

#define PASSWORD “ohsh1022” // Put your wifi password here

#define TOKEN “BBFF-Xr4faUcK8Ylp997Bufs1ax0axIMRsK” // Put your Ubidots’ TOKEN

#define MQTT_CLIENT_NAME “RandomName” // 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 Constants

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

#define VARIABLE_LABEL_1 “Temprature” // Assing the variable label

#define VARIABLE_LABEL_SUBSCRIBE “Temp” // Assing the variable label

#define DEVICE_LABEL “esp” // Assig the device label

//**********var2

#define VARIABLE_LABEL_2 “Current” // Assing the variable label

#define VARIABLE_LABEL_SUBSCRIBE “I” // Assing the variable label

#define DEVICE_LABEL “esp” // Assig the device label

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

#define VARIABLE_LABEL_3 “volt” // Assing the variable label

#define VARIABLE_LABEL_SUBSCRIBE “volt” // Assing the variable label

#define DEVICE_LABEL “esp” // Assig the device label

char mqttBroker[] = “things.ubidots.com”;

char payload1[500];

char topic1[128];

//char topic2[50];

//char topic3[50];

char topicSubscribe[200];

// Space to store values to send

char str_sensor1[128];

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

  • Auxiliar Functions

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

WiFiClient ubidots;

PubSubClient client(ubidots);

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(topicSubscribe);

} else {

  Serial.print("Failed, rc=");

  Serial.print(client.state());

  Serial.println(" try again in 2 seconds");

  // Wait 2 seconds before retrying

  delay(2000);

}

}

}

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

}

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

  • Main Functions

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

void setup() {

Serial.begin(9600);

WiFi.begin(WIFISSID, PASSWORD);

// Assign the pin as INPUT ???/

Serial2.begin(9600, SERIAL_8N1, RXp2, TXp2);

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

sprintf(topicSubscribe, “/v1.6/devices/%s/%s/lv”, DEVICE_LABEL, VARIABLE_LABEL_SUBSCRIBE);

client.subscribe(topicSubscribe);

}

void loop() {

if (!client.connected()) {

client.subscribe(topicSubscribe);  

reconnect();

}

a=Serial2.readString();

V= a.substring(17,23);

v=V.toFloat();

T=a.substring(4,9);

r=T.toFloat();

I=a.substring(30,34);

i=I.toFloat();

//---------------------------------------------------------------------------------------------------------------------------------------------

sprintf(topic1, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);

sprintf(payload1, “%s”, “”); // Cleans the payload

sprintf(payload1, “{”%s":",VARIABLE_LABEL_1);

dtostrf(r, 4, 2, str_sensor1);

sprintf(payload1, “%s {“value”: %s}}”, payload1, str_sensor1);

client.publish(topic1, payload1);

//---------------------------------------------------------------------------------------------------------------------------------------------

sprintf(topic1, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);

sprintf(payload1, “%s”, “”); // Cleans the payload

sprintf(payload1, “{”%s":",VARIABLE_LABEL_2);

dtostrf(i, 4, 2, str_sensor1);

sprintf(payload1, “%s {“value”: %s}}”, payload1, str_sensor1);

client.publish(topic1, payload1);

//----------------------------------------------------------------------------------------------------------------------------------------------

sprintf(topic1, “%s%s”, “/v1.6/devices/”, DEVICE_LABEL);

sprintf(payload1, “%s”, “”); // Cleans the payload

sprintf(payload1, “{”%s":",VARIABLE_LABEL_3);

dtostrf(v, 4, 2, str_sensor1);

sprintf(payload1, “%s {“value”: %s}}”, payload1, str_sensor1);

client.publish(topic1, payload1);

//---------------------------------------------------------------------------------------------------------------------------------------------

Serial.print("V= ");

Serial.print(v );

Serial.print(" T= ");

Serial.print( r );

Serial.print(" I= ");

Serial.println( i) ;

Serial.println(“Publishing data to Ubidots Cloud”);

client.loop();

delay(1000);

}