วันจันทร์ที่ 13 ตุลาคม พ.ศ. 2557

การตั้งวันที่ เวลา ให้ raspberry pi

การตั้งวันที่

sudo date mmddHHMMCCYY

mm = เดือน
dd = วันที่
HH = ชั่วโมง
MM = นาที
CCYY = ปี เช่น 2014

ยกตัวอย่างเช่น เดือน 10 วันที่ 13 15.50 น. ปี 2014
sudo date 101315502014

แล้วลองใช้คำสั่ง date ดูครับ เวลาควรจะเปลี่ยนตามที่เราได้ตั้งไว้

ที่มา
http://spalinux.com/2012/04/change-linux-date-time-using-command-date




ข้อความด้านล่างนี้ ผู้เขียนได้ลองแล้ว แต่ไม่ได้ผล อาจจะเพราะ ทำไม่ถูกเอง หรือ ไม่ได้ผลจริงๆ ก็ไม่แน่ใจเหมือนกัน ใครทำได้ ลองช่วยมาแชร์ให้หน่อยก็ดีครับ จะขอบคุณอย่างยิ่ง


แต่หลังจากตั้งวันที่ให้ Rpi แล้ว เราก็จะเกิดปัญหาใหม่ คือ เมื่อเราปิดไฟหรือ shutdown ไปแล้ว แล้วมาเปิดเครื่องใหม่ เวลาของ Rpi จะไม่ update
ดังนั้นเราต้องมี script ไว้สำหรับ update time เมื่อฟื้นขึ้นมาใหม่ แต่วิธีนี้ก็มีเงื่อนไขอยู่ตรงที่ว่าต้องต่อ internet ด้วย


Before you use the script, you'll need to install ntplib. The download link and installation instructions are here:https://pypi.python.org/pypi/ntplib/.
Copy the the contents of the code below to a file (I named mine "synctime.py"). Then, after the network is running, execute the script (e.g. "python synctime.py"). You will need to do this using elevated (root) privileges for this to work properly.

import time
import os

try:
    import ntplib
    client = ntplib.NTPClient()
    response = client.request('pool.ntp.org')
    os.system('date ' + time.strftime('%m%d%H%M%Y.%S',time.localtime(response.tx_time)))
except:
    print('Could not sync with time server.')

print('Done.')

ที่มา
http://stackoverflow.com/questions/5222951/easy-way-to-get-the-correct-time-in-python



วันนี้ 06/12/2559 ผมมีโอกาสได้ลองใหม่ ดังนี้
sudo apt-get udpate
sudo apt-get install ntpdate
sudo raspi-config
เข้าไปตั้งค่า time zone ให้เป็น asia/bangkok
เรียบร้อย


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