樹莓派4B 無螢幕僅靠網線安裝除錯
阿新 • • 發佈:2022-04-16
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 Headerimport os import time # 第三方 SMTP 服務 mail_host="smtp.163.com" #設定伺服器 mail_user="jack" #使用者名稱 mail_pass="********" #口令,非郵箱登入密碼,而是開通SMTP服務的密碼 sender = '[email protected]' receivers = ['[email protected]'] # 接收郵件,可設定為你的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 即可
還可以確認一下配置