How to refresh the SAS token when using STMicroelectronics B-L475E-IOT01A Discovery kit to connect to IOT Hub

West Coast John 20 Reputation points
2026-07-27T18:13:47.2566667+00:00

I'm using the STMicroelectronics B-L475E-IOT01A Discovery kit to connect to the IOT Hub. The SAS token expires every hour and the connection is dropped and reestablished per the SW design for the Discovery kit, see below.

User's image

I have figured out how to create a new token, copied parts of function nx_azure_iot_hub_client_connect(), but I cannot figure out how to install it in the connection's context.

Are there any examples of how to refresh the token so that it does not expire and reset the connection?

Azure Internet of Things
0 comments No comments

1 answer

Sort by: Most helpful
  1. Allan Solomon Mejia 3,350 Reputation points
    2026-07-27T18:28:25.1833333+00:00

    Hello John,

    This behavior is expected when you're using SAS token authentication. A SAS token contains an expiration (se) timestamp, and once it expires the Azure IoT Hub client must authenticate again. The Azure IoT SDKs generally don't support replacing the SAS token on an existing MQTT/AMQP connection. Instead, the recommended approach is to:

    Generate a new SAS token before the current one expires.

    Disconnect the IoT Hub client.

    Recreate the client using the new SAS token.

    Reconnect and resume sending telemetry.

    If your application requires seamless long-running connectivity, consider using Microsoft Entra ID (Azure AD) authentication (where supported) or X.509 certificates instead of manually managed SAS tokens. These methods eliminate the need for periodic SAS token regeneration.

    If you're using the STM NetX Duo sample (nx_azure_iot_hub_client_connect()), you'll likely need to update the authentication callback or recreate the NX_AZURE_IOT_HUB_CLIENT instance with the new token, as there isn't an API to swap the SAS token on an active connection.

    Please "Accept the Answer" if this information helped you. This will help us and others in the community as well.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.