准备:esp32 s3开发板,ML307R 4G模块,sim卡一张(物联网卡或者手机里拔一张卡出来也可以)
1.接线:
| 设备 A | 设备 B | 连接说明 |
|---|---|---|
| 5V 电池正极 | ML307R 的 VIN | 给模组独立供电 |
| 5V 电池负极 | ML307R 的 GND | 电源回路 |
| ML307R 的 GND | ESP32-S3 的 GND | 关键!信号共地,必须连! |
| ML307R 的 TX | ESP32-S3 的 IO14 | 串口通信(交叉) |
| ML307R 的 RX | ESP32-S3 的 IO13 | 串口通信(交叉) |
| ML307R 的 EN | ESP32-S3 的 IO12 | 开机控制 |
| 电脑 USB 5V | ESP32-S3 的 USB口 | 给 ESP32 供电并下载程序 |
2.测试卡是否正常,是否有加PIN锁密码:
import machine
import time
import sys
# --- 1. 配置串口 ---
uart = machine.UART(1, baudrate=115200, tx=machine.Pin(13), rx=machine.Pin(14))
# --- 2. 配置控制引脚 ---
ctrl_pin = machine.Pin(12, machine.Pin.OUT)
ctrl_pin.value(1)
print(">>> 🔍 SIM卡安全状态精准探测脚本启动...")
time.sleep(2)
# --- 3. 只执行一项任务:查询PIN状态 ---
# 移除了所有其他的干扰指令,直击要害
tasks = [
["AT\r\n", 1, "📶 基础握手"],
["AT+CPIN?\r\n", 3, "🔐 查询SIM卡PIN锁状态"] # 核心指令
]
# --- 4. 循环执行 ---
for task in tasks:
cmd, delay, desc = task
print(f"--------------------------------")
print(f"👉 执行: {desc}")
print(f"📝 指令: {cmd.strip()}")
uart.write(cmd)
time.sleep(delay)
# 读取并打印返回
if uart.any():
res = uart.read()
try:
text = res.decode('utf-8', 'ignore')
print("📩 返回:")
sys.stdout.write(text)
# --- 智能分析返回结果 ---
if "READY" in text:
print("\n✅ 【结论】SIM卡未锁定,可以正常上网。")
elif "SIM PIN" in text:
print("\n⚠️ 【警告】SIM卡已开启PIN码保护!需要输入密码才能使用。")
elif "SIM PUK" in text:
print("\n❌ 【严重】PIN码已锁定,需要输入PUK码解锁!")
elif "ERROR" in text:
print("\n⚠️ 【注意】指令执行出错,可能是卡没插好或已损坏。")
except:
print("解析错误")
else:
print("📩 返回: (超时无数据)")
print("--------------------------------")
print("🔍 探测结束。")
运行结果如下表示正常:

3.使用AT指令测试是否能正常联网
import machine
import time
import sys
# --- 1. 配置串口 (匹配你的接线) ---
# ML307R TX -> ESP32 IO14
# ML307R RX -> ESP32 IO13
uart = machine.UART(1, baudrate=115200, tx=machine.Pin(13), rx=machine.Pin(14))
# --- 2. 配置控制引脚 ---
# ML307R EN -> ESP32 IO12
ctrl_pin = machine.Pin(12, machine.Pin.OUT)
ctrl_pin.value(1) # 保持高电平,让模组工作
print(">>> 自动化测试脚本已启动...")
time.sleep(2)
# --- 3. 定义测试指令列表 ---
# 我们让模组依次执行:查信号 -> 查注册 -> 查ICCID -> 查IP
commands = [
"AT\r\n", # 1. 基础通信测试
"AT+CSQ\r\n", # 2. 查信号质量
"AT+CEREG?\r\n", # 3. 查基站注册状态
"AT+CCID\r\n", # 4. 查SIM卡ID
'AT+HTTPGET="http://httpbin.org/ip",10\r\n' # 5. 终极测试:获取公网IP (这个可能需要等一会)
]
index = 0
# --- 4. 主循环 ---
while True:
# A. 发送指令
if index < len(commands):
cmd = commands[index]
print(f"--------------------------------")
print(f"👉 发送指令: {cmd.strip()}")
uart.write(cmd)
index += 1
time.sleep(2) # 等待模组反应
else:
print(f"--------------------------------")
print("✅ 所有测试已完成。")
break
# B. 读取并打印模组的回复
# 我们循环读取一段时间,确保把回复读完整
start_time = time.time()
response_buffer = ""
while time.time() - start_time < 3: # 每次发送后读取3秒
if uart.any():
try:
# 读取所有可用数据
data = uart.read()
if data:
# 尝试解码,忽略错误字符
text = data.decode('utf-8', 'ignore')
response_buffer += text
# 实时打印
print(f"📩 收到: {text.strip()}")
except Exception as e:
print(f"读取错误: {e}")
time.sleep(0.1)
# 简单检查关键点
if "CSQ" in cmd:
if "+CSQ:" in response_buffer:
print("💡 提示: 看到 +CSQ 说明信号检测正常。")
elif "CEREG" in cmd:
if ",1" in response_buffer or ",5" in response_buffer:
print("🎉 成功: 看到 ,1 或 ,5 说明已连上基站!")
else:
print("⚠️ 注意: 如果显示 ,0 或 ,2 说明未注册网络。")
运行看到类似的结果,说明正常:

备注:如果首次运行代码,发现不行的话,检查下卡是否正常,重新插拔下,然后将esp32 s3和ml307r的电源关闭重启一下就可以了