Send string variable in context with Ubidots library and Arduino


#1

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


#2

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.


#3

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


#4

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?


#5

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.


#6

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


#7

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.


#8

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


#9

Hey @darkdragon,

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

Regards,
Maria C.