Writing the Ubidots command with data retrieved from serial port. (Arduino+ESP8266


#1

When writing a payload to add data to the cloud via arduino+ESP8266 we append the command message with sprintf

 sprintf(payload, "{\"");
sprintf(payload, "%s%s\":%s", payload, VAR_LABEL1, A1);
sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL2, A2);
sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL3, A3);
sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL4, A4);
sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL5, A5);
sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL6, A6);
sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL6, P);
sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL6, T);
sprintf(payload, "%s}", payload);

Variables Ai, P and T are String variables retrieved from other serial port connected to the device.

 if (flow_control) {
Serial3.print("<PE?>");
flow_control = false;
  }
  delay(100);
  if (Serial3.available()) {
while (Serial3.available() > 0) {
  char inChar = Serial3.read();
  Com += inChar;
  flow_control = true;
}
String P = Com.substring(4, 10);

When writing the variable P in the payload an error arises in the arduino’s compilation:

cannot pass objects of non-trivially-copyable type ‘class String’ through ‘…’

Do anyone know how the data must be written in the sprintf function?

Thanks.


#2

Hello @LosPinosGT,

Based on my knowledge, you should pass the string as a char. In this link you will find an example of how to handle the sprintf() method using charts.

I hope this would help you!

All the best,
Maria C


#3

@mariahernandez hi

I tried with a charbut the built payload is:

it is supposed to upload a "0"

Any other ideas??


#4

Dear user,

If I’m not wrong you are using the Ubidots ESP8266 Serial library, don’t you?

If you are using this library the issue is presented in the structure of the command. You are trying to send a variable called A1 in a JSON format meanwhile the library uses another format to handle the messages. Please refer to this link to learn how to build the command properly.

To give you a better idea you command should look like:

Command structure

init#USER_AGENT/VERSION|POST|TOKEN|DEVICE_LABEL=>VARIABLE:VALUE|end#final

Command example

init#smartgemini/1.0|POST|BBFF-xxxxxxxxxx|gemini1060smart=>a1:0|end#final

I hope this would help you!

All the best,
Maria C.


#5

Thanks for your answer, but i guess I’m not clear enough.

The problem is that I read data trough serial port with a device in this case temperature:

if (flow_control) {
        Serial3.print("<TE?>");
        flow_control = false;
      }
      delay(100);
      while (Serial3.available() > 0) {
        char inChar = Serial3.read();
        Com += inChar;
        flow_control = true;
      }
      if (flow_control) {
        String T = Com.substring(4, 6);
        Serial.println(Com);
        Serial.println(T);
        Com = "";
      }
    }

The serial monitor shows the string answered by the device (<TE=39>) and the data parsed from that string (39 [highlighted]). That string is parsed as a String. (I already test it parsing it as a char)

When using that variable T in the payload,

    sprintf(payload, "{\"");
    sprintf(payload, "%s%s\":%s", payload, VAR_LABEL1, A1);
    sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL7, P);
    sprintf(payload, "%s,\"%s\":%s", payload, VAR_LABEL8, T);
    sprintf(payload, "%s}", payload);

And adding it into the command:

sprintf(command, "init#");
sprintf(command, "%s%s/%s|%s|%s|", command, USER_AGENT, VERSION, METHOD, TOKEN);
sprintf(command, "%s%s=>", command, DEVICE_LABEL);
//sprintf(command, "%s%s:%s", command, VARIABLE_LABEL1, str_sensor1);
sprintf(command, "%s%s\r\n", command, payload);
sprintf(command, "%s|end#final", command);

The variable appears either null or with gibberish.

Hope you can help me.


#6

I built two examples for you using charts, take them as a reference to build your code! I already test them and are assigning the values properly! :smiley:

payload_char_malloc.ino (1.4 KB)

payload_char.ino (975 Bytes)

I hope this would help you!

All the best,
Maria C.


#7

YES!!!

You did it!

Thanks a lot.


#8

you should avoid concatenating different variables types, a char variable is different from a string type variable (which is in fact, a C++ object and not a primitive data type). I advise you to use a char array and to avoid the use of Strings as this kind of data uses the heap memory and this one should be available as much as possible for processing purposes.

All the best.