Search code examples
curlarduinoarduino-yun

Calling curl command using byte buffer in Arduino


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?


Solution

  • 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.