出版社: 人民邮电出版社
出品方: 异步图书
原作名: Raspberry Pi Cookbook, 2nd Edition
译者: 韩波
出版年: 2017-3-1
页数: 417
定价: 89.0
装帧: 平装
丛书: O'Reilly动物系列(中译本)
ISBN: 9787115447203
内容简介 · · · · · ·
树莓派(Raspberry Pi)是一款基于Linux系统的、只有一张信用卡大小的卡片式计算机。研发树莓派的起初目的是通过低价硬件和自由软件来推动学校的基础计算机学科教育,但很快树莓派就得到计算机和硬件爱好者的青睐;他们用它学习编程,并创造出各种各样新奇的、风靡一时的软硬件应用。
本书由多产作家Simon Monk编写,是将内容进行全面升级之后的第2版,囊括了丰富的实践示例,详细讲解了树莓派的配置与管理、网络连接、操作系统及软件。另外,本书还介绍了使用Python进行树莓派开发的各项技巧,书中介绍了有关Python编程的基础知识、列表与字典、Python高级特性。同时,本书还介绍了有关机器视觉、硬件基础、控制硬件、电机、数字输入、传感器、显示设备、物联网及Arduino等相关知识。
本书适合程序员和计算机软、硬件爱好者,以及对树莓派感兴趣的读者阅读,...
树莓派(Raspberry Pi)是一款基于Linux系统的、只有一张信用卡大小的卡片式计算机。研发树莓派的起初目的是通过低价硬件和自由软件来推动学校的基础计算机学科教育,但很快树莓派就得到计算机和硬件爱好者的青睐;他们用它学习编程,并创造出各种各样新奇的、风靡一时的软硬件应用。
本书由多产作家Simon Monk编写,是将内容进行全面升级之后的第2版,囊括了丰富的实践示例,详细讲解了树莓派的配置与管理、网络连接、操作系统及软件。另外,本书还介绍了使用Python进行树莓派开发的各项技巧,书中介绍了有关Python编程的基础知识、列表与字典、Python高级特性。同时,本书还介绍了有关机器视觉、硬件基础、控制硬件、电机、数字输入、传感器、显示设备、物联网及Arduino等相关知识。
本书适合程序员和计算机软、硬件爱好者,以及对树莓派感兴趣的读者阅读,也适合作为树莓派相关实践课程的指导用书。
丛书信息
喜欢读"树莓派开发实战(第2版)"的人也喜欢 · · · · · ·
树莓派开发实战(第2版)的话题 · · · · · · ( 全部 条 )



树莓派开发实战(第2版)的书评 · · · · · · ( 全部 0 条 )
读书笔记 · · · · · ·
我来写笔记-
魏智勇 (www.weizhiyong.com)
# 软件与工具资源 - [操作系统官方下载地址:]([http://www.raspberrypi.org/dowloads]) - windows下的SD卡格式化软件 SD Formatter - [wondows下写入操作系统镜像软件]([https://sourceforge.net/projects/win32diskimager/files/latest/download]) - 远程桌面访问VNC 树莓派安装 tightvncserver,电脑安装[RealVNC]([http://www.realvnc.com]) - 远程桌面访问 RDP(比VNC效果好) 树莓派安装xrdp,windows使用 mstsc命令 - 远程...2017-11-09 11:39:28 3人喜欢
# 软件与工具资源 - [操作系统官方下载地址:](http://www.raspberrypi.org/dowloads) - windows下的SD卡格式化软件 SD Formatter - [wondows下写入操作系统镜像软件](https://sourceforge.net/projects/win32diskimager/files/latest/download) - 远程桌面访问VNC 树莓派安装 tightvncserver,电脑安装[RealVNC](http://www.realvnc.com) - 远程桌面访问 RDP(比VNC效果好) 树莓派安装xrdp,windows使用 mstsc命令 - 远程通过SSH方式访问树莓派,windows下可用的软件,putty和[XSHELL](https://www.netsarang.com/products/xsh_overview.html) - Windows下开源且好用的解压软件[Bandizip](https://www.bandisoft.com/bandizip/) - 开源的FTP服务器架设以及客户端软件[FileZilla](https://filezilla-project.org/) - pypi国内镜像douban,使用方式在pip命令后加后缀,例如 pip install pygame -i https://pypi.douban.com/simple # 树莓派操作使用 - 配置文件 /boot/config.txt - ip地址配置文件 /etc/network/interfaces - DHCP配置文件(如果采用静态ip,需要使用下列命令清除) sudo rm /var/lib/dhcp/* - 网络名称配置文件 /etc/hosts - 配置命令 sudo raspi-config - 使用摄像头拍照 rapistill -o xxx.jpg - 使用摄像头录像(ms) raspivid -o video.h264 -t 10000 - 将树莓派作为网络存储服务器 sudo apt-get install samba sudo apt-get install samba-common-bin 为samba添加用户 sudo smbpasswd -a xxx 修改 /etc/samba/smb.conf 第一行的工作组 最下方添加 ```C [USB] path=/media/NAS comment= NAS driver valid users=xxx writeable=yes browseable=yes create mask=0777 public=yes ``` samba restart - 网络打印服务器 [CUPS](http://www.cups.org) sudo apt-get install cups sudo usermod -a -G lpadmin xxx //增加一个浏览器用于界面管理,使用631端口 sudo apt-get install iceweasel [管理界面](http://localhost:631) - 屏幕截图 scrot sudo apt-get install scrot - 延时截图 scrot -d 5 - 鼠标截屏 scrot -s - I2C通讯工具 sudo apt-get install i2c-tools sudo i2cdetect -y 1//如果是旧版本的话把1改为0 - 串口检测工具 minicom sudo apt-get install minicom minicom -b 9600 -o -D /dev/ttyAMA0 首先要启用local echo功能才能看到反馈,按Ctrl+A,Z,E # 树莓派硬件 - 安全规范 - GPIO引脚上的电压不能超过3.3V - 每个输出电流不超过3mA - 电源不高于5V - 5V供电电源输出总电流不超过250mA - 最简单的电压转换办法,利用470欧和220欧电阻串联分压,也可以按比例放大到47k,22k以降低电流损耗 -GPIO输出能力汇总[参考文章](http://bit.ly/laMQ0fh) - 树莓派配件 - [Raspberry Squid](https://github.com/simmonmonk/squid),内置了电阻的LED灯和按钮配件。有pyton squid库 - 可以采用电机驱动装置RasPiBobot Board 为树莓派供电,电机驱动板python库raspirobotboard - 传感器接口板[SenseHAT],python库为sense-hat或者SenseHAT pro, explorerhat - [计算模块](https://www.raspberrypi.org/blog/raspberry-pi-compute-module-new-product/) - Pi Plate原型版用于焊接元件以快速开发 - 自行设计符合HAT标准的原型板,在config文件中启用 dtparam=i2cvc=on,[烧写EEPROM工具](https://github.com/raspberrypi/hats.git) - [树莓派HAT标准硬件设计指南](https://github.com/raspberrypi/hats) - 开关交流电配件 PowerSwitch Tail II(光隔加继电器模块) - 精确控制伺服电机,可以使用[Servo Blaster库](https://github.com/richardghirst/PiBits/tree/master/ServoBlaster)。 - 同时控制多台伺服电机,可以使用Adafruit的伺服电机HAT(hardware at top),[Adafruit文档](http://bit.ly/liomEey) - 控制直流电机,可以通过L293D类似芯片搭建H桥,或者使用[SparkFun](http://bit.ly/ILHVkJ)的H桥模块。 - 控制步进电机可以使用[RaspiRobot](http://www.raspirobot.com) - 监测移动可以使用被动红外运动检测模块PIR - 测量电阻值可以通过连接电容通过测量电容充电时间的方法来实现 - 树莓派没有内置模拟量输入,采集模拟量可以通过[MCP3008](http://bit.ly/I1lSqn)类似的ADC模块实现 - 测量温度可以采用不精确的[TMP36](http://bit.ly/lb3MfIm)或者高精度的数字温度传感器[DS18B20](http://bit.ly/l7EveOI) - 测量温湿度与气压的[Sense HAT](https://pythonhosted.org/sense-hat/api/) - aLaMode板(带GPIO接口的Arduino平台),由于aLaMode设计缺陷,需要在树莓派的以太网口缠一点绝缘胶带处理。 - [树莓派控制硬件视频](http://razzpisampler.oreilly.com) # 树莓派快速使用 - 媒体中心,安装OpenELEC_Pi2系统,使用[kodi](http://kodi.wiki/) - Office办公软件 abiword gnumeric - 浏览器 chromium-browser - 网络摄像头服务器,安装motion - 配置 ```shell sudo vim /etc/motion/motion.conf #将 daemon off改为on #webcam_localhost=on改为off sudo vim /etc/default/motion #将sart_motion_daemon=no 改为yes ``` - 启动 service motion start - 运行老式模拟器游戏 安装 stella - 安装JDK,运行minecraft(在树莓派3中该软件已经安装了) ```shell sudo apt-get install openjdk7-jkd(版本可能需要更新到8) wget http://getspigot.org/spigot18/spigot_server.jar java -Xms512M -Xmx800M -jar spigot_server.jar nogui #sudo vim eula.txt #修改版权文件eula=false 改为true ``` - 运行游戏 [Open Arena](http://www.openarena.ws) (很暴力) - 无线电发射器 安装pifm库,需要在GPIO引脚4上面接根短导线当天线 ```shell mkdir pifm cd pifm wget http://www.icrobotics.co.uk/wiki/imagies/c/c3/Pifm.tar.gz tar -xzf Pifm.tar.gz sudo ./pifm sound.wav 103.0 (待播放文件和频率) ``` - 图像处理 GIMP sudo apt-get install gimp - 收听互联网广播 vlc - gps模块 gpsd 和gpsd-clients ,命令,cgps -s,xgps - 与arduino通讯可以采用pyserial串口或者给arduino 安装 PyFirmata固件 # Linux命令参考 - 查看ip地址 ifconfig (书中这个命令已经过期了) ip a - 在系统启动时自动执行脚本 修改 /etc/rc.local 在第一行添加例如 /user/bin python /home/pi/my_program.py & ** & 符号用于指示后台运行,否则将无法进入系统,非常重要** - 让脚本自动运行 /etc/init.d/my_server(my_server为自定义的脚本文件) sudo chmod +x /etc/init.d/my_server (为文件添加运行权限) - 定期自动运行脚本 crontab -e - 搜索功能 find /home/pi (启动目录) -name abc.py - 查看历史记录命令history !x(执行历史记录中第x条) - 列出usb设备 lsusb - 将命令输出到文本 ls ->abc.txt ->为重定向 - 连接文件 cat a.txt b.txt->c.txt - 管道与正则表达式 ls *.py | grep Jun - 隐藏输出 ls -> /dev/null - 后台运行 abc.py &, 回到前台 fg - 别名 alias l='ls -a' - 使文件立即生效 source .bashrc - 修改时间 sudo date 010203042031(MMDDHHMMYYYY) - 存储空间 df -h #Python机器视觉 - 安装SimpleCV
sudo apt-get update sudo apt-get install ipython python-opencv python-scipy sudo apt-get install python-numpy python-setuptools python-pip sudo pip install svgwrite sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master 运行simpleCV
- 使用python和树莓派摄像头的程序示例
import picamera from SimpleCV import * def get_camera_image(): with picamera.PiCamera() as camera: camera.resolution=(1024,768) camera.awb_mode='off' camera.capture('tmp.jpg') return Image('tmp.jpg')
- python的[picamera](http://picamera.readthedocs.org)文档资料 - 边缘检测,边缘和方法 canny边缘,thresh确定边缘所需长度,distance圆形之间的距离
SimpleCV:1>c=Camera() SimpleCV:2>i=c.getImage() SimpleCV:3>i.show() SimpleCV:4>i2=i.invert() SimpleCV:5>i2.show() SimpleCV:6>coins=i2.findCircle(canny=100,thresh=70,distance=15) SimpleCV:7>coins SimpleCV:8>coins.draw(width=4) SimpleCV:9>coins.show()
from SimpleCV import * c=Camera() while True: i=c.getImage().invert() coins=i.findCircle(canny=100,thresh=70,distance=1) print(len(coins))
- 人脸检测 findHaarFeatures [haar特征参考资料](https://github.com/Itseez/opencv/tree/master/data/haarcascades) - 运动检测blob detection,findBlobs - 光学字符识别OCR,安装tesseract - 利用尽可能少的引脚控制LED,可以使用[Charlieplexing技术](https://en.wikipedia.org/wiki/Charlieplexing) # python 控制树莓派硬件 - 开闭LED,首先需要python有超级用户权限以调用RPi.GPIO库,sudo python led_blink.py才能运行
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18,GPIO.OUT) try: while(True): GPIO.output(18,True) time.sleep(0.5) GPIO.output(18,False) time.sleep(0.5) finally: GPIO.cleanup() #安全模式,使GPIO端口处于输入状态以避免短路
- **PWM输出 **
import RPi.GPIO as GPIO led_pin=18 GPIO.setmode(GPIO.BCM) GPIO.setup(led_pin,GPIO.OUT) pwm_led=GPIO.PWM(led_pin,500) pwm_led.start(100) while True: duty_s=input("Brightness input (0-100):") duty=int(duty_s) pwm_led.ChangeDutyCycle(duty)
- ** 带有GUI的PWM控制程序**
from tkinter import * import RPi.GPIO as GPIO import time led_pin=18 GPIO.setmode(GPIO.BCM) GPIO.setup(led_pin,GPIO.OUT) class App: def __init__(self,master): frame=Frame(master) frame.pack() self.check_var=BooleanVar() check=Checkbutton(frame,text='Pin 18', command=self.update,variable=self.check_var, onvalue=True,offvalue=False) check.grid(row=1) def update(self): GPIO.output(led_pin,self.check_var.get()) root=Tk() root.wm_title('On/Off Switch') app=App(root) root.geometry("200x50+0+0") root.mainloop()
- ** PWM输出**
import RPi.GPIO as GPIO import time led_pin=18 GPIO.setmode(GPIO.BCM) #回调函数 def my_callback(channel): print('You pressed the button') GPIO.setup(led_pin,GPIO.IN,pull_up_down=GPIO.PUD_UP) #bounce time是GPIO库内置的防抖功能 GPIO.add_event_detect(led_pin,GPIO.FALLING,callback=my_callback,bouncetime=100) i=0 while True: i=i+1 print(i) time.sleep(1)
- 使用[RaspiRobot](http://www.raspirobot.com)控制双极步进电机
from rrb3 import * import time rr=RRB3(12.0,12.0) # battery,motor try: while True: delay=input("Delay milliseconds between steps:") steps=input("How many steps forward?") rr.step_forward(int(delay)/1000.0,int(steps)) steps=input("How many steps backward?") rr.step_reverse(int(delay)/1000.0,int(steps)) finally: GPIO.cleanup()
- 使用pygame检测键盘和鼠标移动
import pygame import sys from pygame.locals import * pygame.init() screen=pygame.display.set_mode((640,480)) pygame.mouse.set_visible(0) while True: for event in pygame.event.get(): if event.type==QUIT: sys.exit() if event.type==KEYDOWN: if chr(event.key)=='q': sys.exit() else: print("Code: "+str(event.key)+ " Char: "+chr(event.key)+"\n") if event.type==MOUSEMOTION: print("Mouse:(%d,%d)"%event.pos)
# 物联网与程序示例 - [IFTTT](http://www.ifttt.com)用于建立自动发布的流程网站 - [ThingSpeak](https://thingspeak.com)用于发送twitter - [一些示例程序](http://www.raspberrypicookbook.com)
回应 2017-11-09 11:39:28
-
魏智勇 (www.weizhiyong.com)
# 软件与工具资源 - [操作系统官方下载地址:]([http://www.raspberrypi.org/dowloads]) - windows下的SD卡格式化软件 SD Formatter - [wondows下写入操作系统镜像软件]([https://sourceforge.net/projects/win32diskimager/files/latest/download]) - 远程桌面访问VNC 树莓派安装 tightvncserver,电脑安装[RealVNC]([http://www.realvnc.com]) - 远程桌面访问 RDP(比VNC效果好) 树莓派安装xrdp,windows使用 mstsc命令 - 远程...2017-11-09 11:39:28 3人喜欢
# 软件与工具资源 - [操作系统官方下载地址:](http://www.raspberrypi.org/dowloads) - windows下的SD卡格式化软件 SD Formatter - [wondows下写入操作系统镜像软件](https://sourceforge.net/projects/win32diskimager/files/latest/download) - 远程桌面访问VNC 树莓派安装 tightvncserver,电脑安装[RealVNC](http://www.realvnc.com) - 远程桌面访问 RDP(比VNC效果好) 树莓派安装xrdp,windows使用 mstsc命令 - 远程通过SSH方式访问树莓派,windows下可用的软件,putty和[XSHELL](https://www.netsarang.com/products/xsh_overview.html) - Windows下开源且好用的解压软件[Bandizip](https://www.bandisoft.com/bandizip/) - 开源的FTP服务器架设以及客户端软件[FileZilla](https://filezilla-project.org/) - pypi国内镜像douban,使用方式在pip命令后加后缀,例如 pip install pygame -i https://pypi.douban.com/simple # 树莓派操作使用 - 配置文件 /boot/config.txt - ip地址配置文件 /etc/network/interfaces - DHCP配置文件(如果采用静态ip,需要使用下列命令清除) sudo rm /var/lib/dhcp/* - 网络名称配置文件 /etc/hosts - 配置命令 sudo raspi-config - 使用摄像头拍照 rapistill -o xxx.jpg - 使用摄像头录像(ms) raspivid -o video.h264 -t 10000 - 将树莓派作为网络存储服务器 sudo apt-get install samba sudo apt-get install samba-common-bin 为samba添加用户 sudo smbpasswd -a xxx 修改 /etc/samba/smb.conf 第一行的工作组 最下方添加 ```C [USB] path=/media/NAS comment= NAS driver valid users=xxx writeable=yes browseable=yes create mask=0777 public=yes ``` samba restart - 网络打印服务器 [CUPS](http://www.cups.org) sudo apt-get install cups sudo usermod -a -G lpadmin xxx //增加一个浏览器用于界面管理,使用631端口 sudo apt-get install iceweasel [管理界面](http://localhost:631) - 屏幕截图 scrot sudo apt-get install scrot - 延时截图 scrot -d 5 - 鼠标截屏 scrot -s - I2C通讯工具 sudo apt-get install i2c-tools sudo i2cdetect -y 1//如果是旧版本的话把1改为0 - 串口检测工具 minicom sudo apt-get install minicom minicom -b 9600 -o -D /dev/ttyAMA0 首先要启用local echo功能才能看到反馈,按Ctrl+A,Z,E # 树莓派硬件 - 安全规范 - GPIO引脚上的电压不能超过3.3V - 每个输出电流不超过3mA - 电源不高于5V - 5V供电电源输出总电流不超过250mA - 最简单的电压转换办法,利用470欧和220欧电阻串联分压,也可以按比例放大到47k,22k以降低电流损耗 -GPIO输出能力汇总[参考文章](http://bit.ly/laMQ0fh) - 树莓派配件 - [Raspberry Squid](https://github.com/simmonmonk/squid),内置了电阻的LED灯和按钮配件。有pyton squid库 - 可以采用电机驱动装置RasPiBobot Board 为树莓派供电,电机驱动板python库raspirobotboard - 传感器接口板[SenseHAT],python库为sense-hat或者SenseHAT pro, explorerhat - [计算模块](https://www.raspberrypi.org/blog/raspberry-pi-compute-module-new-product/) - Pi Plate原型版用于焊接元件以快速开发 - 自行设计符合HAT标准的原型板,在config文件中启用 dtparam=i2cvc=on,[烧写EEPROM工具](https://github.com/raspberrypi/hats.git) - [树莓派HAT标准硬件设计指南](https://github.com/raspberrypi/hats) - 开关交流电配件 PowerSwitch Tail II(光隔加继电器模块) - 精确控制伺服电机,可以使用[Servo Blaster库](https://github.com/richardghirst/PiBits/tree/master/ServoBlaster)。 - 同时控制多台伺服电机,可以使用Adafruit的伺服电机HAT(hardware at top),[Adafruit文档](http://bit.ly/liomEey) - 控制直流电机,可以通过L293D类似芯片搭建H桥,或者使用[SparkFun](http://bit.ly/ILHVkJ)的H桥模块。 - 控制步进电机可以使用[RaspiRobot](http://www.raspirobot.com) - 监测移动可以使用被动红外运动检测模块PIR - 测量电阻值可以通过连接电容通过测量电容充电时间的方法来实现 - 树莓派没有内置模拟量输入,采集模拟量可以通过[MCP3008](http://bit.ly/I1lSqn)类似的ADC模块实现 - 测量温度可以采用不精确的[TMP36](http://bit.ly/lb3MfIm)或者高精度的数字温度传感器[DS18B20](http://bit.ly/l7EveOI) - 测量温湿度与气压的[Sense HAT](https://pythonhosted.org/sense-hat/api/) - aLaMode板(带GPIO接口的Arduino平台),由于aLaMode设计缺陷,需要在树莓派的以太网口缠一点绝缘胶带处理。 - [树莓派控制硬件视频](http://razzpisampler.oreilly.com) # 树莓派快速使用 - 媒体中心,安装OpenELEC_Pi2系统,使用[kodi](http://kodi.wiki/) - Office办公软件 abiword gnumeric - 浏览器 chromium-browser - 网络摄像头服务器,安装motion - 配置 ```shell sudo vim /etc/motion/motion.conf #将 daemon off改为on #webcam_localhost=on改为off sudo vim /etc/default/motion #将sart_motion_daemon=no 改为yes ``` - 启动 service motion start - 运行老式模拟器游戏 安装 stella - 安装JDK,运行minecraft(在树莓派3中该软件已经安装了) ```shell sudo apt-get install openjdk7-jkd(版本可能需要更新到8) wget http://getspigot.org/spigot18/spigot_server.jar java -Xms512M -Xmx800M -jar spigot_server.jar nogui #sudo vim eula.txt #修改版权文件eula=false 改为true ``` - 运行游戏 [Open Arena](http://www.openarena.ws) (很暴力) - 无线电发射器 安装pifm库,需要在GPIO引脚4上面接根短导线当天线 ```shell mkdir pifm cd pifm wget http://www.icrobotics.co.uk/wiki/imagies/c/c3/Pifm.tar.gz tar -xzf Pifm.tar.gz sudo ./pifm sound.wav 103.0 (待播放文件和频率) ``` - 图像处理 GIMP sudo apt-get install gimp - 收听互联网广播 vlc - gps模块 gpsd 和gpsd-clients ,命令,cgps -s,xgps - 与arduino通讯可以采用pyserial串口或者给arduino 安装 PyFirmata固件 # Linux命令参考 - 查看ip地址 ifconfig (书中这个命令已经过期了) ip a - 在系统启动时自动执行脚本 修改 /etc/rc.local 在第一行添加例如 /user/bin python /home/pi/my_program.py & ** & 符号用于指示后台运行,否则将无法进入系统,非常重要** - 让脚本自动运行 /etc/init.d/my_server(my_server为自定义的脚本文件) sudo chmod +x /etc/init.d/my_server (为文件添加运行权限) - 定期自动运行脚本 crontab -e - 搜索功能 find /home/pi (启动目录) -name abc.py - 查看历史记录命令history !x(执行历史记录中第x条) - 列出usb设备 lsusb - 将命令输出到文本 ls ->abc.txt ->为重定向 - 连接文件 cat a.txt b.txt->c.txt - 管道与正则表达式 ls *.py | grep Jun - 隐藏输出 ls -> /dev/null - 后台运行 abc.py &, 回到前台 fg - 别名 alias l='ls -a' - 使文件立即生效 source .bashrc - 修改时间 sudo date 010203042031(MMDDHHMMYYYY) - 存储空间 df -h #Python机器视觉 - 安装SimpleCV
sudo apt-get update sudo apt-get install ipython python-opencv python-scipy sudo apt-get install python-numpy python-setuptools python-pip sudo pip install svgwrite sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master 运行simpleCV
- 使用python和树莓派摄像头的程序示例
import picamera from SimpleCV import * def get_camera_image(): with picamera.PiCamera() as camera: camera.resolution=(1024,768) camera.awb_mode='off' camera.capture('tmp.jpg') return Image('tmp.jpg')
- python的[picamera](http://picamera.readthedocs.org)文档资料 - 边缘检测,边缘和方法 canny边缘,thresh确定边缘所需长度,distance圆形之间的距离
SimpleCV:1>c=Camera() SimpleCV:2>i=c.getImage() SimpleCV:3>i.show() SimpleCV:4>i2=i.invert() SimpleCV:5>i2.show() SimpleCV:6>coins=i2.findCircle(canny=100,thresh=70,distance=15) SimpleCV:7>coins SimpleCV:8>coins.draw(width=4) SimpleCV:9>coins.show()
from SimpleCV import * c=Camera() while True: i=c.getImage().invert() coins=i.findCircle(canny=100,thresh=70,distance=1) print(len(coins))
- 人脸检测 findHaarFeatures [haar特征参考资料](https://github.com/Itseez/opencv/tree/master/data/haarcascades) - 运动检测blob detection,findBlobs - 光学字符识别OCR,安装tesseract - 利用尽可能少的引脚控制LED,可以使用[Charlieplexing技术](https://en.wikipedia.org/wiki/Charlieplexing) # python 控制树莓派硬件 - 开闭LED,首先需要python有超级用户权限以调用RPi.GPIO库,sudo python led_blink.py才能运行
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18,GPIO.OUT) try: while(True): GPIO.output(18,True) time.sleep(0.5) GPIO.output(18,False) time.sleep(0.5) finally: GPIO.cleanup() #安全模式,使GPIO端口处于输入状态以避免短路
- **PWM输出 **
import RPi.GPIO as GPIO led_pin=18 GPIO.setmode(GPIO.BCM) GPIO.setup(led_pin,GPIO.OUT) pwm_led=GPIO.PWM(led_pin,500) pwm_led.start(100) while True: duty_s=input("Brightness input (0-100):") duty=int(duty_s) pwm_led.ChangeDutyCycle(duty)
- ** 带有GUI的PWM控制程序**
from tkinter import * import RPi.GPIO as GPIO import time led_pin=18 GPIO.setmode(GPIO.BCM) GPIO.setup(led_pin,GPIO.OUT) class App: def __init__(self,master): frame=Frame(master) frame.pack() self.check_var=BooleanVar() check=Checkbutton(frame,text='Pin 18', command=self.update,variable=self.check_var, onvalue=True,offvalue=False) check.grid(row=1) def update(self): GPIO.output(led_pin,self.check_var.get()) root=Tk() root.wm_title('On/Off Switch') app=App(root) root.geometry("200x50+0+0") root.mainloop()
- ** PWM输出**
import RPi.GPIO as GPIO import time led_pin=18 GPIO.setmode(GPIO.BCM) #回调函数 def my_callback(channel): print('You pressed the button') GPIO.setup(led_pin,GPIO.IN,pull_up_down=GPIO.PUD_UP) #bounce time是GPIO库内置的防抖功能 GPIO.add_event_detect(led_pin,GPIO.FALLING,callback=my_callback,bouncetime=100) i=0 while True: i=i+1 print(i) time.sleep(1)
- 使用[RaspiRobot](http://www.raspirobot.com)控制双极步进电机
from rrb3 import * import time rr=RRB3(12.0,12.0) # battery,motor try: while True: delay=input("Delay milliseconds between steps:") steps=input("How many steps forward?") rr.step_forward(int(delay)/1000.0,int(steps)) steps=input("How many steps backward?") rr.step_reverse(int(delay)/1000.0,int(steps)) finally: GPIO.cleanup()
- 使用pygame检测键盘和鼠标移动
import pygame import sys from pygame.locals import * pygame.init() screen=pygame.display.set_mode((640,480)) pygame.mouse.set_visible(0) while True: for event in pygame.event.get(): if event.type==QUIT: sys.exit() if event.type==KEYDOWN: if chr(event.key)=='q': sys.exit() else: print("Code: "+str(event.key)+ " Char: "+chr(event.key)+"\n") if event.type==MOUSEMOTION: print("Mouse:(%d,%d)"%event.pos)
# 物联网与程序示例 - [IFTTT](http://www.ifttt.com)用于建立自动发布的流程网站 - [ThingSpeak](https://thingspeak.com)用于发送twitter - [一些示例程序](http://www.raspberrypicookbook.com)
回应 2017-11-09 11:39:28
-
魏智勇 (www.weizhiyong.com)
# 软件与工具资源 - [操作系统官方下载地址:]([http://www.raspberrypi.org/dowloads]) - windows下的SD卡格式化软件 SD Formatter - [wondows下写入操作系统镜像软件]([https://sourceforge.net/projects/win32diskimager/files/latest/download]) - 远程桌面访问VNC 树莓派安装 tightvncserver,电脑安装[RealVNC]([http://www.realvnc.com]) - 远程桌面访问 RDP(比VNC效果好) 树莓派安装xrdp,windows使用 mstsc命令 - 远程...2017-11-09 11:39:28 3人喜欢
# 软件与工具资源 - [操作系统官方下载地址:](http://www.raspberrypi.org/dowloads) - windows下的SD卡格式化软件 SD Formatter - [wondows下写入操作系统镜像软件](https://sourceforge.net/projects/win32diskimager/files/latest/download) - 远程桌面访问VNC 树莓派安装 tightvncserver,电脑安装[RealVNC](http://www.realvnc.com) - 远程桌面访问 RDP(比VNC效果好) 树莓派安装xrdp,windows使用 mstsc命令 - 远程通过SSH方式访问树莓派,windows下可用的软件,putty和[XSHELL](https://www.netsarang.com/products/xsh_overview.html) - Windows下开源且好用的解压软件[Bandizip](https://www.bandisoft.com/bandizip/) - 开源的FTP服务器架设以及客户端软件[FileZilla](https://filezilla-project.org/) - pypi国内镜像douban,使用方式在pip命令后加后缀,例如 pip install pygame -i https://pypi.douban.com/simple # 树莓派操作使用 - 配置文件 /boot/config.txt - ip地址配置文件 /etc/network/interfaces - DHCP配置文件(如果采用静态ip,需要使用下列命令清除) sudo rm /var/lib/dhcp/* - 网络名称配置文件 /etc/hosts - 配置命令 sudo raspi-config - 使用摄像头拍照 rapistill -o xxx.jpg - 使用摄像头录像(ms) raspivid -o video.h264 -t 10000 - 将树莓派作为网络存储服务器 sudo apt-get install samba sudo apt-get install samba-common-bin 为samba添加用户 sudo smbpasswd -a xxx 修改 /etc/samba/smb.conf 第一行的工作组 最下方添加 ```C [USB] path=/media/NAS comment= NAS driver valid users=xxx writeable=yes browseable=yes create mask=0777 public=yes ``` samba restart - 网络打印服务器 [CUPS](http://www.cups.org) sudo apt-get install cups sudo usermod -a -G lpadmin xxx //增加一个浏览器用于界面管理,使用631端口 sudo apt-get install iceweasel [管理界面](http://localhost:631) - 屏幕截图 scrot sudo apt-get install scrot - 延时截图 scrot -d 5 - 鼠标截屏 scrot -s - I2C通讯工具 sudo apt-get install i2c-tools sudo i2cdetect -y 1//如果是旧版本的话把1改为0 - 串口检测工具 minicom sudo apt-get install minicom minicom -b 9600 -o -D /dev/ttyAMA0 首先要启用local echo功能才能看到反馈,按Ctrl+A,Z,E # 树莓派硬件 - 安全规范 - GPIO引脚上的电压不能超过3.3V - 每个输出电流不超过3mA - 电源不高于5V - 5V供电电源输出总电流不超过250mA - 最简单的电压转换办法,利用470欧和220欧电阻串联分压,也可以按比例放大到47k,22k以降低电流损耗 -GPIO输出能力汇总[参考文章](http://bit.ly/laMQ0fh) - 树莓派配件 - [Raspberry Squid](https://github.com/simmonmonk/squid),内置了电阻的LED灯和按钮配件。有pyton squid库 - 可以采用电机驱动装置RasPiBobot Board 为树莓派供电,电机驱动板python库raspirobotboard - 传感器接口板[SenseHAT],python库为sense-hat或者SenseHAT pro, explorerhat - [计算模块](https://www.raspberrypi.org/blog/raspberry-pi-compute-module-new-product/) - Pi Plate原型版用于焊接元件以快速开发 - 自行设计符合HAT标准的原型板,在config文件中启用 dtparam=i2cvc=on,[烧写EEPROM工具](https://github.com/raspberrypi/hats.git) - [树莓派HAT标准硬件设计指南](https://github.com/raspberrypi/hats) - 开关交流电配件 PowerSwitch Tail II(光隔加继电器模块) - 精确控制伺服电机,可以使用[Servo Blaster库](https://github.com/richardghirst/PiBits/tree/master/ServoBlaster)。 - 同时控制多台伺服电机,可以使用Adafruit的伺服电机HAT(hardware at top),[Adafruit文档](http://bit.ly/liomEey) - 控制直流电机,可以通过L293D类似芯片搭建H桥,或者使用[SparkFun](http://bit.ly/ILHVkJ)的H桥模块。 - 控制步进电机可以使用[RaspiRobot](http://www.raspirobot.com) - 监测移动可以使用被动红外运动检测模块PIR - 测量电阻值可以通过连接电容通过测量电容充电时间的方法来实现 - 树莓派没有内置模拟量输入,采集模拟量可以通过[MCP3008](http://bit.ly/I1lSqn)类似的ADC模块实现 - 测量温度可以采用不精确的[TMP36](http://bit.ly/lb3MfIm)或者高精度的数字温度传感器[DS18B20](http://bit.ly/l7EveOI) - 测量温湿度与气压的[Sense HAT](https://pythonhosted.org/sense-hat/api/) - aLaMode板(带GPIO接口的Arduino平台),由于aLaMode设计缺陷,需要在树莓派的以太网口缠一点绝缘胶带处理。 - [树莓派控制硬件视频](http://razzpisampler.oreilly.com) # 树莓派快速使用 - 媒体中心,安装OpenELEC_Pi2系统,使用[kodi](http://kodi.wiki/) - Office办公软件 abiword gnumeric - 浏览器 chromium-browser - 网络摄像头服务器,安装motion - 配置 ```shell sudo vim /etc/motion/motion.conf #将 daemon off改为on #webcam_localhost=on改为off sudo vim /etc/default/motion #将sart_motion_daemon=no 改为yes ``` - 启动 service motion start - 运行老式模拟器游戏 安装 stella - 安装JDK,运行minecraft(在树莓派3中该软件已经安装了) ```shell sudo apt-get install openjdk7-jkd(版本可能需要更新到8) wget http://getspigot.org/spigot18/spigot_server.jar java -Xms512M -Xmx800M -jar spigot_server.jar nogui #sudo vim eula.txt #修改版权文件eula=false 改为true ``` - 运行游戏 [Open Arena](http://www.openarena.ws) (很暴力) - 无线电发射器 安装pifm库,需要在GPIO引脚4上面接根短导线当天线 ```shell mkdir pifm cd pifm wget http://www.icrobotics.co.uk/wiki/imagies/c/c3/Pifm.tar.gz tar -xzf Pifm.tar.gz sudo ./pifm sound.wav 103.0 (待播放文件和频率) ``` - 图像处理 GIMP sudo apt-get install gimp - 收听互联网广播 vlc - gps模块 gpsd 和gpsd-clients ,命令,cgps -s,xgps - 与arduino通讯可以采用pyserial串口或者给arduino 安装 PyFirmata固件 # Linux命令参考 - 查看ip地址 ifconfig (书中这个命令已经过期了) ip a - 在系统启动时自动执行脚本 修改 /etc/rc.local 在第一行添加例如 /user/bin python /home/pi/my_program.py & ** & 符号用于指示后台运行,否则将无法进入系统,非常重要** - 让脚本自动运行 /etc/init.d/my_server(my_server为自定义的脚本文件) sudo chmod +x /etc/init.d/my_server (为文件添加运行权限) - 定期自动运行脚本 crontab -e - 搜索功能 find /home/pi (启动目录) -name abc.py - 查看历史记录命令history !x(执行历史记录中第x条) - 列出usb设备 lsusb - 将命令输出到文本 ls ->abc.txt ->为重定向 - 连接文件 cat a.txt b.txt->c.txt - 管道与正则表达式 ls *.py | grep Jun - 隐藏输出 ls -> /dev/null - 后台运行 abc.py &, 回到前台 fg - 别名 alias l='ls -a' - 使文件立即生效 source .bashrc - 修改时间 sudo date 010203042031(MMDDHHMMYYYY) - 存储空间 df -h #Python机器视觉 - 安装SimpleCV
sudo apt-get update sudo apt-get install ipython python-opencv python-scipy sudo apt-get install python-numpy python-setuptools python-pip sudo pip install svgwrite sudo pip install https://github.com/sightmachine/SimpleCV/zipball/master 运行simpleCV
- 使用python和树莓派摄像头的程序示例
import picamera from SimpleCV import * def get_camera_image(): with picamera.PiCamera() as camera: camera.resolution=(1024,768) camera.awb_mode='off' camera.capture('tmp.jpg') return Image('tmp.jpg')
- python的[picamera](http://picamera.readthedocs.org)文档资料 - 边缘检测,边缘和方法 canny边缘,thresh确定边缘所需长度,distance圆形之间的距离
SimpleCV:1>c=Camera() SimpleCV:2>i=c.getImage() SimpleCV:3>i.show() SimpleCV:4>i2=i.invert() SimpleCV:5>i2.show() SimpleCV:6>coins=i2.findCircle(canny=100,thresh=70,distance=15) SimpleCV:7>coins SimpleCV:8>coins.draw(width=4) SimpleCV:9>coins.show()
from SimpleCV import * c=Camera() while True: i=c.getImage().invert() coins=i.findCircle(canny=100,thresh=70,distance=1) print(len(coins))
- 人脸检测 findHaarFeatures [haar特征参考资料](https://github.com/Itseez/opencv/tree/master/data/haarcascades) - 运动检测blob detection,findBlobs - 光学字符识别OCR,安装tesseract - 利用尽可能少的引脚控制LED,可以使用[Charlieplexing技术](https://en.wikipedia.org/wiki/Charlieplexing) # python 控制树莓派硬件 - 开闭LED,首先需要python有超级用户权限以调用RPi.GPIO库,sudo python led_blink.py才能运行
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18,GPIO.OUT) try: while(True): GPIO.output(18,True) time.sleep(0.5) GPIO.output(18,False) time.sleep(0.5) finally: GPIO.cleanup() #安全模式,使GPIO端口处于输入状态以避免短路
- **PWM输出 **
import RPi.GPIO as GPIO led_pin=18 GPIO.setmode(GPIO.BCM) GPIO.setup(led_pin,GPIO.OUT) pwm_led=GPIO.PWM(led_pin,500) pwm_led.start(100) while True: duty_s=input("Brightness input (0-100):") duty=int(duty_s) pwm_led.ChangeDutyCycle(duty)
- ** 带有GUI的PWM控制程序**
from tkinter import * import RPi.GPIO as GPIO import time led_pin=18 GPIO.setmode(GPIO.BCM) GPIO.setup(led_pin,GPIO.OUT) class App: def __init__(self,master): frame=Frame(master) frame.pack() self.check_var=BooleanVar() check=Checkbutton(frame,text='Pin 18', command=self.update,variable=self.check_var, onvalue=True,offvalue=False) check.grid(row=1) def update(self): GPIO.output(led_pin,self.check_var.get()) root=Tk() root.wm_title('On/Off Switch') app=App(root) root.geometry("200x50+0+0") root.mainloop()
- ** PWM输出**
import RPi.GPIO as GPIO import time led_pin=18 GPIO.setmode(GPIO.BCM) #回调函数 def my_callback(channel): print('You pressed the button') GPIO.setup(led_pin,GPIO.IN,pull_up_down=GPIO.PUD_UP) #bounce time是GPIO库内置的防抖功能 GPIO.add_event_detect(led_pin,GPIO.FALLING,callback=my_callback,bouncetime=100) i=0 while True: i=i+1 print(i) time.sleep(1)
- 使用[RaspiRobot](http://www.raspirobot.com)控制双极步进电机
from rrb3 import * import time rr=RRB3(12.0,12.0) # battery,motor try: while True: delay=input("Delay milliseconds between steps:") steps=input("How many steps forward?") rr.step_forward(int(delay)/1000.0,int(steps)) steps=input("How many steps backward?") rr.step_reverse(int(delay)/1000.0,int(steps)) finally: GPIO.cleanup()
- 使用pygame检测键盘和鼠标移动
import pygame import sys from pygame.locals import * pygame.init() screen=pygame.display.set_mode((640,480)) pygame.mouse.set_visible(0) while True: for event in pygame.event.get(): if event.type==QUIT: sys.exit() if event.type==KEYDOWN: if chr(event.key)=='q': sys.exit() else: print("Code: "+str(event.key)+ " Char: "+chr(event.key)+"\n") if event.type==MOUSEMOTION: print("Mouse:(%d,%d)"%event.pos)
# 物联网与程序示例 - [IFTTT](http://www.ifttt.com)用于建立自动发布的流程网站 - [ThingSpeak](https://thingspeak.com)用于发送twitter - [一些示例程序](http://www.raspberrypicookbook.com)
回应 2017-11-09 11:39:28
0 有用 注销倒计时 2017-12-22 20:12:03
良心
0 有用 archer 2017-11-23 21:15:40
还不错
0 有用 qdsxinyee 2020-04-19 15:36:17
这本书的定位应该是那些完全没有计算机开发概念的人看。
0 有用 天行 2018-04-25 11:08:28
2018-04-25读完想读或者能读的chap1-8部分。
0 有用 萬軍 2020-02-16 15:26:02
太过详尽了,没学过计算机都可以玩
0 有用 小白控股 2021-05-25 21:05:30
并无帮助
0 有用 qdsxinyee 2020-04-19 15:36:17
这本书的定位应该是那些完全没有计算机开发概念的人看。
0 有用 萬軍 2020-02-16 15:26:02
太过详尽了,没学过计算机都可以玩
1 有用 YuZ 2019-10-24 18:29:47
入门不错
0 有用 六个馒头Yo 2019-07-10 16:39:34
大略读了一下。介绍树莓派比较多,只有很少的部分介绍系统和Python,大多数都是与各种传感器、Arduino的搭配 需要一些物理基础,电流、电压、I2C、电机,我还是放弃吧