Moin,
ich möchte mein RTC-Modul DS3231 mit dem NTP Zeitserver auf MEZ einstellen. Die Abfrage der NTP-Zeit funktioniert, aber ich weiß nicht, wie ich diese Zeit auf den DS3231 bekommen kann.
Den Output habe ich unten angehängt. Dazu eine Zusatzfrage: was muss ich tun, damit ich nicht immer die doppelte Ausgabe der WLAN-Verbindung bekomme?
(Die ganzen Print-Befehle nehme ich raus)
Python
"""
DS3231 stellen auf MEZ
"""
import time
from time import sleep
import network
import credentials
from machine import RTC
import ds3231
import ntptime
import urtc
#32 clock
#33 data
from machine import I2C, Pin
i2c = I2C(0, scl=Pin(32), sda=Pin(33))
SSID = credentials.SSID
PASSWORD = credentials.PASSWORD
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(SSID, PASSWORD)
sleep(5)
print('Connection successful')
print(station.ifconfig())
ntptime.settime()
time.localtime()
localtime= time.localtime(time.time())
akt_zeit=time.localtime(time.time())
print (" aktuelle Zeit MEZ",akt_zeit)
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
rtc = urtc.DS3231(i2c)
initial_time_tuple = time.localtime() # tuple (microPython)
initial_time_seconds = time.mktime(initial_time_tuple) # local time in seconds
# Convert to tuple compatible with the library
initial_time = urtc.seconds2tuple(initial_time_seconds)
# Sync the RTC
rtc.datetime(initial_time)
current_datetime = rtc.datetime()
print('Current date and time:')
print('Year:', current_datetime.year)
print('Month:', current_datetime.month)
print('Day:', current_datetime.day)
print('Hour:', current_datetime.hour )
print('Minute:', current_datetime.minute)
print('Second:', current_datetime.second)
print('Day of the Week:', days_of_week[current_datetime.weekday])
Display More
Code
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot
('192.168.178.153', '255.255.255.0', '192.168.178.1', '192.168.178.1')
Current date and time:
Year: 2026
Month: 8
Day: 3
Hour: 19
Minute: 33
Second: 30
Day of the Week: Monday
Monat 8
21 33 30
Connection successful
('192.168.178.153', '255.255.255.0', '192.168.178.1', '192.168.178.1')
aktuelle Zeit MEZ (2026, 8, 3, 17, 33, 39, 0, 215)
Current date and time:
Year: 2026
Month: 8
Day: 3
Hour: 17
Minute: 33
Second: 39
Day of the Week: Monday
Display More