Azure IoT Hub no SDK - TLS read error :-0x004C

Gienek
Posts: 10
Joined: Wed Dec 21, 2022 11:09 am

Azure IoT Hub no SDK - TLS read error :-0x004C

Postby Gienek » Tue Feb 27, 2024 1:45 pm

Hi,
I have done registration to DPS, procedure of refreshing SAS tokens and sending telemetry to IoT HUB. It worked for some time. After several days it stopped working. There was several failures. One success and then no more success.

I isolated problem by writing desktop Pyton script which is really bulletproof - every time sends telemtry properly. I use the same data: device id, username, password from Python code in my ESP-IDF C code. And cannot connect.

The minimal example is example of MQTT ssl with pem file from my Azure hostname, and credentials from Python script.
  1.     const esp_mqtt_client_config_t mqtt_cfg = {
  2.         .broker = {
  3.             .address.uri = CONFIG_BROKER_URI,
  4.             .verification.certificate = (const char *)broker_pem_start,
  5.         },
  6.         .session = {
  7.             .keepalive = 240,
  8.             .protocol_ver = MQTT_PROTOCOL_V_3_1_1
  9.         },
  10.         .credentials = {
  11.             .client_id = "theendisreal_1",
  12.             .username = "XXXXXXXXXXX.azure-devices.net/theendisreal_1/?api-version=2018-06-30",
  13.             .authentication.password = "SharedAccessSignature sr=XXXXXXXXXXX.azure-devices.net%2Fdevices%2Ftheendisreal_1&sig=ZZZZZZZZZZZZZZZZ&se=1709043213"
  14.         }
  15.     };
And log is this:
  1. I (6413) MQTTS_EXAMPLE: Other event id:7
  2. I (7293) MQTTS_EXAMPLE: MQTT_EVENT_CONNECTED
  3. I (7293) MQTTS_EXAMPLE: sent subscribe successful, msg_id=45317
  4. I (7293) MQTTS_EXAMPLE: sent subscribe successful, msg_id=37869
  5. I (7293) MQTTS_EXAMPLE: sent unsubscribe successful, msg_id=20820
  6. I (7323) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=45317
  7. I (7323) MQTTS_EXAMPLE: sent publish successful, msg_id=0
  8. I (7363) MQTTS_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=37869
  9. I (7363) MQTTS_EXAMPLE: sent publish successful, msg_id=0
  10. I (7363) MQTTS_EXAMPLE: MQTT_EVENT_UNSUBSCRIBED, msg_id=20820
  11. E (12513) esp-tls-mbedtls: read error :-0x004C:
  12. E (12513) transport_base: esp_tls_conn_read error, errno=Socket is not connected
  13. E (12513) mqtt_client: esp_mqtt_handle_transport_read_error: transport_read() error: errno=128
  14. I (12523) MQTTS_EXAMPLE: MQTT_EVENT_ERROR
  15. I (12523) MQTTS_EXAMPLE: Last error code reported from esp-tls: 0x0
  16. I (12533) MQTTS_EXAMPLE: Last tls stack error number: 0x4c
  17. I (12543) MQTTS_EXAMPLE: Last captured errno : 0 (Success)
  18. E (12543) mqtt_client: mqtt_process_receive: mqtt_message_receive() returned -1
  19. I (12553) MQTTS_EXAMPLE: MQTT_EVENT_DISCONNECTED
After passing expiered password made out of expired sas token i got:
  1. W (7383) mqtt_client: Connection refused, not authorized
  2. I (7383) MQTTS_EXAMPLE: MQTT_EVENT_ERROR
  3. I (7383) MQTTS_EXAMPLE: Connection refused error: 0x5
After passing bad certificate I got other error:
  1. E (6603) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x2700
  2. I (6603) esp-tls-mbedtls: Failed to verify peer certificate!
  3. E (6613) esp-tls: Failed to open new connection
  4. E (6613) transport_base: Failed to open a new connection
As I mentioned it worker once after some time. I tried 2 WiFi and one mobile hotspot. I tried WSS - the same. DPS is working everytime over MQTT SSL/TLS. Ive seend this: https://github.com/espressif/esp-aws-iot/issues/160

I know there is SDK for C, and example for FreeRTOS but to be honest, this code is a mess. And also had the same problem with thransport.

What could cause this error?

Gienek
Posts: 10
Joined: Wed Dec 21, 2022 11:09 am

Re: Azure IoT Hub no SDK - TLS read error :-0x004C

Postby Gienek » Mon Mar 04, 2024 7:58 am

I made one test - tried to connect, send, reconnect. After setting refresh_connection_after_ms:
  1.     mqtt_cfg.network.timeout_ms = 10000;
  2.     mqtt_cfg.network.reconnect_timeout_ms = 10000;
  3.     mqtt_cfg.network.disable_auto_reconnect = false;
  4.     // mqtt_cfg.network.refresh_connection_after_ms = 25000;
it started working. After commeting out it seems to be fixed also.
I have theory: server drops connection if the device is connected for too long or has activity too often.

I'l write code to connect only if needed. If someone has any idea I'l be gratefull :)

funczz
Posts: 2
Joined: Tue Jun 25, 2024 1:01 pm

Re: Azure IoT Hub no SDK - TLS read error :-0x004C

Postby funczz » Wed Jun 26, 2024 1:46 am

Can you provide the complete code to solve this problem?

Who is online

Users browsing this forum: Gaston1980 and 182 guests