0

I made a 4g hotspot last year with a Quectel ec25 and raspberry pi, it has worked just fine for this purpose. Earlier this year I wanted to expand its capabilities to automatically reply to text messages with status updates for certain systems. I was able to figure out sending and receiving a text message with AT commands just fine but I am having trouble getting python to recognize and respond to a text message with key words. I found this code, http://www.python-exemplary.com/index_en.php?inhalt_links=navigation_en.inc.php&inhalt_mitte=raspi/en/gsm.inc.php, and have modified it slightly to make the EC25 work with the USB serial.

I have 2 SSH sessions going at once, one with the command line up and the other with a minicom session to monitor serial. the EC25 is sending indications to the Pi that it is receiving a message, its output when a message is received is "+CMTI: "ME",0" but the pi has no response to it. The code seems to be unresponsive on this part. it will print "listening for incoming SMS..." but then it will never go beyond that, even when it receives text messages.

reply = ser.read(ser.inWaiting())# Clean buf
print "Listening for incoming SMS..."
while True:
    reply = ser.read(ser.inWaiting())
    if reply != "":

I have tried it with just ser.read() and ser.inWaiting() but that sends it into a bad feedback loop.

Here is my code.

# 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)    

this is the output of on the serial, the last line is a message being received

AT+CMGF=1 OK

AT+QURCCFG="urcport","usbmodem" OK

AT+CPMS="ME","ME","ME" +CPMS: 0,255,0,255,0,255

OK AT+CMGD=1,4 OK

+CMTI: "ME",0

I know it has something to do with the "reply = ser.read(ser.inWaiting())" but i cant figure out what to write to make it work. Thank you in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SEAK14
  • 1

1 Answers1

0

The easiest way might be polling the message received. Maybe the EC25 has some possible gpio. Once any message comes, the gpio would generate a pulse signal.

MrVW
  • 11
  • 1