Reputation: 1
I am using IoT Central and am able to successfully send command to the device as well as receive confirmation code. This is executed using the following line:
azure_iot_send_command_response(azure_iot, command.request_id, response_code, AZ_SPAN_EMPTY);
AZ_SPAN_EMPTY is the payload portion that can be replaces with the JSON formatted response.
Most other actions have sub-routines defined to generate such response (such as: generate_telemetry_payload, generate_device_info_payload, generate_properties_update_response...etc).
I cannot see out of the box generate_command_response.
Am I missing something, can I reuse one of the existing routines or do I have to create my own? What is the common way of passing back payload on command execution?
Tried using functions to generate az_span_from_string etc but they do not generate the whole payload.
Tried formattign it using
static const az_span cmd = AZ_SPAN_LITERAL_FROM_STR("Hello world");
but the error is always:
Device XXXXXXX sent a response payload with a syntax error for method XXXXXXXXX in application XXXXXXX Error code: 400.070.002.052 / s6p8v199hr.2
The annoying part is that I cannot see the raw data to be able to figure out where the syntax problem is...hence my desire to use az_span standard method.
Upvotes: 0
Views: 183
Reputation: 1308
IoT Central Azure with C
I Referred this MSDOC for IoT Hub and for IoT central referred this link
void myCommandCallback(IOTContextHandle context_handle, const char* component_name, const char* command_name, const char* payload, size_t payload_len) {
printf("Command received: %s\n", command_name);
IoTCCommandResponseHandle response_handle = IoTCCommandResponse_create(component_name, command_name);
IoTCCommandResponse_setStatus(response_handle, 200);
IoTCCommandResponse_setMessage(response_handle, "Command executed successfully");
IoTCCommandResponse_send(response_handle);
IoTCCommandResponse_destroy(response_handle);
}
int main() {
IoTCInit();
IoTCDeviceCreateInfo device_info;
device_info.deviceId = "my-device-id";
device_info.scopeId = "my-scope-id";
device_info.deviceKey = "my-device-key";
device_info.primaryThumbprint = NULL;
device_info.secondaryThumbprint = NULL;
IoTCDeviceClientHandle device_client = IoTCDeviceClient_create(&device_info);
IoTCDeviceClient_connect(device_client);
IoTCCommandRequestCallback myCommandRequestCallback = {
.callback = myCommandCallback
};
IoTCDeviceClient_registerCommandRequestCallback(myCommandRequestCallback);
while (1)
{
IoTCDeviceClient_doWork(device_client);
IoTCSleep(10);
}
IoTCDeviceClient_destroy(device_client);
return 0;
}
Upvotes: 0