1

I'm trying to add char* variable which contains date to the Azure telemetry payload. But error is occurring.

az_span temp_span = az_span_create(telemetry_payload, sizeof(telemetry_payload));
temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR("{ \"Humidity\": "));
  (void)az_span_u32toa(temp_span, humidity, &temp_span);  
  
  temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR(", \n \"Temperature\": "));
  (void)az_span_u32toa(temp_span, temperature, &temp_span);  

  temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR(", \n \"LocalTime\": "));

  temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR( (uint8_t)getCurrentLocalTimeString() ));
  //Error from above line

  temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR(" }"));
  temp_span = az_span_copy_u8(temp_span, '\0');

getCurrentLocalTimeString function definition is here

static char* getCurrentLocalTimeString()
{
  time_t now = time(NULL);
  return ctime(&now);
}

Error

In file included from C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_base64.h:20,
                 from C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_core.h:18,
                 from G:\AZURE Messaging IoT\Azure_IoT_Hub_ESP8266\Azure_IoT_Hub_ESP8266.ino:40:
G:\AZURE Messaging IoT\Azure_IoT_Hub_ESP8266\Azure_IoT_Hub_ESP8266.ino: In function 'char* getTelemetryPayload()':
Azure_IoT_Hub_ESP8266:335:57: error: cast from 'char*' to 'uint8_t' {aka 'unsigned char'} loses precision [-fpermissive]
  335 |   temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR( (uint8_t)getCurrentLocalTimeString() ));
      |                                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_span.h:108:25: note: in definition of macro 'AZ_SPAN_LITERAL_FROM_STR'
  108 |       .ptr = (uint8_t*)(STRING_LITERAL),              \
      |                         ^~~~~~~~~~~~~~
G:\AZURE Messaging IoT\Azure_IoT_Hub_ESP8266\Azure_IoT_Hub_ESP8266.ino:335:39: note: in expansion of macro 'AZ_SPAN_FROM_STR'
  335 |   temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR( (uint8_t)getCurrentLocalTimeString() ));
      |                                       ^~~~~~~~~~~~~~~~
Azure_IoT_Hub_ESP8266:335:57: error: cast from 'char*' to 'uint8_t' {aka 'unsigned char'} loses precision [-fpermissive]
  335 |   temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR( (uint8_t)getCurrentLocalTimeString() ));
      |                                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_span.h:93:43: note: in definition of macro '_az_STRING_LITERAL_LEN'
   93 | #define _az_STRING_LITERAL_LEN(S) (sizeof(S "") - 1)
      |                                           ^
C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_span.h:125:52: note: in expansion of macro 'AZ_SPAN_LITERAL_FROM_STR'
  125 | #define AZ_SPAN_FROM_STR(STRING_LITERAL) (az_span) AZ_SPAN_LITERAL_FROM_STR(STRING_LITERAL)
      |                                                    ^~~~~~~~~~~~~~~~~~~~~~~~
G:\AZURE Messaging IoT\Azure_IoT_Hub_ESP8266\Azure_IoT_Hub_ESP8266.ino:335:39: note: in expansion of macro 'AZ_SPAN_FROM_STR'
  335 |   temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR( (uint8_t)getCurrentLocalTimeString() ));
      |                                       ^~~~~~~~~~~~~~~~
C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_span.h:93:45: error: expected ')' before string constant
   93 | #define _az_STRING_LITERAL_LEN(S) (sizeof(S "") - 1)
      |                                          ~  ^~
C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_span.h:109:15: note: in expansion of macro '_az_STRING_LITERAL_LEN'
  109 |       .size = _az_STRING_LITERAL_LEN(STRING_LITERAL), \
      |               ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_span.h:125:52: note: in expansion of macro 'AZ_SPAN_LITERAL_FROM_STR'
  125 | #define AZ_SPAN_FROM_STR(STRING_LITERAL) (az_span) AZ_SPAN_LITERAL_FROM_STR(STRING_LITERAL)
      |                                                    ^~~~~~~~~~~~~~~~~~~~~~~~
G:\AZURE Messaging IoT\Azure_IoT_Hub_ESP8266\Azure_IoT_Hub_ESP8266.ino:335:39: note: in expansion of macro 'AZ_SPAN_FROM_STR'
  335 |   temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR( (uint8_t)getCurrentLocalTimeString() ));
      |                                       ^~~~~~~~~~~~~~~~
C:\Users\hansa\Documents\Arduino\libraries\Azure_SDK_for_C\src/az_span.h:125:50: error: expected primary-expression before ')' token
  125 | #define AZ_SPAN_FROM_STR(STRING_LITERAL) (az_span) AZ_SPAN_LITERAL_FROM_STR(STRING_LITERAL)
      |                                                  ^
G:\AZURE Messaging IoT\Azure_IoT_Hub_ESP8266\Azure_IoT_Hub_ESP8266.ino:335:39: note: in expansion of macro 'AZ_SPAN_FROM_STR'
  335 |   temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR( (uint8_t)getCurrentLocalTimeString() ));
      |                                       ^~~~~~~~~~~~~~~~
exit status 1
cast from 'char*' to 'uint8_t' {aka 'unsigned char'} loses precision [-fpermissive]
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

3

This bit of code:

(uint8_t)getCurrentLocalTimeString()

doesn't do what you think it does.

Your function getCurrentLocalTimeString() returns the address of an array of characters which contains a string representing the current local time.

You then try to cast this 32 bit memory address to an 8 bit unsigned integer, which is why the compiler warns that you're losing precision.

The best you can hope for here is publishing the first 8 bits of the memory address of this string, which I'm sure isn't what you want.

Since you seem to want to publish this as a string, you'd just drop the (uint8_t), like so:

temp_span = az_span_copy(temp_span, AZ_SPAN_FROM_STR(getCurrentLocalTimeString() ));
romkey
  • 6,218
  • 3
  • 16
  • 12