树莓派上手记

分类:IOT

树莓派初始配置

sudo raspi-config

连接wifi

wifi: (jessie lite)

  1. 树莓派通过网线连接路由器
  2. SSH登录树莓派,测试无线信号 sudo iwlist wlan0 scan
  3. 配置无线网络 sudo nano /etc/wpa.conf 将下列内容复制进去
    network={
    ssid="WIFI的SSID"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="WIFI的密码"
    }
    

接下来编辑网络配置文件:

sudo nano /etc/network/interfaces

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa.conf  <---增加此行
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf   <---注释掉此行
iface default inet dhcp

保存,退出 重启树莓派,拔掉网线,它将自动连上无线网络。

VNC: 192.168.11.131:5900 设定分辨率: vncserver :1 -geometry 1600x900

查看CPU温度:

通过Python脚本可以方便地查看CPU温度,CPU、内存以及硬盘使用率

建立脚本:

sudo nano get.py

贴入Python代码:

import os

# Return CPU temperature as a character string                                     
def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    return(res.replace("temp=","").replace("'C\n",""))

# Return RAM information (unit=kb) in a list
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
def getRAMinfo():
    p = os.popen('free')
    i = 0
    while 1:
        i = i + 1
        line = p.readline()
        if i==2:
            return(line.split()[1:4])

# Return % of CPU used by user as a character string                               
def getCPUuse():
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))

# Return information about disk space as a list (unit included)                    
# Index 0: total disk space                                                  
# Index 1: used disk space                                                        
# Index 2: remaining disk space                                                    
# Index 3: percentage of disk used                                                 
def getDiskSpace():
    p = os.popen("df -h /")
    i = 0
    while 1:
        i = i +1
        line = p.readline()
        if i==2:
            return(line.split()[1:5])

# CPU informatiom
CPU_temp = getCPUtemperature()
CPU_usage = getCPUuse()

# RAM information
# Output is in kb, here I convert it in Mb for readability
RAM_stats = getRAMinfo()
RAM_total = round(int(RAM_stats[0]) / 1000,1)
RAM_used = round(int(RAM_stats[1]) / 1000,1)
RAM_free = round(int(RAM_stats[2]) / 1000,1)

# Disk information
DISK_stats = getDiskSpace()
DISK_total = DISK_stats[0]
DISK_used = DISK_stats[1]
DISK_perc = DISK_stats[3]

if __name__ == '__main__':
    print('')
    print('CPU Temperature = '+CPU_temp)
    print('CPU Use = '+CPU_usage)
    print('')
    print('RAM Total = '+str(RAM_total)+' MB')
    print('RAM Used = '+str(RAM_used)+' MB')
    print('RAM Free = '+str(RAM_free)+' MB')
    print('')
    print('DISK Total Space = '+str(DISK_total)+'B')
    print('DISK Used Space = '+str(DISK_used)+'B')
    print('DISK Used Percentage = '+str(DISK_perc))

然后执行:

chmod +x get.py
python get.py

```预期输出结果如下:

CPU Temperature = 53.0
CPU Use = 13.5

RAM Total = 497.0 MB
RAM Used = 116.0 MB
RAM Free = 381.0 MB

DISK Total Space = 3.6GB
DISK Used Space = 1.8GB
DISK Used Percentage = 53%

风扇PWM调速

树莓派3B采用基于ARM Cortex-A53的BCM2837 SoC,相比上代性能有了很大的提升,同时发热量也增加不少。冬天还好,夏天则需要散热片+风扇来辅助降温。默认情况下,风扇接到5v针脚后会以最大速率运转,噪音比较大。淘宝上找到一个转接配件,接在GPIO针脚上,然后就可以使用通过Python的GPIO库输出PWM信号,实现根据CPU温度动态控制风扇速度。

$ nano fan.py
#!/usr/bin/env python
# encoding: utf-8
# From:shumeipai.net

import RPi.GPIO
import time

start = 45
stop = 35

RPi.GPIO.setwarnings(False)
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(2, RPi.GPIO.OUT)
pwm = RPi.GPIO.PWM(2, 25) #pwm(channel,frequency)
fan = False
pwm.start(40) #pwm空比,0<=空比<=100

try:
    while True:
        with open('/sys/class/thermal/thermal_zone0/temp') as f:
            cur = int(f.read()) / 1000
            if not fan and cur >= start:
                #CPU温度高于start启动风扇
                pwm.start(60)
                fan = True
            if fan and cur <= stop:
                #CPU温度低于stop时停止风扇
                pwm.stop()
                # pwm.start(30)
                fan = False
        time.sleep(1)
except KeyboardInterrupt:
    pwm.stop()

手动终止进程:

#查找python进程
ps -ef | grep python

杀死pid为1180的进程: kill -s 9 1180

GPIO BCM 2;

#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO
import time

start = 45
stop = 35

RPi.GPIO.setwarnings(False)
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.setup(2, RPi.GPIO.OUT)
pwm = RPi.GPIO.PWM(2, 25) #GPIO channel 2, frequency 25
fan = False
pwm.start(35) #初始空比35(范围0~100)

try:
    while True:
        with open('/sys/class/thermal/thermal_zone0/temp') as f:
            cur = int(f.read()) / 1000
            now = time.strftime("%H:%M:%S",time.localtime(time.time()))
            if not fan and cur >= start:
                pwm.start(80)
                fan = True
                #print("[%s] Fan on @ %s" % (now, cur))
            if fan and cur <= stop:
                #pwm.stop()
                pwm.start(30)
                fan = False
                #print("[%s] Fan off @ %s" % (now, cur))