Send variable from slider Ubidots to arduino


#1

Hello, my problem is very simple, but complex, becasuse the information not is clear.

I have a variable “termostato” in a widget “SLIDER”, and y want capture the value in arduino for realice a mathematic operation and display in a variable Metric in a Widget.

My code:

    /****************************************
     * Include Libraries
     ****************************************/
    #include "UbidotsESPMQTT.h"

    /****************************************
     * Define Constants
     ****************************************/
    #define TOKEN "**********" // Your Ubidots TOKEN

    #define WIFINAME "VTR-1967554" //Your SSID
    #define WIFIPASS "tp2qtFpdrrcw" // Your Wifi Pass
    #define DEVICE_LABEL  "dispositivo2"  // Put here your Ubidots device label
    #define VARIABLE_LABEL  "termostato"  // Put here your Ubidots variable label 


    Ubidots client(TOKEN);

    /****************************************
     * Auxiliar Functions
     ****************************************/
    int valor; int ID_power;
    int valor0;int valor1;

    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]);
      }
      Serial.println();
          Serial.print(" ");
        Serial.print((char)payload[0]);
        Serial.print(" ");
        Serial.print((char)payload[1]);
        Serial.print(" ");
        Serial.print((char)payload[2]);
        Serial.print(" ");
        valor0= payload[0];
        valor1= payload[1];
        valor=payload[0]*87;
        Serial.print(valor);
        Serial.print(" ");
        Serial.print(topic);
    }

    /****************************************
     * Main Functions
     ****************************************/



    void setup() {
      // put your setup code here, to run once:
      client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
      client.setDebug(false); // Pass a true or false bool value to activate debug messages
      Serial.begin(115200);
      client.wifiConnection(WIFINAME, WIFIPASS);
      client.begin(callback);
      client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
      }

    void loop() {
      // put your main code here, to run repeatedly:
      if(!client.connected()){
          client.reconnect();
          client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_LABEL); //Insert the dataSource and Variable's Labels
          }


      client.add("valor_matematico", valor); //Insert your variable Labels and the value to be sent
      client.ubidotsPublish("dispositivo2");
       
      client.loop();
      }

my attempt is not successful

Best regards
Angel Royo


#2

Greetings, you should edit your callback function to parse your subscribed variable’s value. For more information, please refer to this post.

All the best


#3

I don’t find the solution in that Post…

I await a real solution please.

Thank you
AngelRoyo


#4

Greetings @AngelRoyo, as I told you previously, you should edit your callback function to parse the value that arrives from the subscribed topic, you can find an example here. Please read carefully this guide, there you will find the generalities of the callback function and how to use it.

You can also find an example edited callback function example here.

All the best