LoginSignup
1
1

More than 1 year has passed since last update.

【個人的備忘録】Raspberry Pi 3BでGPS同期のNTPサーバを構築した話

Posted at

みちびき対応GPSモジュールの取り付け

秋月で2100円(2021年6月現在)で売っているGPSモジュールを利用する.

それぞれの信号線をRaspberryPiのGPIOに接続する.

この時,TXとRXが交差するように接続する.

1PPS信号線はGPIO18に接続する.

NTPサーバ側(Raspberry Pi)の設定

#まずはアプデ
sudo apt update
sudo apt upgrade

#関連ライブラリインストール
sudo apt install gpsd gpsd-clients pps-tools ntp

#Serial0を使わないように設定
sudo nano /boot/cmdline.txt 
    console=serial0,115200を削除

#pps信号受信,UART有効化のための設定
sudo nano /boot/config.txt 
    末尾に
    core_freq=250
    enable_uart=1
    dtoverlay=pps-gpio,gpiopin=18,assert_falling_edge=true
    を追記

#pps信号受信設定
sudo nano /etc/modules
    末尾に
    pps-gpio
    を追記

#gpsdの設定
sudo nano /etc/default/gpsd
    # Default settings for the gpsd init script and the hotplug wrapper.

    # Start the gpsd daemon automatically at boot time
    START_DAEMON="true"

    # Use USB hotplugging to add new USB devices automatically to the daemon
    USBAUTO="false"

    # Devices gpsd should collect to at boot time.
    # They need to be read/writeable, either by user gpsd or the group dialout.
    DEVICES="/dev/ttyS0 /dev/pps0"

    # Other options you want to pass to gpsd
    GPSD_OPTIONS="-n"

    ↑私の環境での設定例(他もこれで行けるはず)

#再起動
sudo reboot

#GPS情報が取得できているか確認
gpsmon

#ntpの設定
sudo nano /etc/ntp.conf

    # /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
    driftfile /var/lib/ntp/ntp.drift


    ##以下,ログに関する処理.適宜コメントアウト解除のこと
    #logfile /var/log/ntp.log
    #logconfig =all

    # Enable this if you want statistics to be logged.
    #statsdir /var/log/ntpstats/
    #statsdir /var/log/ntpstats/
    #statistics loopstats peerstats clockstats
    #filegen loopstats file loopstats type day enable
    #filegen peerstats file peerstats type day enable #ここを有効化すると,GPS時刻とラズパイ時刻のずれ(offset)をロギングできる
    #filegen clockstats file clockstats type day enable

    # You do need to talk to an NTP server or two (or three).
    #server ntp.your-provider.example

    # pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
    # pick a different set every time it starts up.  Please consider joining the
    # pool: <http://www.pool.ntp.org/join.html>

    #pool 0.debian.pool.ntp.org iburst
    #pool 1.debian.pool.ntp.org iburst
    #pool 2.debian.pool.ntp.org iburst
    #pool 3.debian.pool.ntp.org iburst

    #PPS信号のアドレス設定
    server 127.127.22.0  minpoll 4  maxpoll 4 #2^4=16sごとに時間同期する.
    fudge 127.127.22.0  refid PPS stratum 0 flag3 1 flag4 1

    #GPS情報のアドレス設定(Stratum 1)
    server 127.127.28.2 minpoll 4 maxpoll 4 prefer 
    fudge 127.127.28.2 refid SHM2 stratum 1

    # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
    # details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
    # might also be helpful.
    #
    # Note that "restrict" applies to both servers and clients, so a configuration
    # that might be intended to block requests from certain clients could also end
    # up blocking replies from your own upstream servers.

    # By default, exchange time with everybody, but don't allow configuration.
    #restrict -4 default kod notrap nomodify nopeer noquery limited
    #restrict -6 default kod notrap nomodify nopeer noquery limited

    # Local users may interrogate the ntp server more closely.
    restrict 127.0.0.1
    restrict ::1

    # Needed for adding pool entries
    #restrict source notrap nomodify noquery

    # Clients from this (example!) subnet have unlimited access, but only if
    # cryptographically authenticated.

    #LAN内の端末からのアクセスを許可
    restrict x.x.x.0 mask 255.255.255.0 nomodify notrap noquery #IPアドレスに関して,Xの部分は自分のネットワーク環境に合わせる


    # If you want to provide time to your local subnet, change the next line.
    # (Again, the address is an example only.)
    #broadcast 192.168.123.255

    # If you want to listen to time broadcasts on your local subnet, de-comment the
    # next lines.  Please do this only if you trust everybody on the network!
    #disable auth
    #broadcastclient

#再起動
sudo reboot
#GPSとのずれ確認
watch -n 1 sh -c "ntpstat; ntpq -p -crv;"

クライアント側の設定(別のラズパイとかJetsonNanoとかUbuntu搭載PCとか)

systemd-timesyncdに設定する.

sudo nano /etc/systemd/timesyncd.conf
    以下のように設定する.
    [Time]
    NTP=ntp_server_ip_address

NTPで設定したサーバ以外は時刻同期しないようにする

サーバ,クライアントともにこの操作を行う.

#Raspberry Pi
sudo vi /etc/dhcpcd.conf
    option ntp_servers
    をコメントアウト

#Ubuntu
sudo nano /etc/systemd/timesyncd.conf
    以下のように,コメントアウト
    [Time]
    #FallbackNTP=ntp.ubuntu.com

参考

Raspberry PiでLANケーブル1本で窓際に設置できるNTPサーバを作るには

Raspberry pi3とGPSモジュールを接続してNTPサーバを構築

Raspberry Pi + GPS + NTP

GPSのPPS信号を使った Stratum-1 NTPサーバの作り方

1
1
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
1
1