问题描述
我去年用移远 ec25 和 raspBerry pi 制作了一个 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" 好的
AT+CPMS="ME","ME" +CPMS:0,255,255
好的 AT+CMGD=1,4 好的
+CMTI: "我",0
我知道它与“reply = ser.read(ser.inWaiting())”有关,但我不知道要写什么才能使它工作。提前致谢
解决方法
最简单的方法可能是轮询收到的消息。 也许 EC25 有一些可能的 gpio。一旦有任何消息到来,gpio 就会产生一个脉冲信号。