Subscribing to a variable from another esp32, and then using it in my code

So i have this esp32 IoT project. My partner is using a distance sensor and he is publishing the data to the cloud. this is not a problem. I can also subscribe to his values. The problem is that i dont know how to get this value out of the callback function and use it in my program. Because his distance sensor is supposed to start my program when distance <= x.

Greetings @andylord, hope you are doing great!

The Ubidots ESP32 MQTT library is developed in such a way that you can use the callback to put there any action that you want to trigger when the data arrives to the variable you are subscribing.

For example, lets suppose that you are subscribing to a certain variable, lets say, temperature. When you retrieve the data from that variable, the callback function will be called and all the code in the callback will be executed.

Since the incoming payload data in the callback comes in the form of a pointer to char:
char*
you’d have to transform it into a float value in order to treat it as a number.

here is an example to convert the incoming data to float and based on that value, it prints whether temperature is greater than 70 (too high) or not:

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsEsp32Mqtt.h"
#include <string>
#include <stdio.h>

/****************************************
 * Define Constants
 ****************************************/
const char *UBIDOTS_TOKEN = "user-your-token";  // Put here your Ubidots TOKEN
const char *WIFI_SSID = "user-your-wifi-ssid";      // Put here your Wi-Fi SSID
const char *WIFI_PASS = "user-your-wifi-password";      // Put here your Wi-Fi password
const char *DEVICE_LABEL = "user-your-device's label";   // Replace with the device label to subscribe to
const char *VARIABLE_LABEL = "temperature"; // Replace with your variable label to subscribe to

/****************************************
 * Create the Ubidots object
 ****************************************/
Ubidots ubidots(UBIDOTS_TOKEN);

/****************************************
 * Buffer to temporary store the incoming char* temperature value
 ****************************************/
char dummyChar[128];


/****************************************
 * Auxiliar Functions
 ****************************************/
void temperatureTrigger(float temperatureValue)
{
   if(temperatureValue > 70)
  {
    Serial.println("temperature is too high");
  }
  else
  {
    Serial.println("temperature is at acceptable levels");
    Serial.println(temperatureValue);
  }
}


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();
  snprintf(dummyChar, length + 1, (char*)payload);
  float floatTemperature = atof(dummyChar);
  temperatureTrigger(floatTemperature);
}

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

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  ubidots.setDebug(true);  // uncomment this to make debug messages available
  ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
  ubidots.setCallback(callback);
  ubidots.setup();
  ubidots.reconnect();
  ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LABEL); // Insert the dataSource and Variable's Labels
 
}

void loop()
{
  // put your main code here, to run repeatedly:
  Serial.println("Checking connection");
  if (!ubidots.connected())
  {
    Serial.println("Device not connected. Trying to reconnect");
    ubidots.reconnect();
    Serial.println("Device connected");
    ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LABEL); // Insert the dataSource and Variable's Labels
  }
  Serial.println("Device is connected");
  ubidots.subscribeLastValue(DEVICE_LABEL, VARIABLE_LABEL); // Insert the dataSource and Variable's Labels
  ubidots.loop();
  delay(2000);
}

As a final comment, when you use

Serial.print(floatTemperature);

It will only print two decimal digits, if you want to print the full digits, you can use the length variable to do so.

Please reach back to us if you have any other question.

Best,
-Juan David-

thank you so much! But what if i want to subscribe to another value?

And i want that value to be a float aswell, and use it in my program.