دسترسی نامحدود
برای کاربرانی که ثبت نام کرده اند
برای ارتباط با ما می توانید از طریق شماره موبایل زیر از طریق تماس و پیامک با ما در ارتباط باشید
در صورت عدم پاسخ گویی از طریق پیامک با پشتیبان در ارتباط باشید
برای کاربرانی که ثبت نام کرده اند
درصورت عدم همخوانی توضیحات با کتاب
از ساعت 7 صبح تا 10 شب
ویرایش:
نویسندگان: William Edison
سری:
ناشر: William Edison
سال نشر: 2019
تعداد صفحات: 417
زبان: English
فرمت فایل : PDF (درصورت درخواست کاربر به PDF، EPUB یا AZW3 تبدیل می شود)
حجم فایل: 18 مگابایت
در صورت تبدیل فایل کتاب Linux & Python for Raspberry Pi: Getting started with Linux & Python به فرمت های PDF، EPUB، AZW3، MOBI و یا DJVU می توانید به پشتیبان اطلاع دهید تا فایل مورد نظر را تبدیل نمایند.
توجه داشته باشید کتاب لینوکس و پایتون برای رزبری پای: شروع کار با لینوکس و پایتون نسخه زبان اصلی می باشد و کتاب ترجمه شده به فارسی نمی باشد. وبسایت اینترنشنال لایبرری ارائه دهنده کتاب های زبان اصلی می باشد و هیچ گونه کتاب ترجمه شده یا نوشته شده به فارسی را ارائه نمی دهد.
1. Installation 1.1 Introduction 1.2 Os(Rasbian) Installtion 1.3 Development Environment 2. Linux Basic 2.1 Introduction to Unix & Linux 2.2 Linux Command Syntax 3. Linux File System 3.1 File & Directory Command 3.2 Standard I/O 3.3 File Permission 3.4 File System and Link 4. Shell Scripting 4.1 VI Editor 4.2 Shell programming 5. Process & System 5.1 Process 5.2 System Initialization 5.3 User 5.4 Utilities & APT 6. Python Basic 6.1 Introduction 6.2 Syntax 6.3 Data Structure 6.4 Function 6.5 Module 6.6 Class 6.7 Exception 6.8 File I/O 6.9 Thread 6.10 Networking 7. Python Web Programming 7.1 Introduction 7.2 HTTP 7.3 Flask 7.4 WebSocket Chapter2: Raspberry-Pi for Prototyping 1. Raspberry-Pi & GPIO 1.1. GPIO 1.2. Digital Output 1.3. Digital Input 1.4. Analog Output – PWM 1.5. Analog Input – RC Circuit 1.6. High Level Sensor Modules 2. Raspberry-Pi Communication 2.1 Serial Communication 2.1 UART 2.1 I 2 C 2.1 SPI 2.1 Bluetooth 2.1 BLE - Beacon 3. Raspberry-Pi Multimedia 3.1 Buzzer 3.2 Audio 3.3 Camera 3.4 CCTV 3.5 OpenCV Computer Vision 8. Raspberry-Pi Web IoT 8.1 Introduction 8.2 Web with GPIO 8.3 Camera Web Streaming 9. Appendix 9.1 Arduino Raspberry-Pi https://en.wikipedia.org/wiki/Raspberry_Pi Eben Upton, UK Raspberry Pi Foundation Founder For teaching computer science in developing countries 2011, Alpha board 2012, First sales Broadcom BCM2835 SoC Designed as a PC, Not a development board Raspberry Pi Boards Series https://www.raspberrypi.org/products/ Raspberry Pi Boards Series Raspberry Pi 3 ARMv8 CPU 1.2GHz 64bit quad-core 802.11n Wireless LAN Bluetooth 4.1 Bluetooth Low Energy(BLE) 1GB RAM 4 USB ports 40 GPIO Pins Full HDMI port Ethernet port 3.5mm audio Camera interface(CSI) Display interface(DSI) Micro SD card slot VideoCore IV 3D graphics core Raspberry Pi 3 OS image file download https://www.raspberrypi.org/downloads/raspbian/ 201x-xx-xx-raspbian-jessie.zip Raspbian Jessie with PIXEL : Full Image with PIXEL Desktop Jessi Lite : Minimal Image Burn Image to SD Card ETCHER https://etcher.io/ Windows, MacOSX, Linux Win32Disk Imager https://sourceforge.net/projects/win32diskimager/ Windows Only setup As a PC Connect Monitor, Keyboard, Mouse and Internet Network setup SSH Connection As a develop board Serial Consol TTY USB-Serial Convertor USB(PC) to UART(R-Pi) Network setup SSH Connection Direct Ethernet cable(Internet Sharing) Ethernet(PC) – Ethernet(R-Pi) Network setup SSH Connection As a PC Easiest way to setup but inconvenient Connecting Monitor using HDMI Cable Mouse/Keyboard using USB Ethernet Cable Insert SD Card Power In As a PC open editor as sudo Run sudo leafpad As a PC static IP setup File > Open /etc/dhcpcd.conf Serial Console TTY Enable Eject SD card reader and Connect to PC again Check ‘boot’ drive and 2 files in it cmdline.txt config.txt cmdline.txt (Just checking) config.txt add “enable_uart=1” at the end of the file no spaces in the line Eject SD card from PC, Insert into R-Pi SD slot No Powering to Raspberry Pi yet. Static IP setup sudo vi /etc/dhcpcd.conf (Must Back-up) sudo reboot ifconfig eth0 Check IP address Network Check ip ifconfig ip address Router route ip route DNS cat /etc/resolv.conf Wifi setup sudo iwlist wlan0 scan sudo vi /etc/wpa_supplicant/wpa_supplicant.conf sudo ifdown wlan0 sudo ifup wlan0 ifconfig wlan0 Check IP address SSH Login PuTTy Connection Type : SSH Host Name : Raspberry Pi IP Adress Port : 22 Save RSA Key Login ID : pi Passwrd : raspberry Software Configuration sudo raspi-config 4 Internationalisation options Time Zone > Asia > Seoul Change Locale en_US.UTF-8 ko_KR.EUC-KR ko_KR.UTF-8 Default : en_US.UTF-8 Change Keyboard layout Generic 105-Key Korean-104Key Compatible Module Update sudo apt-get update sudo apt-get upgrade Install Korean Font sudo apt install fonts-unfonts-core Install Korean Keyboard Input sudo apt install ibus sudo apt install ibus-hangul LCD Monitor setup http://elinux.org/index.php?title=RPiconfig /boot/config.txt BIOS in PC max_usb_current Change Usb output limits 600mA j1200mA hdmi_group 1 : CEA, 2: DMT hdmi_mode=87 Custom mode hdmi_cvt width, height, framerate, aspect, margins, interlace, rb Remote Desktop - VNC Raspberry-Pi Configuration GUI Interfaces Enable VNC Service Raspberry-Pi Configuration CLI sudo raspi-config Interfaces VNC Remote Desktop - VNC Raspberry-Pi Server Installation sudo apt-get install x11vnc Execute Server x11vnc Adjust Resolution http://elinux.org/Rpiconfig /boot/config.txt Remote Desktop - VNC PC http://www.realvnc.com/download/ Install VNC Viewer Remote Desktop - xrdp Raspberry-Pi Server Installation sudo apt-get remove vnc4server sudo apt-get remove tightvncserver sudo apt-get install tightvncserver sudo apt-get install xrdp File for server setting /etc/xrdp/xrdp.ini PC(Windows Only) Start > mstsc (Remote Desktop) sesman-XVnc pi / raspberry Python https://www.python.org/downloads/release/python-2711/ Version 2.7.x, Not 3.5.x Python Installation on PC Default Path Enable "Add python.exe to path" Option Python Hello World REPL(Read-Eval-Print-Loop) open Windows command console execute 'python' command python shell prompt opened execute 'print "Hello world" command 'exit()' Source Code make a file named 'hello_world.py' execute command python hello_world.py WinSCP SFTP : FTP based on SSH Transfer/ Receive File https://winscp.net/eng/download.php WinSCP Installation Default Options WinSCP Connect to RaspberryPi WinSCP Left Pannel : PC Local File System Right Pannel : Remote, RaspberryPi File System File upload : File > Upload (F5) to /home/pi Python on RaspberryPi Open PuTTY Check the file "hello_world.py" uploaded ls hello_world.py Execute python hello_world.py GCC Cross Compiler for RaspberryPi http://gnutoolchains.com/raspberry/ Appropriate version for Raspbian Image installed GCC Cross Compiler for RaspberryPi Install on PC Default Options GCC Cross Compiler for RaspberryPi Check Installation C:\SysGCC\Raspberry\bin\arm-linux-gnueabihf-gcc --version Source Code "hello_world.c" make file named hello_world.c GCC Cross Compiler for RaspberryPi Compile arm-linux-gnueabihf-gcc -o hello_world hello_world.c Check the output file named "hello_world" Upload to RaspberryPi using WinSCP /home/pi/hello_world Check and change permission the file on RaspberryPi ~$ file hello_world ~$ chmod 777 hello_world execution the file ~$ ./hello_world NodeJs Download for PC(Windows) https://nodejs.org LTS Version NodeJs Installation on PC Default Options NodeJs Download for RaspberryPi Too old version(v0.10.x) is installed by default Determine download version with CPU archtecture $ uname –m or $ lscpu or $ cat /proc/cpuinfo RaspberryPi CPU is ARMv8, but Rasbian OS is based on 32bit NodeJs Download wget https://nodejs.org/dist/v6.9.4/node-v6.9.4-linux-armv7l.tar.xz Extract tar –xf node-v6.9.4-linux-armv7l.tar.xz move to /opt sudo mv node-v6.9.4-linux-armv7l /opt/nodejs create link apt-get remove nodejs sudo ln –s /opt/nodejs/bin/node /usr/bin/node sudo ln –s /opt/nodejs/bin/npm /usr/bin/npm NodeJs check nodejs version $ node –version Source Code make the file named 'hello_world.js' on PC Run on PC node hello_world.js Upload and Run on Raspberry upload the file using WinSCP node hello_world.js JDK(Java Development Kit) Installation Needed for Eclipse http://java.sun.com http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html Eclipse Installation http://www.eclipse.org/downloads/ Eclipse IDE for JavaEE Developer PyDev Installation Eclipse > Help > Eclipse Marketplace Search "Python" PyDev Configuration Eclipse Preferences Pydev > Interpreters > Python Interpreter Quick Auto-Config Python Project Create a Python new project New > Project Create a Python Source Code File New > PyDev Module Python Project Python Code Running R Click > Run As > Python Run Eclipse CDT(C/C++ Development Tooling) Installation Eclipse Help > Install New Software Click Add Button Name : CDT Location : http://download.eclipse.org/tools/cdt/releases/9.2 Eclipse CDT(C/C++ Development Tooling) Installation Eclipse Help > Install New Software Add Button Click Name : CDT Location : http://download.eclipse.org/tools/cdt/releases/9.2 check all items uncheck Arduino C++ Tools C Project R Click > New Project C Project Fill in the Project Name Project Type Hello World ANSI C Project Toolchanis Cross GCC C Project Basic Settings Fill in the blanks appropriately C Project Select Configurations Leave it as defaults Checked Debug, Release C Project fill in the blanks correctely cross compiler prefix arm-linux-gnueabihf- cross compiler path C:\SysGCC\Raspberry\bin Path of the Cross Compiler C Project C Source code Build Menu > Project > Build All Check the result Console message output file under Debug C Project Running Upload using WinSCP Execute the file on the RaspberryPi using PuTTY Eclipse NodeJs Configuration Nodejs Plugin Installed by default on Eclipse Neon Check the Configuration You can customize NodeJs Project Create a Nodejs Project R Click > New Project > Javascript Project NodeJs Project Fill in the project name Leave all other items as default Click Finish NodeJs Project Create Source Code File R Click > New > File Fill in the File Name with .js extention Click Finish button Edit File Running R Click > Run As > Node.js Application Check the result on the Console RemoteSystem Setup You can control the Remote RaspberryPi with Eclipse Open Perspective Eclipse > Window > Perspective > Open Perspective > Other Remote System Explorer RemoteSystem Setup Register a new Connection Remote System > R Click Login Apply Python Project to Remote Export project produced in PyDev Perspective Remote Systems > Remote File System Apply Python Project to Remote Select a directory on RaspberryPi Apply Python Project to Remote Select Destination Check "Review/Sysnchronize" Option Apply Python Project to Remote Put the file to apply using Synchronize view Apply Python Project to Remote Executing remotely using PuTTY. Apply C Project to Remote R Click on the Project Run As > Run Configurations Select "C/C++ Remote Application" create new run configuration new connection connection type SSH Apply C Project to Remote New Connection Fill in the blanks Remote path Select properly path on the remote Raspberry Pi Apply C Project to Remote Commands to execute before application chmod 744 path/to/output_file_name Check the result in the Console view Apply NodeJs Project to Remote NodeJs Project Export Remote Systems > Remote File System Select a directory on RaspberryPi Check "Review/Synchronize" option Apply NodeJs Project to Remote Put the file to apply using Synchronize view Executing remotely using PuTTY. Unix Multics for GE646 by AT&T, GE, MIT in 1964 Multi User, Multi Processing, Multi Process Pulled down for many problems in 1969 Unix AT&T Lab 1971' Re-do in Assembly by Ken Thompson(US, 1943 ~ ) 1973' Re-written in C language by Dennis Ritchie US, 1941 ~ 2011 Developed C language to Write Unix UC Berkley Buy Source Code from AT&T Added TCP/IP Stack BSD version System V from AT&T Solaris, HP-UX, AIX BSD from UC Berkley SunOS, FreeBSD Minix Minimal Unix-like OS, 1987 Andrew Stuart(Nederland, 1944~) For Education only Linux 1991' University of Helsinki, Linus Torvalds(Finland, 1969~) Created Unix like OS on Minix Free Software, GNU GPL Distributions Red Hat Famous in Enterprise Server Package Manager : RPM Fedora, CentOS, 붉은별 Debian Famous in Desktop Package Manager : APT Ubuntu, Rasbian, Mint Slack ware OpenSUSE, Slax Unix like System Minix Linux Mac OSX POSIX(Portable Operating System Interface) suggested by Richard Stallman(1953 ~ ) in 1985 Standards for Unix Interface Certified AIX HP-UX IRIX macOS, Darwin Solaris FreeBSD NetBSD Linux VxWorks Unix/Linux System Structure Kernel File System Shell Kernel Core Component Device Management Memory, Process Management File System Management CPU Control File System Hierarchical Directory and File, /(root) Managing Files in Disks Mapping Devices, Processes to Files Linux FS Ext3, Ext4 Shell Interfacing between Human and Kernel Shells Bourne Shell Unix Orinate C Shell Similar C language Korn Shell Unix Standard Improved Bourne & C Bash Shell Most Linux Shells Bourne Again Z Shell Mordern Style Command Line Interface Command Shell Built-in Command Execution File in PATH Options Optional dash(-) character Additional Requirements Modifiers for Command Arguments Optional or Mandatory Object of Command Target to Execute Command Line Interface Command Only No options Default Options No arguments Objects or Targets omitted Examples uname date cal ls Command Line Interface Command + Options Additional Requirements Common Options -a : all Command Line Interface Command + Arguments Target Specific No omitted Command Line Interface Command + Options + Arguments Command chaining Executing Multi Commands Simultaneously ; - Semi Colon(;) command-1; command 2; command-n sequential Separated N command No effect error && - Ampersand X 2 No Single & (Background) Depends on Error Stop If Error Occurs Control Characters Process Ctrl + c : Interrupt(terminate) current process, SIGINT Ctrl + d : EOF(End of File) or EOT(End of Transmission) Character Ctrl + z : Suspend current foreground process, SIGSTP Cursor Ctrl + a : move cursor to start of line Ctrl + e : move cursor to end of line Screen Ctrl + u : Clear current line Ctrl + w : Clear current word Ctrl + l : Clear screen Ctrl + s : Stop output to the screen Ctrl + q : Resume output to the screen stopping with Ctrl + s Types of Command Shell Built-in Internal Command Shell Command Executable Program File External Command All Files has X mode in disk Specified the location in $PATH env /bin : basic command for normal users /sbin : basic command for administrators /usr/bin, /usr/sbin : command for specific class user ex : office user, developer, gamer /usr/local/bin, /usr/local/sbin : user compiled, installed program Types of Command Shell Built-in List up help Detail Information help Distinguish type Executable Program File List up echo $PATH Detail Information --help man help Location which Manual page From Unix Manual Paper Book online : http://man7.org/linux/man-pages/index.html Manual page man Navigate key enter : next line space : next page b : prev page /pattern : search pattern n : next pattern q : exit SEE ALSO Relative Command Section Number Command(n) Manual page Inquiring in section man –s man Keyword man –k pwd present working directory ls list up files and directories ls options pathname -l : long, detail -a : all, no hidden file -d : directory -R : recursive -F : file type / : directory * : executable (none) : text @ : link ls file ls /dir ls -l Tree list directory like tree-format -d : directory only -L : depth level -P : pattern only -I : ignore patten File determine file type -i : MIME type -L : symlink Stat display file or file system status Directory Command mkdir make directory -p : make parent cd change directory Path Absolute vs Relative . : current dir .. : parent dir ~ : home dir / : root dir - : previous Making a File touch no content in the file zero size echo 'content' > echo 'hello world' > hello.txt echo & redirection Editor nano vi Nano Editor Linux Basic Test Editor nano [FILENAME] Ctrl + X : eXit Ctrl + O : Save Ctrl + K : Cut, Copy Ctrl + U : Paste Ctrl + ^ : Block Ctrl + W : Search Ctrl + G : Help cp (Copy) Copy File or directory cp option source target File Copy new file name cp fileA fileAA new file name to other dir cp fileA dirA/fileAA no file name to other dir cp fileA dirB -i : Interactive prevent overwriting cp (Copy) Directory Copy -r : recursive To new directory cp –r dirA dirZ create new directory To existing directory cp –r dirA dirB copy to child directory mv (Move) Move File or Directory To Move File File name only mv fileA fileX The same with rename New path mv fileB dirB/fileX mv (Move) To Directory To new directory mv dirA dirX create new directory To existing directory mv dirB dirC move to child directory rmdir (Remove Empty Directory) rmdir [option] DIRECTORY... -p : parent Remove ancestors only empty directory if not empty fail use rm rm (Remove File or Directory) rm [options] file_path -r : recursive Not Empty -f : force ignore nonexistent -d : empty directory Display File content cat [OPTION] [FILE] concatenate file to output If no file, standard input more FILE paging one screen scroll enter : next line space : next page b : prev page / : search n : next pattern q : quit head [OPTION] [FILE] output the first part -n : first NUM lines tail [OPTION] [FILE] output the last part -n : last NUM lines -f : following wc (Word Count) wc [OPTION] FILE no option : lines, words, bytes -l : line counts -w : word counts -c : byte counts -m : character counts useful with pipe(|) grep grep [OPTION] PATTERN [FILE..] print lines matching a pattern OPTION -i : Ignore case -l : print file name -n : line number -v : invert match -c : count -w : word searching -E : extended regexp -F : fixed string -r : recursive egrep grep -E fgrep grep –F rgrep grep -r find search files find path expression action expression -name filename 'filename*' : * with quotation mark -type filetype : f, d, c, b ... -size [+|-]n[c,k,M,G] -atime, -amin, -mtime, -mmin, -ctime, -cmin [+|-]n -user owner_id -perm [-|/] mode - : match all bit / : match any bit -maxpeth n find expression find expression find –perm find search files find path expression action action -print(default) : path and name -ls : print detail -delete : delete files -exec command [{}] \; -exec command {} + execute command {} : Result of find \; : execute separately + : execute at once -ok command [{}] \; : execute command interactively find action find –exec Standard I/O Redirection Input from file instead of stdin < file_name output to file instead of stdout > file_name : overwrite >> file_name : append output to file instead of stderr 2> file_name : overwrite 2>> file_name : append Redirection /dev/null Null Device Special File to discard useless data command 2> /dev/null discarding useless error messages > file_name 2>&1 Redirecting stdout, stderr to same file Pipe ( | ) Connect stdout of process(A) to stdin of another process(B) A | B tee Read from stdin, write to stdout and file Good for output both stdout(terminal) and file Using Redirection and Pipe Together -a : append Linux File Mode Determine can read, write, execute File type - : Normal file d : Directory l : Symbolic link s : socket p : Named Pipe c : Character device b : Block device User Owner Owner ID Group Member of the group Other Not a owner, Not a member File Permission Determine Permission id [USER] print user and group Change Permission chmod mode filename symbolic mode Target u : owner g : group o : other a : all (default, except umask bit) Operator + : add - : remove = : assign Permission r : read w : write x : execute Change Permission Symbolic mode Change Permission chmod mode filename Octal mode 3 digit in octal Change Permission Octal Mode Change Owner chown id FILENAME chown id : group FILENAME chown :group FILENAME Change Group chgrp gid FILENAME Umask newly created file's permission umask : print umask umask value : set umask Umask Calculation Default – Umask_value 2's Complement of umask AND operation Default Permission Directory : 777 File : 666 Example, umask=0022 Umask File System Control how data(File) is stored and retrieved Windows : FAT, NTFS, exFAT Unix : UFS Linux : Ext2/3/4, XFS,JFS MacOS : HFS Flash : UBIFS, JFFS2, YAFFS Linux File System Boot Block (Boot Sector) At the beginning of Storage Special data to start a system Super Block File system metadata (Type, Size, Status) Information of I-Node, Data block I-Node Block Represents a File Stores data of permission, owner, size, pointer of Data Block Data Block Contain file data and directory can be multiple per file I-Node & Data Block ls –il -i : inode Directory Directory is a file Stores file list Pair of I-Node number and file name Link Count Every Directory has 2 items . (Current) .. (Parent) Sub Directory Count Link Connection between a file and data Hard Link Low-level link Make another File Name But not make I-Node, has the same I-Node number No difference between the original and the link Increment Link Count Cannot make a hard-link to directory Soft(Symbolic) Link Similar to Windows shortcut icon Make a new file pointing to the original file File Type : l, @ Permission : 777 Link Update and Read Always the same effect Original, Hard-link, Soft-link Remove Link Remove origin No effect on hard-link Make Dangling soft-link Restorable, Creating a file with the same name as origin Remove hard-link No effect on origin, soft-link Remove soft-link No effect on origin, hard-link Finding Link Hard link find . -samefile find . -inum Symbolic link find . -lname vi [File Name ] Mode command mode edit mode last line mode: Input command at the last line Vi Edit Mode i : Input in front of curser a : Input after the curser o : Input after adding a line at the end of curser O : Input after adding a line before curser A : Input at the end of curser line I : Input in front of curser line r : Overwrite one unit at the curser position R : Overwrite exiting character from curser position x : Delete a unit dd : Delete a line u : Redo Vi Moving Curser h : Move left j : Move down k : Move up l : Move right ^ , 0: To the beginning of the line $ : To the end of the line G : To the end of the document 1G : To the beginning of the document Number G : Move to number line Ctrl + D : A half page down Ctrl + F : A half page up Ctrl + B : One page up Ctrl + U : One page down Vi Copy/ Paste yy : Copy the line with curser Number yy : Copy lines as numbered x : Cut one unit where the curser is located p : Paste Number p : Paste lines as numbered Block Mode v : Blurred Mode y : Block Copy p : Block Paste d : Block Delete > : Block Tap < : Remove Block Tap v , Ctrl + v : Block Column Mode Vi Setting :set nu Show line number :set showmode Show mode :set nonum Hide line number :set ai Auto indent :set cindent C language style indent :set list Show special character :set Show set vi variables :se all Show all variables and set values Save setting ~/.vimrc Vi Breaking Window Open new window Ctrl + w, n : Transfer windows ctrl +ww Close window ctrl +w, q Vi Save, Shut down :w [File Name] Save :e File name Open file :q Shut down :q! Forced shut down :x Save and shut down Meta-characters Glob Patterns : wildcard characters whith file names * : matches any characters ? : matches any single character [ ] : matches one charater given in the bracket ( ) : sub shell $( ) : command substitution Variables Name : alphabet, number(no first letter), _(under score) MY=value : make a variable compgen –v : listup defined variables unset : remove variable export : exports variable to child process Variables Environment Variable : Valid System Wide PATH, HOME, PS1, TERM, SHELL, LOGNAME Special Variables $$ : current PID $# : number of arguments $0 : filename of current script $n : arguments (n is positive decimal) $? : exit state of last command executed $! : PID of last background command read read a line from stdin and save to variable expr evaluate expression exit Exit the shell with a status of N Quotes ' : Single quotation, Ignore meta-character " : Double quotation, Ignore meta-character except 3 character $, `, \ ` : backtick, command substitution test express, [ express ] evaluate conditional expression Exit status : return 0 to true, 1 to fa String Operators = : check if equal != : check if not equals -z : check if size is zero(empty string) -n : check if size is non-zero Arithmetic Operator A –eq B : check if A is equal to B , == A –ne B : check if A is not equal to B, != A –gt B : check if A is greater than B, > A –lt B : check if A is less than B, < A –ge B : check if A is greater than or equal to B, >= A –le B: check if A is less than or equals to B, <= File Test Operators -d file : check if file is a directory -f file : check if file is a normal file -r file : check if file is readable -w file : check if file is writable -x file : check if file is executable -s file : check if file has size greater than 0 -e file : check if file exists Boolean Operators ! : logical negation -o : logical OR -a : logical AND Decision Making if ~ then ~ fi if ~ then ~ else ~ fi if ~ then ~ elif ~ else ~ fi for loop for variable in values while loop while expression until loop run while condition is false until [ condition ] Executing Shell execute command in the new shell process sh my.sh sh : /bin/sh -> dash , minimal shell of bash for executing shell execute command in the new shell process my.sh chmod +x she bang #! /bin/sh #! /bin/env sh execute command in the current shell process source my.sh source : execute command in the current shell process . my.sh . is alias for source Shell Initialization When the shell is invoked, startup files system wide startup /etc/profiles set PATH, PS2 invoke /etc/profile.d/*.sh /etc/profile.d/*.sh custom changes user-specific startup ~/.bashrc ~/.bash_aliases invoked from ~/.bashrc Shell Initialization alias [-p] list up defined alias alias NAME=VALUE define alias NAME for VALUE ~/.bash_aliases invoked from ~/.bashrc 23 Handy bash shell aliases https://www.linuxtrainingacademy.com/23-handy-bash-shell-aliases-for-unix-linux-and-mac-os-x/ Process An Instance of Program running init (systemd) process /sbin/init j /lib/systemd/systemd First process after booting Ancestor of all process PID(Process Identifier) is1 ps –p 1 Parent Process process that created child Child Process process that is created by parent Deamon Process not require controlling terminal Orhpan Process parent dies or get killed before the child init process adopts Zombie Process child dies but parent does not fetch termination status Searching Process ps –ef | grep command pgrep –l command SIGNAL Limited form of Inter Process Communication Signals kill -l SIGNAL Major Signals sending signal kill –signal target Background Process command & run in background ctrl + z go stopped in background jobs list up background run in background fg [job_id] run in foreground nohup run command ignoring hangup signals nohup COMMAND & Init and Systemd First Process started during booting continues running until the system is shut down SysV style Traditional init system from Unix 7 Run-levels /etc/inittab /etc/init.d/* /etc/rc[0-6, S].d/* systemd Replacements for init Since Debian Jessie v8, 2015 RedHat v7.0, 2014 /lib/systemd/systemd /lib/systemd/system/*.service Run Level mode of operation 7 run-levels in Linux SystemV init Run Level Systemd target Mapping runlevels and systemd target Determine current run level runlevel who –r Change run level sudo init [0-6] shutdown -P : power off -h : power off -H : halt, lowest state leaves it power on -r : reboot -c : cancel shutdown -k : shudown warning to users halt -p : poweroff poweroff : power off the machine reboot : reboot the machine wall : write a message to all user Run Level Setting Run Level /lib/systemd/system/default.target change symbolic link to /lib/systemd/system/poweroff.target ( runlevel0 ) /lib/systemd/system/rescue.target ( runlevel1 ) /lib/systemd/system/multi-user.target (runlevel2, 3, 4) /lib/systemd/system/graphical.target (runlevel5) /lib/systemd/system/reboot.target (runlevel6) Startup Service Process that require autorun after booting /etc/rc.local System V Init Style Compatable Invoked by /lib/system/system/rc-local.service add command in the file /lib/system/system/myd.service Systemd Init Style Create .service file systemctl enable systemctl start systemctl stop systemctl disable Example shell, myd.sh write time to ~/date.log every seconds save as /home/pi/myd.sh chmod +x myd.sh Startup Service Easy way : rc.local open /etc/rc.local add the command that invokes myd.sh before "exit 0" su – pi –c '~/myd.sh' & Reboot and Check the process Startup Service Add Service : myd.service Create file /lib/systemd/system/myd.service Startup Service Add Service : myd.service Enable and Start Reboot and Check the Process Stop and Disable Getting User Information Determine Current or User id [ID] who am i Get all users logined who users /etc/passwd stores all user accounts information Getting User Information sudo passwd -aS display all user accounts information Getting User Information sudo chage -l display user accounts expiry information Adding User Account sudo adduser add a user or group to the system Changing Password passwd own password passwd must not be short sudo passwd someone else's password passwd can be short Creating Group sudo groupadd add a group to the system List up all Groups cat /etc/group Adding a user to an Group sudo adduser Remove a user from group sudo deluser Getting Super User authority add user to sudo group /etc/sudoers sudo adduser sudo Sudo with No Password make a file in /etc/sudoers.d no contain '~' or '.' Substitute User su [-] - , -l, --login : login shell Unset all variable and set again Change Home directory Become a Root su – root su - Remove a existing group from system sudo delgroup Remove a User from system sudo deluser Enabling Root Account NOTE : Not Recommended Root Account is locked and not set password Unlock Set New Password Enabling Root Account NOTE : Not Recommended Allow login through ssh /etc/ssh/sshd_config #PermitRootLogin prohibit-password j PermitRootLogin yes Disabling Root Account Lock Account Delete Password sudo passwd –dl root top, htop Display Processes, cpu info, memory info df Show disk space usage -k : kilo -h : human readable tar (tarballs) make archive file tar -cvf a.tar /abc /def aaa bbb make archive with zip compression tar -cvzf a.tar.gz /abc /def aaa bbb extact archive file tar –xvf a.tar extract archive with zip compression tar –xvzf a.tar.gz Disk Utilities lsblk list block devices fdisk manipulate disk partition table -l : list up dd data duplicate, convert and copy a file dd bs=n if=input_file of=output_file mount -l : list up mount [Device] [Mount_point] umount unmout file system umount [Device_name] APT(advanced package tool) Tool system used to manage package in Debian line Linux Package File *.deb Related Command apt apt-get apt-cache apt-config aptitude dpkg Related Setting Files /etc/apt/sources.list /etc/apt/apt.conf /etc/apt/preferences.d apt High level command using apt-get, apt-cache internally Regular user version of apt-get/cache apt list [package-name] --installed --upgradable --all-version apt search apt show apt install apt remove apt edit-sources : sources.list apt update apt upgrade apt full-upgrade apt-get Tool used to handle APT package apt-get update Synchronize package list Base on /etc/apt/sources.list Need to run before upgrade or dist-upgrade apt-get upgrade : Up-to-date all package installed in system apt-get dist-upgrade : Change dependency of new version package apt-get dselect-upgrade apt-get install : Install given package -y : auto yes apt-get remove : Remove given package apt-get purge : Delete and eliminate setting apt-get source apt-get download : Download in current directory apt-cache No system change in apt-cache Offer package related information apt-cache search [package-name] Search package apt-cache show [package-name] See package information dpkg Tool need for package installation, building, deleting and managing Tool for more general user than dpkg is aptitude dpkg -r : Delete dpkg -P : purge Delete dpkg -l [package_name]: Print install package list Python Guido Van Rossum(1956) Created python(1989)in Amsterdam, Netherlands Google (2005~2012) Dropbox (2013 ~) Named after a BBC comedy series “Monty Python’s Flying Circus” Teaching Python instead of LISP to freshmen in MIT Has two Versions : 2.x, 3.x Not compartable Interactive interpreted language Independent platform Dynamic type system Rapid development Simple and easy language Object-oriented language Multiple programming paradigms Garbage Collection Python Shell - Windows IDLE Menu > Python > IDEL GUI-based interactive Python interpreter Quit : Ctrl + D Command Python input at the Dos Quit : Ctrl + Z Enter Version to check Terminal Python shell Basic Rule A code starts with the first column Exceptions-Python notes (#) Use indentation to indicate code block File Script - IDLE File > New File File > Save ~/hello.py Run > Run Module(F5) File Script - Console Creating a file with editor Code-in Running File Script – Hashbang(shebang), Unix Only #! Hashbang, shebang, shabang, pund-bang, hash-pling script interpreter directive Adding Hashbang at the first line of file #! /usr/bin/python #! /usr/local/bin/python #! /usr/bin/env python Adding execution permission PYTHONSTARTUP a script to run whenever Python executed https://docs.python.org/2/using/cmdline.html#environment-variables startup.py Set the File path to Environment variable API Document IDLE Help > Python Docs(F1) Online Standard API : https://docs.python.org/2/library/ Built in Function : https://docs.python.org/2/library/functions.html String Method : https://docs.python.org/2/library/stdtypes.html#string-methods Global Modules : https://docs.python.org/2/py-modindex.html External Modules : https://pypi.python.org/pypi Help system Interactive help system generates documentation on the console using help() A line down : enter, down arrow, j A line above : up arrow, k A page down : space button, f A page above : b quit : q Integrated Development Environment(IDE) Eclipse + Pydev : Slow pace Available with other plug-ins PyCharm Rapid pace Most widely used Free or Charge Other Editors for development Sublime Notepad++ brakets Syntax Case-sensitive Use indentation instead of parentheses pass If functions or conditional sentences are NOT included Comments # a single line comment ‘’’(single quote * 3), “””(double quote*3) : multi-line comments The End of sentence Use a line break character instead of semicolon (;) Use \(backlash) for line-breaking at the end of sentence Korean Hangul Keyboard # -*- coding:utf-8 -*- Show the beginning of file Insert Korean letters into a string literal u ’Korean’ The 'u' in front of the string values Keywords import keyword keyword.kwlist Syntax Comment Variables With no declaration Data type dynamic binding according to a value of the quota The naming convention The first letter in English , under-score(_) A number can be included from the second letter Convention Small letter_small letter Eliminate duplication of the name of embedded module and function del x : Delete variables All variables are objects Garbage Collection Identifying an object id() UID(address) check a is b int.bit_length() Variables Data type Number int(integer) : long in C, 32bit-min) 1, 100, 123 long(Python2 Only) : unlimited precision(as long as a memory allows), Use l, L 1L, 100L, 123L float(real number) : double in C 3.14, 1.2 complex(complex number) : 64bit for each real/imaginary number, use j, J instead of i) 1 + 2j, 3j boolean (subtype of integer) True, False str (Strings) : letter strings "abc", 'hello' Immutable sequence None : No value Built-in Data Structure list[1,2,3], tuple(1,2,3), dict{1:’a’, 2:’b’}, set{1,2,3} type() Identify a dynamic type Data type Letter strings ‘ ’, “ “ Use double quote mix with single quote "hello", 'hello' + concatenation * repetition == True when contexts are the same despite ID’s difference in calculation (Membership check) confirm to check the context len(str) A length of letter strings l Format letters %s: letter strings, %d: integer, %f: real number Indexing : Turple str[0], str[0:4] Immutable Sequence str[0] = 'h' #Error Letter strings Main Methods Startswith(s) : Does it start with -s? Endswith(s) : Does it end with -s? find(s) : index for s , if not -1 rfind(s) : find() from the end count(s) : How often –s appears? lstrip() : Delete the left side of blank rstrip() : Delete the right side of blank strip() : Delete either side of blank isalpha() : Does it consist of English alphabets only? isdigit() : Does it consist of numbers only? isalnum() : Does it consist of English and numbers only? replace(old, new[, count]) : replace old with new letter strings format() : create a visual basic {idx} 'my name is {0} and {1} years old'.format('Lee', 27) {key} 'my name is {name} and {age} years old'.format(age=27, name='Lee') Arithmetic operation ‘abc’ + ‘def’ : ‘abcdef’ link together when adding letters ‘10’ + 2, '10' – 2, '10' / 2 : error Unable to add, subtract and divide between letters and numbers ‘abc’ * 3 : able to multiply letters and numbers ‘abcabcabc’ 2 ** 3 : 8 square 7 / 4 : 1 Integer as a result of integer calculation(Python 2) Real number as a result of real number calculation(Python3) 7 % 4 : 3 Residue check divmod(7, 4) : compute quotient and remainder 7 / 4.0 : 1.75 Real number as a result of real number calculation 7 // 4.0 : 1.0 emission of fraction after a decimal point (2 + 3j) + (3 + 2j) : 5 +5j Complex number calculation i++, i--, ++i, --i : error i+=1, i-=1 Operator Comparison operator a == b a != b a > b a >= b a < b a<=b Logical operator Unable to use &&, ||, ! a and b a or b not a a in b a not in b Comparison & Logical operator Bitwise operator ~ : ~5 Bit reversal (One’s complement) << : 3 << 2 Left Shift, each bit is shifted to the left left bit is dropped Right bit to be filled by 0 s (zeros) >> : 4 >> 1 Right Shift, each bit is shifted to the right right bit is dropped Left bit to be filled with the last bit on the left & : 3 & 2 Bitwise AND | : 3 | 8 Bitwise OR ^ : 15 ^ 6 Bitwise XOR Bitwise operator Type conversion to convert integer int(“10”) int(True) long("1234") to convert real numbers/complex numbers float(‘3.14’) float(123) complex("3+5j") , blanks are Not allowed to convert letters in a string str(10) ‘%s’ %10 Console Input and Output Python 2 input( [prompt] ) evaluate user input easy number input enable string input if enclosed in double quotes raw_input( [prompt] ) return user input for string number input required for conversion print exp1, exp2… Console output Tuples Python 3 input([Prompt]) Same with of raw_input python2 (eliminate raw_input ) print( exp1, exp2...) Console output A function, not a command If-else Condition if : if : if : Practice – credit program Input : score ( 0 ~100) Output : credit (A : ≥ 90, B : ≥ 80, C : ≥ 70, D: ≥ 60, F : < 60 Examples Repetition Statements - for for x in […] : Should be Sequence Type The function of range() xrange() : python2 range() : python3 Practice - Double dice Roll two six-sided dice ten times Sum of two dice output 7 11 Same spot Random number import random random.randint(start, end) Repetition Statements - while while : Practice – Guess the number Random number( 1 ~ 100) insert number you guess Higher/lower output Repeat untill correct answer Examples Practice – Guess the number List Similar to a simple Array language The most frequently used Mutable Sequence Type a group of sequence data types Indexing, Slicing, Concatenation, Iteration Changeable, Resizing Syntax my_list = [1,2,3,4] my_list[0] : 1 my_list[-1] : 4 first from the end my_list[1:3] : [2, 3] my_list[:3] : [1,2,3] my_list[1:] : [2,3,4] my_list[ : ] : [1,2,3,4] my_list[1 : :2] : [2,4] my_list[::2] : [1,3] my_list * 2 : [1,2,3,4,1,2,3,4] my_list + [10,20] : [1,2,3,4,10,20] my_list[1] = 5 : [1,5,3,4] Method append : add at the end insert : index : search for elements count : count elements sort : reverse : remove : pop : remove after getting the last item in the list extend : add list List Functions range( stop), xrange() range(start, stop [, step]) Create a sequence integer list start default : 0 the last item is stop – 1 step default : 1 len(list) sizing list() : type conversion Tuple Read-only List, often use in argument, return value of function Immutable No methods Sequnce Type Indexing, Slicing, Iteration Syntax my_tup = 1,2,3,4 my_tup = (1,2,3,4) my_tup = (1,) Comma is needed when there is an element my_tup[1] : 2 my_tup2] = 5 : Error a,b,c,d = my_tup a,b,c,d = 1,2,3,4 a,b = c, d Function tuple() Type conversion len() Set Set data type No sequence No duplication Syntax my_set = {1,2,3,4,5} my_set = set([1,2,3,4,5]) Methods s1.add(el) : add an element s1.update([1,2,3]):add multiple elements s1.discard(el), s1.remove(el) : remove an element s1.clear() : clear all elements s1.union(s2) , s1 | s2 : s1.intersection(s2) , s1 & s2: s1.difference(s2) , s1 – s2 : s1.symmetric_difference(s2), s1^s2 : s1.issubset(s2), s1 <= s2 : s1 is a subset of s2 s1.issuperset(s2), s1 >= s2 : s1 is a superset of s2 Dictionary Mapping Type Key : value Key is Set Syntax dict() my_dic = {‘a’ : 1, ‘b’:2} my_dic[‘a’] : 1 my_dic[‘b’] = 5 {‘a’:1, ‘b’: 5} my_dic['c'] = 6 {'a':1, 'b' : 5, 'c':6 } Main Methods dic.get(key [, default]) : if given key exists, returns its value. Otherwise, it returns to the given default value. dic.pop(key) : remove the element with given key from dictionary and return its value dic.keys() : return a view object that displays a list of all the keys dic.values() : return a view object that displays a list of all values in a given dictionary dic.items() : return a view object that displays a list of dictionary's (key, value) tuple pairs dic.clear() : remove all items from the dictionary Comprehensions List ,Set, Dictionary Apply function to each element of the matrix of lists and return results in List, Set, Dict [ expression for ~in if condition] Application of arithmetic coding S = {x² : x in {0 ... 9}} V = (1, 2, 4, 8, ..., 2¹²) M = {x | x in S and x even} Comprehension s= [ x**2 for x in range(10)] v= [ 2**x for x in range(13)] m = [x for x in s if x%2 == 0] Function Subroutine A series of code to repeatedly use Syntax Return None Type if return statements not present Main Declaration Type def fn(): def fn(a, b): def fn(*args): Tuple type def fn(num=0) default value def fn(**kwargs) Dictionary type Function Function Namespace LGB(Local, Global, Built-in) Access in Function confusion between local and global variables Global Variable :Accessed by any statement in the entire program Variables are declared inside and outside a function dir(__builtins__) : Built-in name space Lambda Anonymous Inline Function No return statement Syntax Practice – calculator When you put the 4 fundamental arithmetic operations, you can get an output Get the remainder using of the given code plus(a,b), minus(a,b), multiply(a,b), divide(a,b) str2num(str_val) : int | float Examples Code provided Practice – calculator Implemented code Practice - Hangman Figure out a word Given 6 words input a letter or word that a user guess Show as --- Show a part in alphabet if correct 14 chances in total Practice - Hangman Practice - Hangman Module, Package module A piece of software that has a specific functionality *.py file package A unit collecting modules that have a similar functionality *.py a directory collecting files __init__.py file required(Python 2 Only) How to use modules and packages import random imoprt random as r from random import randint from random import * custom module custom package A directory including modules __init__.py file included Choose the latter imported name if duplicated custom package Custom module same with average API Top Level module A module used at the beginning of execution Built-in module: The value of __name__ is '__main__' Use the given module name if it’s not Top Level if __name == '__main__' __init__.py Package should have __init__.py file Normally empty file Specification of modules in response to import * __all__ = [ 'module_name-1' , 'module_name-2' … 'module_name-3'] __init__.py Module Path A location where Python search for Module No separate *.py file sys.builtin_module_names PYTHON_HOME/site-packages Class A system that can classify the same type, DataType A essential template to create objects that have the same structure(functions, variables) Object Unique things generated by Class Share unique function with the value(variables) Practice-Turtle Class The definition of Class Syntax The name of class starts with a capital letter Object generation and application Syntax Instance check type(obj) obj.__class__.__name__ isinstance(obj, Class) Class definition and application __init__ Method Automatically called when you call it to create Objects are initialized Class variable VS Instance variable class variable : variables generated in Class Create a variable in Class Use class name to access Instance access if instance variable of the same name is not present : class variables access if instance variable of the same name is present : instance variables access instance variable : Variables generated through self in method Independent variables are created in each object Instance access Inaccessible with the name of Class, Error Class / Instance variable Static method Method irrelevant to class or instance Call it through Class Able to call it through Instance, not recommended Use @staticmethod Decorator Class method Method for class-related work Irrelevant to Instance Deliver class reference value when you call Use @classmethod Decorator Instance method Method for Instance-related work most of methods Deliver class reference value when you call Encapsulation Block access of instance variables from outside Access through a function The name of variables highlight 2 prefixes only one suffix is allowed to highlight but is not private for a suffix of double line Inheritance Similar class with the existing one Extension of class functionality Reuse code Syntax or Inheritance Method Overriding Redefine the inherited method from Base class Name it as the same with the method Irrelevant to parameter type Multiple Inheritance Designate multiple Base classes Same method name increases ambiguity prioritize class which was put at the front Abstract Class Unimplemented class Impossible to generate objects abc.@abstractmethod included __metaclass__=abc.ABCMeta Hidden methods __call__() A response when you call function method from instance method Application to @decorator Hooking into function calls __iter__(), next() Application to Iteration through for ~in __iter__ Return object that has next() in for statements e __next__() : renamed in python 3.x next Call for every for statements Raise StopIteration after repeating __call__ , Decorator __iter__ , Iterator Generator, yield Able to use for~in statements without __iter__ Replace with yield statements Reentrancy function Exception Error while in operation The program abnormally end If left untreated try : statements that exceptions expected Jump over the except block if exceptions occur except Exception: Execution if certain exceptions occur except : Execution regardless of exceptions else : Execution unless exceptions occur finally Execution regardless of exceptions Exception occurance rase Exceptions Exception information sys.excinfo() Kinds of exception, value, trackback Exception handling Built-in exceptions Exception Root class of all exceptions No forcible regulation of user definition exception class StandardError Base class of all built-in exceptions except Systemexit Arithmetic Error Base class of OverflowError, ZeroDivisionError, FloatingPointError LookupError Base class of IndexError, KeyError EnvironmentError Base class of external exceptions occurance(IOError, OSError) Custom Exception File read f = open(file_name) f.read() f.close() File write f = open(file_name, 'w') f.write(‘content’) f.close() open(name, mode, bufferring) name : file name mode r : read mode w : write mode a : append mode bufferring 0 : no use of buffer 1 : system default buffer use >1 : buffer use of designated size File read / write Context manager Use with ~ as statement Resource management(close()) automatically call functions Automatically call when entering __enter__ : with statement Return reference value in as statements Automatically call when leaving __exit__ : with statement Implementation of function call such as close() File with ~ as Able to omit close() function call Read by line f = open(name) f.readline() f.readlines() f.close() Write by line f = open(name, 'w') f.writelines() f.close() Thread A small process inside of a process Parallel transaction asynchronous processing Easy for memory share and control comparing to parallel process Three ways thread module (low level) thread.start_new_thread(fn_name, (x,y,…)) threading module (high level) Direct generation , Simple th = threading.Thread(target=fn_name, args=(x,y,…)) th.start() Inherited implementation, Detail Control class MyThread(threading.Thread) def fun(): th = MyThread() th.start() Thread module (low level) Threadding module Direct generation Inherited implementation Thread control terminate a thread when main ends No specific functions to kill thread Decide whether or not maintain work through state variables of thread Timer Function call after given period of time threading.Timer(delay, fn, args=[]) time module time() : return present time localtime() : local time gmtime() : Greenwich Mean Time strftime() : Format time as a string %Y :Year, %m: month, %d : date, %H:Hours, %M:Minutes, %S:Seconds delay(sec) : delay operation datetime module date date(year, month, day) today() weekday() year, month, day time time(hour, minute, second, microsecond) strftime(format) hour, minute, second, microsecond datetime datetime(year, month, day, hour, minute, second, microsecond) today() date() time() year, month, day, hour, minute, second, microsecond time, datetime Socket as a server soc = socket(AF_INET, SOCK_STREAM) generate socket stream soc.setsockopt(SOL_SOCKET, SO_REUSEADDR,1) Set closed socket reuse soc.bind( (‘ ‘, 1234) ) Socket binkidng soc.listen(5) Maximum listener : 5 conn, addr = soc.accept() Beginning of waiting msg = conn.recv(1024), msg.decode('utf-8') : Python3 conn.send(‘content’), conn.send(b‘content’), conn.send(‘content’.encode('utf-8') conn.close() soc.close() Socket as a Client soc = socket(AF_INET, SOCK_STREAM) Create a stream socket soc.connect( (host, port) ) msg = soc.recv(1024) : Python2 msg = soc.recv(1024).decode('utf-8') : Python3 transmission soc.send(‘content’) : Python2 soc.send(b‘content’): Python3 soc.send(‘content’.encode('utf-8')) Python3 reception soc.close() Socket close Socket by server Server Execution putty(telnet) localhost 1234 access Socket by Client Socket by server using thread Socket by Client using thread Web Architecture Client/Server Timeline Hyper-Text Transfer Protocol over TCP/IP History HTTP 0.9 : No Spec Sheet HTTP 1.0 : Fix : 1996’ IETF RFC 1945 Difference between spec and implementation Added Header, GET Method HTTP 1.1 : Fix : 1997’ IEFT RFC 2068, Rev. 1999’, RFC 2616(Current Version) Cache Control, connection keep http://tools.ietf.org/html/rfc2616 Feature Connectionless Stateless Request and Response HTTP Request Structure Request Methods HTTP Request Example HTTP Response Structure HTTP Response Example Response Status Code Response Status Code Multipurpose Internet Media Extensions Type Internet Media Type Content-type Syntax Example Socket Webserver SimpleHTTPServer List up current directory; exemplary class running function BaseHTTPServer Compose custom server inheriting BaseHTTPReuqestHandler WSGI Web Server Gateway Interfacce CGI standard for Python (PEP-333) Http Client Flask http://flask.pocoo.org/ Micro framework based on WSGI Armin Ronacher, Austrian(http://lucumr.pocoo.org/) Light framework Able to expand as needed route function Jinja Template Installation pip install flask URL Routing Register individual function handler according to requested URL/Method URL Routing Parameter Sub path of specific URL REST Style Static File Simple file service not registered in route Parameter Collection GET Method from flask import request request.args.get(‘name’) request.values[‘name’] Parameter Collection POST Method from flask import request request.form[‘name’] request.values[‘name’] Parameter Collection POST Method static/form.html Template Jinja2 (http://jinja.pocoo.org/docs/dev/) Basic Tmeplet Engine Data composition to static HTML file render_template() Template templates/test.html Session session.secret_key Flask-Socket.IO https://flask-socketio.readthedocs.io/ Socket.io that can be used with Flask http://socket.io/ Installation sudo pip install flask-socketio sudo apt-get install python-dev sudo pip install gevent or pip install eventlet Main code from flask_socketio import SocketIO, send socketio = SocketIO(app) socketio.run(app) @socketio.on(‘message’) send(‘message’, broadcast=True) socket.send(‘message’) : Use externally emit(‘event name’, ‘message’, broadcast=True) websocket.py static/websocket.html static/websocket.html static/websocket.html GPIO General Purpose Input Output Hava no predefined purpose, unused by default Some pin has Alternative functionality (e.g. Tx, Rx, CLK etc.) Can be configured to be INPUT or OUTPUT Input values are 1(High) or 0(Low) Tri-state : Hi-Z Pull-up or Pull-down Output values are 1(High) or 0(Low) High : 3.3v Low : GND Raspberry Pi 3 GPIO 40 physical pins 26 available GPIO Port 02 ~ 27 2 Types Numbering BCM SoC Board Physical Raspberry Pi GPIO Pin Header Pin header mapping 4 Ways to Control GPIO Low Level C programming Highest Performance, Lowest productivity Dependency to specific H/W board 3rd party C Library Easy way C programming Dependency to specific H/W board and Library /dev/class/gpio Command line or Linux System call Programming Low Hardware Dependency, Cross H/W with Linux Language(Script) Bindings Python, Java, NodeJs(Javascript), Perl, Scratch, etc… Easy Programming Lowest Performance, Highest Productivity No H/W, Platform(OS) Dependency, Cross Platform Direct Register Access Access Register of BCM 28xx Directly Memory GPIO address mapping using mmap() function /dev/mem : Root privilege needed, available for GPIO, I2C, SPI etc. /dev/gpiomem : Rootless, GPIO only Board/Hardware Dependency Needed to reference Soc Datasheet https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf Direct Register Access gpio_register.c<1/4> Direct Register Access gpio_register.c<2/4> Direct Register Access gpio_register.c<3/4> Direct Register Access gpio_register.c <4/4> 3rd party C Library Provide complex register access as an easy C function Dependency to H/W and Library Wiring Pi : http://wiringpi.com/ (Gordon) Arduino wiring-like Library for Raspberry Pi BCM2835 http://www.airspayce.com/mikem/bcm2835/ (Mike McCauley) Wiring-Pi Installation Installed by default on Jessie or later. Not required on most Rasbian recently sudo apt-get install git-core Git clone git://git.drogon.net/wiringPi Cd wiringPi ./build Console Command line can use "gpio" command after installing WiringPi -g options : GPIO pin numbering Wiring Pi Library gpio_wiringpi.c <1/1> BCM2835 Installation wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz tar zxvf bcm2835-1.50.tar.gz cd bcm2835-1.50/ ./configure make sudo make check sudo make install BCM2835 Library gpio_bcm2835.c <1/1> Sysfs Linux Kernel 2.6+ Special File System Includes drivers for GPIO control C Programming using System calls Indepentent of board type, Available on any Linux OS Console command Sysfs C Program – gpio_sysfs.c <1/2> Sysfs C Program - gpio_sysfs.c <2/2> Sysfs Python Program – gpio_sysfs.py <1/2> Sysfs Python Program - gpio_sysfs.py <2/2> Java Binding JDK(Java Development Kit) Default Installed on Raspberry Pi. java –version javac –version Pi4j (http://pi4j.com) Java I/O library for Raspberry Pi Dependency on WiringPi Pin Mapping with WiringPi Installation $ curl –s get.pi4j.com | sudo bash Java Binding – GPIO_Pi4j <1/1> Python GPIO Modules Rpi.GPIO https://pypi.python.org/pypi/RPi.GPIO https://sourceforge.net/projects/raspberry-gpio-python/ (Ben Croston) Installed by default in Raspberry Pi Most popular way in Raspberry Pi Wrapping way of Direct Register Access GPIO and Software PWM support only H/W PWM, SPI, I2C, UART not support Rpi.GPIO basic API import Rpi.GPIO as GPIO Module import GPIO.setmode(GPIO.BCM or GPIO.BOARD) GPIO.BCM : Pin Number that Broadcom Soc defined GPIO.BOARD : Pysical Raspberry-Pi board Pin number GPIO.setup(channel, direction[, intial=state] ) channel : GPIO Pin number direction : GPIO.IN, GPIO.OUT state : GPIO.HIGH, GPIO.LOW GPIO.setup(channel, GPIO.IN [, pull_up_down=pud]) pud = GPIO.PUD_UP, GPIO.PUD_DOWN GPIO.input(channel) GPIO.output(channel, state) GPIO.cleanup() Release Resource before exiting https://sourceforge.net/p/raspberry-gpio-python/wiki/Examples/ Python Rpi.GPIO - gpio_python.py Python GPIO Modules WiringPi-Python https://github.com/WiringPi/WiringPi-Python wrapper of WringPi written in C Arduino style Code Support all H/W functions such as GPIO, Serial, SPI, I2C, Hardware PWM Wiringi-Pi Library dependency Wiring-Pi build/install required Installation sudo pip3 install wiringpi or cd ~ git clone --recursive https://github.com/WiringPi/WiringPi-Python.git sudo apt-get install python-dev python-setuptools swig cd WiringPi-Python ./build.sh Python WiringPi-Python – gpio_wiringpi.py NodeJs GPIO Modules Finding Good module https://www.npmjs.com/ http://node-modules.com/ https://nodejsmodules.org/ http://npmsearch.com/ Major Modules onoff Johnny-Five WiringPi-Node Minor or No more maintaned gpio rpi-gpio pigpio rpio mmm-gpio pi-gpio NodeJs GPIO Modules onoff https://github.com/fivdi/onoff most popular Synchronous, Asynchronous function Linux Sys-fs is used internally epoll is used to interrupt mode, callback Install npm install onoff Basic API var Gpio = require('onoff').Gpio Gpio(gpio, direction) read(callback), readSync() write(value, callback), writeSync(value) watch(callback), unwatch(callback), unwatchAll() direction(), getDirection(direction) unexport() NodeJs onoff module – gpio_onoff.js NodeJs GPIO Modules Johnny-Five http://johnny-five.io/ JavaScript Robotics & IoT Platform Abstractions of many types of H/W boards All API of H/W is the same Arduino, RaspberryPi, BeagleBone, Intel Edison etc. http://johnny-five.io/platform-support/#raspberry-pi-3-model-b raspi-io module is used internally https://github.com/nebrius/raspi-io Installation npm install raspi-io npm install johnny-five NodeJs Johnny-five module – gpio_johnnyfive.js NodeJs GPIO Modules WiringPi-Node https://github.com/WiringPi/WiringPi-Node Node.js bindings to wiringPi Faster than others Install npm install wiring-pi Core functions var wpi = require('wiring-pi') wiringPiSetup(), wiringPiSetupGpio(), wiringPiSetupPhys(), wiringPiSetupSys(), setup(mode) pinMode(pin, mode) digitalRead(pin) digitalWrite(pin, state) LED Blink LED Blink Part LED(Light Emitting Diode) Short Leg: Cathode, Negative( - ) Long Leg: Anode, Positive( + ) The head of cathode is cut. Resister Block flow of electric current Voltage, decrease electric current No electrode Unit: Ω(ohm) Connect Circuit LED Long leg: GPIO18 + Resister LED Short leg: GND Calculating Resistor Value LED Voltage, Check electric current Data sheet 5mm red as base Electric Current 20mA Voltage approx. 1.8V~2.2V Ohm’s kaw V = I * R R = V / I (Supply Voltage – LED Voltage) / Electric Current (3.3 – 2) / 0.02 = 65 Use resistor value most close to 65: 100Ω (220Ω) Reading Resistor In the past, it was difficult to print # 4 or 5 color band Gold or silver to right 4 Color band: 3rd # is multiplier (# of 0) 4th # is margin of error 5 Color band: 4th number is multiplier (# of 0) 5th # is margin of error e.g Brown(1),Black(0),Orange(103),Gold 10,000Ω = 10KΩ, ±5% Red(2),Red(2),Brown(101),Gold 220Ω, ±5% Orange(3),Orange(3),Brown(101),Gold 330Ω, ±5% Brown(1),Black(0),Green(105),Gold 1,000,000Ω = 1MΩ ,±1% Bread Board Prototype before making PCB No need to weld Able to assemble and disassemble Connect to the same row Left and right vertical line for power(Bus) Center 5 spots for part (IC) Composing LED Blink Circuit Led onoff Code Python Rpi.GPIO module - digital_out.py Led onoff Code Python WiringPi module - digital_out_wpi.py Relay Switch Turn on/off 220V(110V) Home Appliance Home appliances other than 5v power control Necessary parts Replay Switch IN, 5V, GND Static semiconductor relay IN, GND 220V Plug fe/male Composition of Relay Switch Circuit Composition of Relay Switch Circuit Relay Switch Code Same as LED Blink Transistor Transistor = Trans + Resistor Semiconductor Device 3 Socket: C(Collector), B(Base), E(Emitter) Change internal resistor value according to base socket electric current (voltage) Use for amplifying circuit and switching circuit Accelerator pedal for automobile Run if base voltage is higher than 2m PNP Run if base voltage is lower than 2m (0.6v) Transistor Switch 2n2222 Commonly used NPN mating type Controls electric current not voltage Protect transistor using resistance Circuit composition USB Connector Vcc – R-PI 5V USB Connector GND – Collector R-PI GPIO18 – Base(220Ohm) R-PI GND - Emitter Transistor Switch Circuit Composition Transistor Switch Code Same as LED Blink Push Button Switch Input Turn on/off LED with button switch Button Switch Two legs are connected Works with only two of four legs connected Connect Circuit #13 – LED – GND Pull-down resistance 5V – button – 10k resistance – GND Button - Digital #7 Pull-up resistance 5V – 10k resistance – button – Digital #7 Button – GND Pull-Up, Pull-Down Resistor Floating Condition Condition unknown while switch is open Weak on noise like surrounding pin voltage, electric statics Solve by connecting 0V(GND) or Vcc (5V) while switch is open When Vcc and GND is directly connected overflowing electric current causes short-circuit Generally, use 10KΩ resistor to solve the issue Pull up when resistor is connected to Vcc and pull down when resistor is connected to GND Pull Up, Pull Down Resistor Connect GND without resistor Fire Push Button Switch Circuit (Pull-up Resistor) Push Button Switch Circuit (Pull-down Resistor) Switch Input Python Rpi.GPIO module - digital_input.py Switch Input Python WiringPi module - digital_input_wpi.py Internal Pull-up/down (Push Button Switch) Pull up resistor is supplied internally GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) Switch Internal Pull-Up/Down Python Rpi.GPIO module – digital_input_pud.py Python WiringPi module – digital_input_pud_wpi.py Magnetic Door Switch Sense door open Switch working by two magnetic closing and opening Necessary parts Magnetic door switch 10KΩ resistor Use same circuit and sketch as Push Button Interrupt / Edge detecting Polling Check pin value and condition periodically Adjust period with delay() function Halt all program while delay() Waste unnecessary CPU When it does not fit on the period, input is omitted Interrupt When condition is met, call-back designated function (ISR) Runs separately from delay() period Interrupt – RPi.GPIO Edge Type GPIO.RISING GPIO.FALLING GPIO.BOTH wait_for_edge(pin, type) add_event_detect(pin, type [, callback=fn, boucetime=ms]) Interrupt / Edge detecting Python Rpi.GPIO module – interrupt.py Interrupt / Edge detecting Python WiringPi module – interrupt_wpi.py LDR (Light Dependent Resistor) 조광센서 양음극 없음 가변저항, 빛에 따라 저항값 변화 밝으면 저항 감소, 어두우면 저항 증가 10Lux : 20 ~ 50kΩ 0Lux : 2MΩ 회로 주변 환경에 따라 풀다운 저항 변경(10K~1M) 가변적인 전압의 특정 값 이상/이하 입력 Analog 신호를 Digital로 인식 LDR (조도센서) 회로구성 LDR(조도센서) Code Python Rpi.GPIO module – ldr.py LDR(조도센서) Code Python WiringPi module – ldr_wpi.py 적외선 센서(Infrared Sensor) 적외선 발생기(IRED) Infrared Emitting Diode LED 모양 일반적으로 리모콘 끝 부분에 장착 육안으로 빛을 볼 수 없슴 카메라 뷰파인더로 확인 가능 동작 전류 : 100mA 동작 전압 : 1.3v ~ 1.7v 필요 저항 : 5v 일때 35Ω (5 – 1.5)v / 0.1A = 35 적외선 센서(Infrared Sensor) 적외선 수신기(Photo Transistor) 적외선 수신에 따른 트랜지스터 적외선 값에 따라 Collector-Emitter 저항 변화 Pull-down 저항 1MΩ 적외선 센서 회로구성 적외선 센서 Code Python Rpi.GPIO module - ir.py 적외선 센서 Code Python wiringpi module - ir_wpi.py 인체 동작 감지 사람의 움직임 유무 감지 인체 온도 36.5o 적외선 파장 감지 필요 부품 Passive Infrared, PIR 센서 1 : Out 2: Vcc 3: GND PIR 센서 Code Python WiringPi module - pir_wpi.py PIR 센서 Code Python Rpi.GPIO module - pir.py Touch Sensor 인체의 전류 감지 3핀 Red : Vcc (3~5V) Green : Data Out Black : GND 단순한 Digital Input Touch Sensor Python Rpi.GPIO module - toche.py Python WiringPi module - touch_wpi.py Analog Out PWM( Pulse Width Modulation) 펄스 폭 변조 지정된 주파수의 펄스의 폭을 조절하여 아날로그 신호로 사용 주파수(Frequency) : Hz, 1초에 일어날 펄스의 갯수 주기(Period) : 한 펄스의 지속 시간 Pulse Width : 하나의 Period에서 활성화된 기간 Duty Cycle : 한 주기 내에서 HIGH 상태 시간 비율 Raspberry-Pi PWM Hardware PWM : BCM2835 SoC 레지스터 ALT0 GPIO12,13 – PWM0, PWM1 레지스터 ALT5 GPIO18, 19 – PWM0, PWM1 Software PWM : CPU Clock에 의존하여 Pulse 생성 GPIO 제한 없슴 정확도 떨어짐 Library(Module) Support Level Python Rpi.GPIO module Software PWM only NodeJs Onoff module No PWM support WiringPi (Python, NodeJs) H/W, S/W support LED Fade Circuit python Rpi.GPIO module S/W PWM support only 관련 함수 pwm = GPIO.PWM(pin, Hz) pwm.start(duty_cycle) pwm.ChangeDutyCycle(duty_cycle) pwm.stop() LED Fade – python Rpi.GPIO module – pwm.py WiringPi module H/W PWM GPIO pin limitation (12,13 / 18, 19) wpi.pinMode(pin, wpi.PWM_OUTPUT) wpi.pwmSetMode(mode) { PWM_MODE_MS : Mark Space , wpi.PWM_MODE_BAL : Balanced(default) } wpi.pwmSetClock(divisor) 19.2Mhz(PWM base clock) / divisor(분주비) wpi.pwmSetRange(range) Define Resolution, default = 1024 19.2M / divisor / range = Hz wpi.pwmWrite(pin, value) root privileges required(sudo) LED Fade python WiringPi module H/W PWM – pwm_wpi_hw.py python WiringPi module H/W Clock PWM – pwm_wpi_hw_clk.py WiringPi module Soft Ware PWM GPIO pin no limitation each pin uses 0.5% of the CPU wpi.pinMode(pin, wpi. OUTPUT) wpi.softPwmCreate(pin, init_value, range) Mininum Pulse Width 100μS, 100Hz for low CPU usage Recommended range = 100 wpi.softPwmWrite(pin, value) LED Fade Python WiringPi module S/W PWM – pwm_wpi_sw.js SG90 Servo Motor 180º 회전 가능 각도 별 제어 가능 Voltage : 4.8v ~ 6v PWM Period : 50Hz(20ms) PWM 펄스의 지속시간으로 회전각도 제어(1~2ms, datasheet) 0.5ms : -90º 1.5ms : 0º 2.5ms : +90º Servo Motor 회로 구성 Servo Motor Frequency : 50Hz 1초(1000ms)에 50Hz 1Period = 20ms(1000/50) -90º : 0.5ms 1Period의 2.5% 0.5 / 20 = 0.025 0º : 1.5ms 1Period의 7.5% 1.5 / 20 = 0.075 +90º : 2.5ms 1Period의 12.5% 2.5 / 20 = 0.125 Servo Motor Python WiringPi module S/W PWM Servo – servo_wpi_sw.py Python WiringPi module H/W PWM Servo – servo_wpi_hw.py Capacitor를 이용한 Analog Input 디지털 입력 핀으로 아날로그 흉내 RC회로(Resistor * Capacitor) Capacitor (콘덴서) 교류는 통과, 직류는 차단 축전지 교류 전압 충전/방전 시 상수 (Time Constant) 전압의 63.2%를 충전하는데 걸리는 시간(초) 반복 : 나머지 전압의 63.2% 충전 TC = R x C Capacitor를 이용한 Analog Input 저항의 크기에 따라 충전 시간 변화 캐패시터의 충전 시간 측정 가변저항 값 유추 고정저항 + 가변저항 1KΩ + LDR RC circuit LDR Analog Input Python Rpi.GPIO module – rc_analog.py RC circuit LDR Analog Input Python WiringPi module – rc_analog_wpi.py Ultra Sonic Sensor Vcc : 5v Trigger : GPIO 24 , 초음파 발생 Echo : GPIO 23, 반사되는 음파 인식 Raspberry-Pi GPIO 3.3v Level Convertor 1K, 2K 저항 GND : Ground Ultra Sonic Sensor 초음파를 발생시켜서 반사되는 시간으로 거리를 계산 Trigger pin에 10us 동안 HIGH 8번의 40hz 초음파 펄스 발생 Echo pin Low상태 펄스 발생 중 Echo pin HIGH 상태 펄스 발생 종료 Echo pin LOW 상태 반사 음을 수신 소요 시간 HIGH 상태인 동안의 시간 Ultra Sonic Sensor 소요시간으로 거리 계산 Speed of Sound Depends on medium, temperature 343.21m/s in dry air 20 C (68 F) 34321cm/s Speed = Distance / Time 34321 = distance / time 34321 = distance / (time/2) 왕복 시간 17160 = distance / time 17160 * time = distance Ultra Sonic Sensor Python Rpi.GPIO module – ultra_sonic.py Ultra Sonic Sensor Python WiringPi module – ultra_sonic_wpi.py DHT-11 온도/습도 Module 온도/습도 센서를 Module로 구성 3핀 Red : Vcc (3~5V) Green : Data Out Black : GND 1Wire 통신 주어진 통신 체계에 마추어 신호 전달 MCU 요청신호가 전달되면 동작 HIGH 레벨 지속 시간에 따라 0, 1 구분 40bit 데이타(습도: 16bit, 온도: 16bit, CheckSum: 8bit Real-time 요구 Rpi.GPIO, onoff Module 높은 실패율 wiringPi Module 또는 전용 Module 사용 https://akizukidenshi.com/download/ds/aosong/DHT11.pdf DHT-11 온도/습도 Module 요청 신호(Start Signal) : HIGHjLOW(18ms) j PULL_UP 수신 데이타(40bit) : 0 = 26~28us HIGH , 1= 50~70us HIGH DHT-11 온도/습도 Module 40bit 전송 데이타 16bit 습도 (예: 53.0%) 8bit : 습도 상위 비트 (0011 0101) :53 8bit : 습도 하위 비트 (0000 0000) : 0 16bit 온도 (예: 24.0oC) 8bit : 온도 상위 비트 (0001 1000) : 24 8bit : 온도 하위 비트 (0000 0000) : 0 8bit : Check Sum (예: 0100 1001) DHT-11 Sensor Python Rpi.GPIO module – dht11.py<1/4> DHT-11 Sensor Python Rpi.GPIO module – dht11.py<2/4> Python Rpi.GPIO module – dht11.py<3/4> Python Rpi.GPIO module – dht11.py<4/4> DHT-11 Sensor Python Adafruit DHT module https://learn.adafruit.com/dht/overview Installation cd ~ git clone https://github.com/adafruit/Adafruit_Python_DHT.git cd Adafruit_Python_DHT sudo python setup.py install dht_adafruit.py DHT-11 Sensor Python WiringPi module – dht11_wpi.py <1/4> Python WiringPi module – dht11_wpi.py <2/4> Python WiringPi module – dht11_wpi.py <3/4> Python WiringPi module – dht11_wpi.py <4/4> Serial VS Parallel Serial Communication Send each bit consecutively Slow One Communication circuit Parallel Communication Send multiple bit at once Fast Need as much circuits as the number of sending bits Serial Communication & Clock Data is sent one by one consecutively Need to distinguish one bit Sender and receiver know same bit Asynchronus Serial Communication Agreement on data distinguishing period Do not send separate clock signal If communication speed is different between Two sides, it is impossible to communicate Need extra starting and ending bit Only 1:1 Communication RS-232(UART) Communication Protocol Synchronus Serial Communication Data signal and bit distinguishing signal (Clock) is separately sent Recognize data signal on clock No need to agree on speed Limit on maximum speed 1:N Communication is possible Master/Slave relations, Master leads clock I2C, SPI Communication protocol UART/RS-232 UART(Universal Asynchronus Receiver Transmitter) Asynchronus Communication mechanical device Most have internal MCU hardware Able to decide clock start/end bit within software Communication speed : Baud Rate Use two data circuits TxD(Transfer), RxD(Receive) RS-232 US EIA(Eletronic Industries Association) Standard for electronical and mechanical (Connector) traits for UART Represents serial port in PC UART/RS-232 Communication MCU UART TTL(Transistor to Transistor Logic) : 0~5V RS232 : -12V ~ +12V RS232 – TTL transfer chip MAX232 / Maxim USB – TTL transfer chip FT232R / FTDI Chip CP2102 / Silicon Lab RaspberryPi UART UART Activation sudo vi /boot/config.txt (Must back-up) enable_uart=1 sudo vi /boot/cmdline.txt (Must back-up) Delete below detail (getty disable) console=serial0,115200 sudo reboot ls /dev/serial* Check below detail serial0 --> ttyS0(UART) serial1 --> ttyAMA0(Bluetooth) Use device name connected to serial0 RaspberryPi URAT Raspberry Pi – PC Communication Transfer Device RS232 to TTL Convertor USB to Serial Convertor Arduino Communication b/w 2 Raspberry Pis Cross connection of Tx, Rx jumper cable Connect GND Raspberry Pi – MCU Board(Arduino) Communication Connect GPIO UART Pin Connect USB UART Communication Between Two Raspberry Pis Compose circuit (Tx –Rx Cross connection) Tx – Rx Rx – Tx GND –GND RaspberryPi - Arduino Compose circuit (Tx –Rx Cross connection) RaspberryPi Tx – Arduino Rx RaspberryPi Rx(3.3v) – Arduino Tx(5v) Level Convertor(Resistor) R1 : 1KΩ R2 : 2KΩ GND -GND minicom Setting Setting sudo apt-get install minicom Execute sudo minicom –b 115200 –D /dev/xxx Setting CTRL-A Z O Q : local Echo on/off P : Add line feed T : Add Carriage Return Esc Save setup as dfl RaspberryPi Serial Communication Programming Python serial Module https://github.com/pyserial/pyserial Install pip3 install pyserial or sudo apt-get install python-serial Basic API import serial port = serial.Serial(‘/dev/xxx’, baudrate=115200, timeout=1.0) port.isOpen() port.write(‘str’) ch = port.read() port.close() RaspberryPi URAT Communication Sender, Python Serial module – uart_write.py Receiver, Python Serial module – uart_read.py RaspberryPi URAT Communication Two-way communication, Python Serial module – uart_rw.py I2C(Inter Integated Circuit) Communication Integrated Circuit Communication Protocol for communication with MCU and peripheral devices Developed by Philips Communication Circuits: 2 circuits (TWI : Two Wire Interface) SCL(Serial Clock) SDA(Serial Data) Need each circuits pull up resistor Actual Communication circuit is only one (SDA) Only half-double Communication is possible Slow sending speed Able to connect multiple devices to one bus Have specific address (7bit) for Slave Maxi 128(27) slave No extra circuit for additional devices Setting RaspberryPi I2C Activation sudo raspi-config Advanced Options > A7 I2C Setting RaspberryPi Check device activation ls –l /dev/i2c* Check module loading in kernel sudo vi /etc/modules “i2c-dev” Check detail, if no detail, add lsmod Check i2c_dev from the list Allow I2C Communication sudo vi /etc/modprobe.d/raspi-blacklist.conf “blacklist i2c-bcm2708” Delete detail or mark command RaspberryPi – Arduion I2C Communication Circuit Connection Arduino Code Sketch Setting RaspberryPi I2C Tool installation $ sudo apt-get install i2c-tools I2C Bus Scan $ i2cdetect –y 1 Check printing of slave address (0x04) designated in Arduino code I2C Programming Python smbus module Installation sudo apt-get install python-smbus sudo apt-get install python3-smbus (python3의 경우) https://github.com/bivab/smbus-cffi Basic API import smbus bus = smbus.SMBus(busNo) bus.write_byte(address, value) bus.read_byte(address) python smbus module - i2c.py I2C Programming NodeJs i2c module Installation npm install i2c https://www.npmjs.com/package/i2cBasic API Basic API i2c = require('i2c') wire = new i2c(addres, {device:'/dev/i2c-x'}) wire.writeByte(byte, function(err){}) wire.readByte(fnction(err, res){}) Rotary Potentiometer Change resistor by switching handle Pin1 : 5V, Pin2 : OUT, Pin3 : GND Fade LED using Variable resistor value output SPI(Serial Peripheral Interconnect) Communication Serial Peripheral Interconnect Communication Developed by Motorola Communication Circuits: 4 SCK, SCLK(Serial Clock): Master sends clock MOSI(Master Output Slave Input): Master send MISO(Master Input Slave Output): Master Receive SS(Slave Select): Select slave by Master Add one specific circuit for each slave '0' to selected slave, '1' for remainder Actual Communication circuits is two (MOSI, MISO) All double Communication, Fast transfer SPI Mode Set start bit(0,1), clock bit(0,1) MCP3008 ADC (Analog Digital Convertor) 10bit resolution (0~ 1023) SPI Communication Exercise variable resistor analog signal Connect Circuit Vdd – 3.3v Vref – 3.3v AGND – GND CLK – CLK(GPIO11) Dout – MISO(GPIO09) Din – MOSI(GPIO10) CS - CE0(GPIO08) CH0 – Variable resistor OUT Reading Variable Resistor as MCP3008 SPI Activation Raspberry-pi SPI Port Activation sudo raspi-config 8.Advanced Options > SPI > Yes ls /dev/spi* MCP3008 Communication http://www.farnell.com/datasheets/808965.pdf 3Byte(24bit) Transfer/ Receive SPI Mode : 0 (CPOL-0,CPHA-0) Speed : 10kHz Transfer 1: Start bit : 1 2: Channel configuration 3: Ignored Receive 1: Ignored 2-3 : 10bit value SPI Programming Python Py-spidev Installation spidev Control Module (/dev/spidev0.0 , /dev/spidev0.1) https://github.com/doceme/py-spidev git clone git://github.com/doceme/py-spidev cd py-spidev Sudo python setup.py install or pip3 install spidev Basic API import spidev spi = spidev.SpiDev() spi.open(bus, channel) recv_list = spi.xfer(send_list) recv_list = spi.xfer2(send_list) spi.max_speed_hz = hz spi.mode = {0b01,0b11,0b10, 0b11} Python spidev module open(port, dev) Port : 0 Dev :CE0 =0, CE1=1 xfer2([byte_1, byte_2, byte_3]) byte_1 : 1 byte_2 : channel config 1000 000 : channel 0 byte_3 : 0(ignored) adc_out r[0] : ignored r[1] : Most significant 2 bit of 10bit r[2] : Least significant 2 bit of 10bit Bluetooth Standard for personal short distance wireless comm. 1994 Ericsson (Swedish Communication Device Vendor) SIG : Bluetooth Special Interest Group https://www.bluetooth.com/ Classic Protocol RFCOMM Radio Frequency Communication Replaced RS232 serial port to wireless Similar to TCP Able to use 30 port (Channel) L2CAP Logical Link Control and Adaption Protocol Packet type, Max 672Bytes Similar to UDP Used as transfer layer of RFCOMM ACL Asynchronous Connection-oriented Logical Similar to IP protocol, Use as sub protocol of L2CaP SCO Synchronous Connection-Oriented Audio transfer protocol (64kb/s) BlueZ http://www.bluez.org Library for Linux official Bluetooth Protocol Stack bluez installation sudo apt-get install bluez bluez related command hciconfig Bluetooth device set hcitool Tool for command transfer and Bluetooth device connection l2ping Request L2CAP echo/ping to given address sdptool Offer SDP (Service Discovery Protocol) for Bluetooth devices frcomm Set and manage Bluetooth sub system RFCOMM Use Bluetooth as Serial(RS232) bluetoothd Service demon handling all Bluetooth devices bluetoothd –v Setting Communication Between Two Raspberry Pi Server sudo hciconfig Check devices and address of installed Bluetooth sudo hciconfig hci0 name “MyName” Set hci0 name to show under scan list sudo hciconfig hci0 piscan Set hci0 to show under scan list, Enable Page and Inquiry scan sudo hciconfig hci0 noscan Set hci0 to not show under scan list, Disable Scan Client hcitool scan Scan possible devices sudo l2ping XX:XX:XX:XX:XX:XX Setting Communication Between Two Raspberry Pi Server sudo vi /lib/systemd/system/bluetooth.service (Must back-up) Add –C at the end of ‘bluetoothd’ Motion error due to version difference b/w bluez5 and SDP sudo systemctl restart bluetooth sudo sdptool browse local Check serial port service in SDP sudo sdptool add SP Add Serial Port service sudo rfcomm listen /dev/rfcomm0 Connect to stand by /dev/rfcomm0 Need separate terminal connection under listening condition after connecting to client sudo minicom –b 115200 –D /dev/rfcomm0 Serial communication with /dev/rfcomm0 Client sudo rfcomm connect /dev/rfcomm0 XX:XX:XX:XX:XX Connect and Bind to /dev/rfcomm0 with remote address channel Eliminate: rfcomm release /dev/rfcomm0 sudo minicom –b 115200 –D /dev/rfcomm0 Android Bluetooth Serial Controller https://play.google.com/store/apps/details?id=nextprototypes.BTSerialController&hl=ko Execute and connect after stand-by at Raspberry Serial Communication Server sudo rfcomm listen /dev/rfcomm0 python bt_serial.py Client python bt_serial.py Bluetooth Socket Python Pybluez module https://github.com/karulis/pybluez sudo apt install libbluetooth-dev dependency lib sudo pip3 install pybluez or sudo apt-get install python-bluetooth Bluetooth Device Scan Python Pybluez module – bt_scan.py Bluetooth Socket Python Server - bt_server.py Bluetooth Socket Python Client – bt_client.py Service Discovery – bt_service_server.py ServerSocket, Stand-by to connect to specific services (MyService) sudo vi /lib/systemd/system/bluetooth.service (Must Back-up) Add ‘-C’ at the end of ‘bluetoothd’ Error due to version difference b/w bluez5 & SDP sudo service bluetooth restart (/etc/init.d/bluetooth) Service Discovery – bt_service_client.py ClientSocket, Connect to specific services (MyService) Service Discovery – bt_service_server.js Service Discovery – bt_service_client.js ClientSocket, Connect to specific services (MyService) BLE Bluetooth Low Energy Bluetooth 4.0 Standard Bluetooth SMART (BLE Device, Sensor) Bluetooth SMART Ready (Laptop, Smart Phone) Profile Conceptual standard difference for Bluetooth motion & control GAP, Generic Access Profile GATT, Generic Attribute Profile ATT, Attribute Protocol GAP Control connection among devices Distinguish motion Advertising: Broadcast to other devices Connection: Connecting two devices Rolls Central Device: PC, Smart Phone Peripheral Device: Sensor etc. BLE GATT Control communication among devices Define data transfer methods using Service, Characteristic Attribute Minimum unit for data transfer /receive Profile Product Service Specific function Characteristic One value for one characteristic Rolls: GATT Server(Slave) : Offer data Sensor GATT Client Central Device PC , Smart Phone GATT Client (Request) j GATT Server Beacon Advertising Specific Devices iBeacon Apple registered mark for indoor positioning system Maxi 31byte payload within PDU iBeacon Data Frame Advertising Data Max 31 Byte payload within PDU Major/Minor Number Recognize Beacon device Tx Power Signal strength measured within 1m of device Use to calculate distance Advertising / Connection Advertising / Scanning Advertising / Scan Response Advertising Interval Electric current consumption difference by Interval Connection / Pairing End Advertising Scan impossible Communication via GATT/Service/Characteristic Making Raspberry-Pi with iBeacon sudo hciconfig hci0 noscan Scan halt sudo hciconfig hci0 leadv3 None Connectable LE Advertising, leadv0 : Connectable sudo hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 01 00 02 C8 00 Advertising send command 0x08 : OGF, Bluetooth command group 0x0008 : OCF, send Beacon command 1E 02 01 : AD Flag(3Byte), 총길이 30byte, General Discorverable Mode 1A 1A : AD Headers(2Bytes), Remainder length 26, Vendor AD Type FF 4C 00 02: Vendor spec. data, Apple ID(4C 00), TypeId(02:iBeacon) 15 : Remainder length: 21 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 : UUID 00 01 : Major Number 00 02 : Minor Number C8 : Tx Power 00 : Null EOF sudo hciconfig hci0 noleadv Advertising send halt Check with Mobile App Locate Beacon App , Radius Networks Android https://play.google.com/store/apps/details?id=com.radiusnetworks.locate&hl=ko iPhone https://itunes.apple.com/kr/app/locate-beacon/id738709014?mt=8 RaspberryPi Beacon Scanner iBeacon Device Locate App, Beacon Transmitter Android 5.0+ Estimote http://estimote.com/ SensorTag http://www.ti.com/sensortag HM-10 http://www.jnhuamao.cn/ Ti CC2540/2541 SoC base RaspberryPi Beacon Scanner hcidump Installation sudo apt-get install bluez-hcidump le scan sudo hcitool lescan --duplicate Exclude same device filtering sudo hcidump --raw Print detail lescan info UUDI, Major/Minor, Tx analysis RaspberryPi Beacon Scanner in Python iBeacon Scanner https://github.com/switchdoclabs/iBeacon-Scanner- http://www.switchdoc.com/ Part of BeaconAir Project Source download & execute git clone https://github.com/switchdoclabs/iBeacon-Scanner- cd iBeacon-Scanner- sudo python testblescan.py MAC addr, UUID, Major, Minor, Tx Power, RSSI Python Beacon Scanner – beacon_scan.py RaspberryPi Beacon Scanner in NodeJs Noble https://github.com/sandeepmistry/noble Refer to : https://blog.truthlabs.com/beacon-tracking-with-node-js-and-raspberry-pi-794afa880318#.khaba1d7m Buzzer Digital output Material Buzzer or Piezo Variable Resistor (Volume Control) Print frequency fit to compass WiringPi softTone oftToneCreate(PIN) Print GPIO Pin # softToneWrite(PIN, FREQUENCY) Designate frequency value to print Compose Circuit for Signal Print Warning Sound Python – buzzer.py Twinkle Twinkle Little Star Python – buzzer_littlestar.py Audio Print sudo raspi-config Advanced Option --> Audio Audio Enable /boot/config.txt Dtparam=audio=on Audio Print Modules Python pyaudio, pygame NodeJs play-sound ALSA Advanced Linux Sound Architecture Linux Kernel Version 2.6 Basic Sound System Replace OSS (Open Sound System) Main utils amixer Sound system check and set alsamixer Sound volume control alsactl Sound driver advance setting aplay Recorder and Player aplay ~/sample.wav speaker-test Print white noise PyAudio https://people.csail.mit.edu/hubert/pyaudio/ Installation sudo apt-get install python-pyaudio Function Recoding Wav Print, mp3 not supported Main Function p = pyaudio.PyAudio()stream = p.open() stream.read(chunk) stream.stop_stream() stream.close() p.terminate() PyAudio Wave File Play Block PyAudio Wave File Play Callback Pygame mixer Module http://www.pygame.org/docs/ref/music.html Installation sudo apt-get install python-pygame Installed by default Basic APIs import pygame pygame.init() pygame.mixer.music.load(‘sample.mp3’) pygame.mixer.music.play() pygame.mixer.music.pause() pygame.mixer.music.unpause() pygame.mixer.music.stop() Pygame mixer code mp3.py Raspi-Camera https://www.raspberrypi.org/documentation/usage/camera/python/README.md Connect to camera Interface Activate Raspberry camera sudo raspi-config 6. Enable Camera Raspi-Camera Take Photo raspistill –o caputre1.jpg Take still photo after 5 seconds raspistill -0 capture2.jpg –w 1280 –h 720 Resolution 1280 x 720 Take Video raspivid –o video.h264 –t 5000 Shoot video for five seconds Check video omxplayer path/video.h264 Notice Preview not shown in VNC Viewer Need to connect Display device PiCamera Module Picamera Module Installation https://github.com/waveform80/picamera sudo apt-get install python-picamera Main Function camera = picamera.PiCamera() camera.rotation = 90 camera.resolution = (1280, 720) camera.start_preview() camera.capture(‘cam1.jpg’) camera.start_recording(‘video.h264’) camera.stop_recoding() camera.stop_preview() camera.close() Pycamera Module Camera Streaming Remote Streaming of Video from Camera Main Service Motion: http://www.lavrsen.dk/foswiki/bin/view/Motion MJPG-Streamer: https://sourceforge.net/projects/mjpg-streamer/ VLC: http://www.videolan.org/vlc/ Motion Sense motion and record, Remote streaming Install sudo apt-get install motion Sett /etc/motion/motion.conf stream_localhost on j off Run & Shut down sudo service motion stop Motion Check Connect to web browser http://ip_address:8081 Camera Streaming Insert motion view to web page MJPG-Streamer Install dependent package sudo apt-get install libjpeg8-dev imagemagick libv4l-dev Source code download wget http://sourceforge.net/code-snapshots/svn/m/mj/mjpg-streamer/code/mjpg-streamer-code-182.zip unzip mjpg-streamer-code-182.zip cd mjpg-streamer-code-182/ mjpg-streamer Apply patch wget https://github.com/swkim01/RaspberryPiWithIOT/raw/master/ch7/input_uvc_patch patch -p0 < input_uvc_patch Build & Install make USE_LIBV4L2=true clean all sudo make DESTDIR=/usr install Run mjpg_streamer -i "input_uvc.so -d /dev/video0 -n -f 30 -r 1280x720" -o "output_http.so -n -w /usr/www" MJPG-Streamer Check result Connect to web browser http://ip_address:8080 VLC Install sudo apt-get install vlc Start Streaming raspivid -o - -t 0 -n | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264 Check VLC Video Install VLC for PC https://www.videolan.org/vlc/download-windows.ko.html File > openNetwork > rtsp://IP_Address:8554/ OpenCV http://opencv.org/ Open Source Computer Vision, Intel Realtime Computer Vision Library Official Support : C, C++, Python, Java Non-officially support part of NodeJs (Do not support rendering) Offer Various Function for Video Handling Filter Divider Detect Characteristics Detect Motion Video Capture Install sudo apt-get install libopencv-dev sudo apt-get install python-opencv (python 2 only) v4l2 Loading sudo apt-get install v4l-utils sudo modprobe bcm2835-v4l2 ls /dev/video0 /etc/modules : auto load at booting time Rendering Image File cv_show.py Printing GUI at Terminal Select display device, DISPLAY=:0 Should connect VNC or monitor e.g.: DISPLAY=:0 python cv_show.py Or export DISPLAY=:0 Video Capture cv_capture.py Video File Play cv_video_play.py Camera Video Record cv_camera_rec.py Sense Motion Difference Video Sense Motion Image face/eye detecting https://github.com/opencv/opencv/tree/master/data/haarcascades Camera Face Sensor General IoT Architecture Web IoT Communication Type Remote Control e.g. Turning On/Off Lamp, Heater, A/C, Audio, Moving Camera etc. Send Command and Get Result HTTP Request and Response Periodic Monitoring e.g. CPU, Memory, Temperature, Humidity etc. Long term interval Typical Polling HTTP Request and Response + Ajax Real-Time Notification(Server Push) e.g. Alarm of Fire/Intrusion/Emergency bell etc. Server Push WebSocket/MQTT/XMPP Web Remote Control LED On/Off LED GPIO #18 Web Remote Control /static/led.html <1/3> /static/led.html <2/3> /static/led.html <3/3> Python – led.py <1/2> Web Remote Control Python – led.py<2/2> Web Remote Monitoring Monitoring Temperatur/Humidity DHT-11 GPIO #24 Web Remote Monitoring dht11.html <1/3> dht11.html <2/3> dht11.html <3/3> Web Remote Monitoring Python - dht11.py <1/2> Web Remote Monitoring Python - dht11.py <2/2> Web Remote Notification Button Switch Bush Button Switch GPIO #23 Web Remote Notification btn_notify.html <1/2> btn_notify.html <3/3> Web IoT Combined Client HTML - /static/web_iot.html Server Python – web_iot.py NodeJs – web_iot.js Web IoT Combined HTML - web_iot.html <1/3> Web IoT Combined HTML - web_iot.html <2/3> Web IoT Combined HTML - web_iot.html <3/3> Web IoT Combined Python - web_iot.py <1/2> Web IoT Combined Python - web_iot.py <2/2> Camera Streaming OpenCV + Web Programming Base64 Encoding data:image/png;base64, Client /static/cam.html Server Python cam.py NodeJs cam.js Camera Streaming HTML – /static/cam.html <2/3> Camera Streaming HTML – /static/cam.html <3/3> Camera Streaming Python – cam.py <1/2> Camera Streaming Python – cam.py <2/2> Arduino? Origin of the Name Arduin: King of Ivrea, Italy (1002), Ousted by Henry II of Germany Bar di Re Arudino: Favorite pub of Arduino developer Massimo Banzi Professor at IDII(Interaction Design Institute Ivera) In Ivrea, Italy College specialized in Art and IT convergence Derived from Wiring, Master’s thesis of a IDII, Colombia University Co-developed Arduino with his colleague David Cuartielles Inspired by program language Processing, made by MIT for designers Arduino Open Source H/W Prototyping Platform Arduino Board + IDE + Community Arduino Borads http://arduino.cc/en/Main/Products Arduino Arduino Arduino Interactive Arduino IDE http://arduino.cc/en/Main/Software Windows, Mac, Linux Driver Installation For Windows Connect PC and Arduino Start -> Control Panel -> System -> Device Manager Update Driver from COMXX list Select Arduino installation directory /Drivers Arduino IDE - Windows Install Driver & Open Device Manager Check COM & LPT List COM Port number differs by device ArduIDE Execution Arduino.exe Tool > Serial Port Select COM port checked at device Arduino IDE - Mac No need to install Driver ArduIDE Execution Arduino.app Tool > Serial Port Select /dev/tty.usbmodemXXX Arduino IDE Arduino HelloWorld Compose Circuit LED Short leg to GND Long leg to 13 Sketch File> Example > 01.Basics > Blink Compile, Upload Experiment Result LED Blink next to 13 port Architecture of Sketch (Source Code) C/C++ Language Use its own Pre-processor Not standard C/C++ Able to call before function decl. Binary Notation (0b or 0B) Setup() Initial one call, initialization loop() Function running while power on Infinite repetition Build Process Create middle file with main function #include "arduino.h" hardware/arduino/cores/arduino/ avr-gcc Compile hardware/tools/avr/bin/ *.o File link & Upload Data Types In/Output Basic Function pinMode(pin, mode) Set designated pin to in/output pin: # of pin to set mode : INPUT, OUTPUT, INPUT_PULLUP delay(ms) Halt program for certain amount of time ms: Time to halt, milliseconds, unsigned long digitalWrite(pin, value) Print HIGH (5V) or Low (0V) to designated pin pin: # of Pin to output value: HIGH or LOW digitalRead(pin) Input HIGH or LOW to given pin value (to read in) pin: # of Pin to input LED Blink Arduino’s HelloWorld LED Blink Part LED(Light Emitting Diode) Short Leg : Cathode, Negative( - ) Long Leg: Anode, Positive( + ) Cathode head is cut. Register Interfere electric flow Decrease Voltage or electric current No Electrode Unit: Ω(ohm) Connect Circuit LED Long Leg: 13 Port + Resistor LED Short Leg: GND LED Blink Sketch Serial Monitor Serial Communication tool t/w Arduino and PC Tools > Serial Monitor or Short Cut command Ctrl + Shift + M Select Baud(Transfer Speed) same as Sketch Serial Input Output Serial.begin(baud) Same value for Baud transfer speed and transfer/receive Serial.begin(9600) Serial.available() Readable bites (Saved in Buffer) Serial.read() Check with if sentence before call Serial.read() Turn around by reading first bit When there is no data, turnaround -1 Serial.write() Output binary data Serial.print(); Output ASCII Text Serial.println() Add line feed character to print() function SerialEvent Call when serial data is arrived Print Serial Monitor Hello World LED On/Off with Serial Monitor Execute at Loop LED On/Off with Serial Monitor Execute at SerialEvent Input Push Button Switch On/Off LED with button switch Button Switch Two legs are connected Connecting two legs of four is enough Connect Circuit 13 pin – LED – GND Pulldown resistor 5V – button – 10k resister – GND Button - Digital 7 pin Pullup resistor 5V – 10k Resistor – Button – Digital 7 pin Button – GND Push Button Switch Circuit (Pull Up Resistor) Push Button Switch Circuit (Pull Down Resistor) Push Button Switch Sketch Push Button Circuit (Internal Pullup) Offer pull up resistor MCU internally pinMode(PIN, INPUT_PULLUP); 0,1 cannot be used simultaneously with serial comm. Relay Switch Sketch Same as LED Blink Composing LED Fade Circuit LED Fade Sketch Analog Input ADC(Analog to Digital Converter) included Analog-Digital Transducer A0 ~ A5 (6 pins) Declare in order of constant A0 = 14, A1=15 10 bit resolution: 0~1023 analogRead() Rotary Potentiometer Necessary parts Rotary Variable Resistor Change resistor value by turning handle Pin1 : 5V, Pin2 : OUT, Pin3 : GND 220Ω Resistor LED Composing Variable Resistor Circuit Composing Variable Resistor Circuit External Library Library not included in Arduino IDE Must be installed separated as needed Able to re-use well-used function by drafting them as library Have a same name folder within the library folder Adding Library Sketchbook location Windows : File > Setting Mac : Arduino > Setting Copy external library directory file to library directory Special characters are not allowed in directory name Restart Arduino IDE Sketch > Get Library > Select DHT-11 Temperature/ Humidity Module Compose temp./humidity sensor as module 4 pins 1 : Vcc (3~5V) 2 : Data Out Need 10k pull up resistor Cannot use Internal pull up (100k) 3 : Not used 4 : GND Have DHT-22 as well with better function https://learn.adafruit.com/dht/overview Composing DHT-11 Circuit DHT-11 Library https://github.com/adafruit/DHT-sensor-library Change the folder name to “DHT” after unzipping Copy to /libraries/ Restart IDE DHT-11 Sketch (Cont.)