rtc.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/bash
  2. set -e
  3. Version=1.0
  4. echo -e "\e[93mHeadless Node RTC addon v$Version :\e[0m"
  5. echo -e "wareck@gmail.com"
  6. echo -e ""
  7. if [ -f /boot/dietpi.txt ];then DietPi_="YES";else DietPi_="NO";fi
  8. if ! [ -f .rtc ]
  9. then
  10. echo -e "This script will install RTC addon."
  11. echo -e "This will be done in three steps:"
  12. echo -e "1: Enable and install i2c prototcol to dialog with RTC board."
  13. echo -e "2: Reboot."
  14. echo -e "3: Install software , init RTC clock and synchronise clock."
  15. echo -e ""
  16. echo -e "After reboot, start this script again to do the second step."
  17. echo -e
  18. if [ $DietPi_ = "YES" ]
  19. then
  20. echo -e "\e[38;5;154mDietpi\e[0m image detected:"
  21. echo -e "This script will enable /etc/rc.local for AutoStart"
  22. echo -e "(disabled by default on Dietpi).\n"
  23. fi
  24. read -n 1 -r -s -p "Press any key to continue..."
  25. echo -e "\n\n\e[95mRaspberry update :\e[0m"
  26. sudo apt-get update
  27. echo -e "\n\e[95mInstalling libraries :\e[0m"
  28. sudo apt install python-smbus python3-smbus python-dev python3-dev i2c-tools rpi.gpio -y
  29. if ! grep "dtparam=i2c1=on" /boot/config.txt
  30. then
  31. sudo bash -c 'echo "" >>/boot/config.txt'
  32. sudo bash -c 'echo "# Enable i2c for RTC" >>/boot/config.txt'
  33. sudo bash -c 'echo "dtparam=i2c1=on" >>/boot/config.txt'
  34. fi
  35. if ! grep "i2c-dev" /etc/modules ; then sudo bash -c 'echo "i2c-dev" >>/etc/modules';fi
  36. if ! grep "i2c-bcm2708" /etc/modules ; then sudo bash -c 'echo "i2c-bcm2708" >>/etc/modules';fi
  37. if ! grep "rtc-ds1307" /etc/modules ; then sudo bash -c 'echo "rtc-ds1307" >>/etc/modules';fi
  38. if ! grep "rtc-pcf8563" /etc/modules ; then sudo bash -c 'echo "rtc-pcf8563" >>/etc/modules';fi
  39. if ! grep "dtoverlay=i2c-rtc,ds1307" /boot/config.txt >/dev/null
  40. then
  41. sudo bash -c 'echo "" >>/boot/config.txt'
  42. sudo bash -c 'echo "# RTC clock ds1307 based" >>/boot/config.txt'
  43. sudo bash -c 'echo "dtoverlay=i2c-rtc,ds1307" >>/boot/config.txt'
  44. fi
  45. echo -e "\n\e[31mFirst step of RTC install was done.\e[0m"
  46. echo -e "\e[31mYou need to reboot and start rtc.sh again!\n\e[0m"
  47. touch .rtc && exit 0
  48. fi
  49. function Startup_ {
  50. echo -e "\n\e[95mAdding RTC to RPI startup:\e[0m"
  51. if [ $DietPi_ = "YES" ];then Dietpi_startup_;fi
  52. if ! grep "echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device || true" /etc/rc.local >/dev/null 2>&1
  53. then
  54. sudo bash -c 'sed -i -e "s/exit 0//g" /etc/rc.local'
  55. sudo bash -c 'echo "#RTC module init" >>/etc/rc.local'
  56. sudo bash -c 'echo "echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device || true" >> /etc/rc.local'
  57. sudo bash -c 'echo "sudo hwclock -s" >>/etc/rc.local'
  58. sudo bash -c 'echo "exit 0" >>/etc/rc.local'
  59. fi
  60. echo -e "Done."
  61. echo -e "\n\e[95mDisable fake RTC service:\e[0m"
  62. sudo apt-get -y remove fake-hwclock
  63. sudo update-rc.d -f fake-hwclock remove
  64. sudo systemctl disable fake-hwclock || true
  65. sudo systemctl disable fake-hwclock.service || true
  66. echo -e "Done."
  67. echo -e "\n\e[95mRTC Checks:\e[0m"
  68. echo -e -n "Internal time: "
  69. date
  70. echo -e -n "RTC module time: "
  71. sudo bash -c 'hwclock -r'
  72. echo -e ""
  73. echo -e "\e[97mRTC Installation was finish !\e[0m\n"
  74. }
  75. function install_step2 {
  76. sudo modprobe i2c-dev
  77. echo -e "\n\e[95mCreate device:\e[0m"
  78. sudo bash -c 'echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device > /dev/null 2>&1' || true
  79. echo "Done."
  80. echo -e "\n\e[95mRTC clock reset:\e[0m"
  81. sudo bash -c 'hwclock -r'
  82. echo -e "\n\e[95mRTC clock setup :\e[0m"
  83. sudo bash -c 'hwclock -w'
  84. echo "Done."
  85. Startup_
  86. }
  87. function Dietpi_startup_ {
  88. if ! [ -f /etc/rc.local ]
  89. then
  90. cat <<'EOF'>>/tmp/rc.local
  91. #!/bin/bash
  92. exit 0
  93. EOF
  94. sudo cp /tmp/rc.local /etc/
  95. sudo chmod +x /etc/rc.local
  96. fi
  97. sudo systemctl disable rc-local
  98. sudo systemctl disable rc.local
  99. cat <<'EOF'>> /tmp/rc-local.service
  100. # This file is part of systemd.
  101. #
  102. # systemd is free software; you can redistribute it and/or modify it
  103. # under the terms of the GNU Lesser General Public License as published by
  104. # the Free Software Foundation; either version 2.1 of the License, or
  105. # (at your option) any later version.
  106. # This unit gets pulled automatically into multi-user.target by
  107. # systemd-rc-local-generator if /etc/rc.local is executable.
  108. [Unit]
  109. Description=/etc/rc.local Compatibility
  110. ConditionFileIsExecutable=/etc/rc.local
  111. After=network.target
  112. [Service]
  113. Type=forking
  114. ExecStart=/etc/rc.local start
  115. TimeoutSec=0
  116. RemainAfterExit=yes
  117. GuessMainPID=no
  118. EOF
  119. sudo cp /tmp/rc-local.service /lib/systemd/system/rc-local.service
  120. sudo systemctl daemon-reload
  121. }
  122. function rtc_check {
  123. echo -e "\e[95m...Install step 2...\e[0m\n"
  124. echo -e "\e[95mRTC check :\e[0m"
  125. sudo su -c 'echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device' > /dev/null 2>&1 | true
  126. sudo i2cdetect -y 1
  127. echo -e "\e[31mDS1307 RTC module: You must see \"UU\" on address 68.\e[0m"
  128. echo -e "\e[31mPCf8563 RTC module: You must see \"UU\" on address 51.\e[0m"
  129. echo -e "\e[31mIf not, double check your wiring and try again.\e[0m"
  130. echo -e "\e[31mIf still not, choose \"Quit\" and follow instructions.\e[0m\n"
  131. PS3='
  132. Please enter your choice: '
  133. options=("Check again" "Continue" "Quit")
  134. select opt in "${options[@]}"
  135. do
  136. case $opt in
  137. "Check again")
  138. rtc_check
  139. break
  140. ;;
  141. "Continue")
  142. break
  143. ;;
  144. "Quit")
  145. echo -e "\n\e[93mIf you don't have \"UU\" on address 68\e[0m:"
  146. echo -e "\n\e[93mFirst double check your wiring.\e[0m\n"
  147. echo -e "\e[93mthen type :\e[0m"
  148. echo -e "\e[93msudo su\e[0m"
  149. echo -e "\e[93mecho ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device\e[0m"
  150. echo -e "\e[93mexit\e[0m"
  151. echo -e ""
  152. echo -e "\e[93mthen \e[0m"
  153. echo -e "\e[93m./rtc.sh\n\e[0m"
  154. exit 0 && break
  155. ;;
  156. *) echo invalid option;;
  157. esac
  158. done
  159. }
  160. if [ -f .rtc ]
  161. then
  162. rtc_check
  163. install_step2
  164. fi