Ubidots Community

How to send counter button data?

Hi. I have two buttons that will increment the value. The first button is for the whole number and the second button is for the decimal points. And they will sum up together as a single variable then send the data to ubidots. Now my problem is that upon seeing my serial monitor, the button is publishing my desired button values which is 1.20, but when I look at my ubidots I can only see 0.00 values. Can someone help me please?

Ubidots:

This is the serial monitor:

serial monitor

This is my code:

/*****************************************
* Include Libraries
****************************************/
#include <ESP8266WiFi.h>
#include <ConfigManager.h>
#include <PubSubClient.h>

#define ECHO D2
#define TRIG D4
#define buttonPin1 D5
#define buttonPin2 D6
#define SENSOR A0

/****************************************
* Define Constants
****************************************/
namespace {
 const char * AP_NAME = "JESVINKER Device"; // Assigns your Access Point name
 const char * MQTT_SERVER = "industrial.api.ubidots.com"; 
 const char * TOKEN = "*************"; // Assigns your Ubidots TOKEN
 const char * DEVICE_LABEL = "FLOOD-MONITORING"; // Assigns your Device Label
 const char * VARIABLE_LABEL = "max-height"; // Assigns your Variable Label
 const char * VARIABLE_LABEL1 = "my-variable1"; // Assigns your Variable Label

 int button_state1 = 0;
 int button_state2 = 0;
 int prestate1 = 0;
 int prestate2 = 0;
 int count_value2 = 1;
 int limit2 = 12;
 int distance;
 int mapVal;
 int value1;

 float limit1 = 1.0;
 float count_value1 = 0.0;
 float dis;
 float level;
 float value;
}

char topic[150];
char payload[50];
String clientMac = "";
unsigned char mac[6];
unsigned long previousMillis_one = 0; // store the last time updated
unsigned long previousMillis_two = 0; // store the last time updated
unsigned long previousMillis_three = 0; // store the last time updated
unsigned long previousMillis_four = 0; // store the last time updated

long five_seconds_interval = 5*1000UL; // interval desired (millisenconds)
long ten_seconds_interval = 10*1000UL; // interval desired (millisenconds)
long one_hundred_mil_interval = 0.1*1000UL;
long one_second_interval = 1*1000UL;

struct Config {
 char name[20];
 bool enabled;
 int8 hour;
} config;

/****************************************
* Initialize a global instance
****************************************/
WiFiClient espClient;
PubSubClient client(espClient);
ConfigManager configManager;

/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
}

void reconnect() {
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientMac.c_str(), TOKEN, NULL)) {
  Serial.println("connected");
  break;
  } 
else {
    configManager.reset();
    Serial.print("failed, rc=");
    Serial.print(client.state());
    Serial.println(" try again in 3 seconds");
    for(uint8_t Blink=0; Blink<=3; Blink++) {
      digitalWrite(LED2, LOW);
      delay(1000);
      digitalWrite(LED2, HIGH);
      delay(1000);
   }
  }
 }
}

String macToStr(const uint8_t* mac) {
 String result;
 for (int i = 0; i < 6; ++i) {
 result += String(mac[i], 16);
 if (i < 5)result += ':';
 }
return result;
}

/****************************************
* Main Functions
****************************************/
void setup() {
 Serial.begin(115200);
 /* Declare PINs as input/outpu */
 pinMode(buttonPin1, INPUT);
 pinMode(buttonPin2, INPUT);
 pinMode(TRIG, OUTPUT);
 pinMode(ECHO, INPUT_PULLUP);
 pinMode(PIN_RESET, INPUT);
 pinMode(LED, OUTPUT);
 pinMode(LED2, OUTPUT);
 digitalWrite(LED2, LOW);

/* Assign WiFi MAC address as MQTT client name */
 WiFi.macAddress(mac);
 clientMac += macToStr(mac);

/* Access Point configuration */
 configManager.setAPName(AP_NAME);
 configManager.addParameter("name", config.name, 20);
 configManager.addParameter("enabled", &config.enabled);
 configManager.addParameter("hour", &config.hour);
 configManager.begin(config);

/* Set Sets the server details */
client.setServer(MQTT_SERVER, 1883);
client.setCallback(callback);

/* Build the topic request */
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
}

void loop() {  
 configManager.reset();
 configManager.loop();    

/* MQTT client reconnection */
if (!client.connected()) {
  reconnect();
}

if (client.connect(clientMac.c_str(), TOKEN, NULL))
{
 digitalWrite(LED2, HIGH);
 delay(100);
 digitalWrite(LED2, LOW);
 delay(100);
}
/* Sensor Reading */
button_state1 = digitalRead(buttonPin1);
button_state2 = digitalRead(buttonPin2);
value = count_value2 + count_value1;
value1 = analogRead(SENSOR);

/* Build the payload request */
unsigned long currentMillis = millis();
if (currentMillis - previousMillis_three > one_hundred_mil_interval) 
{
 previousMillis_three = currentMillis;
 if (button_state1 == 1 && prestate1 == 0) 
 {
   count_value1 += 0.1;
   prestate1 = 1;
 }
 else if(button_state1 == 0) 
 {
   prestate1 = 0;
 }
} 
if(count_value1 > limit1)
{
 count_value1 = 0.0;
}

if (currentMillis - previousMillis_four > one_hundred_mil_interval) 
 {
  previousMillis_four = currentMillis;
 if (button_state2 == 1 && prestate2 == 0) 
 {
   count_value2 += 1;
   prestate2 = 1;
 }
 else if(button_state2 == 0) 
 {
   prestate2 = 0;
 }
} 
if(count_value2 > limit2)
{
 count_value2 = 0;
}

 if (currentMillis - previousMillis_one > one_second_interval) 
   {
   previousMillis_one = currentMillis;
    Serial.println("five seconds interval");

/* Building the Ubidots request */
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\": %d}", VARIABLE_LABEL, value); // Adds the variable label

Serial.println("Publishing values to Ubidots Cloud");
Serial.print("Max Height = ");
Serial.println(value);
client.publish(topic, payload);
client.loop(); 
}
Serial.println("looping..");
delay(1000);
}

Good day @itschristianjay,

I hope all is well.

Can you please print in the serial monitor the payload you are sending to the Ubidots device and send us an image? In that way, we can detect what could be happening that it is not corresponding to the Max Height value.

I will be attentive to your response.

Best,
-Isabel