Compiling problem when connecting ESP32 to Azure Iot Central.

Abball 50 Reputation points
2024-04-11T01:11:13.0466667+00:00

Refer to "Connect ESP32 to Azure IoT Central" topic in https://learn.microsoft.com/en-us/answers/questions/1627220/connect-esp32-to-azure-iot-central , I followed the guidance, specifically adjusting the Azure IoT_PnP_Template.cpp file. As a result, I successfully compiled the code, and the outcome meets expectations.

Moving forward, I am interested in enhancing the functionality of Azure IoT_PnP_Template.cpp by enabling the transmission of parameters from the void loop(). This adjustment would greatly enhance usability. Below, I've outlined the changes I made to Azure IoT_PnP_Template.cpp and Azure_IoT_Central_ESP32.ino for your review:

Azure _IoT_PnP__Template.cpp (Please see 'double x2', 'double x3' and 'temperature' in Azure _IoT_PnP__Template.cpp)

static int generate_telemetry_payload(
  uint8_t* payload_buffer, size_t payload_buffer_size, size_t* payload_buffer_length, double x2);
static int generate_telemetry_payload(uint8_t* payload_buffer, size_t payload_buffer_size, size_t* payload_buffer_length, double x2)
{
  az_json_writer jw;
  az_result rc;
  az_span payload_buffer_span = az_span_create(payload_buffer, payload_buffer_size);
  az_span json_span;
  float temperature, humidity, uvIndex;
  int32_t visibleLight, infraredLight;
  // Acquiring data from sensors.
  temperature = x2;
  humidity = 30;
  
  uvIndex = 200;
  // The index is multiplied by 100 so to get the
  // integer index, divide by 100!
  uvIndex /= 100.0;  
  
  visibleLight = 31;
  infraredLight = 32;
  rc = az_json_writer_init(&jw, payload_buffer_span, NULL);
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed initializing json writer for telemetry.");
  rc = az_json_writer_append_begin_object(&jw);
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed setting telemetry json root.");
  rc = az_json_writer_append_property_name(&jw, AZ_SPAN_FROM_STR(TELEMETRY_PROP_NAME_TEMPERATURE));
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding temperature property name to telemetry payload.");
  rc = az_json_writer_append_double(&jw, temperature, DOUBLE_DECIMAL_PLACE_DIGITS);
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding temperature property value to telemetry payload. ");
  rc = az_json_writer_append_property_name(&jw, AZ_SPAN_FROM_STR(TELEMETRY_PROP_NAME_HUMIDITY));
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding humidity property name to telemetry payload.");
  rc = az_json_writer_append_double(&jw, humidity, DOUBLE_DECIMAL_PLACE_DIGITS);
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding humidity property value to telemetry payload. ");
  rc = az_json_writer_append_property_name(&jw, AZ_SPAN_FROM_STR(TELEMETRY_PROP_NAME_UV_INDEX));
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding uv index property name to telemetry payload.");
  rc = az_json_writer_append_double(&jw, uvIndex, DOUBLE_DECIMAL_PLACE_DIGITS);
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding uv index property value to telemetry payload.");
  rc = az_json_writer_append_property_name(&jw, AZ_SPAN_FROM_STR(TELEMETRY_PROP_NAME_VISIBLE_LIGHT));
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding visible light property name to telemetry payload.");
  rc = az_json_writer_append_int32(&jw, visibleLight);
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding visible light property value to telemetry payload.");
  rc = az_json_writer_append_property_name(&jw, AZ_SPAN_FROM_STR(TELEMETRY_PROP_NAME_INFRARED_LIGHT));
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding infrared light property name to telemetry payload.");
  rc = az_json_writer_append_int32(&jw, infraredLight);
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed adding infrared light property value to telemetry payload.");
  rc = az_json_writer_append_end_object(&jw);
  EXIT_IF_AZ_FAILED(rc, RESULT_ERROR, "Failed closing telemetry json payload.");
  payload_buffer_span = az_json_writer_get_bytes_used_in_destination(&jw);
  if ((payload_buffer_size - az_span_size(payload_buffer_span)) < 1)
  {
    LogError("Insufficient space for telemetry payload null terminator.");
    return RESULT_ERROR;
  }
  payload_buffer[az_span_size(payload_buffer_span)] = null_terminator;
  *payload_buffer_length = az_span_size(payload_buffer_span);
 
  return RESULT_OK;
}
int azure_pnp_send_telemetry(azure_iot_t* azure_iot, double x3)
{
  _az_PRECONDITION_NOT_NULL(azure_iot);
  time_t now = time(NULL);
  if (now == INDEFINITE_TIME)
  {
    LogError("Failed getting current time for controlling telemetry.");
    return RESULT_ERROR;
  }
  else if (last_telemetry_send_time == INDEFINITE_TIME ||
           difftime(now, last_telemetry_send_time) >= telemetry_frequency_in_seconds)
  {
    size_t payload_size;
    last_telemetry_send_time = now;
    if (generate_telemetry_payload(data_buffer, DATA_BUFFER_SIZE, &payload_size, x3) != RESULT_OK)
    {
      LogError("Failed generating telemetry payload.");
      return RESULT_ERROR;
    }
    if (azure_iot_send_telemetry(azure_iot, az_span_create(data_buffer, payload_size)) != 0)
    {
      LogError("Failed sending telemetry.");
      return RESULT_ERROR;
    }
  }
  return RESULT_OK;
}

Azure_IoT_Central_ESP32.ino (Please see '42' in Azure_IoT_Central_ESP32.ino)

void loop()
{
  
  if (WiFi.status() != WL_CONNECTED)
  {
    azure_iot_stop(&azure_iot);
    connect_to_wifi();
  }
  else
  {
    switch(azure_iot_get_status(&azure_iot))
    {
      case azure_iot_connected:
        if (send_device_info)
        {
          (void)azure_pnp_send_device_info(&azure_iot, properties_request_id++);
           send_device_info = false; // Only need to send once.
        }
        else if (azure_pnp_send_telemetry(&azure_iot, 42) != 0)
        {
          LogError("Failed sending telemetry.");          
        }
        break;
      case azure_iot_error:
        LogError("Azure IoT client is in error state." );
        azure_iot_stop(&azure_iot);
        break;
      case azure_iot_disconnected:
        azure_iot_start(&azure_iot);
        break;
      default:
        break;
    }
    azure_iot_do_work(&azure_iot);
  }
}

After compiling, I encountered an error message stating "too many arguments to function 'int azure_pnp_send_telemetry(azure_iot_t*)". Despite my efforts to resolve this issue, I have been unable to find a solution. I'm still developing my coding skills. I would greatly appreciate your help and support. Thank you for assistance.

Azure IoT Central
Azure IoT Central
An Azure hosted internet of things (IoT) application platform.
347 questions
0 comments No comments
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 13,471 Reputation points
    2024-04-11T20:56:06.5966667+00:00

    Hi @Abball Thank you for posting the question here.

    The code linking is relied based on the included header file references. In this case, the compiler executes Azure_IoT_Central_ESP32.ino as the main program and looks for reference functions based on the definitions included in the header files. For this specific azure_pnp_send_telemetry function, the definition for reference has been defined in the line 70 of file. This lets the complier look for a function with the appropriate arguments.

    Please make sure to modify the method signature in the following code Azure_IoT_PnP_Template.h too to resolve this error.

    Hope this helps! Please let us know if you still encounter any issues.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.


0 additional answers

Sort by: Most helpful