LoginSignup
4
3

More than 5 years have passed since last update.

Raspberry-pi で起動時に自動的に時刻合わせをする

Last updated at Posted at 2018-08-25

背景

Raspberry-pi で時刻合わせがなぜか自動でキマらないのでやりました。

開発環境

Raspberry-pi 3B
Python 2.7.13

コード

以下、コードになります。
ntplibを用いてdateで合わせます。時刻取得部分についてはPyPlのexampleをそのまま流用しました。ntpサーバーはNICTから配信されているものを使ってます。

ntp_get.py
from time import ctime
import os
import ntplib

c = ntplib.NTPClient()
response = c.request('ntp.nict.jp', version=3)

print(response.offset)
#-0.143156766891
print(response.version)
#3
now=ctime(response.tx_time)
print(now)
# 'Sun May 17 09:32:48 2009'
print(ntplib.leap_to_text(response.leap))
# 'no warning'
print(response.root_delay)
# 0.0046844482421875
print(ntplib.ref_id_to_text(response.ref_id))
# 193.190.230.66

now_year = now[20:24]  
print(now_year)

now_day = now[8:10]  
print(now_day)

now_month = now[4:7]
if now_month=="Jan":
    now_month=1
if now_month=="Feb":
    now_month=2
if now_month=="Mar":
    now_month=3
if now_month=="Apr":
    now_month=4
if now_month=="May":
    now_month=5
if now_month=="Jun":
    now_month=6
if now_month=="Jul":
    now_month=7
if now_month=="Aug":
    now_month=8
if now_month=="Sep":
    now_month=9
if now_month=="Oct":
    now_month=10
if now_month=="Nov":
    now_month=11
if now_month=="Dec":
    now_month=12
print(now_month)

now_time = now[11:19]  
print(now_time)

os.system("sudo date -s"+" "+"'"+str(now_year)+"-"+str(now_month)+"-"+str(now_day)+" "+str(now_time)+"'")

あとは、出来たファイルに権限を付与し、スタートアップで起動するようにすれば完成です。

terminal
>>>chmod 755 ntp_get.py
~/.config/lxsession/LXDE-pi/autostart
@python /home/pi/python/ntp_get.py

参考文献

ntplib

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3