Help add Variable

Help with Sending Time to Variable

I am using this function but the ESP32 reboots and does not upload value

ubidots.add(VARIABLE_LABEL, value, nullptr, <timestamp_segundos>);

Hola Lusvin,

Respondiéndote por el canal de soporte. En lugar de nullptr vamos a poner “NULL”, así entre comillas. Además, en lugar de enviar el timestamp en milisegundos, vamos a hacerlo en segundos, por lo que elimina los últimos 3 ceros del timestamp que acabas de poner.

Quedaría algo como:

ubidots.add(VARIABLE_LABEL, value, “NULL”, <timestamp_segundos>);

Alejandro

Greetings @metrikamonitoreo,

I hope you are doing great.

We have identified an issue with our ESP32 library, specifically within the class constructor. This error prevents the class from being instantiated as a global object, contrary to the examples provided.

As a temporary workaround, we propose a solution utilizing pointers to bypass the issue. Given the advanced nature of this solution, which requires a strong understanding of C, we are cautious about recommending it as a permanent fix. We are actively working on a more robust solution. In the meantime, here is the pointer-based approach:

The following sketch demonstrates how to retrieve and send values to and from Ubidots (without context):

#include <Arduino.h>
#include <Ubidots.h>

/****************************************
 * Define Constants
 ****************************************/
const char* UBIDOTS_TOKEN = "BBFF-ij2eOkYbTCrdrk6c7fdAxvpK7CTtVy";
const char* WIFI_SSID = "UbiGuests";                             
const char* WIFI_PASS = "clave123456789ubi";
const char* DEVICE_LABEL = "3c6105657e84";  
const char* VARIABLE_LABEL = "random-var";       

/****************************************
 * Functions declaration 
 ****************************************/
// Function to retrieve a value from Ubidots
float getValue();
// Function to send a value to Ubidots
bool sendValue();

// Create a pointer to a instance of the Ubidots class to use it globally
Ubidots* ubidots{nullptr};

// Setup function
void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  ubidots = new Ubidots(UBIDOTS_TOKEN, UBI_HTTP);
  ubidots->setDebug(true); //Comment or set to false to disable debug messages
}

// Loop function
void loop() {
  Serial.println("Loop function");
  sendValue();
  delay(5000);
}

// This function sends a random value to Ubidots 
bool sendValue() {
   // replace this by the actual value that you want to send
  float value = random(0, 100) * 100; 
  // Add the variable 
  ubidots->add(VARIABLE_LABEL, value);
  // Send the value to Ubidots
  if (ubidots->send()) {
    Serial.println("Value sent to Ubidots");
    return true;
  }
  Serial.println("Error sending value to Ubidots");
  return false;
} 

float getValue() {
  float value = ubidots->get(DEVICE_LABEL, VARIABLE_LABEL);
  if (value != ERROR_VALUE) {
    Serial.println("Value: " + String(value));
    return value;
  }
  Serial.println("Error retrieving variable: " + String(VARIABLE_LABEL));
  return ERROR_VALUE;
}

In the suggestion above, instead of declaring an instance of the Ubidots class directly (as the examples do), we use a pointer to an instance of the Ubidots class.

This change allows us to instantiate the Ubidots object dynamically using the new operator in the setup function. This approach ensures that the ubidots object remains valid beyond the setup function, addressing the issue of not being able to instantiate the Ubidots class as a global object.

Note for example that in order to enable debugging, you should use:

ubidots->setDebug(true);

Instead of

ubidots.setDebug(true);

Here is a summary about what you need to know regarding pointers:

  • Accessing Methods: When you use a pointer to an object, you must use the arrow -> operator to call its methods. For instance, to enable debugging, you should write:

Sending values with context:

The code snippet below works exactly as the one above, but this one allows you to send values in the context of the variable, specifically, it declares a counter variable and sends it in the context:

#include <Arduino.h>
#include <Ubidots.h>

/****************************************
 * Define Constants
 ****************************************/
const char* UBIDOTS_TOKEN = "BBFF-ij2eOkYbTCrdrk6c7fdAxvpK7CTtVy";
const char* WIFI_SSID = "UbiGuests";                             
const char* WIFI_PASS = "clave123456789ubi";
const char* DEVICE_LABEL = "3c6105657e84";  
const char* VARIABLE_LABEL = "random-var";       

/****************************************
 * Functions declaration 
 ****************************************/
// Function to retrieve a value from Ubidots
float getValue();
// Function to send a value with context to Ubidots
bool sendValue();
// Function to parse the uint32_t value to a char array
void uint32ToChar(uint32_t value, char* buffer, size_t bufferSize);


/****************************************
 * Variables declaration 
 ****************************************/
uint32_t counter {0};
char* context = (char*)malloc(sizeof(char) * 100);
char counterStrBuffer[10];

// Create a pointer to a instance of the Ubidots class to use it globally
Ubidots* ubidots{nullptr};


// Setup function
void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  ubidots = new Ubidots(UBIDOTS_TOKEN, UBI_HTTP);
  ubidots->setDebug(true); //Uncomment this line for printing debug messages
}

// Loop function
void loop() {
  Serial.println("Loop function");
  sendValue();
  counter++;
  delay(5000);
}

// This function sends a random value to Ubidots with the counter variable on its context
bool sendValue() {

  // Format the counter variable as a string
  uint32ToChar(counter, counterStrBuffer, sizeof(counterStrBuffer));
  // Add the counter variable to the context
  ubidots->addContext("counter", counterStrBuffer);
  ubidots->getContext(context);
  // Add the variable and its context to Ubidots
  ubidots->add(VARIABLE_LABEL, random(0, 100) * 100, context);
  // Send the value to Ubidots
  if (ubidots->send()) {
    Serial.println("Value sent to Ubidots");
    return true;
  }
  Serial.println("Error sending value to Ubidots");
  return false;
} 

float getValue() {
  float value = ubidots->get(DEVICE_LABEL, VARIABLE_LABEL);
  if (value != ERROR_VALUE) {
    Serial.println("Value: " + String(value));
    return value;
  }
  Serial.println("Error retrieving variable: " + String(VARIABLE_LABEL));
  return ERROR_VALUE;
}

void uint32ToChar(uint32_t value, char* buffer, size_t bufferSize) {
    // Format the uint32_t value as a string and store it in buffer
    snprintf(buffer, bufferSize, "%u", value);
}```