Esp32 arduino ide sending gps data

Greetings from Colombia!

I have trying to send the data for the actual position of my esp32 device to the Ubidots platform, but I could not do it from the Arduino IDE. When the device is running, the platform doesn’t recognize the variable or the position.

PD: Sorry for my bad English.

This is the code that I have used:

#include "UbidotsEsp32Mqtt.h"

const char *UBIDOTS_TOKEN = "****************************";  
const char *WIFI_SSID = "********"; 
const char *WIFI_PASS = "*******";   
const char *DEVICE_LABEL = "MyEsp32";
const char *VARIABLE_LABEL = "gps";

const int PUBLISH_FREQUENCY = 5000;

unsigned long timer;

float latitud = 1.23164;
float longitud = -77.29426;

char context[25];

int gps = 1;

Ubidots ubidots(UBIDOTS_TOKEN);

/****************************************
 * Auxiliar Functions
 ****************************************/
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();
}

/****************************************
 * Main Functions
 ****************************************/
void setup()
{
  Serial.begin(115200);
  ubidots.setDebug(true);
  ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
  ubidots.setCallback(callback);
  ubidots.setup();
  ubidots.reconnect();

  timer = millis();
}

void loop()
{
  if (!ubidots.connected())
  {
    ubidots.reconnect();
  }
  if (abs(millis() - timer) > PUBLISH_FREQUENCY) // triggers the routine every 5 seconds
  {
    sprintf(context, "lat=%.2f$lng=%.2f", latitud, longitud);
    ubidots.add(VARIABLE_LABEL, 1, context); 
    ubidots.publish(DEVICE_LABEL);
    Serial.println(context);
    timer = millis();
  }
  ubidots.loop();
}

Hello @martinalejandro93,

Find below the loop function that uses the addContext and getContext methods of the library to easily build and send context over MQTT through the ESP32

void loop()
{
  if (!ubidots.connected())
  {
    ubidots.reconnect();
  }
  if (abs(millis() - timer) > PUBLISH_FREQUENCY) // triggers the routine every 5 seconds
  {
    /* Reserves memory to store context key values, add as much as you need */
    char* str_lat = (char*)malloc(sizeof(char) * 10);
    char* str_lng = (char*)malloc(sizeof(char) * 10);

    /* Saves the coordinates as char */
    sprintf(str_lat, "%f", latitud);
    sprintf(str_lng, "%f", longitud);

    /* Reserves memory to store context array */
    char* context = (char*)malloc(sizeof(char) * 30);

    /* Adds context key-value pairs */
    ubidots.addContext("lat", str_lat);
    ubidots.addContext("lng", str_lng);

    /* Builds the context with the coordinates to send to Ubidots */
    ubidots.getContext(context);

    ubidots.add(VARIABLE_LABEL, 1, context); 
    ubidots.publish(DEVICE_LABEL);
    timer = millis();

    /* free memory */
    free(str_lat);
    free(str_lng);
    free(context);
  }
  ubidots.loop();
}

For your reference, I took as a guide the example found here for the ESP8266 that basically has the same methods to build and send context.

Best,

–David

Hi @martinalejandro93,

Were you able to test this code?

Furthermore, we detect a bug in the ESP32 MQTT library, specifically in the getContext method so if you tested the example above you probably were getting a bad JSON payload.
We already fix this bug, you can find the PR here.

With this in mind, can you download the library again so you have the latest updates. Once downloaded, simply the test the code, it should work now as expected.

Best,

–David