Esempio n. 1
0
    def cb_enumerate(self, uid, connected_uid, position, hardware_version, 
                 firmware_version, device_identifier, enumeration_type):
        
        if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
           enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:
            
            # Initialize GPS
            if device_identifier == BrickletGPSV2.DEVICE_IDENTIFIER:
                self.gps = BrickletGPSV2(uid, self.ipcon)

                self.gps.set_date_time_callback_period(TimeServer.GPS_UPDATE_PERIOD)
                self.gps.set_coordinates_callback_period(TimeServer.GPS_UPDATE_PERIOD)

                self.gps.register_callback(BrickletGPSV2.CALLBACK_DATE_TIME, self.cb_time_updated)
                self.gps.register_callback(BrickletGPSV2.CALLBACK_COORDINATES, self.cb_location_updated)

            # Initialize OLED display
            if device_identifier == BrickletOLED128x64.DEVICE_IDENTIFIER:
                self.oled = BrickletOLED128x64(uid, self.ipcon)
                self.oled.clear_display()

            # Initialize RTC
            if device_identifier == BrickletRealTimeClock.DEVICE_IDENTIFIER:
                self.rtc = BrickletRealTimeClock(uid, self.ipcon)

                self.rtc.register_callback(BrickletRealTimeClock.CALLBACK_DATE_TIME, self.cb_rtc_time_update)
                self.rtc.set_date_time_callback_period(TimeServer.RTC_UPDATE_PERIOD)
Esempio n. 2
0
    def get_rtc_time(self):
        if self.rtc_uid == None:
            return False

        try:
            # Create Real-Time Clock device object
            self.rtc = BrickletRealTimeClock(self.rtc_uid, self.ipcon)
            year, month, day, hour, minute, second, centisecond, _ = self.rtc.get_date_time(
            )
            self.rtc_time = datetime.datetime(year, month, day, hour, minute,
                                              second, centisecond * 10000)
        except:
            return False

        return True
Esempio n. 3
0
    def get_rtc_time(self):
        if self.rtc_uid == None:
            return False

        try:
            # Create Real-Time Clock device object
            if self.rtc_device_identifier == BrickletRealTimeClock.DEVICE_IDENTIFIER:
                self.rtc = BrickletRealTimeClock(self.rtc_uid, self.ipcon)
                year, month, day, hour, minute, second, centisecond, _ = self.rtc.get_date_time(
                )
            else:
                self.rtc = BrickletRealTimeClockV2(self.rtc_uid, self.ipcon)
                year, month, day, hour, minute, second, centisecond, _, _ = self.rtc.get_date_time(
                )

            self.rtc_time = datetime.datetime(year, month, day, hour, minute,
                                              second, centisecond * 10000)
        except:
            return False

        return True
Esempio n. 4
0
    def get_rtc_time(self):
        if self.rtc_uid == None:
            return False

        try:
            # Create Real-Time Clock device object
            self.rtc = BrickletRealTimeClock(self.rtc_uid, self.ipcon)
            year, month, day, hour, minute, second, centisecond, _ = self.rtc.get_date_time()
            self.rtc_time = datetime.datetime(year, month, day, hour, minute, second, centisecond * 10000)
        except:
            return False

        return True
Esempio n. 5
0
    def get_rtc_time(self):
        if self.rtc_uid == None:
            return False

        try:
            # Create Real-Time Clock device object
            if self.rtc_device_identifier == BrickletRealTimeClock.DEVICE_IDENTIFIER:
                self.rtc = BrickletRealTimeClock(self.rtc_uid, self.ipcon)
                year, month, day, hour, minute, second, centisecond, _ = self.rtc.get_date_time()
            else:
                self.rtc = BrickletRealTimeClockV2(self.rtc_uid, self.ipcon)
                year, month, day, hour, minute, second, centisecond, _, _ = self.rtc.get_date_time()

            self.rtc_time = datetime.datetime(year, month, day, hour, minute, second, centisecond * 10000)
        except:
            return False

        return True
Esempio n. 6
0
class RTCTimeToLinuxTime:
    def __init__(self):
        # Create IP connection
        self.ipcon = IPConnection()

        # Connect to brickd
        self.ipcon.connect(HOST, PORT)
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE,
                                     self.cb_enumerate)
        self.ipcon.enumerate()

        self.enum_sema = Semaphore(0)
        self.rtc_uid = None
        self.rtc_device_identifier = None
        self.rtc = None
        self.rtc_time = None
        self.timer = None

    # go trough the functions to update date and time
    def __enter__(self):
        if self.is_ntp_present():
            return -1, None
        if not self.get_rtc_uid():
            return -2, None
        if not self.get_rtc_time():
            return -3, None
        if self.are_times_equal():
            return 1, self.rtc_time
        if not self.set_linux_time():
            return -4, None

        return 0, self.rtc_time

    def __exit__(self, type, value, traceback):
        try:
            self.timer.cancel()
        except:
            pass

        try:
            self.ipcon.disconnect()
        except:
            pass

    def is_ntp_present(self):
        # FIXME: Find out if we have internet access and ntp is working, in
        #        that case we don't need to use the RTC time.
        return False

    def get_rtc_uid(self):
        try:
            # Release semaphore after 1 second (if no Real-Time Clock Bricklet is found)
            self.timer = Timer(1, self.enum_sema.release)
            self.timer.start()
            self.enum_sema.acquire()
        except:
            return False

        return True

    def get_rtc_time(self):
        if self.rtc_uid == None:
            return False

        try:
            # Create Real-Time Clock device object
            if self.rtc_device_identifier == BrickletRealTimeClock.DEVICE_IDENTIFIER:
                self.rtc = BrickletRealTimeClock(self.rtc_uid, self.ipcon)
                year, month, day, hour, minute, second, centisecond, _ = self.rtc.get_date_time(
                )
            else:
                self.rtc = BrickletRealTimeClockV2(self.rtc_uid, self.ipcon)
                year, month, day, hour, minute, second, centisecond, _, _ = self.rtc.get_date_time(
                )

            self.rtc_time = datetime.datetime(year, month, day, hour, minute,
                                              second, centisecond * 10000)
        except:
            return False

        return True

    def are_times_equal(self):
        # Are we more then 3 seconds off?
        if abs(int(self.rtc_time.strftime("%s")) - time.time()) > 3:
            return False

        return True

    def set_linux_time(self):
        if self.rtc_time == None:
            return False

        try:
            # Set date as root
            command = [
                '/usr/bin/sudo', '-S', '/bin/date',
                self.rtc_time.strftime('%m%d%H%M%Y.%S')
            ]
            Popen(command, stdout=PIPE, stdin=PIPE).communicate(SUDO_PASSWORD)
        except:
            return False

        return True

    def cb_enumerate(self, uid, connected_uid, position, hardware_version,
                     firmware_version, device_identifier, enumeration_type):
        # If more then one Real-Time Clock Bricklet is connected we will use the first one that we find
        if device_identifier in [
                BrickletRealTimeClock.DEVICE_IDENTIFIER,
                BrickletRealTimeClockV2.DEVICE_IDENTIFIER
        ]:
            self.rtc_uid = uid
            self.rtc_device_identifier = device_identifier
            self.enum_sema.release()
# Callback function for date and time callback
def cb_date_time(year, month, day, hour, minute, second, centisecond, weekday, timestamp):
    print("Year: " + str(year))
    print("Month: " + str(month))
    print("Day: " + str(day))
    print("Hour: " + str(hour))
    print("Minute: " + str(minute))
    print("Second: " + str(second))
    print("Centisecond: " + str(centisecond))
    print("Weekday: " + str(weekday))
    print("Timestamp: " + str(timestamp))
    print("")

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    rtc = BrickletRealTimeClock(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    # Register date and time callback to function cb_date_time
    rtc.register_callback(rtc.CALLBACK_DATE_TIME, cb_date_time)

    # Set period for date and time callback to 5s (5000ms)
    # Note: The date and time callback is only called every 5 seconds
    #       if the date and time has changed since the last call!
    rtc.set_date_time_callback_period(5000)

    raw_input("Press key to exit\n") # Use input() in Python 3
    ipcon.disconnect()
Esempio n. 8
0
        else:
            activCH2 = False
        if activCH2 == True:
            dr.set_selected_state(2, False)
            oled.write_line(3, 0, "Beleuchtung:      AUS")
            rlb2.set_color(brightness, 0, 0)
        else:
            dr.set_selected_state(2, True)
            oled.write_line(3, 0, "Beleuchtung:      EIN")
            rlb2.set_color(0, brightness, 0)


if __name__ == "__main__":
    time.sleep(10)
    ipcon = IPConnection()  # Create IP connection
    rtc = BrickletRealTimeClock(UID_RTC, ipcon)  # Create device object
    dr = BrickletDualRelay(UID_DR, ipcon)  # Create device object
    rlb1 = BrickletRGBLEDButton(UID_RLB_1, ipcon)  # Create device object
    rlb2 = BrickletRGBLEDButton(UID_RLB_2, ipcon)  # Create device object
    oled = BrickletOLED128x64(UID_OLED, ipcon)  # Create device object
    temp = BrickletTemperature(UID_TEMP, ipcon)  # Create device object
    mb1 = BrickMaster(UID_MB1, ipcon)  # Create device object
    mb2 = BrickMaster(UID_MB2, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected
    time.sleep(1)
    oled.clear_display()
    oled.write_line(1, 1, "Starting Application...")

    #***********Brick-Config********************************************
                 timestamp):
    print("Year: " + str(year))
    print("Month: " + str(month))
    print("Day: " + str(day))
    print("Hour: " + str(hour))
    print("Minute: " + str(minute))
    print("Second: " + str(second))
    print("Centisecond: " + str(centisecond))
    print("Weekday: " + str(weekday))
    print("Timestamp: " + str(timestamp))
    print("")


if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    rtc = BrickletRealTimeClock(UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected

    # Register date and time callback to function cb_date_time
    rtc.register_callback(rtc.CALLBACK_DATE_TIME, cb_date_time)

    # Set period for date and time callback to 5s (5000ms)
    # Note: The date and time callback is only called every 5 seconds
    #       if the date and time has changed since the last call!
    rtc.set_date_time_callback_period(5000)

    raw_input("Press key to exit\n")  # Use input() in Python 3
    ipcon.disconnect()
Esempio n. 10
0
class TimeServer:
    GPS_UPDATE_PERIOD = 5000
    RTC_UPDATE_PERIOD = 1000

    def __init__(self, host, port):
        # Available devices that we use
        self.gps = None
        self.rtc = None
        self.oled = None
        self.buzzer = None

        # GPS information
        self.last_gps_time = None
        self.last_gps_position = None

        self.ipcon = IPConnection() 
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, 
                                     self.cb_enumerate)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, 
                                     self.cb_connected)
        self.ipcon.connect(host, int(port))
        self.ipcon.enumerate()

    def cb_enumerate(self, uid, connected_uid, position, hardware_version, 
                 firmware_version, device_identifier, enumeration_type):
        
        if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
           enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:
            
            # Initialize GPS
            if device_identifier == BrickletGPSV2.DEVICE_IDENTIFIER:
                self.gps = BrickletGPSV2(uid, self.ipcon)

                self.gps.set_date_time_callback_period(TimeServer.GPS_UPDATE_PERIOD)
                self.gps.set_coordinates_callback_period(TimeServer.GPS_UPDATE_PERIOD)

                self.gps.register_callback(BrickletGPSV2.CALLBACK_DATE_TIME, self.cb_time_updated)
                self.gps.register_callback(BrickletGPSV2.CALLBACK_COORDINATES, self.cb_location_updated)

            # Initialize OLED display
            if device_identifier == BrickletOLED128x64.DEVICE_IDENTIFIER:
                self.oled = BrickletOLED128x64(uid, self.ipcon)
                self.oled.clear_display()

            # Initialize RTC
            if device_identifier == BrickletRealTimeClock.DEVICE_IDENTIFIER:
                self.rtc = BrickletRealTimeClock(uid, self.ipcon)

                self.rtc.register_callback(BrickletRealTimeClock.CALLBACK_DATE_TIME, self.cb_rtc_time_update)
                self.rtc.set_date_time_callback_period(TimeServer.RTC_UPDATE_PERIOD)

    def cb_connected(self, connected_reason):
        self.ipcon.enumerate()

    def cb_time_updated(self, d, t):
        fix, satelite_num = self.gps.get_status()
        if fix:
            year, d = d % 100, int(d/100)
            month, d = d % 100, int(d/100)
            day = d % 100

            millisecond, t= t % 1000, int(t/1000)
            second, t = t % 100, int(t/100)
            minute, t = t % 100, int(t/100)
            hour = t % 100

            self.last_gps_time = datetime(2000+year, month, day, hour, minute, second, microsecond=millisecond*1000, tzinfo=from_zone)
            self.update_rtc_time(self.last_gps_time)

            if self.oled:
                self.oled.write_line(3, 2, "GPS Time: %02d:%02d:%02d.%02d" % (self.last_gps_time.hour, self.last_gps_time.minute, self.last_gps_time.second, millisecond/10))
                self.oled.write_line(4, 2, "GPS Date: %02d.%02d.%d" % (self.last_gps_time.day, self.last_gps_time.month, self.last_gps_time.year))

    def cb_location_updated(self, latitude, ns, longitude, ew):
        fix, satelite_num = self.gps.get_status()
        if fix:
            self.last_gps_position=GpsLocation(latitude, ns, longitude, ew)
            if self.oled:
                self.oled.write_line(6, 1, "Location: %.2f %s %.2f %s" % (self.last_gps_position.latitude, ns, self.last_gps_position.longitude, ew))    

    def update_rtc_time(self, dt):
        if self.rtc:
            self.rtc.set_date_time(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond/10000, dt.weekday()+1)

    def cb_rtc_time_update(self, year, month, day, hour, minute, second, centisecond, weekday, timestamp):
        if self.oled:
            self.oled.write_line(0, 2, "RTC Time: %02d:%02d:%02d.%02d" % (hour, minute, second, centisecond))
            self.oled.write_line(1, 2, "RTC Date: %02d.%02d.%d" % (day, month, year))

    def get_current_time(self):
        if self.rtc:
            year, month, day, hour, minute, second, centisecond, weekday = self.rtc.get_date_time()
            dt = datetime(year, month, day, hour, minute, second, 0, tzinfo=from_zone)

            timestamp = (calendar.timegm(dt.timetuple()) * 1000 )+(centisecond*10)  # I'm not sure if this is the best way

            return timestamp
        else:
            return 0
Esempio n. 11
0
class RTCTimeToLinuxTime:
    def __init__(self):
        # Create IP connection
        self.ipcon = IPConnection()

        # Connect to brickd
        self.ipcon.connect(HOST, PORT)
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate)
        self.ipcon.enumerate()

        self.enum_sema = Semaphore(0)
        self.rtc_uid = None
        self.rtc_device_identifier = None
        self.rtc = None
        self.rtc_time = None
        self.timer = None

    # go trough the functions to update date and time
    def __enter__(self):
        if self.is_ntp_present():
            return -1, None
        if not self.get_rtc_uid():
            return -2, None
        if not self.get_rtc_time():
            return -3, None
        if self.are_times_equal():
            return 1, self.rtc_time
        if not self.set_linux_time():
            return -4, None

        return 0, self.rtc_time

    def __exit__(self, type, value, traceback):
        try:
            self.timer.cancel()
        except:
            pass

        try:
            self.ipcon.disconnect()
        except:
            pass

    def is_ntp_present(self):
        # FIXME: Find out if we have internet access and ntp is working, in
        #        that case we don't need to use the RTC time.
        return False

    def get_rtc_uid(self):
        try:
            # Release semaphore after 1 second (if no Real-Time Clock Bricklet is found)
            self.timer = Timer(1, self.enum_sema.release)
            self.timer.start()
            self.enum_sema.acquire()
        except:
            return False

        return True

    def get_rtc_time(self):
        if self.rtc_uid == None:
            return False

        try:
            # Create Real-Time Clock device object
            if self.rtc_device_identifier == BrickletRealTimeClock.DEVICE_IDENTIFIER:
                self.rtc = BrickletRealTimeClock(self.rtc_uid, self.ipcon)
                year, month, day, hour, minute, second, centisecond, _ = self.rtc.get_date_time()
            else:
                self.rtc = BrickletRealTimeClockV2(self.rtc_uid, self.ipcon)
                year, month, day, hour, minute, second, centisecond, _, _ = self.rtc.get_date_time()

            self.rtc_time = datetime.datetime(year, month, day, hour, minute, second, centisecond * 10000)
        except:
            return False

        return True

    def are_times_equal(self):
        # Are we more then 3 seconds off?
        if abs(int(self.rtc_time.strftime("%s")) - time.time()) > 3:
            return False

        return True

    def set_linux_time(self):
        if self.rtc_time == None:
            return False

        try:
            # Set date as root
            command = ['/usr/bin/sudo', '-S', '/bin/date', self.rtc_time.strftime('%m%d%H%M%Y.%S')]
            Popen(command, stdout=PIPE, stdin=PIPE).communicate(SUDO_PASSWORD)
        except:
            return False

        return True

    def cb_enumerate(self, uid, connected_uid, position, hardware_version,
                     firmware_version, device_identifier, enumeration_type):
        # If more then one Real-Time Clock Bricklet is connected we will use the first one that we find
        if device_identifier in [BrickletRealTimeClock.DEVICE_IDENTIFIER,
                                 BrickletRealTimeClockV2.DEVICE_IDENTIFIER]:
            self.rtc_uid = uid
            self.rtc_device_identifier = device_identifier
            self.enum_sema.release()
class HardwareTimeSource:
    GPS_UPDATE_PERIOD = 5000
    RTC_UPDATE_PERIOD = 1000

    def __init__(self, host, port):
        # Available devices that we use
        self.gps = None
        self.rtc = None
        self.oled = None
        self.time_handler=None

        # GPS information
        self.last_gps_time = None
        self.last_gps_position = None

        self.ipcon = IPConnection() 
        self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, 
                                     self._cb_enumerate)
        self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, 
                                     self._cb_connected)
        self.ipcon.connect(host, int(port))
        self.ipcon.enumerate()

    def _cb_enumerate(self, uid, connected_uid, position, hardware_version, 
                 firmware_version, device_identifier, enumeration_type):
        
        if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or \
           enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE:
            
            # Initialize GPS
            if device_identifier == BrickletGPSV2.DEVICE_IDENTIFIER:
                self.gps = BrickletGPSV2(uid, self.ipcon)

                self.gps.set_date_time_callback_period(HardwareTimeSource.GPS_UPDATE_PERIOD)
                self.gps.set_coordinates_callback_period(HardwareTimeSource.GPS_UPDATE_PERIOD)

                self.gps.register_callback(BrickletGPSV2.CALLBACK_DATE_TIME, self._cb_time_updated)
                self.gps.register_callback(BrickletGPSV2.CALLBACK_COORDINATES, self._cb_location_updated)

            # Initialize OLED display
            if device_identifier == BrickletOLED128x64.DEVICE_IDENTIFIER:
                self.oled = BrickletOLED128x64(uid, self.ipcon)
                self.oled.clear_display()

            # Initialize RTC
            if device_identifier == BrickletRealTimeClock.DEVICE_IDENTIFIER:
                self.rtc = BrickletRealTimeClock(uid, self.ipcon)

                self.rtc.register_callback(BrickletRealTimeClock.CALLBACK_DATE_TIME, self._cb_rtc_time_update)
                self.rtc.set_date_time_callback_period(HardwareTimeSource.RTC_UPDATE_PERIOD)

        if self.rtc and self.gps and self.ready_handler:
            # We are ready to server time
            self.ready_handler()

    def _cb_connected(self, connected_reason):
        self.ipcon.enumerate()

    def _cb_time_updated(self, d, t):
        fix, satelite_num = self.gps.get_status()
        if fix:
            year, d = d % 100, int(d/100)
            month, d = d % 100, int(d/100)
            day = d % 100

            millisecond, t= t % 1000, int(t/1000)
            second, t = t % 100, int(t/100)
            minute, t = t % 100, int(t/100)
            hour = t % 100

            self.last_gps_time = datetime(2000+year, month, day, hour, minute, second, microsecond=millisecond*1000, tzinfo=utc_zone)

            if self.time_handler:
                self.time_handler(self.last_gps_time)

            if self.oled:
                self.oled.write_line(3, 2, "GPS Time: %02d:%02d:%02d.%02d" % (self.last_gps_time.hour, self.last_gps_time.minute, self.last_gps_time.second, millisecond/10))
                self.oled.write_line(4, 2, "GPS Date: %02d.%02d.%d" % (self.last_gps_time.day, self.last_gps_time.month, self.last_gps_time.year))

    def _cb_location_updated(self, latitude, ns, longitude, ew):
        fix, satelite_num = self.gps.get_status()
        if fix:
            self.last_gps_position=GpsLocation(latitude, ns, longitude, ew)
            if self.oled:
                self.oled.write_line(6, 1, "Location: %.2f %s %.2f %s" % (self.last_gps_position.latitude, ns, self.last_gps_position.longitude, ew))

    def _cb_rtc_time_update(self, year, month, day, hour, minute, second, centisecond, weekday, timestamp):
        if self.oled:
            self.oled.write_line(0, 2, "RTC Time: %02d:%02d:%02d.%02d" % (hour, minute, second, centisecond))
            self.oled.write_line(1, 2, "RTC Date: %02d.%02d.%d" % (day, month, year))

    def register_gps_time_handler(self, gps_time_handler):
        self.time_handler=gps_time_handler

    def register_bricklets_discovery_finished(self, ready_handler):
        self.ready_handler=ready_handler

    def get_rtc_time(self):
        if self.rtc:
            year, month, day, hour, minute, second, centisecond, weekday = self.rtc.get_date_time()
            dt = datetime(year, month, day, hour, minute, second, centisecond*10000, tzinfo=utc_zone)
            return dt
        else:
            raise Exception("RTC is not initialized!")

    def update_rtc_time(self, dt):
        if self.rtc:
            # TODO: Check if dt has timezone set to UTC, throw exception otherwise
            self.rtc.set_date_time(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond/10000, dt.weekday()+1)
        else:
            raise Exception("RTC is not initialized!")
Esempio n. 13
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "XYZ"  # Change XYZ to the UID of your Real-Time Clock Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_real_time_clock import BrickletRealTimeClock

if __name__ == "__main__":
    ipcon = IPConnection()  # Create IP connection
    rtc = BrickletRealTimeClock(UID, ipcon)  # Create device object

    ipcon.connect(HOST, PORT)  # Connect to brickd
    # Don't use device before ipcon is connected

    # Get current date and time
    year, month, day, hour, minute, second, centisecond, weekday = rtc.get_date_time(
    )

    print("Year: " + str(year))
    print("Month: " + str(month))
    print("Day: " + str(day))
    print("Hour: " + str(hour))
    print("Minute: " + str(minute))
    print("Second: " + str(second))
    print("Centisecond: " + str(centisecond))
    print("Weekday: " + str(weekday))

    # Get current timestamp (unit is ms)
#!/usr/bin/env python
# -*- coding: utf-8 -*-

HOST = "localhost"
PORT = 4223
UID = "XYZ" # Change XYZ to the UID of your Real-Time Clock Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_real_time_clock import BrickletRealTimeClock

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    rtc = BrickletRealTimeClock(UID, ipcon) # Create device object

    ipcon.connect(HOST, PORT) # Connect to brickd
    # Don't use device before ipcon is connected

    # Get current date and time
    year, month, day, hour, minute, second, centisecond, weekday = rtc.get_date_time()

    print("Year: " + str(year))
    print("Month: " + str(month))
    print("Day: " + str(day))
    print("Hour: " + str(hour))
    print("Minute: " + str(minute))
    print("Second: " + str(second))
    print("Centisecond: " + str(centisecond))
    print("Weekday: " + str(weekday))

    # Get current timestamp (unit is ms)
    timestamp = rtc.get_timestamp()