TA的每日心情 | 开心 2026-1-3 07:12 |
|---|
签到天数: 594 天 连续签到: 1 天 [LV.9]以坛为家II
举人
- 积分
- 528
|
【前言】
前面我测试了一下蓝牙,是没有成功的,今天我重新从镜像包中复制了一份驱动文件到驱动包中,然后蓝牙就正常工作了,现在分享一下读取小米温湿度计的工程。
【原因查找】
我原先工作是正常的,编译了开机加载wifi的脚本,脚本代码:
今天开机,在加载日志查找到有报错信息:
- Found device dev-ttyLP0.device - /dev/ttyLP0.
- [FAILED] Failed to start gpuconfig.…NXP i.MX/LS GPU (Former rc_gpu.S).
- See 'systemctl status gpuconfig.service' for details.
- [ 6.649813] fec 42890000.ethernet end0: renamed from eth0
- [ OK ] Started dbus.service - D-Bus System Message Bus.
- [ OK ] Started avahi-daemon.service - Avahi mDNS/DNS-SD Stack.
- Starting wpa_supplicant.service - WPA supplicant...
- [ OK ] Started wpa_supplicant.service - WPA supplicant.
- [ OK ] Reached target network.target - Network.
- Starting ntpsec.service - Network Time Service...
- Starting rc-local.service▒▒m - /etc/rc.local Compatibility...
- Starting ssh.service - OpenBSD Secure Shell server...
- Starting systemd-user-sess…vice - Permit User Sessions...
- [ OK ] Finished e2scrub_reap.serv…ine ext4 Metadata Check Snapshots.
- [ OK ] Started loadcpufreq.servic… needed to enable cpufreq scaling.
- [ OK ] Started ntpsec.service - Network Time Service.
- [ OK ] Finished systemd-user-sess…ervice - Permit User Sessions.
- [ OK ] Started systemd-logind.service - User Login Management.
- Starting cpufrequtils.serv…: set CPUFreq kernel parameters...
- Starting lightdm.service - Light Display Manager...
- [ 7.446894] mlan: loading out-of-tree module taints kernel.
- Starting polkit.service - Authorization Manager...
- [ 7.580534] wlan: Loading MWLAN driver
- [ 7.611959] wlan: Register to Bus Driver...
- [ OK 7.627216] vendor=0x0471 device=0x0205 class=0 function=1
- m] Started cpufrequtils.servi…SB: set CPUFreq kernel parameters.
- [ 7.650750] Attach moal handle ops, card interface type: 0x109
- [ 7.665562] rps set to 0 from module param
- [ 7.936006] SDIW612: init module param from usr cfg
- [ 7.941478] card_type: SDIW612, config block: 0
- [ 7.949769] cfg80211_wext=0xf
- [ 7.952938] max_vir_bss=1
- [ 7.955799] cal_data_cfg=none
- [ 7.958853] ps_mode = 1
- [ 7.961504] auto_ds = 1
- [ 7.963954] host_mlme=enable
- [ 7.966939] drv_mode = 1
- [ 7.969609] fw_name=nxp/sduart_nw61x_v1.bin.se
- [ 7.974239] SDIO: max_segs=128 max_seg_size=65535
- [ 7.979021] rx_work=1 cpu_num=2
- [ 7.982265] Enable moal_recv_amsdu_packet
- [ 7.986434] Attach mlan adapter operations.card_type is 0x109.
- [ 7.999043] wlan: Enable TX SG mode
- [ 8.005623] wlan: Enable RX SG mode
- [ 8.013492] Request firmware: nxp/sduart_nw61x_v1.bin.se
- [ 8.019144] wlan_sdio mmc2:0001:1: loading /lib/firmware/nxp/sduart_nw61x_v1.bin.se failed with error -22
- [ 8.028823] wlan_sdio mmc2:0001:1: Direct firmware load for nxp/sduart_nw61x_v1.bin.se failed with error -22
- [ 8.038689] wlan_sdio mmc2:0001:1: Falling back to sysfs fallback for: nxp/sduart_nw61x_v1.bin.se
- [ OK ] Started polkit.service - Authori[ 8.286184] cfg80211: failed to load regulatory.db
- zation Manager.
- [ 8.383945] imx8_media_dev: module is from the staging directory, the quality is unknown, you have been warned.
- [ 8.395358] mx8-img-md: Registered mxc_isi.0.capture as /dev/video0
- [ 8.423629] unregister ISI channel: mxc_isi.0
- [ 8.586076] CAN device driver interface
- [ 8.728557] rtc-pcf2131-i2c 2-0053: oscillator stop detected, date/time is not reliable
- [ 8.795744] rtc-pcf2131-i2c 2-0053: registered as rtc1
- [ 9.089033] fsl_mc_err_probe: No ECC DIMMs discovered
- [ 9.257536] usb 1-1.2: Found UVC 1.00 device USB Camera (1bcf:28c4)
- [ 9.318054] usbcore: registered new interface driver uvcvideo
- [ 9.327884] WLAN: request_firmware() failed, error code = -2
- [ 9.333629] woal_request_fw failed
- [ 9.337095] Firmware Init Failed
- [ 9.343103] Free module params
- [ 9.350458] woal_add_card failed
- [ 9.354932] usb 1-1.2: 3:1: cannot get freq at ep 0x86
- [ 9.360275] wlan_sdio: probe of mmc2:0001:1 failed with error -1
- [ 9.370562] wlan: Register to Bus Driver Done
- [ 9.378650] wlan: Driver loaded successfully
- [ 9.397460] usb 1-1.2: 3:2: cannot get freq at ep 0x86
- [ 9.438675] usb 1-1.2: 3:3: cannot get freq at ep 0x86
- [ 9.472384] usb 1-1.2: Warning! Unlikely big volume range (=4096), cval->res is probably wrong.
- [ 9.481152] usb 1-1.2: [7] FU [Mic Capture Volume] ch = 1, val = 0/4096/1
- [ 9.489006] usbcore: registered new interface driver snd-usb-audio
- [ 12.391091] rc.local[371]: Cannot find device "mlan0"
- [FAILED] Failed to start rc-local.s…[0m - /etc/rc.local Compatibility.
复制代码 查找到出错信息:
loading /lib/firmware/nxp/sduart_nw61x_v1.bin.se failed with error -22
然后查看这个文件:-rw-r--r-- 1 root root 0 Nov 23 2025 sduart_nw61x_v1.bin.se
发现他的文件大小是0,因此考虑这个文件出错了。
【解决方法】
网上搜了好久都没有找到,因此想起我原来的安装包中有这个镜像的源文件,打开原来烧写SD卡的虚拟机找到压缩包,刚好找到了这个文件:
我将这个文件拷到/lib/firmware/nxp/目录,替换这个文件,然后重启系统,发现报错信息没有了。
【蓝牙测试】
安装bluez bluetooth库
- apt install -y bluez bluetooth
复制代码 扫描发现可以成功的使用了:
- (venv_mediapipe) root@localhost:/home/mypro# bluetoothctl
- Agent registered
- [CHG] Controller 80:A1:97:50:27:AC Pairable: yes
- [bluetooth]#
复制代码 【编写小米测试程序】
- from dataclasses import dataclass
- import asyncio
- from bleak import BleakClient, BleakError
- # 你的设备配置(和 Windows 脚本一致,无需修改)
- MAC = "A4:C1:38:99:9A:07"
- CHARACTERISTIC_UUID = "ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6" # 已验证正确的特征值
- @dataclass
- class Result:
- temperature: float
- humidity: int
- voltage: float
- battery: int = 0
- async def read_ble_data(client):
- """读取并解析温湿度数据(完全复用你的 Windows 解析逻辑)"""
- try:
- # 读取特征值(和 Windows 脚本完全一致)
- buff = await client.read_gatt_char(CHARACTERISTIC_UUID)
-
- # 你的原始解析逻辑(保留不变,确保数据格式和 Windows 一致)
- temp = int.from_bytes(buff[0:2], byteorder='little', signed=True) / 100
- humidity = int.from_bytes(buff[2:3], byteorder='little')
- voltage = int.from_bytes(buff[3:5], byteorder='little') / 1000
- battery = round((voltage - 2) / (3.261 - 2) * 100, 2)
-
- # 确保电池百分比在 0-100 之间(避免异常值)
- battery = max(0, min(100, battery))
-
- result = Result(
- temperature=temp,
- humidity=humidity,
- voltage=voltage,
- battery=int(battery) # 转为整数更直观
- )
- return result
- except Exception as e:
- print(f"数据读取/解析失败:{str(e)}")
- return None
- async def main(address):
- client = BleakClient(address, timeout=30.0)
- connect_attempts = 3 # 连接重试次数(解决连接不稳定)
-
- try:
- # 连接重试逻辑(适配 Debian 蓝牙连接波动)
- for attempt in range(connect_attempts):
- print(f"\n🔌 连接设备 {address}(尝试 {attempt+1}/{connect_attempts})")
- print("💡 请立即短按 LYWSD03MMC 侧面按键唤醒设备!")
- await asyncio.sleep(0.5) # 给用户 0.5 秒按按键
-
- try:
- if not client.is_connected:
- await client.connect()
- if client.is_connected:
- print("✅ 连接成功!")
- print("📌 若后续读取失败,请再次短按设备按键唤醒\n")
- break
- except BleakError as e:
- print(f"❌ 第 {attempt+1} 次连接失败:{str(e)}")
- if attempt < connect_attempts - 1:
- print("⏳ 3 秒后重试...")
- await asyncio.sleep(3)
-
- # 连接失败后的退出逻辑
- if not client.is_connected:
- print("\n❌ 所有连接尝试失败!")
- print("排查:1. 设备是否有电 2. 距离是否 ≤ 1 米 3. 关闭手机等其他蓝牙设备")
- return
-
- # 循环读取数据(和 Windows 脚本一致,间隔 1 秒)
- while True:
- result = await read_ble_data(client)
- if result:
- print(f"[读取成功] {result}")
- else:
- print("⚠️ 未读取到有效数据,请短按设备唤醒")
-
- # 异步睡眠(替代 time.sleep,不阻塞事件循环)
- await asyncio.sleep(1)
-
- except BleakError as e:
- print(f"\n🔴 蓝牙连接异常:{str(e)}")
- print("💡 解决方案:重新运行脚本,按提示及时唤醒设备")
- except KeyboardInterrupt:
- print("\n\n👋 手动退出程序")
- finally:
- # 确保断开连接
- if client.is_connected:
- await client.disconnect()
- print("✅ 已断开设备连接")
- if __name__ == "__main__":
- # 适配 Debian 系统的 asyncio 事件循环(解决嵌入式 Linux 兼容问题)
- try:
- asyncio.run(main(MAC))
- except RuntimeError as e:
- # 若出现事件循环错误,用备用方式启动
- loop = asyncio.get_event_loop()
- loop.run_until_complete(main(MAC))
复制代码
最后测试效果如下:
- (venv_mediapipe) root@localhost:/home/mypro# python lywsd03mmc_read.py
- 🔌 连接设备 A4:C1:38:99:9A:07(尝试 1/3)
- 💡 请立即短按 LYWSD03MMC 侧面按键唤醒设备!
- ✅ 连接成功!
- 📌 若后续读取失败,请再次短按设备按键唤醒
- [读取成功] Result(temperature=19.97, humidity=51, voltage=2.897, battery=71)
- [读取成功] Result(temperature=19.97, humidity=51, voltage=2.897, battery=71)
- [读取成功] Result(temperature=19.97, humidity=51, voltage=2.897, battery=71)
- [读取成功] Result(temperature=19.97, humidity=51, voltage=2.897, battery=71)
- [读取成功] Result(temperature=19.97, humidity=51, voltage=2.897, battery=71)
复制代码 【总结】
经过一天一夜的排查,终于把蓝牙给用上了,下一步,我将实现与mcxw71的交互。
|
|