วันอังคารที่ 6 พฤษภาคม พ.ศ. 2557

raspberry pi 7-segment with 74hc595

สวัสดีครับเพื่อนๆทุกท่าน แม้ว่าจะมีไม่มาก :)

ต่อจากบทก่อนหน้านี้  ได้ศึกษากันไปแล้ว ในการใช้ python ควบคุมการทำงานของ LED หลอดเล็กๆ
ในตอนนี้ ผมได้ทำการต่อ raspberry pi เข้ากับ LED บอร์ดใหญ่ที่ใช้ไฟ 12 โวลต์(ซื้อจากฟิวเจอร์คิท)
                                  รูปแผง LED 4 หลัก ใช้ไฟเลี้ยง 12 โวลต์

ซึ่ง Raspberry pi อย่างที่ทราบกันแล้วว่าใช้ไฟแค่ 5 โวลต์ และ gpio มีไฟแค่ 3.3 โวลต์ ก็เลยจำเป็นต้องหา buffer มาคั่นกลาง เพื่อให้สามารถขับ LED แผงนี้ได้
โดยผมใช้ IC เบอร์ 7407 เป็น buffer ดังที่เคยเขียนไปในตอนก่อนหน้านี้แล้ว




 และเพื่อให้สามารถใช้ gpio ที่มีอยู่ได้อย่างพอเพียง จึงได้ใช้ IC เบอร์ 74hc595 ที่ทำให้เราใช้ gpio เพียง 5 ขาในการขับ LED ทั้ง 4 หลักนี้



โดยจากขา gpio ของ raspberry pi ต่อเข้ากับ 74hc595 ตรงๆก่อน แล้วต่อขา output ของ 74hc595(Q0-Q7) ต่อเข้ากับ input ของ 7407 (1A-6A)

แล้วนำ 1Y-6Y ต่อกับ LED ของเรา โดยที่มี r 4.7k pull up ไว้



เมื่อบัดกรีวงจรเสร็จเรียบร้อยแล้ว ก็เป็นอย่างที่เห็นคับ

แล้วผมก็ลองเขียน code โดยใช้ python ดู เพื่อลองขับ LED ก็สามารถใช้งานได้ครับ
โดยเราต้องนำ file ของเรา ไปวางไว้ที่ raspberry pi ก่อน
แล้วทำให้สามารถ run ได้โดยคำสั่ง chmod +x my_file.py
แล้วใช้คำสั่ง python my_file.py เพื่อ run โปรแกรม




ตัวอย่าง code ครับ
#! /usr/bin/python

import RPi.GPIO as GPIO
import time

#Data Pins needed on the RPi
DATAIN=11 #DS
LATCH=13  #STCP
CLOCK=15  #SHCP
CLEAR=22  #MR Low
OE=23     #Output Enable Low

#GPIO definition
def setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.cleanup()
    GPIO.setup(DATAIN,GPIO.OUT)
    GPIO.setup(CLOCK,GPIO.OUT)
    GPIO.setup(LATCH,GPIO.OUT)
    GPIO.setup(CLEAR,GPIO.OUT)
    GPIO.setup(OE,GPIO.OUT)

    GPIO.output(LATCH,False) #Latch is used to output the saved data
    GPIO.output(CLEAR,True)  #Clear must always be true. False clears registers
    GPIO.output(OE,False)    #Output Enable speaks for itself. Must be False to display
    GPIO.output(CLOCK,False) #Used to shift the value of DATAIN to the register
    GPIO.output(DATAIN,False)#Databit to be shifted into the register
def clean():
    GPIO.output(CLEAR,False)
    GPIO.output(CLEAR,True)
    GPIO.output(LATCH,True)
    GPIO.output(LATCH,False)

setup()
clean()
x=0

while 1:
 if x > 32:
  x=0
  clean()

 GPIO.output(DATAIN,True)
 GPIO.output(CLOCK,True)
 GPIO.output(CLOCK,False)

 GPIO.output(LATCH,True)
 GPIO.output(LATCH,False)
 time.sleep(0.06)
 x = x+1


ผมอาจจะอธิบายไม่ค่อยละเอียดนะครับ ถ้าหากมีข้อสงสัยก็ถามกันได้ ที่ niran@flowco.co.th ครับ

ที่มา
http://www.sysstem.at/2013/09/7-segment-display-with-74hc595-shift-register-and-raspberry-pi/



ตัวอย่างเพิ่มเติม

#! /usr/bin/python

import RPi.GPIO as GPIO
import time

#Data Pins needed on the RPi
DATAIN=11 #DS
LATCH=13  #STCP
CLOCK=15  #SHCP
CLEAR=22  #MR Low
OE=23     #Output Enable Low


#GPIO definition
def setup():
    GPIO.setmode(GPIO.BOARD)
    GPIO.cleanup()
    GPIO.setup(DATAIN,GPIO.OUT)
    GPIO.setup(CLOCK,GPIO.OUT)
    GPIO.setup(LATCH,GPIO.OUT)
    GPIO.setup(CLEAR,GPIO.OUT)
    GPIO.setup(OE,GPIO.OUT)

    GPIO.output(LATCH,False) #Latch is used to output the saved data
    GPIO.output(CLEAR,True)  #Clear must always be true. False clears registers
    GPIO.output(OE,False)    #Output Enable speaks for itself. Must be False to display
    GPIO.output(CLOCK,False) #Used to shift the value of DATAIN to the register
    GPIO.output(DATAIN,False)#Databit to be shifted into the register
#Clean up display
def clean():
    GPIO.output(CLEAR,False)
    GPIO.output(CLEAR,True)
    GPIO.output(LATCH,True)
    GPIO.output(LATCH,False)

#Shift data
def shift(BitOut):
  if BitOut == 1:
       BitOut=True
  else:
       BitOut=False
  GPIO.output(DATAIN,BitOut)
  GPIO.output(CLOCK,True)
  GPIO.output(CLOCK,False)

#Push Data to display
def WriteOut():
  GPIO.output(LATCH,True)
  GPIO.output(LATCH,False)

#write number
def WriteNum(num):
  for i in range(0,8):
    shift((num>>i)%2)

from array import *
number_array = array('i',[0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6])

setup()
clean()
x=0

for i in range(0,9999):
    x = i/1000
    WriteNum(number_array[x])
    x = (i%1000)/100
    WriteNum(number_array[x])
    x = (i%100)/10
    WriteNum(number_array[x])
    x = i%10
    WriteNum(number_array[x])
    WriteOut()   
    time.sleep(1)
    clean()




 

วันจันทร์ที่ 28 เมษายน พ.ศ. 2557

auto run your application

auto run your application


บทความวันนี้จะเกี่ยวกับเรื่อง auto run application
หลังจากที่เราเขียน application ตัวหนึ่งเสร็จแล้ว ขั้นตอนต่อไปก็คงต้องเป็นการตั้งค่าให้ Rpi run application ทันที หลังจากที่ boot เครื่องใหม่ (auto run)

ซึ่งก็ทำได้ไม่ยากครับ ดังนี้



แก้ไข rc.local
sudo nano /etc/rc.local
โดยเพิ่มบรรทัดนี้เข้าไป ในบรรทัดก่อนสุดท้าย (เหนือ exit 0)
sudo python /home/pi/my_project.py

หรือถ้าเป็น application อย่างอื่น ก็ลองแบบนี้ครับ
cd /home/pi/any_folder_u_define
sudo ./your_application

กด CTRL+X, "Y" และ Enter เพื่อออกและ save

ทดสอบด้วยการ reboot แล้วรอดูผลกันครับ :)

ที่มา
http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/scripts
http://stackoverflow.com/questions/22353134/raspberry-pi-auto-starting-a-program

วันศุกร์ที่ 25 เมษายน พ.ศ. 2557

เตรียมเครื่อง Linux PC ไว้สำหรับเล่น raspberry pi

1. have linux pc (ubuntu11)
2. sudo apt-get update
3. sudo apt-get install build-essential git
4. mkdir rpi in home directory
5. cd rpi
6. sudo git clone git://github.com/raspberrypi/tools.git คำสั่งนี้จะ clone raspbian's official cross compile นานหน่อย
7. cd ~/
8. nano .bashrc
9. insert "export PATH=$PATH:$HOME/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin" on the last line off file.
10. type source .bashrc for update path.
11. type "arm-linux-gnueabihf-gcc -v" for check it work or not.

12. install java runtime "sudo apt-get install openjdk-7-jre"
14. typically “~/Downloads/”)then type: “tar -xvzf eclipse-cpp-kepler-R-linux-gtk.tar.gz -C ~/
15. now u can use eclipse in "~/eclipse/eclipse &"
16. start new c++ project
17. next,next then In the “Cross compiler prefix” field type: “arm-linux-gnueabihf-”.
18. In the “Cross compiler path” field type the full path to the toolchain: “/home/kong/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/”
19. after adding source code, build all
20. Lets go there via a console window with cd “cd ~/workspace/HelloRPiWorld/Debug/“. Type in “ls -al” the “HelloRPiWorld” Binary file is highlighted in green.
21. in eclipse program, To deploy the binary on the Raspberry Pi, make sure that the RPi board is powered and connected to your network. The next step is click on “Window->Show View->Other”. This will open a show view window. In this window select the “Remote Systems” folder and then select “Remote Systems Details” and press OK
22. If the “Remote Systems Details” shows up in the bottom of the workspace, drag it such that its tab shares  the same region as the project explorer tab
23. Now right click in the “Remote Systems Detail” area and click on “New Connection”. In the “Select Remote System Type” window, select “SSH only” then click “Next”
24. In the “Remote SSH Only System Connection” (Figure 9) type in the IP address of the RPi in the “Host name” field and give the connection a valid name in the “Connection name” field. I typed “Raspberry Pi”.  Then click “Finish”.
25. At this point a second device (Raspberry Pi) shows up in the “Remote Systems Detail” tab (Figure 10). Right click on the Raspberry Pi resource and click connect. You will be prompted to enter the username and password. Make sure that you utilize “username:pi” and “password:raspberry” (if you haven’t changed the default password). You may get a warning windows asking you if you are sure that you want to accept this connection. Affirm that you want to proceed with the connection. At this point you should be connected to your RPi from Eclipse.
26. Right click on the Raspberry Pi resource again  in the “Remote Systems Detail” tab and click on the “Show in Remote Systems View”. This will open a “Remote Systems” tab in the same region of the workspace. In this tab, one can navigate the root file systems of both the Local Linux OS and that of the Raspbian/Raspberry Pi.
27. Locate the HelloWorld Binary file on our local desktop PC and drag it to the home directory on the Raspberry Pi (You could also right click on the binary and click on “Copy” and then right click on the home folder in the Raspberry Pi and click “Paste”. This effectively copies the binary file to the home directory of the Raspberry Pi.
28. Now right click on the SSH terminal icon under the Raspberry Pi icon (Figure 11) in the  “Remote Systems” and select “Launch Terminal” . This should launch a terminal window in the bottom of the Eclipse workspace (Figure 12). This is basically a console / terminal window connected to the Raspberry Pi via SSH.
29. In the terminal window type “ls -al” to confirm that the “HelloWorld” binary indeed got copied into the home directory of the Raspberry Pi’s root file system. Then change the “HelloWorld” binary’s permission to make it executable: “chmod +x HelloWorld

ที่มา
http://www.lab4inf.fh-muenster.de/lab4inf/docs/Embedded-Software/Eclipse_Cross_Development.pdf

วันเสาร์ที่ 19 เมษายน พ.ศ. 2557

blog ดีๆ เกี่ยวกับ Raspberry Pi

http://www.together.in.th/

http://www.makeuseof.com/tag/7-operating-systems-you-can-run-with-raspberry-pi/

http://xmodulo.com/2014/04/lightweight-web-server-raspberry-pi.html

www.intelliadmin.com/index.php/2013/07/sprinkler-system-controlled-by-a-raspberry-pi/

http://www.raspberrypi-spy.co.uk/2014/04/how-to-change-the-command-line-font-size/

http://www.youtube.com/watch?v=OYXXvaypUKQ&feature=share

http://www.raspberrypi-spy.co.uk/2014/04/hdmipi-raspberry-pi-hd-lcd-screen-beta/

http://www.raspberrypthai.com

http://www.davidhunt.ie/blog/

http://www.together.in.th/

วันเสาร์ที่ 12 เมษายน พ.ศ. 2557

Rpi configure wifi

ถ้าต้องการใช้ wifi ให้ทำดังนี้ครับ

ต่อ wifi adaptor เข้ากับบอร์ด Rpi
USB Wifi Dongle with Case (IEEE 802.11g - 54 Mb) Chip Set RT2070 
                                                              ภาพที่ 1

ทำการ remote desktop

เปิด wpa_gui ตามภาพที่ 2

                                                       ภาพที่ 2

กด scan
                                                        ภาพที่ 3

กด scan
                                                      ภาพที่ 4

จะเห็นรายชื่อ wifi network ขึ้นมา ให้ double click ที่รายชื่อที่เราต้องการเชื่อมต่อ
ในช่อง PSK ให้เราใส่ password แล้วกด add

                                                    ภาพที่ 5
กด connect

                                                        ภาพที่ 6

configure เพิ่มนิดหน่อยเพื่อให้ auto connect หลังจาก boot ใหม่ ดังนี้
ที่บอร์ด Rpi
nano /etc/wpa_supplicant/wpa_supplicant.conf
กด ctrl+x เพื่อออกและ save


nano /etc/network/interfaces
กด ctrl+x เพื่อออกและ save

sudo reboot
แล้วใช้ nmap ลองหา ip ของ wireless lan ของ Rpi ดู

ถ้าต้องการให้เป็น static ip
nano /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet dhcp

#allow-hotplug wlan0
auto wlan0
iface wlan0 inet static
        wireless-essid <myssid>
        address 192.168.0.100
        netmask 255.255.255.0
        gateway 192.168.0.1
        network 192.168.0.0
        broadcast 192.168.0.255
        pre-up wpa_supplicant -B w -D wext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
        post-down killall -q wpa_supplicant
iface default inet manual

หากต้องการให้เป็น dhcp ให้แก้ไขเป็นดังนี้
auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

วันนี่ได้เอา wifi ตัวนี้มาต่อกับ rpi2 แต่ใช้ไม่ได้ พบวิธีแก้ไขดังนี้
sudo apt-get install rpi-update เสร็จแล้ว reboot

ที่มา
https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md
http://www.raspyfi.com/wi-fi-on-raspberry-pi-a-simple-guide/
http://www.suntimebox.com/raspberry-pi-tutorial-course/week-3/day2-1-wireless-network-setup/
http://www.raspberrypi.org/forums/viewtopic.php?t=26795
http://www.raspberrypi.org/forums/viewtopic.php?t=11517
http://www.unzeen.com/article/2598/

ทำ Rpi ให้เป็น static ip

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

ให้เราทำการ ssh เข้าไปที่บอร์ด Rpi แล้วพิมพ์

nano /etc/network/interfaces

auto lo
iface lo inet loopback
#iface eth0 inet dhcp
iface eth0 inet static
address 192.168.0.6
netmask 255.255.255.0
gateway 192.168.0.1
network 192.168.0.0
broadcast 192.168.0.255

การใส่เครื่องหมาย # เป็นการ comment หรือการไม่นำมารวมอยู่ใน code ของเรา

แค่นี้เองครับ เราก็ได้ Rpi ที่เป็น static ip แล้ว

หรือมีอีกวิธีนึง คือ
เข้าไปที่ sudo nano /etc/dhcpcd.conf
แล้วพิมพ์เพิ่มเติม ที่บรรทัดสุดท้าย ดังนี้

# Custom static IP address for eth0.
interface eth0
static ip_address=192.168.0.200/24
static routers=192.168.0.1
static domain_name_servers=192.168.0.1

ที่มา
http://sizious.com/2015/08/28/setting-a-static-ip-on-raspberry-pi-on-raspbian-20150505/

remote desktop ด้วย tight vnc

บทความตอนนี้จะอธิบายถึงการ remote desktop ทั้งจาก windows และจาก Linux

ที่บอร์ด Rpi

sudo apt-get install  tightvncserver รอจนเสร็จ
vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565



ที่ windows pc
download UltraVnc : http://www.uvnc.com/downloads/ultravnc.html
เปิดโปรแกรม ultravnc แล้วใส่ ip:1

กด connect แล้วใส่ password


ที่ Linux PC
sudo apt-get install xtightvncviewer
เมื่อเสร็จแล้ว ก็ run คำสั่ง xtightvncviewer xxx.xxx.xxx.xxx:1
ที่ต้องใส่ :1 เพราะว่า vnc server ของเราเปิด port 1 อยู่นั่นเอง


ที่มา
http://raspberry-pi-th.blogspot.com/2012/09/tightvncserver-raspberry-pi-x-window.html
http://marc-abramowitz.com/archives/2006/02/17/tightvnc-on-ubuntu/