DS 1307 RTC Simple Bash Client



  • Connecting to DS 1307 RTC using i2cset, i2cget commands with shell scripting.


    1. Pinout
    DS 1307 Omega2+
    GND GND
    VCC 5V
    SDA SDA
    SCL SCL
    DS N/C
    1. Script
    • Save this script under directory /root/bin as rtc "/root/bin/rtc"
    • Other paths are ok but you should change line 101.
    • Give exec the script chmod +x /root/bin/rtc
    #!/usr/bin/env ash
    
    #~ /*****************************************
    #~ * Platform	: Omega2 / Omega2+
    #~ * App		: DS 1307 RTC Simple Bash Client 
    #~ * File		: rtc.sh
    #~ * Date		: Tue Jun 12 11:27:40 EEST 2018
    #~ * Version	: 1.0.0
    #~ * Email		: info@yasinkarabulak.com
    #~ * © Yasin KARABULAK
    #~ * Licence 	: MIT License
    #~ * 
    #~ *******************************************/
    
    SUFFIX="Rtc"
    
    # device address 0x68
            # reg control   0x07
            # year          0x06
            # month         0x05
            # date          0x04
            # day           0x03
            # hour          0x02
            # minute        0x01
            # second        0x00
    
    REGCONTROL=$(i2cget -y 0 0x68 0x07)
    YEAR=$(i2cget -y 0 0x68 0x06)
    MONTH=$(i2cget -y 0 0x68 0x05)
    DATE=$(i2cget -y 0 0x68 0x04)
    DAYOFWEEK=$(i2cget -y 0 0x68 0x03) # day of week 1-7 monday-sunday
    HOUR=$(i2cget -y 0 0x68 0x02)
    MINUTE=$(i2cget -y 0 0x68 0x01)
    SECOND=$(i2cget -y 0 0x68 0x00)
    
    function setRtc {
            i2cset -y 0 0x68 0x06 0x$(date +%y)
            i2cset -y 0 0x68 0x05 0x$(date +%m)
            i2cset -y 0 0x68 0x04 0x$(date +%d)
            i2cset -y 0 0x68 0x03 0x$(date +%u)
            i2cset -y 0 0x68 0x02 0x$(date +%H)
            i2cset -y 0 0x68 0x01 0x$(date +%M)
            i2cset -y 0 0x68 0x00 0x$(date +%S)
            sleep 1
            /root/bin/rtc
            exit
    }
    
    function yearRtc {
            printf "20%.2x" $YEAR
    }
    
    function monthRtc {
            printf "%.2x" $MONTH
    }
    
    function dayRtc {
            printf "%.2x" $DATE
    }
    
    function hourRtc {
            printf "%.2x" $HOUR
    }
    
    function minuteRtc {
            printf "%.2x" $MINUTE
    }
    
    function secondRtc {
            printf "%.2x" $SECOND
    }
    
    function dayofweekRtc {
            printf "%.2x" $DAYOFWEEK
    }
    
    function dateRtc {
            echo "$(yearRtc)$(monthRtc)$(dayRtc)$(hourRtc)$(minuteRtc)$(secondRtc)"
    }
    
    function helpRtc {
    	echo -e "-------------------------------"
    	echo -e "Commands [multi use permitted!]"
    	echo -e "-------------------------------"
    	echo -e "<set> Sets the RTC DS1307"
    	echo -e "<year> Show the year from RTC"
    	echo -e "<month> Show the month from RTC"
    	echo -e "<day> Show the day from RTC"
    	echo -e "<hour> Show the hour from RTC"
    	echo -e "<minute> Show the minute from RTC"
    	echo -e "<second> Show the second from RTC"
    	echo -e "<dayofweek> Show which day of the week from RTC"
    	echo -e "<date> Show the date [YYYYMMDDHHMMSS] from RTC"
    	echo -e "<setSysDate> Set the system date from RTC"
    	echo -e "<help> Show this help and exit"
    }
    
    function setSysDateRtc {
            date --set="$(yearRtc)-$(monthRtc)-$(dayRtc)" > /dev/null
            date --set="$(hourRtc):$(minuteRtc):$(secondRtc)" > /dev/null
            echo "RTC Date : " $(/root/bin/rtc)
            echo "SYS Date : " $(date +%Y-%m-%d" "%H:%M:%S)
    }
    
    if [ -z "$1" ]; then
            YEAR=$(yearRtc)
            MONTH=$(monthRtc)
            DATE=$(dayRtc)
            HOUR=$(hourRtc)
            MINUTE=$(minuteRtc)
            SECOND=$(secondRtc)
            DAYOFWEEK=$(dayofweekRtc)
    
        TARIH="$YEAR-$MONTH-$DATE $HOUR:$MINUTE:$SECOND"
        echo $TARIH
    exit
    fi
    
    for arg in $@; do
            EXISTS=type "$arg$SUFFIX" &>/dev/null && eval "$arg$SUFFIX" || helpRtc
    done
    
    echo 
    
    
    1. Using
    root@Omega-C617:~# rtc help
    -------------------------------
    Commands [multi use permitted!]
    -------------------------------
    <set> Sets the RTC DS1307
    <year> Show the year from RTC
    <month> Show the month from RTC
    <day> Show the day from RTC
    <hour> Show the hour from RTC
    <minute> Show the minute from RTC
    <second> Show the second from RTC
    <dayofweek> Show which day of the week from RTC
    <date> Show the date [YYYYMMDDHHMMSS] from RTC
    <setSysDate> Set the system date from RTC
    <help> Show this help and exit
    
    root@Omega-C617:~# /root/bin/rtc
    2018-06-12 11:55:50
    Show help: <rtc help>
    
    root@Omega-C617:~# /root/bin/rtc year month day
    20180612
    
    root@Omega-C617:~# /root/bin/rtc day month year
    12062018
    
    root@Omega-C617:~# /root/bin/rtc hour minute second
    115633
    
    root@Omega-C617:~# /root/bin/rtc setSysDate
    RTC Date :  2018-06-12 11:56:46 Show help: <rtc help>
    SYS Date :  2018-06-12 11:56:46
    

    Notes :

    • rtc set command is sets the RTC Clock using system date,
    • rtc setSysDate command is sets the system date using RTC date.

Log in to reply
 

Looks like your connection to Community was lost, please wait while we try to reconnect.