[SOLVED] Send string variable in context with Ubidots library and Arduino

Hi from Italy.
I use Ubidots library with my Arduino and I can send successfully float values to my dashboard.
But I have troubles with dynamic string variables (I receive a Json double quote(?) error).
For example:

char myGlobalArray[10];
ReadDeviceStatus(); // this function update a global char array
client.add(VARIABLE_LABEL_1, 2, myGlobalArray);
client.sendAll();

I had to format text in some way with sprintf?
Thanks.
Marco

Dear @darkdragon,

As first, can you specify is which is the library used by you? Thanks!

Reviewing the portion of code provided, I note that you are trying to send a char array as context of a variable, right? For this you can reference to this sample code where show how to build the context using the sprintf method.

All the best,
Maria C.

I use Ubidots-Arduino-Ethernet library (as suggested in your article “Connect the Arduino UNO with Ubidots using the Arduino Ethernet Shield”).
Now it seems that I have other problems with timers in my sketch…I’ll do other tests and I will update you as soon as possible (I’ll paste my real sketch).
Thank you so much.
Marco

Well, I’m going crazy :frowning:
Following the examples I have tried this:

char contextProg[20];
sprintf(contextProg, "\"program\":%s", mydataRec.program);
client.add(VARIABLE_LABEL_7,2, mydataRec.program);

note: mydataRec.program is a char[] variable

Not works (JSON parse error - Expecting property name enclosed in double quotes)

Can you help me?

Dear @darkdragon,

You are trying to send mydataRec.program as context of a variable, when the real context should be the char built with the sprintf method which is contextProg. Also, I recommend you print the variable contextProg after built it in order to check if was built properly. You should have something like below:

  char contextProg[20];
  sprintf(contextProg, "\"program\":%s", mydataRec.program);
  Serial.println(contextProg);
  client.add(VARIABLE_LABEL_7,2, contextProg);
  client.sendAll();

Just to give you a better idea, take as reference the code below:

  /* Sends latitude and longitude for watching position in a map */
  char context[25];
  sprintf(context, "\"lat\":1.2343, \"lng\":132.1233");
  client.add(VARIABLE_LABEL, 1, context);
  client.sendAll();

I hope this would help you!

All the best,
Maria C.

Sorry, it was my mistake!
But I have noticed an issue(?):
this code works:

char contextProg[10];
char test[10] = "1.2345";
sprintf(contextProg, "\"program\":%s", test);
client.add(VARIABLE_LABEL_7,2, contextProg);
client.sendAll();

this code NOT works:

char contextProg[10];
char test[10] = "hello";
sprintf(contextProg, "\"program\":%s", test);
client.add(VARIABLE_LABEL_7,2, contextProg);
client.sendAll();

{"detail": "JSON parse error - No JSON object could be decoded"}

The question is: does the parser require a KEY/ VALUE pair with value convertible in number?
Thank you
Marco

To be able to send an ASCII string as context you need to assigned the string with double quotes. Having as result something like below:

char contextProg[10];
char test[10] =  "\"hello\"";
sprintf(contextProg, "\"program\":%s", test);
client.add(VARIABLE_LABEL_7,2, contextProg);
client.sendAll();

I hope this would help you!

All the best,
Maria C.

Hi Maria
Now seems to work!
I have issue with transmission of multiple context, but I think it’s due to a memory lack (I trust I can solve it).
Thank you very much.
I’ll keep you updated.
Marco

Hey @darkdragon,

I’m glad to read that! Good luck with your IoT project! :smiley:

Regards,
Maria C.

Hello Maria,
Since I could change this code with floating variables, I need my GPS module to obtain latitude and longitude and those coordinates are the ones that I assign to my conductivity variable.

Thanks in advance.

/* Sends latitude and longitude for watching position in a map */
char context[25];
sprintf(context, ““lat”:1.2343, “lng”:132.1233”);
client.add(VARIABLE_LABEL, 1, context);
client.sendAll();

Greetings, have you tried to look into the official docs examples? You can find there several examples to send context in C, python and bash.

All the best

I’m using the example “UbidotsSaveValuesWithContext from the library” UbidotsEthernet.h "

/********************************
 * Libraries included
 *******************************/
#include <Ethernet.h>
#include <SPI.h>
#include <UbidotsEthernet.h>

/********************************
 * Constants and objects
 *******************************/
/* Assigns the Ubidots parameters */
char const * TOKEN = "Assign_your_Ubidots_TOKEN_here"; // Assign your Ubidots TOKEN
char const * VARIABLE_LABEL = "position"; // Assign the unique variable label to send the data

/* Enter a MAC address for your controller below */
/* Newer Ethernet shields have a MAC address printed on a sticker on the shield */
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

/* initialize the instance */
Ubidots client(TOKEN);

/********************************
 * Main Functions
 *******************************/
void setup() {
  Serial.begin(9600);
  //client.setDebug(true);// uncomment this line to visualize the debug message
  /* start the Ethernet connection */
  Serial.print(F("Starting ethernet..."));
  if (!Ethernet.begin(mac)) {
    Serial.println(F("failed"));
  } else {
    Serial.println(Ethernet.localIP());
  }
  /* Give the Ethernet shield a second to initialize */
  delay(2000);
  Serial.println(F("Ready"));
}

void loop() {
  Ethernet.maintain();
  /* Sends latitude and longitude for watching position in a map */
  char context[25];
  **_sprintf(context, "\"lat\":1.2343, \"lng\":132.1233");_**
  client.add(VARIABLE_LABEL, 1, context);
  client.sendAll();
  delay(5000);
}

I need to be able to enter the latitude and longitude measured by the GPS.

Greetings, you need to read the coordinates from your module (usually float type variables) and cast them to char type to be added into the context. You can reference some examples in the link to our docs posted in my previous reply.

All the best

Thanks, it works.