วันพุธที่ 8 กรกฎาคม พ.ศ. 2558

Script ตั้งเวลา RaspberryPi จาก USB GPS



   การตั้งเวลาโดยอ้างเวลาดาวเทียม  GPS




   ถ้าหาก Raspberry pi รีสตาร์ทหรือดับไป แล้วติดเครื่องขึ้นมาใหม่ เวลาจะเป็นเท่าไร ดังนั้น ต้องเตรียม Script ทำการตั้งเวลา โดยอ่านค่าจาก USB GPS โดยคำสั่ง

   nano startup_time.sh

 #!/bin/sh

device1="/dev/ttyUSB0"
if [ -e "$device1" ]
then
 date -s '01/01/2014 00:01'
 sleep 1
 pkill ntpd
 pkill gpsd
 gpsd -b -n -D 2 /dev/ttyUSB0
 sleep 2
 GPSDATE=`gpspipe -w | head -10 | grep TPV | sed -r 's/.*"time":"([^"]*)".*/\1/' | head -1`
 echo $GPSDATE
 date -s "$GPSDATE"
 /usr/sbin/ntpd
 echo 'Finnish seting Date'
#python /home/pi/my_python/lcd-date.py
 exit
else
 echo 'GPS does not Exit !!!!!!!'
fi 



  ทำการตั้ง Autorun on boot
 
  nano /etc/rc.local


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
 printf "My IP address is %s\n" "$_IP"
fi
   /home/pi/startup_time.sh
exit 0

ไม่มีความคิดเห็น:

แสดงความคิดเห็น