User avatar
tlfong01
Posts: 1312
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Cat Home DIY Q&A - RTC How To

Thu Jan 24, 2019 1:10 pm

...
Introduction

I am building a smart home system using DIY components KY019 relay, MCP23017 IO extender, ZE1 Solenoid, Puro-XD-12 Solenoid valve, ESP8266-12. Now I am starting a new guy called RTC (Real Time Clock) DS3231. This device can do real time clock, temperature sensor, and also has a EEPROM. I have already written a python program to do basic testing, and read time, set alarm etc [1], [2]. I will us RTC to work with cat feeder, smart fountain etc.

I am listing the first ping program here. This is a read/write/register program to make sure I2C is working OK. This is a MCV (Mininal, Complete, Verifiable) program.

[1] https://www.raspberrypi.org/forums/view ... 0#p1346190 (RTC Program v1.7)
[2] https://www.raspberrypi.org/forums/view ... 5#p1350076 (RTC Alarm Clock Test Program)

...

Code: Select all

# DS3231 Real Time Clock Test Program v1.62 tlfong01 2019jan24hkt1726

# Hardware Software Description
# Rpi hardware = Rpi3B+
# Rpi software = $ hostnamectl = raspberrypi Raspbian GNU/Linux 9 (stretch) Linux 4.14.34-v7+ arm 
# Python = >>> sys.version = 3.5.3 Jan 19 2017

# Program Description
# Write, read, and print DS3231's Day Register (lower 4 bits only)

import smbus

i2cCh = smbus.SMBus(1)

dvAddrByte = 0x68 # RTC DS3231 I2C Device Address
dayRegAddrByte = 0x03 # RTC DS3231 Day Register Address

# *** Read Write Print Device/Register Functions ***

def writeDviTwoBytes(i2cPort, dvAddrByte, dataByte1, dataByte2):
    i2cPort.write_byte_data(dvAddrByte, dataByte1, dataByte2)
    return

def readDvRegOneByte(i2cPort, dvAddrByte, DvRegAddrByte):
    readByte = i2cPort.read_byte_data(dvAddrByte, DvRegAddrByte)
    return readByte

def writeDvRegOneByte(i2cPort, dvAddrByte, DvRegAddrByte, writeByte):
    writeDviTwoBytes(i2cPort, dvAddrByte, DvRegAddrByte, writeByte)
    return

def printDvRegOneByte(i2cPort, dvAddrByte, DvRegAddrByte, printTitle):
    readByte = readDvRegOneByte(i2cPort, dvAddrByte, DvRegAddrByte)
    print(printTitle, hex(readByte))

# *** Test Functions ***

def testWriteReadPrintDayReg(testDataByte):
    printDvRegOneByte(i2cCh, dvAddrByte, dayRegAddrByte, 'Byte Read    =')
    writeDvRegOneByte(i2cCh, dvAddrByte, dayRegAddrByte, testDataByte)
    print('Byte Written =', hex(testDataByte))
    printDvRegOneByte(i2cCh, dvAddrByte, dayRegAddrByte, 'Byte Read    =')
    return

def test():
    print('\nWrite Read Day Register #1')
    testWriteReadPrintDayReg(0x5)
    print('\nWrite Read Day Register #2')
    testWriteReadPrintDayReg(0x7)
    return

# *** Main ***

test()

# *** Sample Output ***

# *** End ***
I am an electronics and smart home hobbyist.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30178
Joined: Sat Jul 30, 2011 7:41 pm

Re: Cat Home DIY Q&A - RTC How To

Thu Jan 24, 2019 3:11 pm

Please keep this all in one thread.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Working in the Applications Team.

Return to “Automation, sensing and robotics”