去年我用 Quectel ec25 和树莓派做了一个 4g 热点,它为此目的工作得很好。今年早些时候,我想扩展它的功能,以自动回复带有某些系统状态更新的短信。我能够弄清楚使用 AT 命令发送和接收文本消息就好了,但是我无法让 python 识别和响应带有关键字的文本消息。我找到了这段代码 http://www.python-exemplary.com/index_en.php?inhalt_links=navigation_en.inc.php&inhalt_mitte=raspi/en/gsm.inc.php并稍微修改了它以使 EC25 与USB 串口。
我有 2 个 SSH 会话同时进行,一个带有命令行,另一个带有 minicom 会话来监控串行。EC25 正在向 Pi 发送它正在接收消息的指示,收到消息时它的输出是“+CMTI:“ME”,0”,但 pi 没有响应。代码在这部分似乎没有响应。它会打印“正在收听传入的短信......”但它永远不会超出这个范围,即使它收到短信也是如此。
reply = ser.read(ser.inWaiting())# Clean buf
print "Listening for incoming SMS..."
while True:
reply = ser.read(ser.inWaiting())
if reply != "":
我只使用 ser.read() 和 ser.inWaiting() 进行了尝试,但这会将其发送到错误的反馈循环中。
这是我的代码。
# SIMSMS1.py
# pip install pyserial
import RPi.GPIO as GPIO
import serial
import time, sys
import datetime
P_BUTTON = 24 # Button, adapt to your wiring
def setup():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(P_BUTTON, GPIO.IN, GPIO.PUD_UP)
SERIAL_PORT = "/dev/ttyUSB3" # Raspberry Pi 3
ser = serial.Serial(SERIAL_PORT, baudrate = 115200, timeout = 5)
setup()
ser.write("AT+CMGF=1\r") # set to text mode
time.sleep(1)
ser.write('AT+QURCCFG="urcport","usbmodem"\r') #set URC Indication
time.sleep(1)
ser.write('AT+CPMS="ME","ME","ME"\r') #set memory to Mobile equipment message storage
time.sleep(1)
ser.write('AT+CMGD=1,4\r') # delete all SMS
time.sleep(1)
reply = ser.read(ser.inWaiting())# Clean buf
print "Listening for incoming SMS..."
while True:
reply = ser.read(ser.inWaiting())
if reply != "":
ser.write("AT+CMGR=1\r") # read message
time.sleep(3)
reply = [ser.read(ser.inWaiting())]
print "SMS received. Content:"
print reply
if "getStatus" in reply:
t = str(datetime.datetime.now())
if GPIO.input(P_BUTTON) == GPIO.HIGH:
state = "Button released"
else:
state = "Button pressed"
ser.write('AT+CMGS="+1xxxxxxxxxx"\r') #designate phone number
time.sleep(3)
msg = "Sending status at " + t + ":--" + state
print "Sending SMS with status info:" + msg
ser.write(msg + chr(26))
time.sleep(3)
ser.write('AT+CMGD=1,4\r') # delete all messages
time.sleep(3)
ser.read(ser.inWaiting()) # Clear buf
time.sleep(5)
这是串行的输出,最后一行是正在接收的消息
AT+CMGF=1 正常
AT+QURCCFG="urcport","usbmodem" OK
AT+CPMS="ME","ME","ME" +CPMS: 0,255,0,255,0,255
确定 AT+CMGD=1,4 确定
+CMTI: "我",0
我知道这与“reply = ser.read(ser.inWaiting())”有关,但我不知道要写什么才能让它工作。先感谢您