Python Forum

Full Version: storing datime on rtc
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm trying to store the actual datetime onto a RCT device, but I just cannot get the time format correct.

Any help is appreciated.
TIA

#!/usr/bin/python
# -*- coding: utf-8 -*-
import smbus
import time
from datetime import datetime as dt

address = 0x68
register = 0x00
now = dt.now()

#sec min hour week day month year
NowTime = [
        hex(now.second),
        hex(now.minute),
        hex(now.hour),
        hex(now.weekday()),
        hex(now.day),
        hex(now.month),
        hex(now.year-2000)
        ]

#this line works!!
#NowTime = [0x00,0x00,0x18,0x04,0x6,0x07,0x7e4] 

w  = ["SUN","Mon","Tues","Wed","Thur","Fri","Sat"];

bus = smbus.SMBus(1)

def ds3231SetTime():
        bus.write_i2c_block_data(address,register,NowTime)

def ds3231ReadTime():
        return bus.read_i2c_block_data(address,register,7);

ds3231SetTime()

while 1:
        t = ds3231ReadTime()
        t[0] = t[0]&0x7F  #sec
        t[1] = t[1]&0x7F  #min
        t[2] = t[2]&0x3F  #hour
        t[3] = t[3]&0x07  #week
        t[4] = t[4]&0x3F  #day
        t[5] = t[5]&0x1F  #mouth
        print("20%x/%02x/%02x %02x:%02x:%02x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],w[t[3]-1]))
        time.sleep(1)
error:
['0x10', '0x24', '0x13', '0x0', '0x6', '0x7', '0x14']
Traceback (most recent call last):
  File "ds3231.py", line 34, in <module>
    ds3231SetTime()
  File "ds3231.py", line 29, in ds3231SetTime
    bus.write_i2c_block_data(address,register,NowTime)
TypeError: Third argument must be a list of at least one, but not more than 32 integers
hex() is a function that takes a number and turns it into a string (one that looks like a hex representation of a decimal number). That makes NowTime a list of strings, not a list of integers.

>>> type(hex(24))
<class 'str'>
0x18 and 24 are the exact same thing. You don't need to convert one into the other. I think you just need to get rid of the hex conversions.

>>> 0x18 == 24
True