cancel
Showing results for 
Search instead for 
Did you mean: 

How to receive the time from the network server?

Sivanand
Visitor

Split from RF Communication with Nucleo-WL55JC Boards - Seeking Guidance

This is a different question.


 Hiii,

I need to receive the time from the network server. How to fix this issue.Give me the clear steps to fix .

5 REPLIES 5
Andrew Neil
Super User

Welcome to the forum.

Please give more details on your setup, what you are trying to achieve, and what you have done so far:

How to write your question to maximize your chances to find a solution

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
AScha.3
Super User

Ask the Ai ...

>How to receive the time from the network server on stm32 board?

 

Receiving time from a network server on an STM32 board is best achieved by implementing an SNTP (Simple Network Time Protocol) client over LwIP (Lightweight IP) (TCP/IP stack). The board requests time from an NTP server (e.g., pool.ntp.org), receives the timestamp, and uses it to update the internal Hardware Real-Time Clock (RTC). 
 
Key Implementation Steps
  • Configure Hardware: Enable the RTC peripheral and configure an LSE (Low-Speed External) crystal for better accuracy in STM32CubeIDE.
  • Setup Networking: Initialize LwIP (Ethernet or Wi-Fi) with DHCP to get an IP address.
  • Implement SNTP: Enable the SNTP client in LwIP configuration, set the server address (e.g., 0.pool.ntp.org), and set the update interval.
  • Update RTC: Use sntp_set_time_callback to trigger a function upon receiving time, which then calls HAL_RTC_SetTime and HAL_RTC_SetDate to update the RTC.
  • Retrieve Time: Use HAL_RTC_GetTime and HAL_RTC_GetDate to read the updated time. 
**********************************************
or sidekick here:  klick on AScha3_0-1769854158995.png

 

 

1. Synchronizing STM32 Time with a Network Time Server (NTP/SNTP)

To receive the time from a network server on an STM32 board, the recommended approach is to implement an SNTP (Simple Network Time Protocol) client using the LwIP stack. The process involves:

  • Project Setup: Use STM32CubeIDE to configure your project, enabling Ethernet (for network communication), USART (for debugging), and RTC (for timekeeping).
  • SNTP Communication: The SNTP client sends a request to an NTP server (typically on UDP port 123). The server responds with a packet containing the current timestamp.
  • RTC Update: The received timestamp is converted to a human-readable format and stored in the STM32's RTC. For better accuracy, use the LSE (Low-Speed External) oscillator as the RTC clock source.
  • Periodic Synchronization: The application can periodically update the RTC with new timestamps from the SNTP server (e.g., every 15 seconds or as needed), ensuring the RTC remains accurate.

2. Implementing an SNTP Client on STM32 with LwIP

To implement an SNTP client for time synchronization:

  • Configure Peripherals: Set up the RTC, Ethernet (in RMII mode), and USART for debugging. Ensure the board is connected to a network.
  • Enable LwIP Middleware: In STM32CubeIDE, enable LwIP and configure SNTP settings (including update delay and server address).
  • Code Integration:
    • Include SNTP and time libraries.
    • Initialize SNTP after DHCP assigns an IP address.
    • Set the SNTP server IP and operation mode.
    • When a timestamp is received, convert it and update the RTC.
    • Print results via the serial terminal for debugging.
  • Operation: The SNTP client will periodically request time from the server, update the RTC, and output the results. Adjust the SNTP polling interval as needed for your application.

3. Summary

To receive the time from a network server on an STM32 board, implement an SNTP client using the LwIP stack. Configure the necessary peripherals, enable LwIP and SNTP in STM32CubeIDE, and periodically synchronize the RTC with the network time server. This approach ensures your STM32 device maintains accurate time for logging and time-sensitive applications.

For additional support or to discuss your specific application with ST experts, you’re welcome to join conversations in STM32 forums.

If you feel a post has answered your question, please click "Accept as Solution".

@AScha.3's AI wrote:
Receiving time from a network server on an STM32 board is best achieved by implementing an SNTP (Simple Network Time Protocol) client over LwIP (Lightweight IP) (TCP/IP stack).

That's assuming a TCP/IP network.

It's not clear that @Sivanand has a TCP/IP network.

 

@Sivanand - this is why it's important that you give full details of your setup, what you are trying to achieve, and what you have done so far:

How to write your question to maximize your chances to find a solution

 

See also: How to use STM32 Sidekick

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
Sivanand
Visitor

Hi everyone,

I’m working with an STM32WL55JC1 board and using LoRaWAN. STM32 has no Rtc module i need time from the network sever to count the time for sending data.

 

  • MCU: STM32WL55JC1

  • Network: LoRaWAN (not TCP/IP, no LwIP, no SNTP)

  • Goal: 

  • Request the current network time from the LoRaWAN Network Server.

  • Receive the timestamp on the device.

ok...

Receiving time from a LoRaWAN network server on an STM32 board is achieved by
implementing the DeviceTimeReq MAC command (LoRaWAN L2 1.0.3+), which prompts the server to respond with the current GPS time in a downlink (DeviceTimeAns). This requires using the LmHandler or LoRaMac libraries to send an uplink, typically followed by processing the downlink in the MAC command handler to update the STM32's RTC. 
Steps to Receive Network Time: 
  1. Initialize LoRaWAN Stack: Use the STM32CubeWL library to initialize the LoRaWAN stack and join a network via OTAA.
  2. Send Time Request: In the firmware, call LmHandlerDeviceTimeReq() or manually send a MAC command DeviceTimeReq. This request is often piggybacked on a standard sensor data uplink.
  3. Handle Downlink Command: The network server responds with DeviceTimeAns (usually in RX1 or RX2 windows). The LoRaWAN stack handles this, triggering a callback function where the 5-octet time payload (GPS time) is received.
  4. Update RTC: Parse the received time (seconds since GPS epoch) and use the HAL_RTC_SetTime() and HAL_RTC_SetDate() functions to synchronize the STM32's real-time clock.
  5. Frequency Configuration: Ensure the LoRaWAN stack is configured for the correct region to enable proper Class A downlink reception. 
Note: The returned time has a maximum inaccuracy of    ±100 ms . 
If you feel a post has answered your question, please click "Accept as Solution".