忘備録-備忘録

技術的な備忘録

起動時にNTPと時刻を合わせる

2018-11-13 20:06:00 | raspberry ...

Raspberry PiはRTCを持っていないために電源を切ると日付、時刻の設定が乱れてしまいます。電源投入時に1度だけNTPサーバにアクセスし時刻を取得するようにしました。

1.ntpdateのインストール

NTPサーバから時刻情報を取得するコマンドntpdateをインストールします。

$ sudo apt-get update

$ sudo apt-get install ntpdate

2.systemed設定ファイル作成

次の内容のファイルをntpdate.service名前で作成します。

[Unit]
Description=Set time with ntpdate
After=network.target

[Service]
# use -u to avoid conflict with ntpd
ExecStart=/usr/sbin/ntpdate -u pool.ntp.org

# we may not have network yet, so retry until success
Restart=on-failure
RestartSec=60s

[Install]
WantedBy=multi-user.target

3.サービスとして起動

作成したファイルをコピーしサービスとして起動させます。

$ sudo cp ntpdate.service /lib/systemd/system

$ sudo systemctl enable ntpdate.service


起動時にNTPと時刻を合わせる2に別のやり方を書きました。参考にしてください。

Raspberry pi Zero W を USB OTG Serialで使う

2018-11-13 19:32:00 | raspberry ...

Raspberry Pi Zero WをUSBケーブル1本で使用する方法がここに書いてありました。Raspberry Pi Zero WのUSB端子をUSB serial デバイスとして設定しホストコンピュータからアクセスします。WindowsからCOM4と認識されたRaspberry Pi Zero WのUSBポートにアクセスしているところを次に示します。

image

 

設定方法

一度、通常起動してからの設定が簡単です。普通にインストール&起動させてから /boot/config.txt,/boot/cmdline.txt を書き換えます。

/boot/config.txt ファイル

#ファイルの最後に2行追加する
enable_uart=1
dtoverlay=dwc2

/boot/cmdline.txtファイル

改行せずに1行で入力します。

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=xxxxxxxx-xx rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_serial

root=PARTUUID=xxxxxxxx-xx の部分はインストールするごとに変わります。rootwaitの後を書き換えます。

最後にgettyが動作するようにコマンドラインから次のように入力します。

$ sudo ln –s /lib/systemd/system/getty@.service /etc/systemd/system/getty.target.wants/getty@ttyGS0.service

SDカードにRasberianをインストールし、起動前に設定を行い、USBケーブル1本のみで動作させることもできます。しかし、最後の設定はWindows上から行うことができません。ホストマシンがLinuxであれば最後の設定は、SDカードをホストLinuxマシンにマウントし、次のように行います。

$ sudo ln -s /lib/systemd/system/getty@.service /media/user/uuid-of-sd-card/etc/systemd/system/getty.target.wants/getty@ttyGS0.service

 

DSC_0950 (2)