The Azure SDK for C Arduino ESP2866 sample and consequently the port here of that to the RPi Pico w contains a basic Telemetry sample where the payload is just an incremented count and is formed by “manual” creation of the payload json string. Telemtry here has been formalised with the help of the ArduinoJson library. Telemetry properties are also presented here.

TelemetryPayloads

The manual “basic” Payload Json creation

static char* getTelemetryPayload()
{
  az_span temp_span = az_span_create(telemetry_payload, sizeof(telemetry_payload));
  temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR("{ \"msgCount\": "));
  az_result res = az_span_u32toa(temp_span, telemetry_send_count++, &temp_span);
  temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR(" }"));
  temp_span = az_span_copy_u8(temp_span, '\0');

  return (char*)telemetry_payload;
}

Using ArduinoJson

#include <ArduinoJson.h>
DynamicJsonDocument doc(1024);
char jsonStr[64];

static char* getTelemetryPayload()
{
  int chk = DHT.read11(dhtPin);
  if (chk == DHTLIB_OK) {
    doc["msgCount"]   = telemetry_send_count ++;
    doc["temp"]   = DHT.temperature;
    doc["humidity"]   = DHT.humidity;
    serializeJson(doc, jsonStr);
    az_span temp_span = az_span_create_from_str(jsonStr);
    az_span_to_str((char *)telemetry_payload, sizeof(telemetry_payload), temp_span);
  }
  else
    telemetry_payload[0] = 0;
  return (char*)telemetry_payload;
}

The Telemetry Payload is called as part of the Telemetry send:

  if (az_result_failed(az_iot_hub_client_telemetry_get_publish_topic(
          &client, NULL, telemetry_topic, sizeof(telemetry_topic), NULL)))
  {
    Serial.println("Failed az_iot_hub_client_telemetry_get_publish_topic");
    return;
  }
  char *   payload = getTelemetryPayload();
  mqtt_client.publish(telemetry_topic, payload, false);

This provides a more generic approach for generating the payload Json string and can be simply extended to add or remove telemetry name value pairs.

Telemetry Options

The Azure IoT Hub Arduino Raspberry Pi Pico with Telemetry Repository repository contains a number of real telemetry source options:

Telemetry Properties

A Telemetry message from the device to the hub can also contain properties. For example, it might signal a warning. The Light Dependant Resistor with CDN contains a simple property with the LDR Telemetry:

///
// Add a (sample) property to telemetry message
///

az_iot_message_properties * GetProperties(int value)
{
  uint32_t msgLength;
  az_result az_result;
  if (value>100)
  {
    msgLength = (uint32_t)strlen(NO_WARNING);
    az_span string = AZ_SPAN_LITERAL_FROM_STR(NO_WARNING);
    uint8_t a[64];
    az_span s = AZ_SPAN_FROM_BUFFER(a);
    az_span_copy(s, string);
    az_result = az_iot_message_properties_init(&properties, s, msgLength);
  }
  else
  {
    msgLength = (uint32_t)strlen(WARNING);
    az_span string = AZ_SPAN_LITERAL_FROM_STR(WARNING);
    uint8_t a[64];
    az_span s = AZ_SPAN_FROM_BUFFER(a);
    az_span_copy(s, string);
    az_result = az_iot_message_properties_init(&properties, s, msgLength);
  }
  if(az_result = AZ_OK )
    return &properties;
  else
    return NULL;
} 

Then add the property to the telemetry:

  int telemetryValue;
  char *   payload = getTelemetryPayload("LightIntensity", &telemetryValue);
  
  // Add a property to the message  
  az_iot_message_properties * properties = GetProperties(telemetryValue);
  
  if (az_result_failed(az_iot_hub_client_telemetry_get_publish_topic(
          &client, properties, telemetry_topic, sizeof(telemetry_topic), NULL)))
  {
    Serial.println("Failed az_iot_hub_client_telemetry_get_publish_topic");
    return;
  }
  mqtt_client.publish(telemetry_topic, payload, false);

Apps

The MonitorTelemetry console app can both display the Telemetry Payload sent to the Hub from the Device as well as optionally display injected properties as well as system properties.


 TopicSubtopic
  Next: > Jekyll
   
 This Category Links 
Category:Pico W AzSDK Index:Pico W AzSDK
  Next: > RPI-Pico-Arduino-AzSDK
<  Prev:   RPI-Pico-Arduino-AzSDK