树莓派4B 无屏幕仅靠网线安装调试


1.配置无线上网用户名和密码,sd卡插到Windows电脑上,在boot盘中新建wpa_supplicant.conf文件,内容如下

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
 
network={
ssid="WiFi-A"         #WIFI账号,最好不用出现中文
psk="12345678"    #WIFI密码
key_mgmt=WPA-PSK
}

2.开启SSH连接,sd卡插到Windows电脑上,在boot盘中新建 SSH文件,无后缀空白文件

3.开机自动发送IP地址到邮箱 send_ip_address_email.py 脚本内容如下,脚本自动化执行前要先验证效果是否可行,查路由器获取IP,用 AdvancedIPScanner 软件扫描局域网获取IP,因为我已忘记路由器密码所以使用的是 AdvancedIPScanner 扫描局域网获取IP:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import smtplib
import traceback
from email.mime.text import MIMEText
from email.header import Header
import os
import time
 
# 第三方 SMTP 服务
mail_host="smtp.163.com"  #设置服务器
mail_user="jack"    #用户名
mail_pass="********"   #口令,非邮箱登录密码,而是开通SMTP服务的密码
 
sender = 'jack@163.com'
receivers = ['1036984571@qq.com']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

# 获取ifconfig命令内容
cmd='ifconfig'
m=os.popen(cmd)
ip_msg=m.read()
m.close()
 
message = MIMEText(ip_msg, 'plain', 'utf-8')
message['From'] = Header("Raspberry", 'utf-8')
message['To'] =  Header("Master", 'utf-8')
 
subject = 'Raspberry Current IP Address'
message['Subject'] = Header(subject, 'utf-8')
    
def mail():
    ret=True
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(mail_user,mail_pass)  
        smtpObj.sendmail(sender, receivers, message.as_string())
        print "Email Send Success !!"
        smtpObj.quit()
    except smtplib.SMTPException:
        traceback.print_exc()
        print "Error: cannot send email !!"
        ret=False
    return ret

while(1):
    ret_msg=mail()
    if ret_msg:
        break
    else:
        time.sleep( 300 )

4.配置开机执行发送IP脚本,修改 /etc/rc.local

在 exit 0 之前添加代码

sleep 30 
if [ -f "/boot/email" ]; then
python /home/pi/Downloads/send_ip_address_email.py
fi

具体如下,这里面不能有报错命令否则可能走不到我们的代码,我遇到过执行不存在的文件报错导致死活看不到发邮件效果,要创建  /boot/email  无后缀空文件开启邮件服务

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sleep 30     #睡眠3秒,用于等待连上网络,没有网络也发送不出邮件
if [ -f "/boot/email" ]; then
  python /home/pi/Downloads/send_ip_address_email.py    #终端执行验证命令是否可行
fi

exit 0

5.输出HDMI信号到电视

修改 /boot/config.txt 确保有这两个配置

disable_overscan=1  

hdmi_drive=2

我的具体配置如下:

# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
disable_overscan=1     #自动适配电视屏幕,能满屏显示

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=4

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
hdmi_drive=2    #确保输出模式中有声音

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
#dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Uncomment this to enable infrared communication.
#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2

[all]
#dtoverlay=vc4-fkms-v3d
start_x=1
gpu_mem=128

#snd_bcm2835.enable_headphones=1
#snd_bcm2835.enable_hdmi=1 
#snd_bcm283.enable_compat_alsa=0

6.仅仅上面可能还无法输出声音,还需配置如下,vnc viewer 连上

sudo raspi-config

 用 tab 键切换到确定然后 enter 即可

 还可以确认一下配置

相关