วันศุกร์ที่ 28 ธันวาคม พ.ศ. 2555

GPIO RPI + Arduin

บทความนี้นำมาจากเวป http://raspberry-pi-th.blogspot.com/


จากครั้งที่แล้ว เราได้เปิดการใช้งาน Serial Port ของ Raspberry Pi ไปแล้ว วันนี้เราจะมาลองเพิ่มประสิทธิภาพของ Raspberry Pi ด้วยการเพิ่ม Arduino ให้สื่อสารกับ Raspberry Pi ผ่านทาง Serial Communication โดยจะมีการรับส่ง คำสั่งและส่งค่ากลับผ่านทาง Serial Port ของ Raspberry Pi และ Serial Port ของ Arduino เพื่อเป็นแนวทางในการนำ Arduino มาใช้เป็น I/O ของ Raspberry Pi ซึ่งเราจะได้ I/O ที่เป็นดิจิตอล และ ได้ Analog input เพิ่มจาก Arduino นั่นก็แปลว่า Raspberry Pi เราจะมีความสามารถมากขึ้นไปอีก
ปล. สำหรับมือใหม่ Arduino คือไมโครคอนโทรลเลอร์ ที่มีการอ้างไปหาตำแหน่งหน่วย Input/output ของไมโครคอนโทรลเลอร์ ด้วยรูปแบบที่เหมือนๆ กัน มาใส่ไว้ที่ Bootloader ไม่ว่าจะเอาไมโครคอนโทรลเลอร์ตระกูลใด(ที่สามารถทำได้) ให้มาอยู่ใน Platform ของ Arduino แล้วหล่ะก็ ชื่อของ Input / Output ก็จะเหมือนๆ กัน ทำให้เง่ายต่อการศึกษา และป็นที่นิยมมาก
หลักการ ก็คือ Arduino และ Raspberry Pi เองจะต้องมีการเขียนโปรแกรมเพื่อรอรับค่า และส่งค่า ผ่านทาง Serial Comm เมื่อได้รับคำสั่ง (ที่เราเองเป็นคนกำหนดแล้ว) ก็จะให้แต่ละบอร์ด ไปทำตามคำสั่งนั้น แล้วส่งผลลัพธ์กลับไป ผ่านทาง Serial Comm เหมือนเดิม นั่นก็แปลว่า ทั้งทางฝั่ง Arduino และทางฝั่ง Raspberry Pi จะต้องมีโปรแกรมรองรับเป็นของตัวเอง แต่คุยกันด้วยรูปแบบการสื่อสาร UART  นั่นเป็นหลักการ เปรียบได้กับ ไม่ว่าบอร์ดนั้นจะมาจากไหน ตระกูลใด Platform ใดก็ตาม แต่ถ้าต้องการคุยกันให้รู้เรื่อง ต้องคุยกันเป็นแบบมาตรฐาน เช่น คุยกันรูปแบบ UART  สิ่งที่แต่ละบอร์ดต้องทำนั้นก็คือ ทำให้สัญญาณที่ต้องการจะสื่อสารกันนั้น อยู่ในรูปแบบภาษากลาง เป็นมาตรฐาน (ในที่นี่คือ UART) นั่นเอง เหมือนกันกับ แต่ละชาติ แต่ละภาษา หากต้องการสื่อสารกัน เราจะใช้ภาษาอังกฤษ เป็นตัวกลางในการสื่อสารนั่นเอง
ที่นี่ เรามาลองทำโจทย์กันง่ายๆ ก่อน เพื่อให้เกิดความเข้าใจ ระหว่างการเขียนโปรแกรมให้ Raspberry Pi และ Arduino ให้สื่อสารกันได้ โดยเราจะให้  Raspberry Pi ทำการอ่านค่าการกดสวิทช์ที่ต่ออยู่กับช่องดิจิตอลของ  Raspberry Pi ช่องที่ 24 และ 26 เมื่อมีการกดสวิทช์เกิดขึ้น  Raspberry Pi จะส่งค่าคำขอไปที่ Arduino เพื่อให้ทำการอ่านค่าสัญญาณอะนาล๊อก (ซึ่งอาจจะเป็นค่าจากเซนเซอร์แบบใดๆก็ได้) ในที่นี้ เราจะให้ R ปรับค่าได้เป็นตัวสร้างสัญญาณอะนาล๊อก ที่ช่องสัญญาณ AN0 และ AN1 ของบอร์ด Arduino แล้วให้ Arduino ทำการส่งค่ากลับไปบอก Raspberry Pi ส่วน Raspberry Pi เมื่อได้ค่ามาแล้วให้แสดงผลผ่านทาง Console ต่อไป
ในด้านฮาร์ดแวร์นั้น เนื่องจาก Arduino เองสามารถส่งสัญญาณแรงดันออกมาได้ถึง 5 V เพราะฉะนั้น ที่ด้านส่งข้อมูลออกของ Arduino จะต้องทำการปรับแรงดันให้เหลือ ในระดับที่ปลอดภัยสำหรับ Raspberry Pi เสียก่อน ส่วนทางด้านรับข้อมูลเข้าของ Arduino ไม่จำเป็นต้องปรับแรงดัน เพราะแรงดันจากขาออก 3.3V จาก Raspberry Pi นั้น เป็นระดับที่ Arduino เข้าใจได้
Arduino                                                  Raspberry Pi
   Rx<--------------------------------------------->Tx
   Tx (ปรับลงเหลือ 3.3 V ก่อน)<---------------> Rx
   GND<----------------------------------------->GND
ในการแปลงแรงดันจาก 5V ให้ลดลงเหลือ 3.3V เราใช้วิธี Voltage Divider โดยเราจะใช้ตัวต้านทานค่า 10k ทำการแบ่งแรงดัน  (ผมลองวัดค่าดูแล้ว ค่าไม่เกิน 3.3V และอยู่ในช่วงที่ Raspberry Pi เข้าใจระดับสัญญาณได้)
Raspberry Pi and Arduino Experiment
คำเตือน : ระวังเรื่องของระดับสัญญาณด้วยนะครับ อย่าให้เกิน 3.3V สำหรับตำแหน่งใดๆ ก็ตามที่จะต่อเข้า GPIO ของ Raspberry Pi
การพัฒนาโปรแกรมทางฝั่ง Arduino
สามารถเริ่มต้นศึกษาได้จากตัวอย่างที่ผมได้ทำไว้แล้ว จากที่นี่http://mechacity.blogspot.com/2011/03/arduino.html  ทำการเขียนโค๊ดบน ARDUINO IDE แล้วทำการโปรแกรมลงบนบอร์ด Arduino
ด้วยโค๊ด
const int analogInPin0 = A0;    // Analog input pin that the potentiometer is attached to
const int analogInPin1 = A1;    // Analog input pin that the potentiometer is attached to
void setup() {
  // initialize serial communications at 115200 bps:
  Serial.begin(115200);
}
void loop() {
  unsigned int cmd;
  unsigned int sensorValue = 0;        // value read from the pot
  if(Serial.available() > 0){
    cmd = int(Serial.read()) - 48;
    switch(cmd){
      case 1:
        sensorValue = analogRead(analogInPin0);
        Serial.print("A0=" );
        Serial.print(sensorValue,DEC);
        Serial.print("|" );
        Serial.flush();
        break;     
      case 2:
        sensorValue = analogRead(analogInPin1);
        Serial.print("A1=" );
        Serial.print(sensorValue,DEC);
        Serial.print("|" );
        Serial.flush();
        break;   
    }
  }   
}
Arduino Firmware
หมายเหตุ : ในระหว่างการทำการโปรแกรมลงบอร์ด Arduino เราต้องไม่ให้ pin RX ของ Arduino ต่ออยู่กับอะไรในวงจร เพราะวงจรในบอร์ด Arduino ต้องใช้ pin RX ในการโปรแกรม (Burn) ลงชิพ
การพัฒนาโปรแกรมทางฝั่ง Raspberry Pi
เรายังคงใช้ภาษา Python แล้วโมดูล Python-serial ในการพัฒนาโปรแกรม โดยนำโค๊ดจากตัวอย่างที่แล้ว มาทำการดัดแปลงให้เข้ากับโจทย์ที่เราจะทดลอง บันทึกชื่อไฟล์ arduinoProj1.py
โค๊ด
#! /usr/bin/python
import serial
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(24,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(26,GPIO.IN,pull_up_down=GPIO.PUD_UP)
ser = serial.Serial('/dev/ttyAMA0',115200,timeout=1)
ser.open()
time.sleep(2)
def readLine(ser):
    str = ""
    while 1:
        ch = ser.read(1)
        if(ch == '|'):
            break
        str += ch
    return str
while True:
    line = ''
    if(GPIO.input(24) == False):
        ser.write('1')
        print 'button1 pressed'
        line = readLine(ser)
    elif(GPIO.input(26) == False):
        ser.write('2')
        print 'button2 pressed'
        line = readLine(ser)
    try:
        if line <> '':
            data = line.split('=')
            print 'Analog channel ' + data[0] + ' value = ' + data[1]
    except KeyboardInterrupt:
        pass
        GPIO.cleanup()
        ser.close()
        print 'Good bye!!!'
Raspberry Pi Firmware
ทดสอบการทำงาน ด้วยการรันโปรแกรมผ่าน Console ด้วยคำสั่ง (ภายใน directory ที่เก็บไฟล์ arduinoProj1.py) ด้วยคำสั่ง sudo ./arduinoProj1.py  จากนั้นทำการกดปุ่มที่ต่ออยู่กับ GPIO 24 และ 26 ของ Raspberry Pi สังเกตผลลัพธ์บนหน้าจอ Console แล้วทดลองปรับค่าความต้านทาน สังเกตการเปลี่ยนแปลง
Arduino and Raspberry Pi
จริงๆ แล้วเราควรที่จะเลือกใช้ Arduino Firmware ที่ยืดหยุ่นกว่านี้ ผมหมายถึง เราไม่ควรทำการแก้ไขโค๊ดที่ฝั่ง Arduino มากนัก แต่ควรจะมีโค๊ดที่สามารถปรับเปลี่ยนการทำงานตั้งแต่ฝั่ง Raspberry Pi เพื่อกำหนดการทำงานของ Arduino แทน ซึ่งในอินเตอร์เนตมี Library ประเภทนี้ อยู่ ตอนต่อไป ผมอาจจะนำมาสาธิตให้ดูนะครับ

บทความนี้นำมาจาก http://raspberry-pi-th.blogspot.com/2012/11/raspberry-pi-and-arduino-experiment.html

ทำบ้านให้ปลอดภัย Raspberry PI ยามเฝ้าบ้าน

วันนี้ เรามาลองสร้างระบบเตือนภัยจากผู้บุกรุก โดยเราจะให้เจ้าบอร์ด Raspberry Pi ของเราทำการส่งเมล์ไปหาเรา ถ้ามีใครสักคนพยายามงัดแงะหน้าต่าง หรือประตูของเรา หรือจะเช็คจุดไหนก็ได้ ที่สามารถต่อสวิทช์ หรืออุปกรณ์ตรวจสอบใดๆ ก็ได้ที่ทำให้เกิดการเปลี่ยนแปลง logic จาก Low ไป High เพื่อที่เราจะได้ต่อเข้ากับ GPIO ของบอร์ด Raspberry Pi เมื่อเกิดการเปลี่ยนแปลงสถานะ logic ณ จุดใดๆ ที่ต่ออยู่กับ GPIO ของ Raspberry Pi แล้ว ให้เจ้าบอร์ด Raspberry Pi ของเราส่งเมล์มาบอกเรา เพียงเท่านี้ เราก็จะได้ระบบรักษาความปลอดภัย ที่กินไฟน้อยที่สุดในโลกแล้ว (อาจจะฟังดูเวอร์ไปเล็กน้อย :P ) 
อันนี้ เป็นวงจรการทำงานพื้นฐานนะครับ แน่นอน ถ้าจะทำจริงๆ ก็ต้องดัดแปลง ปรับปรุงให้เกิดความเสถียรภาพมากกว่านี้ ไม่ว่าจะเป็นระบบสำรองไฟฟ้า หากเกิดกรณีไฟฟ้าดับ และระบบป้องกันไฟเกิน และอื่นๆ อีกที่ควรจะใส่เข้าไป เพื่อให้ระบบเราทำงานได้ แม้ในสภาวะที่ไม่ปกติด้วยนะครับ ก็ลองไปคิดๆ กันดูว่าต้องมีอะไรบ้าง 
Raspberry Pi Home Security System
ในที่นี้ ผมจะสมมติให้ push button ทำหน้าที่เป็นสวิทช์ประตู เมื่อมีการเปลี่ยน logic จาก high เป็น low ให้ raspberry Pi ทำการเริ่มส่งเมล์ มาตามที่กำหนดไว้ในโค๊ด 
Raspberry Pi simple Circuit
ด้วยความสามารถของ Python เราสามารถที่จะเรียก package smtplib เพื่อติดต่อกับ mail server (ในที่นี้ ผมต้องการติดต่อกับ mail server ของ Gmail ) แล้วทำการส่งข้อความไปที่ inbox ของ mail ปลายทางที่เราต้องการได้ (ซึ่งนั่น น่าจะเป็นเมล์ของเรานะครับ) เรามาดูโค๊ดกันเลยครับ 
สร้างไฟล์นี้ด้วยคำสั่ง nano mail.py 
แล้วเขียนโค๊ดตามข้างล่างนี้ (ระวังเรื่อง indent ด้วยนะครับ สำหรับภาษาไพธอนเราจะใช้การ indent เป็นตัวจัดกลุ่มของคำสั่ง ) 
#--------------------------- Code start here ------------------------------------------------
#! /usr/bin/python 
import RPi.GPIO as GPIO 
import smtplib 
import time 
LOOP = True 
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(26,GPIO.IN,pull_up_down=GPIO.PUD_UP) 
SMTP_SERVER = 'smtp.gmail.com' 
SMTP_PORT = 587 
sender = 'raspberry security system' 
recipient = 'wisit.paewkratok@gmail.com' 
subject = 'Raspberry Pi Alert System' 
body  = 'The door is opned!!!' 
headers = ["From: " + sender, 
           "Subject: " + subject, 
           "To: " + recipient, 
           "MIME-Version: 1.0", 
           "Content-Type: text/html"] 
headers = "\r\n".join(headers) 
username = ‘ชื่อล๊อกอินของ Gmail' 
password = 'พาสเวิร์ดสำหรับ Gmail' 
while True: 
    if(GPIO.input(26) == False): 
        print('Mail alert sending....\n') 
        server = smtplib.SMTP(SMTP_SERVER,SMTP_PORT) 
        server.ehlo() 
        server.starttls() 
        server.ehlo() 
        server.login(username,password) 
        server.sendmail(sender,recipient,headers + "\r\n\r\n" + body) 
        server.quit() 
        GPIO.cleanup() 
        LOOP = False 
        print('Program terminated\n\n') 
        break 
    time.sleep(1) 
    print('program is running ' + time.ctime() )
#----------------------------------- End Code ----------------------------------------------------------
ทำการบันทึกไฟล์ แล้วทำการเปลี่ยนให้ไฟล์นี้สามารถ execute ได้ ด้วยคำสั่ง
sudo chmod 755 mail.py 
แล้วทำการทดลองรันสคริปต์ไพธอน ด้วยคำสั่ง 
sudo ./mail.py 
Python sending mail to Gmail server 
ถ้าทุกอย่างถูกต้อง เราจะพบเมล์ในกล่องขาเข้าของจดหมาย จาก raspberry security system
จากตัวอย่างที่นำเสนอนี้ เป็นรูปแบบอย่างง่าย นะครับ หากจะนำไปใช้จริงๆ เพื่อนต้องปรับแต่งให้ระบบของเพื่อนๆ สามารถที่จะตรวจสอบได้ตลอดเวลา ว่ายังทำงานอยู่หรือเปล่า และเมื่อเวลาเกิดเหตุการณ์ขึ้นแล้ว จะให้ Email ส่งมาเตือนกี่ครั้ง และจะรีเซตระบบกลับเหมือนเดิมได้อย่างไร นั่นเป็นสิ่งที่ต้องนำกลับไปคิดกันต่อนะครับ ลองๆ เอาไปประยุกต์กันดูนะครับ หวังว่าคงเริ่มสนุกกับ Raspberry Pi แล้วนะครับ

ขอบคุณและที่มาของบทความ: http://raspberry-pi-th.blogspot.com/2012/10/simple-home-security-system-by.html

แหล่งเรียนรู้ RPI ประเทศไทย

แหล่งเรียนรู้ RPI  ประเทศไทย

http://raspberry-pi-th.blogspot.com/
http://www.raspberrypithai.com

How to install XBMC on Raspberry pi! (The Easy Way)

เป็นเหมือนสมาร์ททีวี

How to Step to Step install RPI


Six years ago, Raspberry Pi founder Eben Upton set out to reignite programming in schools with a cheap, compact computing platform. Despite targeting students, his foundation's $35 computer captured the imaginations of tinkers worldwide, resulting in overwhelming demand. Interest was so high, that distributorsPremier Farnell and RS buckled under the strain of preorders in February. The former outfit later said demand was 20 times greater than its supply, with orders hitting 700 a second at one point.
When the first 10,000 devices shipped in mid-April, the organization graciously sent us a sample for coverage. Along with a hands-on review of the Pi, today we'll be covering basic steps for setting up the computer and other elemental post-installation tasks to get you up and running with applications. In other words, this should serve as a starting point no matter what you want to do with your Raspberry Pi.
We received a Model B ($35), which is powered by a Broadcom BCM2835 SoC that includes a 700MHz ARM1176JZF-S CPU core, 256MB of RAM and a Broadcom VideoCore IV GPU with OpenGL ES 2.0 that supports 1080p at 30FPS as well as H.264 and MPEG-4 high-profile decoding for smooth Blu-ray playback. Connectivity includes two USB ports, Ethernet, HDMI, RCA video, an SD card slot, a 3.5mm audio jack and two rows of 13 General Purpose Input/Output (GPIO) pins for further expansion.
The Model A ($25, to be released at a later date) ships without Ethernet and has a single USB port. Both models measure 85.60mm x 53.98mm x 17mm, although the SD card and connectors overlap the PCB board edges. Besides the Raspberry Pi itself, you'll also need various other items before you can configure and use the device:
  • 5v micro-USB power adapter with at least 700mA (many micro-USB phone chargers work).
  • SD card, or micro-SD card in an adapter, with the OS preloaded (4GB to 32GB recommended).
  • USB keyboard and mouse (PS/2 to USB adapters might work, but we haven't tested this).
  • Powered USB hub if you intend to have more than two USB devices connected.
  • Display or TV with HDMI, DVI, Composite or SCART.
  • Ethernet cable.
As indicated, the Raspberry Pi uses an SD card for storage. Both of distributors sell preloaded SD cards, but they're easy enough to make yourself if you have a spare card laying around. Currently you can use the Unix tool "dd" to perform the task, or for those using Windows, "Win32DiskImager." We assume you're on Windows or you probably wouldn't need this guide. Before we get started, download the latest Debian Squeeze image andWin32DiskImager. Insert the SD card into your PC if you haven't yet.
Extract both archives and start the imaging tool by double clicking Win32DiskImager.exe. It should find your SD card as the application starts and display it in the top right hand corner of the window. Click on the folder icon, then navigate to and select the Debian ISO you extracted. Double check that the correct drive letter is selected, then click on "write" to load the image. This'll take upwards of five minutes.
Once the process is finished, a popup will notify you that the write was successful. Close the box, exit the application, unmount the SD card from your PC and attach it to the Raspberry Pi. Assuming everything went well, you're ready to fire the device up. The first time the computer boots from the SD card it will automatically configure itself. It will then reboot and load up once again to the login screen.
The default username for Debian is pi and the password is raspberry. You can then load the LXDE desktop environment by entering startx. A few moments later, the desktop will load up as below:
Applications can be found by clicking the icon on the far left of the toolbar, similar to the Start menu in Windows. Creating a user account and updating the OS in Windows is pretty straightforward, but the steps are different in Linux. To create a user account, click on the menu, open the Accessories folder and select LXTerminal. In the terminal window, type sudo adduser username and press enter. For example:
Debian will create the user and prompt you to set a password, as well as other personal information. Fill in the applicable fields and type y to confirm the information is correct. The result looks like this:
With your account created, we can add it to the sudoers list. This will let you issue commands as an administrator. Working in the same terminal window, enter the command sudo leafpad /etc/sudoers.
Leafpad will load the Sudoers file. Under the heading "# User Privilege specification" add the following text,username ALL=(ALL) ALL for the new username exactly as it is displayed for the user pi:
Select File and Save, then close Leafpad to finish. Now you've created an account and given it the ability to perform administrative tasks as sudo, which is needed for updates and new packages or applications.
Unlike Windows, Debian is traditionally updated from the terminal. To check for and install updates, you can use the following command: sudo apt-get update && sudo apt-get upgrade as seen below:
Similarly, you can install supported packages through the terminal with sudo apt-get install packagename. Debian has various packages pre-installed, ranging from the web browser, Midori, to a music player as well as programming and educational applications. Popular packages are also available if you need more than the stock software. We may cover some of these programs in future guides.
As is the case with Linux in general, wireless can be a bit patchy with the Raspberry Pi. If you want to use wireless, the organization has information on its Wiki regarding USB Wi-Fi hardware. Wireless niggles aside, USB external hard disks and flash media will automatically mount when inserted in Debian and full read/write support for NTFS is also included, in case you need to access a Windows-based drive.
Considering its price, the Raspberry Pi is quite remarkable. It's obviously no powerhouse, but $35 gets you a system capable of office work, light image editing, browsing, programming, emailing and so on. The Pi's versatility makes it suitable for various dedicated roles too, such as a torrent box downloading through external USB or network drives, or as an XMBC-based HTPC, which we might cover eventually.
 
It's easy to lose track of the Pi in-between other regular-sized devices.
Although there are many obvious uses for the Raspberry Pi, the platform's open nature means your imagination is the limit. It'll be interesting to see what the community cooks up in the coming months. We'll be watching and posting our own how-to guides when time comes.
ref: http://www.techspot.com/review/527-raspberry-pi/

คอมพิวเตอร์ขนาดจิ๋ว Raspberry PI


Raspberry Pi คืออะไร

Raspberry Pi ก็คือคอมพิวเตอร์ขนาดจิ๋ว มีแต่สิ่งที่จำเป็นเพื่อการประมวลผล ราคาไม่แพงเกินไป
บอร์ดใช้ชิป SoC ของ Broadcom BCM2835 ซึ่งบรรจุ ARM1176JZFS พร้อมทั้งหน่วยประมวลผลเลขทศนิยม (floating point) ทำงานที่ความถี่สัญญาณนาฬิกา 700Mhz DRAM ขนาด 256 MiB ซึ่งถ้าเป็น microcontroller ทั่วๆ ไป อาจมี RAM น้อยกว่านี้ (เป็น SRAM เพราะไม่มี MMU) งานบางอย่างอาจต้องใช้หน่วยความจำเยอะ เช่น งานประมวลผลภาพ Raspberry Pi จึงมีความเหมาะสม
อันที่จริง ก็มีพวก evaluation board อยู่มากมายเหมือนกัน แต่ราคาแพงมาก โดยที่ Raspberry Pi อาจมองว่ามันดูคล้าย tablet หรือ netbook ที่ไม่มีจอ ไม่มีแบตเตอรี่ ไม่มีคีย์บอร์ดหรือจอสัมผัส ก็ได้ ถ้าอยากได้ต้องหามาต่อเอง และถ้าต่อ Raspberry Pi กับ Monitor และ keyboard ก็สามารถใช้มันได้พอๆ กับ PC เหมือนกัน นอกจากนี้ยังสามารถต่ออุปกรณ์อื่นๆ ตามใช้ชอบได้เหมือนกัน และยังมี GPIO ที่รับส่งข้อมูลได้สารพัดนึกเหมือนกับพวก Microcontroller จึงอาจไปประยุกต์ใช้ในการควบคุมอย่างอื่นได้ เช่น ต่อ relay เพื่อเปิดปิดกาต้มน้ำไฟฟ้า
ที่มา : http://www.raspberrypithai.com