basic raspberry pi - staff.informatics.buu.ac.thnutthanon/887496/raspberrypi.pdf · basic raspberry...

90
BASIC RASPBERRY PI For Internet of Things Max Innovation Technology Co., Ltd. [email protected]

Upload: others

Post on 02-Nov-2019

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

BASIC RASPBERRY PI For Internet of Things

Max Innovation Technology Co., Ltd. [email protected]

Page 2: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 1

Basic Raspberry Pi for Internet of Things

Contents ขนตอนการเตรยม .................................................................................................................................................. 3

การ Format SD Card .................................................................................................................................... 4

การ Burn Image File .................................................................................................................................... 5

การก าหนดคา IP แบบ Direct Config เพอใชในการก าหนดคาครงแรก .......................................................... 8

การก าหนดคา IP แบบ Static ...................................................................................................................... 11

การตงคา IP Address ส าหรบ Wi-Fi ดวย Command Line ...................................................................... 15

การตงคาทวไปของ Raspberry Pi ...................................................................................................................... 19

การขยายขนาด Space ใน SD Card ใหเตมพนท ......................................................................................... 19

การก าหนดการ Boot เขาหนาจอของ Raspberry Pi ................................................................................... 20

การก าหนด Raspberry Pi Name ................................................................................................................ 21

การก าหนด SSH ........................................................................................................................................... 22

การตงเวลา Raspberry Pi ............................................................................................................................ 24

การอพเดท Raspberry Pi .................................................................................................................................. 28

การตดตง Remote Desktop ............................................................................................................................ 29

การตดตง XRDP ........................................................................................................................................... 29

การตดตง VNC ............................................................................................................................................. 31

การใชงาน GPIO ................................................................................................................................................. 33

การสงงานผาน GPIO ดวย Command line................................................................................................ 34

การเขยน Program Python พนฐาน ................................................................................................................. 35

ตวแปรในภาษา python............................................................................................................................... 35

Syntax ในภาษา python ............................................................................................................................ 35

Page 3: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 2

Basic Raspberry Pi for Internet of Things

การก าหนดตวแปร ........................................................................................................................................ 36

ชนดขอมล ..................................................................................................................................................... 37

Operator ..................................................................................................................................................... 40

Condition .................................................................................................................................................... 42

Loop ............................................................................................................................................................ 42

Function ..................................................................................................................................................... 43

การใช Python เพอควบคม GPIO ...................................................................................................................... 44

GPIO Output .............................................................................................................................................. 44

GPIO Input .................................................................................................................................................. 45

การใชงานกลอง USB Camera........................................................................................................................... 46

การหากลอง USB ......................................................................................................................................... 46

สงภาพจาก WebCam ไปแสดงบน Web Browser ..................................................................................... 46

บนทกรปจากกลอง USB Camera ................................................................................................................ 47

ท า Raspberry Pi เปน MQTT Broker .............................................................................................................. 51

Socket Programming ...................................................................................................................................... 53

ทดลองท า Raspberry Pi เปน Socket Server ............................................................................................ 55

ทดลองท า Raspberry Pi เปน Socket Server รบคา JSON ....................................................................... 58

ทดลองสงขอมลผาน JSON และบนทกลง Database SQLite ..................................................................... 60

การใช MQTTSpy ........................................................................................................................................ 66

ทดลองใชงาน Blynk ........................................................................................................................................... 67

QT programming ............................................................................................................................................. 68

Reference ......................................................................................................................................................... 69

Page 4: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 3

Basic Raspberry Pi for Internet of Things

Basic Raspberry Pi

ขนตอนการเตรยม Raspberry Pi เปนคอมพวเตอรขนาดเลกมขนาดใหญกวาบตรเครดตเลกนอยซงประสทธภาพของมนนน

มมากเกนกวาราคาและนบวาเปนกระแสทเปลยนแปลงวงการ Embedded System นอกจากในตว Raspberry

Pi ทสามารถตดตง OS ลงไปไดแลวความสามารถของมนในการเชอมตอกบอปกรณ Sensor ภายนอก อกทงยงท า

ใหการควบคมอปกรณตาง ๆ งายขน เกดความนยมในการน าไปใชในการเรยนการสอนการและทดลองตาง ๆ

Raspberry Pi นบวาเปน Embedded System อกตวหนงทงายตอการพฒนา และปจจบน Raspberry

Pi ไดออกผลตภณฑมาหลายรนตงแต Raspberry Pi Model A, Model B, Model B+, Raspberry Pi 2 และ

Raspberry Pi 3

Page 5: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 4

Basic Raspberry Pi for Internet of Things

การ Format SD Card ใชโปรแกรม SD Formatter โดยเลอก Drive ทเปน SD Card ของเรา และก าหนด Option ในการ Format

โดยเลอก Option Format Type เปน Full (Erase) และ Format Size Adjustment เปน ON

Page 6: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 5

Basic Raspberry Pi for Internet of Things

การ Burn Image File ส าหรบ Raspberry Pi นนสามารถเลอกใช OS ในการใชงานไดมากมาย โดยเราสามารถ Download OS ไดจาก

https://www.raspberrypi.org/downloads/

ซง OS ทนยมใช บน Raspberry Pi นนนยมใช RASPBIAN ซงเปน Official ของ Raspberry Pi โดย

Version ทจะใชในหนงสอเลมนจะเปน RASPBIAN Jessie

Page 7: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 6

Basic Raspberry Pi for Internet of Things

หลงจากทเรา Download Jessie จาก Web Site ของ Raspberry Pi แลวจะได File Image เราจะตดตงลงบน

SD Card โดยใช Program Win32DiskImage เลอก Image File และเลอก Device ทเปน SD Card แลวท าการ

Write

หลงจากทเขยน Image ลงบน SD Card แลวใหเราลองเสยบ SD Card แลว Boot หนงรอบ รอประมาณ

2 หรอ 3 นาท เพราะคา Configuration ตงตนจะถกก าหนดมาในไฟล cmdline.txt ดงกรอบดานลาง

dwc_otg.lpm_enable=0 Console=serial0,115200 Console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet init=/usr/lib/raspi-config/init_resize.sh

หลงจากท Boot เสรจเรยบรอยแลว File cmdline.txt จะถกเปลยนคา Configuration ซงจะมผลหากเรา

ตองการ Set คา IP Address แบบ direct Config

Page 8: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 7

Basic Raspberry Pi for Internet of Things

(จะรไดยงไงวาการ Boot ของ Raspberry Pi เสรจเรยบรอยแลวใหเราดไฟทแสดงคา Status สเขยวบน Board

เมอ Boot เสรจไฟสเขยวนจะดบลงและไมมการกระพรบ)

คา Configuration ใน File cmdline.txt หลงจาก Boot เสรจเรยบรอยแลวจะแสดงตามดานลาง ซง

หากเรา Login เขาใชตว Raspberry Pi จะอยใน path /boot

dwc_otg.lpm_enable=0 Console=serial0,115200 Console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Page 9: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 8

Basic Raspberry Pi for Internet of Things

การก าหนดคา IP แบบ Direct Config เพอใชในการก าหนดคาครงแรก การก าหนดคาแบบ Direct Config นใชเพยงสาย LAN ตอกบ Raspberry Pi กบคอมพวเตอรของเรา ซง

ไมจ าเปนตองตอ Monitor หรอ Keyboard เลย ท าใหงายและสะดวกในการก าหนดคา Raspberry Pi เบองตน

นอกจากนยงใชในกรณฉกเฉนทไมสามารถเขา Raspberry Pi จากปญหาการก าหนดคา Network Interface ไม

ถกตองอกดวย

หลงจากทเราไดท าการตดตง OS ลงบน SD Card เรยบรอยแลวในการใชงานจรงเราจ าเปนทจะตอง

เชอมตอสาย LAN กบบอรด Raspberry Pi เพอใชในการ Remote เขาไป Config คาตาง ๆ แตการทเราจะ

Remote เขาไป Config คาตาง ๆ นนจ าเปนทจะตองก าหนดคา IP Address ของ Raspberry Pi วธการทงาย

ทสดในการก าหนดคา IP Address ในกรณทเราไมม Monitor, Keyboard, Mouse เราสามารถ Config ในไฟล

cmdline.txt และใช Tool อยาง Telnet, Putty เขาไปก าหนดคาตาง ๆ ในเบองตนไดโดยการเพมขอความ “

ip=192.168.1.100” ทายไฟล cmdline.txt

dwc_otg.lpm_enable=0 Console=serial0,115200 Console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait ip=192.168.100.100

โดยรปแบบการเชอมตอทงายทสดคอน า Raspberry Pi เสยบสาย LAN ตอตรงกบ Notebook

หลงจากนนทดสอบโดยการ ping ไปท IP Address ทเราก าหนดไวแตตองอยาลม Set IP Address ท

เครองเราเปน IP Address วงเดยวกนกบ Raspberry Pi ดวย

Page 10: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 9

Basic Raspberry Pi for Internet of Things

เราสามารถใช Putty เพอ Remote เขาไปกไดเชนกน

ปกตแลวการเชอมตอ SSL จะมการยนยนโดยใช RSA Key ในการเชอมตอโดยเมอเรากดปม yes จะเปน

การเกบและจ า Key นไวท Program Putty ของเรา

Page 11: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 10

Basic Raspberry Pi for Internet of Things

เมอ Connect เขามาท Raspberry Pi แลวจะมการใหใส Login และ Password โดยคา Default ของ

Raspberry Pi ตว Login จะใช

Login: pi Password raspberry

Page 12: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 11

Basic Raspberry Pi for Internet of Things

การก าหนดคา IP แบบ Static ในการก าหนด IP Address ของ Raspberry Pi และ Linux Distro อนจะมการก าหนดในรปแบบคลาย ๆ กน

และใหเราแกไข File Interface ตามน

auto lo iface lo inet loopback auto eth0 iface eth0 inet static address 192.168.100.10 gateway 192.168.100.200 netmask 255.255.255.0 network 192.168.100.0 broadcast 192.168.100.255

Page 13: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 12

Basic Raspberry Pi for Internet of Things

ใน Linux Jessie นหาก Set แค Interface File นนจะไมสามารถใชงาน Network ตามคา Config ของ

เราไดจ าเปนตองก าหนดในสวนของ File /etc/dhcpcd.conf เพมตอทาย (ส าหรบ OS ทใช Jessy เทานน สวน

OS ตวเกา ไมตองท า)

interface eth0 static ip_address=192.168.100.10/24 static routers=192.168.100.1 static domain_name_servers=192.168.100.200

นอกจากนเรายงจ าเปนตองแกไขไฟล cmd Line .txt โดยน า IP Address ออกมเชนนน IP Address แต

ยงคงเปนตวเดมตามทก าหนดใน cmdline .txt โดยไฟลนจะอยท /boot/cmdline.txt

dwc_otg.lpm_enable=0 Console=serial0,115200 Console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Page 14: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 13

Basic Raspberry Pi for Internet of Things

หากเราตรวจสอบ IP Address ดวยค าสง ifConfig จะพบวายงเปน IP Address เตมอย ใหเราท าการ

restart Raspberry Pi ดวยค าสง sudo shutdown -r now หรอ sudo reboot

หรอใชการ Restart Interface eth0 กไดดวยค าสง

sudo ifdown eth0 sudo ifup eth0

Page 15: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 14

Basic Raspberry Pi for Internet of Things

เมอ Raspberry Pi Reboot มาเรยบรอยแลวเราท าการทดสอบโดยการ ping ไปท IP ทเราแกไข

ตดตง RealVNC for Raspberry Pi Download

https://www.realvnc.com/download/binary/latest/debian

/arm/

ไดไฟล VNC-5.3.2-Linux-ARM-DEB.tar.gz ใช winSCP copy ไป

tar xvf VNC.tar.gz

sudo dpkg -i <VNC-Server-package-name>.deb [<VNC-

Viewer-package-name>.deb]

Page 16: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 15

Basic Raspberry Pi for Internet of Things

การตงคา IP Address ส าหรบ Wi-Fi ดวย Command Line ในการใชงาน Raspberry Pi นนคงเปนการล าบากทเราจะหา Mouse, Keyboard และ Monitor เพอ

เรมตนใชงานในตอนแรกโดยเขาใช GUI mode เพอทจะ Config Wi-Fi อกทางเลอกหนงเราสามารถก าหนดคา

Wi-Fi Configuration โดยการใช Command

เรมตนเราควรสแกน Wi-Fi ดวยค าสง

sudo iwlist wlan0 scan

Page 17: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 16

Basic Raspberry Pi for Internet of Things

และดในสวนของ SSID ทเราตองการจะเชอมตอ

Page 18: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 17

Basic Raspberry Pi for Internet of Things

ใหเราท าการเขาไปแกไขไฟล wpa_supplicant.conf

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

โดยเพมในสวนของ network

Page 19: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 18

Basic Raspberry Pi for Internet of Things

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB network={ ssid="BUAKAEW_AP01" psk="BuaKaew159357" key_mgmt=WPA-PSK }

หลงจากนนท าการ restart Wi-Fi interface ดวยค าสง

sudo ifdown wlan0 sudo ifup wlan0

และตรวจสอบ IP Address อกครงหนงจะพบวา wireless lan จะได IP Address ของ access point

Page 20: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 19

Basic Raspberry Pi for Internet of Things

การตงคาทวไปของ Raspberry Pi ในการก าหนดคาพนฐานของ Raspberry Pi กอนทเราจะเรมนนเราจะใชค าสง

sudo raspi-config

การขยายขนาด Space ใน SD Card ใหเตมพนท ในการ Burn SD Card จาก Image File ของ Raspberry Pi ครงแรกจะมเนอททถกใชงานใน SD Card

ประมาณ 4 GB หากเนอท SD Card เรามมากกวา 4 GB

การทเราจะมองเหน Space ของการดไดเตม 100% นนจ าเปนทตองมการขยาย File System โดยการ

เลอกขอ 1 Expand File System

Page 21: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 20

Basic Raspberry Pi for Internet of Things

เมอท าการขยายเสรจจะมขนหนาจอ Confirm

การก าหนดการ Boot เขาหนาจอของ Raspberry Pi ในการ boot เขาหนาจอของ Raspberry Pi นนหลงจากทเรา burn Image os ของ Raspberry Pi ลง

ไปแลวนนคา Default จดบธเขาในโหมด g u i เราสามารถเปลยนไดโดยการเขาไปทเมน ขอ 3 boot option

Page 22: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 21

Basic Raspberry Pi for Internet of Things

โดยสวนใหญแลวจะเลอกการ Boot แบบเขา Console เพอให Login จะเปนการประหยด Resource

มากกวาการเขาแบบ GUI และค านงถงเรอง Security ดวย

การก าหนด Raspberry Pi Name ในการเปลยนชอ Raspberry Pi ของเรานนใหเขาไปทเมน 9 Advanced Options เลอก Host Name

Page 23: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 22

Basic Raspberry Pi for Internet of Things

การก าหนด SSH ในการก าหนดให Raspberry Pi เปด Secure Shell ทใหเราสามารถ Telnet แบบมการเขารหสขอมลใน

การรบสง หรอเรยกสน ๆ วา SSH นน ใหเขาไปทเมน 9 Advanced Options เลอก A4 SSH

Page 24: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 23

Basic Raspberry Pi for Internet of Things

และ Confirm ในการ Enable SSH

นอกจากนยงมการก าหนดคาการใชงานอน ๆ เชน SPI I2C Serial 1-Wire audio GPIO กท าลกษณะเดยวกน

เมอเราแกไขคา Config ตาง ๆ ใน Raspberry Pi Config แลวกดปม finish จะใหเรา Boot Raspberry Pi

Page 25: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 24

Basic Raspberry Pi for Internet of Things

การตงเวลา Raspberry Pi การก าหนดเวลาของ Raspberry Pi สามารถก าหนดไดหลายแบบ แบบงายสดคอการก าหนดใน

International Option > Time Zone (การตงเวลาแบบน Raspberry Pi จ าเปนตองมการเชองตอ internet

เพอ Update เวลากบ NTP Server)

และเลอก Time Zone เปน Asia / Bangkok

Page 26: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 25

Basic Raspberry Pi for Internet of Things

Page 27: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 26

Basic Raspberry Pi for Internet of Things

ใชค าสง date เพอดวนเวลา

แตถาตองการก าหนดเวลาแบบ Manual จะใช Command

sudo date -s “Wed Aug 3 22:30:00 ICT 2016”

การตงคา Keyboard ก าหนดคาของ Keyboard Layout ท File /etc/default/keyboard ก าหนด XKBLAYOUT=”us”

Page 28: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 27

Basic Raspberry Pi for Internet of Things

# KEYBOARD CONFIGURATION FILE # Consult the keyboard(5) manual page. XKBMODEL="pc105" XKBLAYOUT="us" XKBVARIANT="" XKBOPTIONS="" BACKSPACE="guess"

Page 29: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 28

Basic Raspberry Pi for Internet of Things

การอพเดท Raspberry Pi ในครงแรกเราจ าเปนทจะตอง Update Package List เพอตรวจหารายการทเปน Program ใหม ๆ จาก

Repository หรอจาก Server ของระบบปฏบตการนนเอง วามการ update อะไรใหม ๆ หรอไมดวยค าสง

sudo apt-get update

หลงจากการใหเราท าการอพเกรด Install Package ตาง ๆ เมอ ท าการ update หรอตรวจสอบรายชอ

ใหม ๆ มาแลว upgrade กจะเปนการดาวนโหลดและตดตง Program ใหม ๆ เหลานนลงไปใน Raspberry Pi

นนเอง (อนนจะนาน)

sudo apt-get upgrade

Page 30: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 29

Basic Raspberry Pi for Internet of Things

การตดตง Remote Desktop ในการใชงาน Raspberry Pi นอกจากใช Secure Shell เพอใช Command Line ในการสงงานแลว ยง

สามารถใชการ Remote เขาไปใชงานในลกษณะของ Graphic Mode โดยตดตง Remote desktop ของ

Raspberry Pi นนจะมการตดตงใชงาน 2 ตวไดแก

การตดตง XRDP XRDP ใชโปรโตคอล Remote desktop protocol ในการใชงาน ซงเราจ าเปนจะตองตดตง Package

sudo apt-get install xrdp

ในการใชงานจากฝง client เราสามารถใช Program Remote desktop หรอ Smart TTY

Page 31: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 30

Basic Raspberry Pi for Internet of Things

แนะน าใหใช Program SmartTTY เพราะเราสามารถเปด Program ท Run บน XWindows ใหสามารถ

มาท างานบนหนาจอเครองท Remote ได

Page 32: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 31

Basic Raspberry Pi for Internet of Things

การตดตง VNC ส าหรบคนทไมชอบใช Remote Desktop เราสามารถใช VNC แทนไดโดยตดตง TightVNC Server ซง

เปน Software ส าหรบ Remote ทไมมคาใชจาย

sudo apt-get install tightvncserver

หลงจากนนรนใช VNC server โดยตองก าหนด Password กอน

tightvncserver

การใชงานเพลงเปด VNC ขนมาและใส IP Address : 1

Page 33: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 32

Basic Raspberry Pi for Internet of Things

Page 34: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 33

Basic Raspberry Pi for Internet of Things

การใชงาน GPIO ในการใชงาน Raspberry Pi นนเราสามารถใชขา GPIO ตาง ๆ ในการเชอมตอกบอปกรณภายนอกและ

Sensor ซงรปแบบตาจะเปนตามรปดานบนจะสงเกตเหนวา Pi นนสามารถใชไดทง I2C, SPI และ UART ดงนน

เราจ าเปนตองรวาขาไหนสามารถใชงาน Function อะไรไดบาง ระดบแรงดนทใชของแตละขาจะอยท 3.3 v การ

จดเรยงขา GPIO เปนตามภาพ

Page 35: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 34

Basic Raspberry Pi for Internet of Things

การสงงานผาน GPIO ดวย Command line GPIO output

อยางแรกเราตองก าหนด GPIO วาขาไหนจะใหเปน output

echo "4" > /sys/class/gpio/export //ตง GPIO4 เปน GPIO echo "out" > /sys/class/gpio/gpio4/direction //ก าหนดโหมดใหเปน GPIO out echo "1" > /sys/class/gpio/gpio4/value // สงคา 1 ออกขา GPIO 4 echo "0" > /sys/class/gpio/gpio4/value // สงคา 0 ออกขา GPIO 4

GPIO Input

echo "22" > /sys/class/gpio/export //ตง GPIO4 เปน GPIO echo "in" > /sys/class/gpio/gpio22/direction //ก าหนดโหมดใหเปน GPIO out cat /sys/class/gpio/gpio22/value // อานคาขา GPIO 4

การยกเลกการใช GPIO

echo "22" > /sys/class/gpio/unexport

Page 36: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 35

Basic Raspberry Pi for Internet of Things

การเขยน Program Python พนฐาน ในการใชงาน Microcontroller และ embedded System สวนใหญนนนอกจาก ภาษา C แลวยงม

ภาษาทนยมใชอก คอ Python ซงเปนภาทม Library ใหเลอกใชส าหรบเชอมตอกบ Hardware ใหอยางมากมาย

และมรปแบบ Syntax ทไมซบซอนจงงายตอการพฒนา

Python กคอมนเปนทง Cross Platform และม Open Source License โดยทมอสระในการแกไข Library ตาง

ๆ ท Python ใหมา รวมถงการน า Software ทพฒนาขนจาก Python ไปท าประโยชนทางธรกจไดอยางเตมทโดย

ไมเสยเงนนอกจากนตวโครงสรางและ Syntax ของภาษาคอนขางอานงาย เขาใจงาย และม Object ดาน Data

Structure รองรบอยหลายแบบแลว ท าให Python เปนของ ฟร และของ ด ทชวยใหพฒนา Program ไดเรวขน

ตวแปรในภาษา python การใชงานตวแปรในภาษา python นนคอนขางทจะงายเนองดวยไมจ าเปนตองประกาศตวแปรกสามารถทจะ

น าไปใชงานได

X = 10

Y = 20.5

Uname = “maximus”

Syntax ในภาษา python ลกษณะซนแทกของภาษา python ไมจ าเปนตองมการปดบรรทดดวยโคลอนหรอการใชเครองหมายปกกาในการ

ก าหนดชวงของการเขยน Program ซงจะใชการแทกหรอเคาะ Space ในการก าหนดชวงของการเขยน Program

โดยใน 1 บรรทดจะเขยนการท างานไดแคสเตจเดยว

Page 37: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 36

Basic Raspberry Pi for Internet of Things

#include <stdio.h>

Void main(void){

Int x = 1;

while (x < 10){

printf(“%d\n”);

x++;

}

}

x = 1

While x<10:

Print x

x++

การก าหนดตวแปร Python ไมจ าเปนตองประกาศตวแปรกอนใชงาน สามารถก าหนดคาขนมา และเรยกใชไดเลย แตการตวชอตว

แปรของ ไพทอนตองเปนไปตามกฏดงน

- ขนตนดวยตวอกษรตงแต 1 ตวขนไป

- ตวแปรหามมชองวาง

- หามมสญลกษณพเศษเชน #,?,$,... (คนทเขยน PHP มาอยาลงไปใส $ เขานะครบ)

- ตวแปรตองไมซ ากบค าสงวน ดค าสงวนไดทายบทความน

- ตวแปรใชตวพมพเลกพมพใหญ มความหมายตางกน (case-sensitive แบบเดยวกบ PHP)

Page 38: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 37

Basic Raspberry Pi for Internet of Things

ชนดขอมล ชนดขอมลของไพทอน ไพทอนมชนดขอมลพนฐานอย 5 ชนดดงน

- Numbers เกบขอมลตวเลข

x=10

y=10.5

- String เกบขอมลตวอกษร

x=”maximus”

- List เกบขอมลไดมากวา 1 คาใน 1 ตวแปร หรอทเรยกวา compound type

var_list = [ 'abcd', 123 1.23, 'Mindphp.com', 20.2 ]

var_tinylist = [123, 'mindphp']

print (var_list) # แสดงคาทงหมดในตวแปล var_list

print (var_list[0]) # แสดงรายการแรกของตวแปล var_list

print (var_list[1:3]) # แสดงรายการท สอง ถงสามรายการถดไป

print (var_list[2:]) # แสดงรายการทสามถง สดทาย

print (var_tinylist * 2) # แสดงรายการทงหมดของตวแปล var_tinylist สองครง

print (var_list + var_tinylist) # แสดงรายการทงหมดของ var_list, var_tinylist รวมกน

- Tuple อานวา "ทเพล" เกบขอมลไดมากวา 1 คาใน 1 ตวแปร ใชส าหรบเกบล าดบ หรอทเรยกวา sequence

type

ทเบลเปนตวแปลทเกบขอมลแบบล าดบ (sequence data type) คลาย ๆ กบ ตวแปลชนดลส ทเปลสจะเกบคาของสมาชกแตละตวไวใน เครองหมาย (...,...,...)

Page 39: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 38

Basic Raspberry Pi for Internet of Things

ตวอยาง Tuples.py var_tuples = ( 'abcd', 123 1.23, 'Mindphp.com', 20.2 ) var_tinytuples = (123, 'mindphp') print (var_tuples) # แสดงคาทงหมดในตวแปล var_tuples print (var_tuples[0]) # แสดงรายการแรกของตวแปล var_tuples print (var_tuples[1:3]) # แสดงรายการท สอง ถงสามรายการถดไป print (var_tuples[2:]) # แสดงรายการทสามถง สดทาย print (var_tinytuples * 2) # แสดงรายการทงหมดของตวแปล var_tuples สองครง print (var_tuples + var_tinytuples) # แสดงรายการทงหมดของ var_tuples, var_tinytuples รวมกน ผลทได ('abcd', 123, 1.23, 'Mindphp.com', 20.2) abcd (123, 1.23) (1.23, 'Mindphp.com', 20.2) (123, 'mindphp', 123, 'mindphp') ('abcd', 123, 1.23, 'Mindphp.com', 20.2, 123, 'mindphp') Tuples เมอสรางขนมาแลวเราไมสามารถเพมคาใหตวแปลได เชน ไดเชน ตามตวอยาง ตวแปล var_tuples ม index ทงหมด 5 ตว เราไมสามารถ var_tuples[6] = My Var' # ไมสามารถท าได จากตางจาก List ทสามารถก าหนด คาใหมใหกบ index เดม ได

Page 40: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 39

Basic Raspberry Pi for Internet of Things

-Dictionary เกบขอมลไดมากวา 1 คาใน 1 ตวแปรเชนกน หรอทเรยกวา table type เทยบไดกบตวแปร array

ใน php

ตวแปลชนดดกชนนาร ในไพทอน ดกชนนารในไฟทอนเรยกไดวาเปน hash table type คลายกบ ตวแปล array ใน php และ hash ใน ภาษา Perl มาก คอจะม key และ value คกนไปเสมอ key สามารถ เอา data type อะไรของ ไพธอนมาก าหนด กได แตแนะน าวาใช แค string และ numbers จะดกวา ตวแปล ดกชนนาร ถาก าหนดคาใหอยในเครองหมาย curly braces {...}" และ key จะถกก าหนดอยในเครองหมา square braces "[]" ตวอยาง Dictionary.py var_dict = {} var_dict['one'] = "This is one" var_dict[2] = "This is two" var_tinydict = {'one': 'This is one ของ var_tinydict', 2:'This is two ของ var_tinydict', 'dept': 'sales'} print (var_dict) # แสดงคาทงหมดของ var_dict print (var_dict['one']) # แสดงเฉพาะ key "one" ของ ตวแปล var_dict print (var_dict[2]) # แสดงเฉพาะ key "2" ของ ตวแปล var_dict print ( var_dict.keys() ) # แสดง key ทงหมดของ ตวแปล var_dict print ( var_dict.values()) # แสดง values ทงหมดของ var_dict print ('---------------------------') print (var_tinydict) # แสดงคาทงหมดของ var_dict print (var_tinydict['one']) # แสดงเฉพาะ key "one" ของ ตวแปล var_dict print (var_tinydict[2]) # แสดงเฉพาะ key "2" ของ ตวแปล var_dict print ( var_tinydict.keys() ) # แสดง key ทงหมดของ ตวแปล var_dict print ( var_tinydict.values()) # แสดง values ทงหมดของ var_dictผลทได

Page 41: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 40

Basic Raspberry Pi for Internet of Things

{2: 'This is two', 'one': 'This is one'} This is one This is two dict_keys([2, 'one']) dict_values(['This is two', 'This is one']) --------------------------- {'dept': 'sales', 2: 'This is two ของ var_tinydict', 'one': 'This is one ของ var_tinydict'} This is one ของ var_tinydict This is two ของ var_tinydict dict_keys(['dept', 2, 'one']) dict_values(['sales', 'This is two ของ var_tinydict', 'This is one ของ var_tinydict'])

Operator

Arithmetic Operator Description Example

+ การบวก 3+4=7

- การลบ 3-4=-1

* การคณ 3*4=12

/ การหาร 23/4=5.75

% การหารเอาผลลพธทเปนเศษ 23%4=75

// การหารเอาผลลพธทเปนจ านวนเตม 23//4=5

** ยกก าลง 3**4=81

Page 42: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 41

Basic Raspberry Pi for Internet of Things

Comparison Operator Description Example

== เปรยบเทยบเทากนหรอไม If (a == b) :

!= เปรยบเทยบไมเทากนหรอไม If (a != b) :

> เปรยบเทยบมากกวา If (a > b) :

< เปรยบเทยบนอยกวา If (a < b) :

>= เปรยบเทยบมากกวาหรอเทากบ If (a >= b) :

<= เปรยบเทยบนอยกวาหรอเทากบ If (a <= b) :

Logical Operator Description Example

and เงอนไข และ If (a and b) :

or เงอนไข หรอ If (a or b) :

not เงอนไข ตรงขาม If not (a and b) :

Membership Operator Description Example

in จะเปนจรงเมอคาอยในตวแปร For x in List :

Not in จะเปนจรงเมอมคาไมอยในตวแปร For x not in List :

Page 43: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 42

Basic Raspberry Pi for Internet of Things

Condition If x > 30 :

print(‘a’)

Elif x > 15 :

print(b’)

Else:

print(‘c’)

ใน Python ไมม Switch case

Loop ใน Python Loop สามารถใช For Loop และ While

For Loop มรปแบบ

รปแบบ ตวอยาง ผลลพธ

for ตวแปร in ล าดบขอบเขต : ค าสง

for x in 'word': print( x )

w o r d

for count in range(1, 5): print(count)

1 2 3 4

Page 44: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 43

Basic Raspberry Pi for Internet of Things

While

รปแบบ ตวอยาง ผลลพธ

while นพจน: ขอความสง(s)

count = 0 while (count <= 4): print ('Is:', count) count = (count + 1)

Is: 0 Is: 1 Is: 2 Is: 3 Is: 4

while True: n = input("Please enter 'hello':") if n.strip() == 'hello': break

Please enter ‘hello’: X Please enter ‘hello’: Max Please enter ‘hello’: hello

Function การประกาศ function ใน python จะตองขนตนดวย def และตามดวยชอ function (argument) และ return

คาออกจาก function ซงการเรยกใชกเพยงอางองชอ function และปอน parameter value ตามตวอยาง

ตวอยาง ผลลพธ

def sum(*args): out = 0 for i in args: out += i return out print(sum(1,3,5,7,9))

25

Page 45: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 44

Basic Raspberry Pi for Internet of Things

การใช Python เพอควบคม GPIO

GPIO Output ตอ LED ท GPIO 4 โดย Program จะท างานสงให LED ตดดบสลบกน 1 วนาท

import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(4,GPIO.OUT) while 1: GPIO.output(4, True) time.sleep(1) GPIO.output(4, False) time.sleep(1)

*** โหมดของ GPIO บน Raspberry Pi จะม 2 mode คอ BCM (ส าหรบอางองตาม GPIO) และ BOARD จะนบ

ตามขา pin ทอยบน board

Page 46: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 45

Basic Raspberry Pi for Internet of Things

GPIO Input ตอวงจรเพมโดยขา 22 ตอกบ switch

import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(4,GPIO.OUT) GPIO.setup(22,GPIO.IN) while 1: in_val=GPIO.input(22) if in_val==1 : GPIO.output(4,True) else : GPIO.output(4,False)

นอกจากนใน pin ของ Raspberry Pi ยงมสวน Interface ในรปแบบอนอก ไดแก SPI (Serial Peripheral

Interface) และ I2C (Inter Integrate Circuit Bus)

Page 47: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 46

Basic Raspberry Pi for Internet of Things

การใชงานกลอง USB Camera

การหากลอง USB เมอเราเสยบกลองแลว สามารถ Scan หาวาพบกลองหรอไมโดยค าสง

lsusb

สงภาพจาก WebCam ไปแสดงบน Web Browser

sudo apt-get install motion

แกไขไฟล motion.conf

sudo nano /etc/motion/motion.conf # Start in daemon (background) mode and release terminal ( Default off) daemon on # TCP/IP port for the http server to Listen on ( Default 0 = disabled) webcontrol_port 8080 # Restrict control Connect ions to localhost only ( Default on) webcontrol_localhost on # Output for http server, select off to choose raw text plain ( Default on) webcontrol_html_output on

Page 48: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 47

Basic Raspberry Pi for Internet of Things

และก าหนดให motion ท างานเปน Daemon Service

sudo nano /etc/ Default /motion # Set to 'yes' to enable the motion daemon start_motion_daemon=yes

Start Motion Service

sudo service motion start

บนทกรปจากกลอง USB Camera

sudo apt-get install fswebcam

fswebcam Image.jpg fswebcam -r 1280x720 --no-banner Image3.jpg fswebcam -d /dev/video0 -r 640x480 test123.jpg

Page 49: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 48

Basic Raspberry Pi for Internet of Things

mkdir webcam

sudo nano webcam.sh #!/bin/bash DATE=$(date +"%Y-%m-%d_%H%M") fswebcam -r 1280x720 --no-banner /home/pi/webcam/$DATE.jpg

ls -alt webcam

./webcam.sh

crontab -e

Page 50: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 49

Basic Raspberry Pi for Internet of Things

# m h dom mon dow Command * * * * * /home/pi/webcam.sh 2>&1

Page 51: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 50

Basic Raspberry Pi for Internet of Things

Page 52: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 51

Basic Raspberry Pi for Internet of Things

ท า Raspberry Pi เปน MQTT Broker

Update และ Upgrade Raspberry Pi ใหเรยบรอย

ตดตง Software ทเกยวของ

sudo apt-get install libssl-dev sudo apt-get install cmake sudo apt-get install libc-ares-dev sudo apt-get install uuid-dev sudo apt-get install daemon

ตดตง LibWebSocket

#Copy libwebsockets-1.4-chrome43-firefox-36.tar.gz by WinSCP tar zxvf libwebsockets* cd libwebsockets-1.4-chrome43-firefox-36 mkdir build cd build cmake .. sudo make install sudo ldconfig #Rebuild Library Cash cd #go home

ตดตง Mosquito

wget http://mosquitto.org/files/source/mosquitto-1.4.4.tar.gz tar zxvf mosquitto-1.4.4.tar.gz cd mosquitto-1.4.4

Page 53: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 52

Basic Raspberry Pi for Internet of Things

แกไฟล

sudo nano config.mk WITH_WEBSOCKETS:=yes

Make and Install

make sudo make install

Copy config

sudo cp mosquitto.conf /etc/mosquitto

Edit Websocket เพม 2 บรรทด

sudo nano /etc/mosquitto/mosquitto.conf listener 9001 protocal websockets

Add user mosquito ( Password p@ssw0rd)

sudo adduser mosquitto

Page 54: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 53

Basic Raspberry Pi for Internet of Things

Socket Programming สงทเราตองรคอ OSI Layer

และการสอสารผาน URL

Page 55: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 54

Basic Raspberry Pi for Internet of Things

การท า Hand Checking

Page 56: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 55

Basic Raspberry Pi for Internet of Things

Basic Topology

ทดลองท า Raspberry Pi เปน Socket Server

Python

01_tcp_server.py from socket import * import thread BUFF = 1024 HOST = '' PORT = 9999 def handler(clientsock,addr):

Page 57: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 56

Basic Raspberry Pi for Internet of Things

while 1: data = clientsock.recv(BUFF) if not data: break print "Receive data + data + "from" + str(addr) clientsock.send("GOT YOUR DATA " + data) if "close" == data.rstrip(): break clientsock.close() print addr, "closed Connect ion" #log on Console if __name__=='__main__': ADDR = (HOST, PORT) serversock = socket(AF_INET, SOCK_STREAM) serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) serversock.bind(ADDR) serversock.listen(5) while 1: print 'Wating for Connect ion', PORT clientsock, addr = serversock.accept() print Connect ed from:', addr thread.start_new_thread(handler, (clientsock, addr))

Page 58: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 57

Basic Raspberry Pi for Internet of Things

ใชงานโดย เปด Heculess

Page 59: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 58

Basic Raspberry Pi for Internet of Things

ทดลองท า Raspberry Pi เปน Socket Server รบคา JSON

Page 60: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 59

Basic Raspberry Pi for Internet of Things

Page 61: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 60

Basic Raspberry Pi for Internet of Things

ทดลองสงขอมลผาน JSON และบนทกลง Database SQLite SQLite Database

- ในหลาย ๆ ระบบทใช Embedded System เปนสวนประกอบตองการดคาและสามารถตรวจสอบขอมล

ยอนหลงได

- การเกบขอมลตองสามารถดยอนหลงโดยใชวนท เดอน หรอป เปนตวก าหนดในการเลอกดได

- ในกรณทขอมลมมหาศาลตองมกลไกในการเลอกแสดงขอมล

ท าไมเราเลอก SQLite

- เลก เรว ดแลงาย ดวยซอรสโคดทบรรจอยในไฟลเดยว ( แตเปนแสนบรรทด )

- นยมใชในอปกรณขนาดเลกเชน Smart phone, Embedded Linux

- มระบบการท างานเชน in-memory database

- ซพพอรตชดค าสง SQL

- Flat File Friendly

ไฟลทใชในการตดตง

sqlite3_3.7.13-1+deb7u1_armhf.deb

ค าสงทใชในการตดตง

sudo dpkg -i sqlite3_3.7.13-1+deb7u1_armhf.deb ถาม Error จากการตดตงใหลอง

sudo apt-get update sudo apt-get upgrade sudo apt-get -f install

Page 62: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 61

Basic Raspberry Pi for Internet of Things

ทดสอบเขา SQL Lite ดวย

Sqlite3 test.db

สราง Table

CREATE TABLE TEMP_T(NUM INTEGER PRIMARY KEY AUTOINCREMENT, ID INTEGER,TEM FLOAT, PIR TEXT, TIME TEXT );

Insert Data

insert into TEMP_T VALUES(NULL, 01, 23.12, "ON", "12:45:00"); insert into TEMP_T VALUES(NULL, 02, 24.45, "OFF", "12:50:00"); insert into TEMP_T VALUES(NULL, 01, 23.45, "OFF", "12:55:00"); insert into TEMP_T VALUES(NULL, 03, 23.45, "ON", "13:00:00");

Page 63: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 62

Basic Raspberry Pi for Internet of Things

Select Data

Select * from TEMP_T;

Insert Data ดวย Python

insert_SQLite.py import sqlite3 as lite import sys con = lite. Connect ('test.db') with con: cur = con.cursor() cur.execute("insert into TEMP_T VALUES(NULL, 01, 23.12, 'ON', '12:00:00');") if con: con.close()

Page 64: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 63

Basic Raspberry Pi for Internet of Things

Select Data ดวย Python

select_SQLite.py import sqlite3 as lite import sys con = lite. Connect ('test.db') with con: cur = con.cursor() cur.execute("SELECT * FROM TEMP_T") rows = cur.fetchall() for row in rows: print row

Page 65: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 64

Basic Raspberry Pi for Internet of Things

File 03_tcp_server_json_db.py

Page 66: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 65

Basic Raspberry Pi for Internet of Things

*** ตอง Run Script ใน Parth ทม File test.db อย

Page 67: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 66

Basic Raspberry Pi for Internet of Things

การใช MQTTSpy

Page 68: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 67

Basic Raspberry Pi for Internet of Things

ทดลองใชงาน Blynk

Page 69: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 68

Basic Raspberry Pi for Internet of Things

QT programming

Page 70: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 69

Basic Raspberry Pi for Internet of Things

Reference http://mosquitto.org/

http://mqtt.org/

Page 71: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 70

Basic Raspberry Pi for Internet of Things

Project TieText

sudo apt-get -y install fswebcam

sudo apt-get -y install libav-tools

apt-get install streamer

sudo streamer -c /dev/video0 -o /home/pi/camera/outputfile.jpeg

streamer -t 0:0:2 -c /dev/video0 -f rgb24 -r 3 -o /home/pi/camera/outputfile.avi

Install FFmpeg

cd /usr/src

sudo git clone git://git.videolan.org/x264

cd x264

sudo ./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl

sudo make

sudo make install

cd /usr/src

sudo git clone https://github.com/FFmpeg/FFmpeg.git

cd ffmpeg

sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree

Page 72: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 71

Basic Raspberry Pi for Internet of Things

make

sudo make install

ffmpeg -f video4linux2 -r 25 -s 640x480 -i /dev/video0 out.avi

ffmpeg -f v4l2 -r 25 -s 640x480 -i /dev/video0 out.avi

Page 73: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 72

Basic Raspberry Pi for Internet of Things

Open CV

http://opencv-python-

tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html

sudo apt-get install python-opencv

Python Capture VDO

import numpy as np import cv2 cap = cv2.VideoCapture(0) while(True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()

Python Saving VDO

import numpy as np import cv2 cap = cv2.VideoCapture(0) # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) while(cap.isOpened()): ret, frame = cap.read() if ret==True: frame = cv2.flip(frame,0)

Page 74: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 73

Basic Raspberry Pi for Internet of Things

# write the flipped frame out.write(frame) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # Release everything if job is finished cap.release() out.release() cv2.destroyAllWindows()

Python Play VDO from File

import numpy as np import cv2 cap = cv2.VideoCapture('vtest.avi') while(cap.isOpened()): ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()

http://www.pygame.org/docs/tut/camera/CameraIntro.html

Page 75: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 74

Basic Raspberry Pi for Internet of Things

QT Wiring Pi

WiringPi คอ กลมของไลบรารทพฒนาโดย http://wiringpi.com ส าหรบเขาถงการใชงาน GPIO ของชพ

BCM2835 ทเปนชพประมวลผลหลกของบอรด Raspberry Pi ซงสามารถใชงานไดจากภาษา C/C++ และภาษา

อนๆ อกหลากหลายภาษาท WiringPi ออกแบบชดค าสงใหมลกษณะคลายกบการใชงานภาษา Wiring ท าใหผท

เคยใชงานบอรด Arduino รสกคนเคยกบรปแบบชดค าสงและท าใหการควบคมฮารดแวรของบอรด Raspberry Pi

สามารถท าไดงายขน

วธตดตง WiringPi ของบอรด Raspberry Pi

- เชอมตอบอรด Raspberry Pi กบ Internet

- เปดโปรแกรม LXTerminal

- ในขนตอนการใชค าสง update และ upgrade จะใชเวลาในการคอนขางนาน หากเคย update และ upgrade

ไปแลว อาจทดลองใชค าสง git clone git://git.drogon.net/wiringPi ในหวขอถดไปดกอน แลวหากไม Error ก

สามารถขามขนตอนนไปได

o พมพค าสง sudo apt-get update แลวรอจนอพเดทเสรจ

o พมพค าสง sudo apt-get upgrade แลวรอจนอพเกรดเสรจ

- พมพค าสง sudo apt-get install git-core เพอตดตง Git Core

- พมพค าสง sudo apt-get install libi2c-dev เพอตดตงไลบรารของ I2C

- พมพค าสง git clone git://git.drogon.net/wiringPi เพอสรางลงค WiringPi จาก Git

- พมพค าสง cd wiringPi กด Enter

Page 76: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 75

Basic Raspberry Pi for Internet of Things

- พมพค าสง git pull origin เพอดงซอรสโคดมาจากตนทาง

- พมพค าสง ./build เพอคอมไพล WiringPi

- ทดสอบการตดตง WiringPi พมพค าสง gpio –v จะปรากฏขอความดงภาพ

- พมพค าสง gpio readall จะปรากฏตาราง GPIO ดงภาพ

Page 77: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 76

Basic Raspberry Pi for Internet of Things

QT Camera ใช ตองใช Version 5.3 ขนไป และตองลง QtMultimedia ดวย

- Check Version

$qmake –version

- Install QTMultimedia

$sudo apt-get install qtmultimedia5*

$sudo apt-get install qtdeclarative5-dev

$sudo apt-get install qtbase5-dev

sudo apt-get install qt5-default libqt5multimedia5 libqt5multimedia5-plugins \

libqt5multimediaquick-p5 libqt5multimediawidgets5 \

qtmultimedia5-dev qtmultimedia5-examples

sudo apt-get install gstreamer0.10-plugins-*

apt-get install python-wxgtk3

Python GUI Kivy

sudo apt-get update

sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev

libsdl2-ttf-dev \

Page 78: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 77

Basic Raspberry Pi for Internet of Things

pkg-config libgl1-mesa-dev libgles2-mesa-dev \

python-setuptools libgstreamer1.0-dev git-core \

gstreamer1.0-plugins-{bad,base,good,ugly} \

gstreamer1.0-{omx,alsa} python-dev cython

เอารปรวมเปน VDO

sudo apt-get install mencoder

ถาตดตงไมไดใชวธน

wget http://www.mplayerhq.hu/MPlayer/releases/mplayer-export-snapshot.tar.bz2

bunzip2 *

tar xvf mplayer-export-snapshort.tar

cd mplayer-export-2016-03-26/

sudo apt-get install ffmpeg --- ถาไมสามารถลงไดใหไปท า configure เลย มนจะ download ffmpeg มาให

./configure

make

< ... wait a long time here ...>

./mencoder

sudo make install

ls *.jpg > stills.txt

mencoder -nosound -ovc lavc -lavcopts

vcodec=mpeg4:aspect=16/9:vbitrate=8000000 -vf scale=1920:1080 -o

timelapse.avi -mf type=jpeg:fps=24 mf://@stills.txt

Page 79: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 78

Basic Raspberry Pi for Internet of Things

Mount Windows Share Folder in Linux

$sudo mount -t cifs //192.168.87.248/GreigeInspection /mnt -o user=MDRS,password=159357

$sudo umount /mnt

sudo mount -t cifs //192.168.84.38/GreigeInspection /mnt -o user=MDRS,password=159357

Page 80: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 79

Basic Raspberry Pi for Internet of Things

sudo mount -t cifs -o username=administrator,password=p@ssw0rd,domain=192.168.84.3

//192.168.84.6/machine /mnt

ท า Auto mount

วธท 1 ทไฟล /etc/fstab

//192.168.84.38/GreigeInspection /mnt cifs

defaults,rw,username=administrator,password=Asdf1234,domain=192.168.84.3,uid=pi,gid=pi,x-

systemd.automount 0 0

*****ถา boot มาแลวมนไม mount ตองไปเพมใน /etc/rc.local

sudo mount -av

****** ถาไมได ใชวธท 2

วธท 2 ใน File /etc/rc.local ******** วธน OK

สราง script mountshare.sh ม delay นดนง

#! /bin/sh -e

sleep 50

sudo mount -t cifs -o username=administrator,password=p@ssw0rd,domain=192.168.84.3

//192.168.84.9/machine /mnt

exit 0

sudo mount -t cifs -o username=administrator,password=Asdf1234,domain=192.168.84.3

//192.168.84.38/GreigeInspection /mnt #!/bin/sh -e

sudo /home/pi/mountshare.sh &

#

# 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

Page 81: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 80

Basic Raspberry Pi for Internet of Things

# value on error.

...

Refresh mount table

sudo mount -av

******* ถาไมได ใชวธสดทาย

วธท 3 ใช crontab

สราง script mountshare.sh ม delay นดนง

#! /bin/sh -e

sleep 50

sudo mount -t cifs -o username=administrator,password=p@ssw0rd,domain=192.168.84.3

//192.168.84.6/machine /mnt crontab -e

@reboot /home/pi/mountshare.sh &

Page 82: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 81

Basic Raspberry Pi for Internet of Things

การท า Script ใหเปน service start|stop ไดใน init.d

Create script in /etc/init.d sudo nano /etc/init.d/mountshare.sh

mountshare.sh

#! /bin/sh # /etc/init.d/mountshare

### BEGIN INIT INFO

# Provides: mountshare

# Required-Start: $remote_fs $syslog

# Required-Stop: $remote_fs $syslog

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: Simple script to start a program at boot

# Description: A simple script from www.stuffaboutcode.comwhich

will start / stop a program a boot / shutdown.

### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system case "$1" in start) echo "Starting mountshare" # run application you want to start sudo mount -t cifs -o

username=administrator,password=p@ssw0rd,domain=192.168.84.3

//192.168.84.6/machine /mnt ;; stop) echo "Stopping mountshare" # kill application you want to stop sudo umount /mnt ;; *) echo "Usage: /etc/init.d/mountshare {start|stop}" exit 1 ;;

Page 83: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 82

Basic Raspberry Pi for Internet of Things

esac

exit 0

Make script executable

sudo chmod 755 /etc/init.d/mountshare.sh

Test starting the program

sudo /etc/init.d/mountshare.sh start

Test stopping the program

sudo /etc/init.d/mountshare.sh stop

Register script to be run at start-up To register your script to be run at start-up and shutdown, run the following command:

sudo update-rc.d mountshare.sh defaults

f you ever want to remove the script from start-up, run the following command:

sudo update-rc.d -f mountshare.sh remove

ท า Automatic Run Python in X-Windows

วธท 1

ท File: /etc/xdg/lxsession/LXDE-pi/autostart

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

Page 84: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 83

Basic Raspberry Pi for Internet of Things

เพม @/usr/bin/python /home/pi/camera/finalMDRS.py **** ตองอยกอน @xscreensaver

@lxpanel --profile LXDE @pcmanfm --desktop --profile LXDE @lxterminal @leafpad @xscreensaver -no-splash@

วธท 2 ****** ใชได

ท File: /home/pi/.config/lxsession/LXDE-pi/autostart

@lxpanel --profile LXDE @pcmanfm --desktop --profile LXDE @/usr/bin/python /home/pi/MDRS/finalMDRS.py @xscreensaver -no-splash@

หรอ

@lxterminal --command “/home/pi/MDRS/RunFinalMDRS.sh”

Df

Page 85: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 84

Basic Raspberry Pi for Internet of Things

Tkinter Graphic

ตองตดตง

sudo apt-get install python-tk

sudo apt-get install python-imaging-tk

sudo apt-get install idle python-pmw python-imaging

Page 86: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 85

Basic Raspberry Pi for Internet of Things

Setting up I2C

The RTC board uses what is known as the I2C (pronounced 'eye squared see') protocol to

communicate between itself and the Pi. The I2C allows the Pi to communicate with a lot of

different devices at the same time that are also connected to the same I2C pins on the Pi.

Simon Monk has written very good instructions about how to set up your Pi to be able to use

i2c on the Adafruit site. Follow the full instructions, including checking that everything has

worked by going through the manual instructions too.

Plugging in the Board

It is always best to turn your Pi off before plugging anything onto the GPIO pins. Always do this

using the sudo halt or sudo shutdown -h nowcommands.

Now plug the board onto your Raspberry Pi GPIO pins on the inside corner with the RTC board

pointing inwards, as shown below.

Page 87: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 86

Basic Raspberry Pi for Internet of Things

Setting up the RTC

You now need to configure your Pi to recognise and use the board.

Turn on the Pi and verify that the board is seen by running sudo i2cdetect -y 1 at the

command line. You should see ID #68 show up (the I2C address of the DS1307 Real Time Clock

chip):

If you have an older Rev. 1 Pi, you will have to run sudo i2cdetect -y 0 as the I2C bus address

changed from 0 to 1 from rev 2 onwards.

Setting the RTC Clock

The RTC module must be loaded by the kernel by running:

sudo modprobe rtc-ds1307

Now you need to be running as the super user; type in:

sudo bash

and then, if you have a Rev.2 Pi or later:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

Page 88: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 87

Basic Raspberry Pi for Internet of Things

Or if you have the older Rev.1 Pi, use:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device

Type exit to stop being the 'root' user.

Check the time on the RTC device using:

sudo hwclock -r

If this is the first time the module has been used, it will report back Jan 1 2000, and you'll need

to set the time to the current time.

The easiest way is to connect it up to Ethernet or Wifi. It will automatically set the time from the

network. You can check the time using the datecommand:

Run sudo hwclock -w to write the system time to the RTC board.

You can then verify it with sudo hwclock -r:

So, now the RTC clock is set, is that it? Well, no. The next time you boot your Pi, it will not know

that it needs to read the clock from the RTC, so if it is not connected to the internet it will again

be showing the wrong time.

Set Date

sudo date -s "Wed Dec 14 08:58:00 UTC 2016"

sudo date -s "Wed Dec 14 16:14:00 ICT 2016"

Page 89: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 88

Basic Raspberry Pi for Internet of Things

Making Your Pi Use the Real Time Clock

You will need to add the RTC kernel module to the file /etc/modules so it is loaded when the

Pi boots. In a terminal window run sudo nano /etc/modules and add rtc-ds1307 at the end of

the file:

Next you will need to add the DS1307 device creation at boot by editing the /etc/rc.local file

by running

sudo nano /etc/rc.local

and add the following lines to the file:

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

sudo hwclock -s

date

Just before the exit 0. Note: If you have a Rev 1 Pi, replace i2c-1 by i2c-0 above.

Page 90: Basic Raspberry Pi - staff.informatics.buu.ac.thnutthanon/887496/RaspberryPi.pdf · Basic Raspberry Pi for Internet of Things การตั้งค่า IP Address ส าหรับ

Page. 89

Basic Raspberry Pi for Internet of Things

To save the file, press Ctrl+X, Y then return.

To test this out, shutdown your Pi, unplug any ethernet cable or wifi dongle and then turn the Pi

back on. Use the following command in a terminal window to check the date is still set:

date