树莓派从零开始学习记录

树莓派学习从零开始

  • 树莓派系统的安装

    使用官方推荐的TF卡烧写工具 Win32DiskImager写入镜像2016-09-23-raspbian-jessie.img

  • 连接树莓派

    最简单的连接方法是: (此方法需要电脑无线连接至网络)

    1. 使用一根网线连接笔记本(笔记本已连接到无线网)和树莓派
    2. 电脑端安装putty软件
    3. 依次打开网络和共享中心更改适配器设置``无线网络连接属性共享勾选允许其他网络用户通过此计算机的Internet连接来连接---本地连接

      如图:

    4. 电脑端win+R打开cmdshell,输入:arp -a

      Microsoft Windows [版本 6.1.7601]
      版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
      
      C:\Users\Administrator>arp -a
      
      接口: 192.168.137.1 --- 0xc
        Internet 地址         物理地址              类型
        192.168.137.210       b8-xx-xx-xx-xx-eb     动态
        192.168.137.255       ff-ff-ff-ff-ff-ff     静态
        224.0.0.22            01-00-5e-00-00-16     静态
        224.0.0.252           01-00-5e-00-00-fc     静态
        239.255.255.250       01-00-5e-7f-ff-fa     静态
        255.255.255.255       ff-ff-ff-ff-ff-ff     静态
      
      接口: 192.168.0.108 --- 0xd
        Internet 地址         物理地址              类型
        192.168.0.1           c8-3a-35-5e-ef-10     动态
        192.168.0.107         b8-xx-xx-xx-xx-be     动态
        192.168.0.109         8c-89-a5-72-07-5e     动态
        192.168.0.110         40-c6-2a-2c-86-ef     动态
        192.168.0.255         ff-ff-ff-ff-ff-ff     静态
        224.0.0.22            01-00-5e-00-00-16     静态
        224.0.0.252           01-00-5e-00-00-fc     静态
        239.255.255.250       01-00-5e-7f-ff-fa     静态
        255.255.255.255       ff-ff-ff-ff-ff-ff     静态
      
      C:\Users\Administrator>
      

      查看接口“192.168.137.1 —0xc”下的 动态IP
      即:192.168.137.210 ,后面的MAC地址便是树莓派的有线网卡的物理地址

    5. 打开putty输入该动态IP,便可进入树莓派的shell界面

      注意  使用该方法连接树莓派,树莓派在重启或者重连后其动态IP都会改变,每次连接都要重新查看其新的动态IP,建议,首先对树莓派配置无线网络

  • 修改默认账户”pi”的初始密码

    树莓派默认账户是pi,初始密码是raspberry
    修改pi的密码如下

    pi@raspberrypi:/ $ passwd pi
    Changing password for pi.
    (current) UNIX password:
    Enter new UNIX password:
    Retype new UNIX password:
    
  • 解锁root账户、设置root账户密码

    树莓派使用的linux是debian系统,所以树莓派启用root和debian是相同的。
    debian里root账户默认没有密码,但账户锁定。

    当需要root权限时,由默认账户经由sudo执行,Raspberry pi 系统中的
    Raspbian默认用户是pi 密码为raspberry
    重新开启root账号,可由pi用户登录后,在命令行下执行

    pi@raspberrypi:~ $ sudo passwd root
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully
    

    执行此命令后系统会提示输入两遍的root密码,输入你想设的密码即可,然后在执行
    sudo passwd --unlock root

    pi@raspberrypi:~ $ sudo passwd --unlock root
    passwd: password expiry information changed.
    

    这样就可以解锁root账户了。

  • 创建新账号并设置密码

    创建用户johnwick  命令useradd

    root@raspberrypi:/home/pi# useradd johnwick
    

    给新账户设置密码

    root@raspberrypi:/home/pi# passwd johnwick
    Enter new UNIX password:
    
  • 添加新用户到sudo组

    切换到root账户,输入指令visudo

    pi@raspberrypi:~ $ su
    Password:
    root@raspberrypi:/home/pi# visudo
    

    在文档的最后一行添加账户johnwick,配置:执行sudo命令式免密码,保存。

    # This file MUST be edited with the 'visudo' command as root.
    #
    # Please consider adding local content in /etc/sudoers.d/ instead of
    # directly modifying this file.
    #
    # See the man page for details on how to write a sudoers file.
    #
    Defaults        env_reset
    Defaults        mail_badpass
    Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    
    # Host alias specification
    
    # User alias specification
    
    # Cmnd alias specification
    
    # User privilege specification
    root    ALL=(ALL:ALL) ALL
    
    # Allow members of group sudo to execute any command
    %sudo   ALL=(ALL:ALL) ALL
    
    # See sudoers(5) for more information on "#include" directives:
    
    #includedir /etc/sudoers.d
    
    pi ALL=(ALL) NOPASSWD: ALL
    
    johnwick  ALL=(ALL) NOPASSWD: ALL        
    
  • 自定义用户的命令行提示符颜色

    为了方便也为了美观,需要对shell界面的命令提示符的颜色进行配置
    修改root账户下的.bashrc文件
    先找到root账户的家目录

    注意:配置哪个账户就到哪个账户的家目录下修改.bashrc文件,一定不要修改了全局环境量
     比如:给johnwick账户配颜色,就先

    [root@raspberrypi/home/johnwick]#su johnwick
    johnwick@raspberrypi:~ $ cd ~
    johnwick@raspberrypi:~ $ sudo .bashrc
    #把PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h\[\e[36;40m\]\w\[\e[0m\]]\\$"复制到最后一行
    #重新加载`.bashrc`文件
    johnwick@raspberrypi:~ $ source .bashrc
    

    >

    root@raspberrypi:/home# cd ~ 
    再使用`nano`打开存放环境变量的文件`.bashrc`       
    # ~/.bashrc: executed by bash(1) for non-login shells.
    
    # Note: PS1 and umask are already set in /etc/profile. You should not
    # need this unless you want different defaults for root.
    # PS1='${debian_chroot:+($debian_chroot)}\h:\w\$ '
    # umask 022
    
    # You may uncomment the following lines if you want `ls' to be colorized:
    # export LS_OPTIONS='--color=auto'
    # eval "`dircolors`"
    # alias ls='ls $LS_OPTIONS'
    # alias ll='ls $LS_OPTIONS -l'
    # alias l='ls $LS_OPTIONS -lA'
    #
    # Some more alias to avoid making mistakes:
    # alias rm='rm -i'
    # alias cp='cp -i'
    # alias mv='mv -i'
    PS1="\[\e[37;40m\][\[\e[31;40m\]\u\[\e[37;40m\]@\h\[\e[36;40m\]\w\[\e[0m\]]\\$"
    

    >

    在最后面添加上配置好的自己喜欢的字体颜色配置
    配置完成后重新加载.bashrc文件 命令:root@raspberrypi:~# source .bashrc

    我常用的三个账户的颜色配置方案:

        绿色
        PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h\[\e[36;40m\]\w\[\e[0m\]]\\$"
        黄色
        PS1="\[\e[37;40m\][\[\e[33;40m\]\u\[\e[37;40m\]@\h\[\e[36;40m\]\w\[\e[0m\]]\\$"
        红色
        PS1="\[\e[37;40m\][\[\e[31;40m\]\u\[\e[37;40m\]@\h\[\e[36;40m\]\w\[\e[0m\]]\\$"
    

  • 创建get.py文件,运行后显示树莓派的CPU温度RAM、ROM

    创建get.py文件 直接使用命令

    [johnwick@raspberrypi~]$nano get.py
    

    把下面的代码写进get.py文件,并保存,退出。

        import os
    
    # Return CPU temperature as a character string                                     
    def getCPUtemperature():
        res = os.popen('sudo cat /sys/class/thermal/thermal_zone0/temp').readline()
        tempfloat=float(res) / 1000
        temp=str(tempfloat)
        return(temp)
    
    
    # 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))
    

    运行get.py文件

    [johnwick@raspberrypi~]$sudo python get.py
    
    CPU Temperature = 40.242
    CPU Use = 0.1
    
    RAM Total = 947.0 MB
    RAM Used = 225.0 MB
    RAM Free = 722.0 MB
    
    DISK Total Space = 30GB
    DISK Used Space = 3.7GB
    DISK Used Percentage = 13%
    
  • 查看周围的无线网络的ESSID(网络名称)、网络信息、信号强度

    命令: iwlist scan

    [johnwick@raspberrypi~]$sudo iwlist scan
    wlan0     Scan completed :
      Cell 01 - Address: C8:3A:35:5E:EF:10
                Channel:1
                Frequency:2.412 GHz (Channel 1)
                Quality=44/70  Signal level=-66 dBm
                Encryption key:on
                ESSID:"Tenda_5EEF10"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
                          24 Mb/s; 36 Mb/s; 54 Mb/s
                Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s
                Mode:Master
                Extra:tsf=0000000000000000
                Extra: Last beacon: 80ms ago
                IE: Unknown: 000C54656E64615F354545463130
                IE: Unknown: 010882840B162430486C
                IE: Unknown: 030101
                IE: Unknown: 2A0104
                IE: Unknown: 2F0104
                IE: Unknown: 32040C121860
                IE: Unknown: 2D1AFE181BFFFF000001000000000000000000000000000                                                                                        000000000
                IE: Unknown: 3D16010D000000000000000000000000000000000000000                                                                                        0
                IE: Unknown: DD090010180202F02C0000
                IE: WPA Version 1
                    Group Cipher : CCMP
                    Pairwise Ciphers (1) : CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: DD180050F2020101000003A4000027A4000042435E00623                                                                                        22F00
      Cell 02 - Address: C8:3A:35:52:FC:30
                Channel:6
                Frequency:2.437 GHz (Channel 6)
                Quality=35/70  Signal level=-75 dBm
                Encryption key:on
                ESSID:"WXtencent"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
                          24 Mb/s; 36 Mb/s; 54 Mb/s
                Bit Rates:6 Mb/s; 9 Mb/s; 12 Mb/s; 48 Mb/s
                Mode:Master
                Extra:tsf=0000000000000000
                Extra: Last beacon: 80ms ago
                IE: Unknown: 0009575874656E63656E74
                IE: Unknown: 010882840B162430486C
                IE: Unknown: 030106
                IE: Unknown: 2A0100
                IE: Unknown: 2F0100
                IE: IEEE 802.11i/WPA2 Version 1
                    Group Cipher : CCMP
                    Pairwise Ciphers (1) : CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: 32040C121860
                IE: Unknown: 2D1AFE181BFFFF000001000000000000000000000000000                                                                                        000000000
                IE: Unknown: 3D16060D000000000000000000000000000000000000000                                                                                        0
                IE: Unknown: DD090010180203102C0000
                IE: WPA Version 1
                    Group Cipher : CCMP
                    Pairwise Ciphers (1) : CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: DD180050F2020101000003A4000027A4000042435E00623                                                                                        22F00
      Cell 03 - Address: 58:6A:B1:1C:B2:10
                Channel:11
                Frequency:2.462 GHz (Channel 11)
                Quality=26/70  Signal level=-84 dBm
                Encryption key:on
                ESSID:"111111"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                          11 Mb/s; 12 Mb/s; 18 Mb/s
                Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                Mode:Master
                Extra:tsf=0000000000000000
                Extra: Last beacon: 80ms ago
                IE: Unknown: 0006313131313131
                IE: Unknown: 010882848B0C12961824
                IE: Unknown: 03010B
                IE: Unknown: 0706434E49010D14
                IE: Unknown: 2A0100
                IE: IEEE 802.11i/WPA2 Version 1
                    Group Cipher : CCMP
                    Pairwise Ciphers (1) : CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: 32043048606C
                IE: Unknown: 2D1AEC1103FFFF000000000000000000000000000000000                                                                                        000000000
                IE: Unknown: 3D160B08040000000000000000000000000000000000000                                                                                        0
                IE: Unknown: DD180050F2020101810003A4000027A4000042435E00623                                                                                        22F00
                IE: Unknown: DD1E00904C33EC1103FFFF0000000000000000000000000                                                                                        00000000000000000
                IE: Unknown: DD1A00904C340B080400000000000000000000000000000                                                                                        000000000
      Cell 04 - Address: F4:83:CD:53:F0:05
                Channel:1
                Frequency:2.412 GHz (Channel 1)
                Quality=52/70  Signal level=-58 dBm
                Encryption key:on
                ESSID:"TP-LINK_F005"
                Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                          9 Mb/s; 12 Mb/s; 18 Mb/s
                Bit Rates:24 Mb/s; 36 Mb/s; 48 Mb/s; 54 Mb/s
                Mode:Master
                Extra:tsf=0000000000000000
                Extra: Last beacon: 80ms ago
                IE: Unknown: 000C54502D4C494E4B5F46303035
                IE: Unknown: 010882848B960C121824
                IE: Unknown: 030101
                IE: Unknown: 2A0100
                IE: IEEE 802.11i/WPA2 Version 1
                    Group Cipher : CCMP
                    Pairwise Ciphers (1) : CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: 32043048606C
                IE: Unknown: 2D1AEE111BFFFFFF0000000000000000000100000000000                                                                                        000000000
                IE: Unknown: 3D16010D060000000000000000000000000000000000000                                                                                        0
                IE: Unknown: 7F080000000000000040
                IE: WPA Version 1
                    Group Cipher : CCMP
                    Pairwise Ciphers (1) : CCMP
                    Authentication Suites (1) : PSK
                IE: Unknown: DD180050F2020101000003A4000027A4000042435E00623                                                                                        22F00
                IE: Unknown: DD0900037F01010000FF7F
        lo        Interface doesn't support scanning.           
        eth0      Interface doesn't support scanning.
    
  • 配置树莓派无线网络

    树莓派可以存储多个网络的信息,将常用的几个网络的ssid和psk配置好,一并存储即可,这样,当树莓派更换网络后也会自动连接

    # 编辑wifi文件
    sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    # 在该文件最后添加下面的话
    network={
      ssid="WIFINAME"
      psk="password"
    }
    # 引号部分分别为wifi的名字和密码
    # 保存文件后几秒钟应该就会自动连接到该wifi
    # 查看是否连接成功
    ifconfig wlan0
    

    查看树莓派网卡信息(IP地址、有线网卡和无线网卡的MAC地址)

            [johnwick@raspberrypi~]$ifconfig
    eth0      Link encap:Ethernet  HWaddr b8:xx:xx:xx:xx:eb
              inet addr:192.168.137.26  Bcast:192.168.137.255  Mask:255.255.255.0
              inet6 addr: fe80::4152:ce36:8645:2e67/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:2417 errors:0 dropped:0 overruns:0 frame:0
              TX packets:1220 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:193242 (188.7 KiB)  TX bytes:178039 (173.8 KiB)
    
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:264 errors:0 dropped:0 overruns:0 frame:0
              TX packets:264 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1
              RX bytes:21840 (21.3 KiB)  TX bytes:21840 (21.3 KiB)
    
    wlan0     Link encap:Ethernet  HWaddr b8:xx:xx:xx:xx:be
              inet6 addr: fe80::5151:9360:9963:5f17/64 Scope:Link
              UP BROADCAST MULTICAST  MTU:1500  Metric:1
              RX packets:665 errors:0 dropped:665 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:237933 (232.3 KiB)  TX bytes:0 (0.0 B)
    
  • 无法登陆路由器管理界面的情况下如何查看树莓派的无线IP地址

    有时,使用树莓派连接外界网络(知道账号和密码,但不能进入管理界面查看已经连接无线网络的树莓派的无线IP地址),此时,我们可以继续使用之前的方法,使用一根网线连接笔记本,通过动态IP地址ssh到shell然后查看pI的网络信息

    [johnwick@raspberrypi~]$ifconfig wlan0
    wlan0     Link encap:Ethernet  HWaddr b8:xx:xx:xx:xx:be
              inet addr:192.168.0.159  Bcast:192.168.0.255  Mask:255.255.255.0
              inet6 addr: fe80::5151:9360:9963:5f17/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:2096 errors:0 dropped:938 overruns:0 frame:0
              TX packets:227 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:262474 (256.3 KiB)  TX bytes:39358 (38.4 KiB)
    

    使用此方法可以省去网线连接笔记本的不便,但也比不上配置固定IP方便

  • 配置路由器端口映射

    1. 给树莓派绑定静态IP
    2. 设置端口段映射
    3. 选择DDNS服务商,做动态DNS解析

    以上步骤均从路由器端进行设置,设置完成后即可局域网内或者外网访问树莓派主机

  • 设置时区
    [johnwick@raspberrypi~]$sudo dpkg-reconfigure tzdata
    
    Package configuration
    
      ┌─────────────────────────┤ Configuring tzdata ├──────────────────────────┐
      │ Please select the geographic area in which you live. Subsequent         │
      │ configuration questions will narrow this down by presenting a list of   │
      │ cities, representing the time zones in which they are located.          │
      │                                                                         │
      │ Geographic area:                                                        │
      │                                                                         │
      │                          Africa                ↑                        │
      │                          America               ▒                        │
      │                          Antarctica            ▒                        │
      │                          Australia             ▮                        │
      │                          Arctic Ocean          ▒                        │
      │                          Asia                  ▒                        │
      │                          Atlantic Ocean        ▒                        │
      │                          Europe                ↓                        │
      │                                                                         │
      │                                                                         │
      │                   <Ok>                       <Cancel>                   │
      │                                                                         │
      └─────────────────────────────────────────────────────────────────────────┘
    

    选择Asia>>chongqing或者shanghai

  • 10
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值