树莓派GPIO用法示例(Python)

非阻塞用法示例如下:

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

btn_input = 4;
LED_output = 17;

# GPIO btn_input set up as input.
GPIO.setup(btn_input, GPIO.IN)
GPIO.setup(LED_output, GPIO.OUT)

# handle the button event
def buttonEventHandler_rising (pin):
    # turn LED on
    GPIO.output(LED_output,True)
    
def buttonEventHandler_falling (pin):
    # turn LED off
    GPIO.output(LED_output,False)

	
GPIO.add_event_detect(btn_input, GPIO.RISING, callback=buttonEventHandler_rising) 
GPIO.add_event_detect(btn_input, GPIO.FALLING, callback=buttonEventHandler_falling)
 
try:  
    while True : time.sleep(0.1)  
finally:
    GPIO.remove_event_detect(btn_input)
    GPIO.cleanup()

参考链接


树莓派Zero W/WH(Raspberry Pi Zero W/WH) GPIO针脚定义

树莓派Zero W,有两款小型号,一款是Raspberry Pi Zero W,另一款是Raspberry Pi Zero WH,两者的区别是一个出厂的时候没有焊接排针,另一款焊接了排针。WWireless的缩写,WHWireless With Head的缩写。

树莓派Zero W/WH(Raspberry Pi Zero W/WH) GPIO针脚定义如下图:
继续阅读树莓派Zero W/WH(Raspberry Pi Zero W/WH) GPIO针脚定义

Raspberry Pi Zero W配置Wi-Fi AP

最近在配置Raspberry Pi Zero W,使用的系统为2018-06-27-raspbian-stretch-lite,我们的需求是把这台Raspberry Pi Zero W配置为开放Wi-Fi模式的AP

Raspberry Pi Zero W只有一块无线网卡,如果被配置成AP模式不是太好操作,可以通过预留的Macro USB接口外接一个USB有线网卡来实现远程访问,当然也可以直接接入显示器,USB Hub外接鼠标键盘操作。

执行如下脚本配置:

#目前测试发现udhcpd在标准树莓派上能正常工作,但是在Pi Zero W上,重启之后,
#不能正常分配IP,应该是服务在无线网卡没有初始化完成就已经启动导致的,
#我们使用dnsmasq替代后可以正常工作

$ sudo apt-get -y remove udhcpd

$ sudo apt-get -y install hostapd dnsmasq

#备份配置文件
$ sudo cp /etc/dnsmasq.conf /etc/dnsmasq.conf.bak

#配置分配的IP段
$ sudo sed -i "s/^#dhcp-range=192.168.0.50,192.168.0.150,12h/dhcp-range=192.168.0.50,192.168.0.150,12h/g" /etc/dnsmasq.conf

#AP名字
$ export AP_NAME="AP"

#为无线网卡配置静态IP
$ export WLAN_IP=192.168.0.1

$ sudo ifconfig wlan0 $WLAN_IP

$ sudo sed -i '$a\interface wlan0' /etc/dhcpcd.conf

$ echo "static ip_address=${WLAN_IP}/24" | sudo tee -a /etc/dhcpcd.conf

#hostapd配置,配置为开放模式
$ sudo touch /etc/hostapd/hostapd.conf

$ echo "interface=wlan0" | sudo tee -a /etc/hostapd/hostapd.conf

$ echo "ssid=${AP_NAME}" | sudo tee -a /etc/hostapd/hostapd.conf

#WiFi工作的频段 1-13
$ echo "channel=9" | sudo tee -a /etc/hostapd/hostapd.conf

#硬件工作模式 g simply means 2.4GHz
$ echo "hw_mode=g" | sudo tee -a /etc/hostapd/hostapd.conf

#验证方式为开放模式 1=wpa, 2=wep, 3=both
$ echo "auth_algs=1" | sudo tee -a /etc/hostapd/hostapd.conf
    
# 802.11n support	 	 
$ echo "ieee80211n=1" | sudo tee -a /etc/hostapd/hostapd.conf

#备份配置文件
$ sudo cp /etc/default/hostapd /etc/default/hostapd.bak

#修改配置文件
$ sudo sed -i "s/#DAEMON_CONF=\"\"/DAEMON_CONF=\"\/etc\/hostapd\/hostapd.conf\"/g" /etc/default/hostapd

#启动networking和hostapd服务,注意先后顺序,先使用networking服务设置IP,再更新hostapd
$ sudo service networking restart

$ sudo service hostapd restart

$ sudo service dnsmasq restart

#设置开机启动
$ sudo update-rc.d hostapd enable

#重启设备,检查配置是否已经生效
$ sudo reboot

参考链接


树莓派2B启用I2C

树莓派自带I2C控制器,但是默认没有启用,我们需要手工启用,具体操作如下:

I2C is a very commonly used standard designed to allow one chip to talk to another. So, since the Raspberry Pi can talk I2C we can connect it to a variety of I2C capable chips and modules.Here are some of the Adafruit projects that make use of I2C devices and modules:

The I2C bus allows multiple devices to be connected to your Raspberry Pi, each with a unique address, that can often be set by changing jumper settings on the module. It is very useful to be able to see which devices are connected to your Pi as a way of making sure everything is working.

To do this, it is worth running the following commands in the Terminal to install the i2c-tools utility.

$ sudo apt-get install -y python-smbus
$ sudo apt-get install -y i2c-tools

Installing Kernel Support (with Raspi-Config)

继续阅读树莓派2B启用I2C

raspberry pi新系统SSH连接被拒绝的解决方法

将全新的树莓派系统烧录,开机然后用SSH远程连接,结果SSH连接提示“connection refused”,导致连接树莓派失败。出现错误的原因是自2016-11-25官方发布的 Raspbian系统镜像,系统默认禁用了SSH服务。As of the November 2016 release, Raspbian has the SSH server disabled by default.

出错的详细信息为:

ssh: connect to host 192.168.43.220 port 22: Connection refused

官方的解决方案是:

  • SSH disabled by default; can be enabled by creating a file with name "ssh" in boot partition

如果有显示器,开机后,在树莓派配置中将SSH开启即可。但在没有显示器,首次开机需要用SSH登陆的时候,就需要在系统烧录完毕后,进入到boot分区盘,新建一个名为ssh的空白文件就行了。

完成后再将SD卡插回树莓派,就可以正常使用SSH了。

也可以在树莓派系统上执行如下命令:

$ cd /boot

$ sudo touch ssh

$ sudo reboot

注意,最好在系统烧录完成后,立即执行上面的命令。重启过程中,会诱发磁盘检查,因此第一次重启会比较慢。另外,目前发现,如果后期安装了openssh-server再配置,会导致系统崩溃(2018-06-27-raspbian-stretch-lite)。

此文件会在重启完成后被自动删除。

默认的登录用户名pi 默认密码raspberry

参考链接


raspberry pi新系统SSH连接被拒绝的解决方法

树莓派系统升级Raspbian Wheezy到Raspbian Jessie(树莓派2B+)

树莓派官方已经放出了Raspbian 8 Jessie。这是Raspbian Wheezy 2015.5.5之后的一次比较大的版本更新。而老的Raspbian Wheezy版本也可以通过简单的步骤升级到Raspbian 8 Jessie且不用重新刷写镜像。下面将介绍如何升级,要知道,升级有可能会破坏现有系统,在此之前请一定注意备份好老的系统。如果你对之前的系统做的更改越小,升级将会越顺利。

1. 更新当前系统到最新
$ sudo apt-get update

$ sudo apt-get upgrade

$ sudo apt-get dist-upgrade
2. 更新软件下载源
$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

$ sudo sed -i 's/wheezy/jessie/g' /etc/apt/sources.list

$ sudo sed -i 's/#deb http:\/\/mirrordirector/deb http:\/\/mirrordirector/g' /etc/apt/sources.list
3. 升级到Raspbian 8 Jessie
$ sudo apt-get update

$ sudo apt-get upgrade

$ sudo apt-get dist-upgrade

参考链接


树莓派系统升级 Wheezy 到 Raspbian Jessie