Search code examples
azureazure-iot-hubarduino-esp8266azure-iot-sdk

AzureIoTHub: How to send telemetry data in JSON format?


I used the example repo of Azure für sending telemetry data: https://github.com/Azure/azure-iot-arduino/tree/master/examples/esp8266/iothub_ll_telemetry_sample

I did not modify any code. This is the important part:

const char* telemetry_msg = "test_message";
message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
result = IoTHubDeviceClient_LL_SendEventAsync(device_ll_handle, message_handle, send_confirm_callback, NULL);

Why does my body-result looks like an ASCII array? (Note: Using Azure IoT explorer)

Is it meant to be an array like this. Do I have to unpack this array on the other side?

enter image description here

I want to be able to send the data in JSON format like the azure device simulator does:

enter image description here


Solution

  • Nevermind, figured it out. The following example will send data in JSON format:

    char telemetry_msg_buffer[80];
    sprintf(telemetry_msg_buffer, "{\"temperature\":11.11,\"humidity\":12.12,\"scale\":\"13.13\"}");
    message_handle = IoTHubMessage_CreateFromString(telemetry_msg_buffer);