0

I'm currently working on Arduino devices and tying to use "process library" to call my REST API. Here's my code snippet.

void api_send_cell(uint8_t* data)
{
    Process p;    
    p.begin("curl");
    p.addParameter("-X Post");
    p.addParameter("-H content-type: multipart/form-data");
    p.addParameter("-k " + url);
    p.addParameter("-F data=");
    p.addParameter(buf);
    p.run();
}

But the thing is that my data (uin8_t buffer) is a series of raw data which are just numbers from 0 to 255. Because the process needs string for parameter just like real command, I couldn't just put my data to addParamter function.

So, I think I have to convert those bytes to string representation (for example, hex string) somehow.

Is there any solution for this problem?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
goofcode
  • 23
  • 1
  • 6
  • do you have a Yun board? this is not the way to do network communication in Atmega of Arduino Yun. use the Bridge library. – Juraj Nov 23 '18 at 09:01
  • You can improve your question by specifying the format of the string you need. Should the numbers be separated by spaces, commas, tabs, or something else? – jfowkes Nov 23 '18 at 12:08
  • @Juraj I'm using dragino LG01, and I thought it is ok to use process library because there are some example codes using that library. – goofcode Nov 23 '18 at 17:22

1 Answers1

0

You need to use sprintf to convert your uint8_t data to a string:

char string_data[LENGTH]; // LENGTH = Whatever length is sufficient to hold all your data
int i=0;
int index = 0;
//NUMBER_OF_ITEMS_OF_DATA could be constant or another variable
for (i=0; i<NUMBER_OF_ITEMS_OF_DATA; i++)
{
   index += sprintf(&string_data[index], "%d,", data[i]);
}
p.addParameter(string_data);

This will convert an array like {1,2,3,4,5} into the string "1,2,3,4,5,".

You can change the "%d," in the sprintf call to get another format. You may also want to remove the trailing , depending on your requirements.

jfowkes
  • 1,495
  • 9
  • 17
  • Thank you for answering. Well, this is good enough. What i was trying to do was sending raw image data which uses a byte for a pixel. I was wondering if there is any way to send byte array itself, because it is lot easier to handle on server side and doesn't require any parsing process. – goofcode Nov 23 '18 at 17:28
  • Yeah, if you have zeros in your data you'll need to encode them somehow because HTTP is text-based and will interpret actual zero-bytes (0x00) as NULL. I'm not qualified to say what happens next, but it likely won't be what you intend. – jfowkes Nov 25 '18 at 22:09
  • Great, I decided to use hex because Arduino has String data type that gets byte and convert it to hex string. Thank you by the way. – goofcode Nov 26 '18 at 02:38