Skip to content

Latest commit

 

History

History
67 lines (42 loc) · 3.68 KB

File metadata and controls

67 lines (42 loc) · 3.68 KB

This code connects an ESP32 to Wi-Fi, synchronizes the device's time with an NTP server to get UTC time, and adjusts it to reflect Israel Standard Time (UTC+2). The adjusted time is then converted into a human-readable tuple.

Functionality Breakdown: ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1. Wi-Fi Connection

Code: sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect(ssid, password)

while not sta_if.isconnected(): time.sleep(1)

Purpose: Connects the ESP32 to a specified Wi-Fi network.

Details: 1.Activates the station interface (STA_IF). 2.Connects to the specified ssid and password. 3.Waits until the device successfully connects to the network.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2. Synchronize Time with NTP

Code: ntptime.settime()

Purpose: Fetches the current UTC time from an NTP server and sets the system's time.

Details: 1.The ntptime module queries an NTP server for accurate UTC time. 2.Updates the system clock with this time.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3. Fetch and Adjust Time

Code: utc_time = time.time() israel_time_seconds = utc_time + 2 * 3600 israel_time_tuple = time.localtime(israel_time_seconds)

Purpose: Adjusts UTC time to Israel Standard Time (UTC+2).

Details: 1.time.time() returns the current UTC time in seconds since the epoch (Jan 1, 1970). 2.Adds an offset of 2 * 3600 seconds to shift the time to Israel Standard Time. 3.Converts the adjusted time into a time.localtime() tuple, making it human-readable.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I added a few enhancements to improve stability and error handling:

  1. (for _ in range(10) Retry Loop): Instead of waiting indefinitely, the code retries 10 times (10 seconds) to connect to Wi-Fi. If the connection fails, it prints "Failed to connect to Wi-Fi." instead of crashing.

  2. (try-except for NTP Sync): ntptime.settime() can fail if there’s no internet or the NTP server is unreachable. The try-except block catches this error and prints "NTP time sync failed:" instead of stopping execution.

  • These additions make the program more robust, ensuring it doesn’t hang or crash if Wi-Fi or NTP fails.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Resources and websites:

1.https://www.chatgpt.com/ 2.https://www.perplexity.ai/