0

I have a MQTT device that connects to Central and submits telemetry. I have created some commands on Central that trigger successfully on the device. However, when I send string value via command, I am having troubles finding the right command to store that value in a variable for further use in the code. I can display the content of the value as follows:

LogInfo("URL: %*s", az_span_size(command.payload)-2, az_span_ptr(command.payload) + 1);

This results in:

[INFO] URL: https://raw.githubusercontent.com/testurl"

However I also have an issue when I send a different command (say version) and display it in the same way, I get:

[INFO] Firmware Version: 1.0.1"thubusercontent.com/testurl"

So I guess, I have 2 questions:

  1. How do I store the value in the variable in C++ (Arduino); and
  2. How do I clear buffer so the next command content is not tainted by the previous value?

It would be great if I could use az_ command set as this is all happening in SDK C context.

Many thanks

Tried using az_span_to_string and it crashes. Cannot see any other direcgtly relevant az_ function taht I could use.

Here is the code being used:

if (az_span_is_content_equal(command.command_name, COMMAND_NAME_OTA_URL))
  {
    LogInfo("URL: %*s", az_span_size(command.payload)-2, az_span_ptr(command.payload) + 1);
    response_code = COMMAND_RESPONSE_CODE_ACCEPTED;
  }
  else if (az_span_is_content_equal(command.command_name, COMMAND_NAME_TOGGLE_C12880))
  {
    SEN_C12880 = !SEN_C12880;
    LogInfo("Sensor State: %s", (SEN_C12880 ? "ON" : "OFF"));
    response_code = COMMAND_RESPONSE_CODE_ACCEPTED;
  }
  else if (az_span_is_content_equal(command.command_name, COMMAND_NAME_TOGGLE_FIRMWARE_VERSION))
  {
      LogInfo("Firmware Version: %*s", az_span_size(command.payload)-2, az_span_ptr(command.payload) + 1);
      response_code = COMMAND_RESPONSE_CODE_ACCEPTED;
  }
  else if (az_span_is_content_equal(command.command_name, COMMAND_NAME_DISPLAY_TEXT))
  {
    // The payload comes surrounded by quotes, so to remove them we offset the payload by 1 and its size by 2.
    LogInfo("OLED display: %.*s", az_span_size(command.payload) - 2, az_span_ptr(command.payload) + 1);
    response_code = COMMAND_RESPONSE_CODE_ACCEPTED;
  }
  else
  {
    LogError("Command not recognized (%.*s).", az_span_size(command.command_name), az_span_ptr(command.command_name));
    response_code = COMMAND_RESPONSE_CODE_REJECTED;
  }
  • Please provide your code in related to your question. Instead of using the image, you could simply described that it received a "400: Invalid request". – hcheung Apr 28 '23 at 01:48
  • Your error is "400: Invalid request" which means whatever you send to the server is invalid. But your questions have nothing to related in dealing with the error. Anyway, To your question 1, If you want to keep a copy of the `command.payload` which is presumed as a string or char array, you can create a buffer and copy the data into it, see [strcpy()](https://cplusplus.com/reference/cstring/strcpy/). To your question 2: you don't need to clear the buffer, as char array is NULL-terminated. So whatever you copy into the buffer is terminated at the end with NULL. – hcheung Apr 28 '23 at 02:01
  • Many thanks...I have made progress and resolved it with sprintf using %*s format Will post solution below. – codehampster Apr 28 '23 at 03:08

1 Answers1

0

So...after a bit of digging and good ol' trial 'n' error...it really comes to the following:

Defne variables:

char cmd_payload[1024];
char cmd_name[128];

In the Azure_IoT_PnP_Template.cpp:

int azure_pnp_handle_command_request(azure_iot_t* azure_iot, command_request_t command)
{
  _az_PRECONDITION_NOT_NULL(azure_iot);

  uint16_t response_code;

  sprintf(cmd_name, "%*s",az_span_size(command.command_name), az_span_ptr(command.command_name));
  String str_cmd_name = String(cmd_name, az_span_size(command.command_name));
  strcpy(cmd_name, str_cmd_name.c_str());

  The rest is history!!!