วันอังคารที่ 4 พฤศจิกายน พ.ศ. 2557

format sd-card

สวัสดีครัช พี่น้อง
หายกันไปนานเบย(อีกแล้ว) คราวนี้มาบอกเกร็ดเล็กเกร็ดน้อยครับ

เมื่อเรานำ sd-card ไปลง image rasbian แล้ว เมื่อเราถอด sd-card ไปเสียบที่เครื่อง pc เราจะเห็นพื้นที่เหลืออยู่นิดเดียว ซึ่งถ้าเราต้องการ format ทั้งหมดเพื่อนำไปใช้อย่างอื่นต่อ จะมีวิธีการดังต่อไปนี้ครับ

1. download โปรแกรม SD Formatter ก่อน ถ้าเราใช้เครื่อง pc windows ก็เลือกของ windows ถ้าเราใช้เครื่อง pc mac ก็เลือกของ mac


รูปที่ 1 download โปรแกรม SD Formatter

2. install SD Formatter
3. เปิดโปรแกรม SD Formatter แล้วเลือก drive ที่เสียบ SD Card ไว้


รูปที่ 2 โปรแกรม SD Formatter

4. เลือก option
5. เลือก format size adjustment เป็น on แล้วกด ok


รูปที่ 3 size adjustment


6. คลิก format และ ok


รูปที่ 4 หลังจาก Format เรียบร้อยแล้ว

size ของ sd card ก็จะกลับมาใหญ่เหมือนเดิม

วันจันทร์ที่ 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
เรียบร้อย


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

ทำ printer ธรรมดาให้เป็น wireless printer

เดี๋ยวผมจะลองทำดู ตามนี้เลย http://www.instructables.com/id/Turn-any-printer-into-a-wireless-printer-with-a-Ra/
หรือว่าใครทำแล้ว ลองมา update ให้ดูกันหน่อยก็ได้นะครับ

วันพุธที่ 1 ตุลาคม พ.ศ. 2557

python รับค่าจาก keyboard, อ่านค่าเวลา, การเปิดไฟล์ ปิดไฟล์ อ่านไฟล์ เขียนไฟล์, ออกจากโปรแกรม, keyboard interrupt, การหาบรรทัดล่างสุดของไฟล์, การแยก text ด้วย split

สวัสดีครัชพี่น้อง

วันนี้เป็นเกล็ดเล็กเกล็ดน้อย เกี่ยวกับการเขียนโปรแกรม python script บน RPi ครับ

การรับค่าจาก keyboard
ก็ง่ายๆครับ ตามนี้เลย
number = input("please input your choice : ")
ที่มา
https://sites.google.com/site/dotpython/input-and-output/2-2-list



การอ่านค่าเวลา
import time
my_time = time.time()
ที่มา
http://tutor0x.blogspot.com/2011/11/python_19.html

การ เปิด, write, close file
example_file = open('your_file_name.txt', 'w')
example_file .write("hello world\n")
example_file .close()
ที่มา
http://citecclub.org/forum/python-articles-59/%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B9%80%E0%B8%9B%E0%B8%B4%E0%B8%94%E0%B9%84%E0%B8%9F%E0%B8%A5%E0%B9%8C%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2%E0%B9%84%E0%B8%9E%E0%B8%98%E0%B8%AD%E0%B8%99-1151/

http://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python/2676-%E0%B8%9F%E0%B8%B1%E0%B8%87%E0%B8%81%E0%B9%8C%E0%B8%8A%E0%B8%B1%E0%B9%88%E0%B8%99%E0%B9%80%E0%B8%81%E0%B8%B5%E0%B9%88%E0%B8%A2%E0%B8%A7%E0%B8%81%E0%B8%B1%E0%B8%9A-%E0%B9%84%E0%B8%9F%E0%B8%A5%E0%B9%8C--python.html

http://stackoverflow.com/questions/4488570/how-do-i-write-a-tab-in-python


การออกจากโปรแกรม
import sys
sys.exit()


keyboard interrupt

To solve the exception problem, add a separate except-clause that catches theKeyboardInterrupt exception, and raises it again:
for record in database:
    try:
        process(record)
        if changed:
            update(record)
    except KeyboardInterrupt:
        raise
    except:
        # report error and proceed
or, even better:

for record in database:
    try:
        process(record)
        if changed:
            update(record)
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        # report error and proceed



การหาบรรทัดล่างสุด


  f = open('file.txt', "r")
  firstLine = f.readline()
  f.seek(-2,2)
  while f.read(1) != "\n":
    f.seek(-2,1)
  last = f.readline()

  print "Last Line: %s" %(last)

ที่มา
http://stackoverflow.com/questions/3346430/most-efficient-way-to-get-first-and-last-line-of-file-python


การแยก text ด้วย split

จากตัวอย่าง "การหาบรรทัดล่างสุด" เราได้ตัวแปรที่ชื่อว่า last ที่เก็บข้อมูลของบรรทัดสุดท้ายอยู่
โดยเมื่อเราใช้ method "split" เราก็จะได้ข้อมูลใหม่ ที่เป็น array ออกมา
มีวิธีการใช้ ดังตัวอย่างต่อไปนี้

สมมติว่าใน ตัวแปร last มีข้อมูล "1\t2\n" import array


a = last.split("\t",1) ======================== (1)

print str(a[0]) ========================(2)
print str(a[1]) ========================(3)

(1) แยกข้อมูลโดยใช้ \t เป็นจุดแยกข้อมูล จากตัวแปร last แล้วเก็บไว้ในตัวแปร array a
(2) สั่ง print ค่าที่อยู่ในตัวแปร array a ตัวที่ 0 ซึ่งผลลัพธ์ออกมาคือ "1"
(3) สั่ง print ค่าที่อยู่ในตัวแปร array a ตัวที่ 1 ซึ่งผลลัพธ์ออกมาคือ "2"

วันจันทร์ที่ 29 กันยายน พ.ศ. 2557

python print จากข้อมูลที่เป็น int ให้เป็น string

ถ้าเราต้องการจะ print ข้อมูลที่รับมาเป็น int โดยให้แสดงผลในรูปของตัวอักษร (string)
ให้ใช้ chr(x) นะครัช
(งงอยู่เป็นชั่วโมง เฮ้ออออ)


def WaitResponseVR(DataNum):
  rcv = port1.read(DataNum)
  if rcv != '':
   return ord(rcv)  ============== แปลง character ตัวเดี่ยวๆ เป็นค่า integer.
  else:
    return ''"


for y in range (0,72):
    ans = WaitResponse(1)
    if ans == "":
      print "No response"
    else:
      Data += chr(ans) ================== แปลงจาก int เป็น string
  print Data

การต่อพ่วงอุปกรณ์หลายๆตัวบน Rpi

บางครั้งเราอาจจำเป็นต้องต่ออุปกรณ์หลายๆอย่างเข้ากับ Rpi ซึ่งอาจจะเป็นสาเหตุทำให้อุปกรณ์ของเราทำงานไม่สมบูรณ์ หรือ ไม่ทำงานเลย เนื่องจากกำลังไฟของ Rpi มีค่อนข้างจำกัด

วันนี้ผู้เขียนได้ต่อ usb to serial จำนวน 2 ตัวเข้ากับ Rpi
ตอนแรกก็ลองเขียน code ดู คิดว่าใช้ไฟแค่นี้ไม่น่าจะเป็นปัญหา แต่ทดลองไปทดลองมา ทำยังไง้ ก็ไม่มีอะไรส่งออกมาจาก serial เลย

เลยเอะใจ ไหนลองต่อ usb hub แล้วทดสอบดูใหม่ซิ

ออกเฉย!!!

work ซะงั้น

import serial
import time

port = serial.Serial("/dev/ttyUSB0", baudrate=19200, timeout=1.0)
port = serial.Serial("/dev/ttyUSB1", baudrate=19200, timeout=1.0)

while 1:
  port.write(chr(0x01))
  time.sleep(1)





เวลาท่านใดทำโปรเจคด้วย Rpi แล้วมีอุปกรณ์ต่อพ่วงเยอะหน่อย แต่ทำแล้วไม่ work ก็ลองใช้ usb hub ดูนะครับ :)

วันอังคารที่ 23 กันยายน พ.ศ. 2557

python serial read

วันนี้ได้เขียนหลายเรื่องเลย งานเร่งด่วนมาก
ตอนนี้จะเกี่ยวกับการอ่านค่า ที่ได้รับจาก serial port

ตัวอย่าง code

#! /usr/bin/python

import time
import serial

port = serial.Serial("/dev/ttyUSB0", baudrate=19200, timeout=1.0)

i = 1

while 1:
  port.write(chr(i))
  rcv = port.read(1) ================== (1)
  if rcv != '':================== (2)
    ans = ord(rcv) ================== (3)
    if ans == 0x61:
      print "Status Ready"
    else:
      print rcv
  else: ================== (4)
    print "Loss communication"


(1) คืออ่านค่าจาก serial port ใส่ในตัวแปร rcv
(2) ตรวจสอบว่าค่า rcv ที่อ่านมา มีค่าอะไรอยู่หรือไม่ ถ้ามีก็ทำต่อไป ถ้าไม่มีก็กระโดดไปที่ (4)
(3) เปลี่ยนตัวแปร rcv ที่เป็น string ให้เป็น int เพื่อที่เราจะสามารถเปรียบเทียบค่ากับ hex ที่เราต้องการได้


ที่มา http://stackoverflow.com/questions/12929593/how-to-test-for-hexidecimal-output-on-serial-port-in-python

python port.write

ในการเขียน python ให้ส่งออกข้อมูลผ่าน serial port มีอยู่ 2-3 แบบ ที่ผู้เขียนเจอมา

โดยมีตัวอย่าง code ดังต่อไปนี้

import serial
import time

port = serial.Serial("/dev/ttyUSB0", baudrate=19200, timeout=1.0)

while 1:
  port.write(chr(0x01))
  time.sleep(1)

โดยที่เห็นใน code ด้านบน จะส่งออกมาเป็น 0x01 ฐาน16 จริงๆ



แต่ถ้าเราเปลี่ยน code เป็น port.write("1") เราจะเห็นข้อมูลต่างออกไป คือ 0x31



แต่ถ้าเราเปลี่ยน code เป็น port.write("0x01")
สิ่งที่ RPi ส่งออกมาจะเป็น 0x30 0x78 0x30 0x31 ซึ่งถ้าเปลี่ยนเป็น ascii เราจะเห็นเป็น 0x01




สรุป
การส่งข้อมูล serial

port.write(chr(0x01))
port.write("1")
port.write("0x01")

ให้ผลที่ต่างกัน เราสามารถเลือกใช้ให้เหมาะสมกับงานของเราได้ครัช