Spitz AX - Issues with Sending AT Commands

Hello everyone,

I am currently working on a project to send automated SMS messages using a GL-Inet Spitz AX router (model: GL-X3000) through Python scripts. I aim to control a 5G modem connected via the MHI interface (/dev/mhi_DUN).

I’ve been able to successfully send AT commands using the echo command in the shell (e.g., echo -e "AT\r" > /dev/mhi_DUN), and I receive expected responses from the modem. However, I am encountering issues when trying to replicate this functionality in Python using the PySerial library.

Here are the methods and tools I’ve tried so far:

  1. Minicom: minicom -D /dev/mhi_DUN -b 115200
    I can read responses from the modem but am unable to type or send commands directly within Minicom.
  2. Screen: screen /dev/mhi_DUN 115200
    Attempts to use this tool resulted in a /dev/null/utmp: Not a directory error, and it terminates immediately.
  3. Picocom: picocom -b 115200 /dev/mhi_DUN
    Similar to Screen, Resulted in FATAL: failed to add port: Filedes is not a tty
  4. Python (PySerial): I wrote a script to send commands and read responses. Commands like ser.write(b'AT\r') are used to write to the modem. However, when I try to read the response with ser.read_all(), I encounter an OSError: [Errno 22] Invalid argument.

Here’s a snippet of my Python code:

import serial
import time

ser = serial.Serial('/dev/mhi_DUN', 115200, timeout=1)
ser.write(b'AT\r')
time.sleep(1)
response = ser.read_all()
print(response)
ser.close()

The error occurs specifically at the ser.read_all() call. I’ve tried adjusting various serial port settings (baud rate, timeouts) but to no avail.

Has anyone experienced similar issues or have suggestions on how to resolve these communication problems with MHI devices in Python? Any help or guidance would be greatly appreciated as my goal is to automate SMS sending via this setup.

Thank you in advance!

Hello everyone,

I wanted to update you all on my progress regarding the issue I was having with sending automated SMS messages using Python scripts on my GL-Inet Spitz AX router. I found a solution using a shell script instead of Python, which I hope will be useful for others facing similar challenges.

Here’s the working script:

sms_automation.sh

#!/bin/sh
echo "Start SMS Monitor..."
MODEM="/dev/mhi_DUN"

while true; do
  # Set the modem to text mode
  echo -e "AT+CMGF=1\r" > $MODEM
  sleep 1

  # Read all SMS messages with a timeout to prevent hanging
  echo -e "AT+CMGL=\"ALL\"\r" > $MODEM; timeout 5 cat $MODEM

  # Variable RESPONSE is not needed here as we do not programmatically react to the content
  # If you need to store and process contents, RESPONSE should be used
  RESPONSE=$(echo -e "AT+CMGL=\"ALL\"\r" > $MODEM; timeout 5 cat $MODEM)

  # Check if an SMS with "Start" was received
  if echo "$RESPONSE" | grep -q "Start"; then
    echo "Start message found. Sending response SMS."
    # Send the response SMS
    (echo -e "AT+CMGF=1\r"; echo -e "AT+CMGS=\"+49xxxxxxxxx\"\r"; echo -e "Test successful\x1A") > $MODEM
    sleep 1
    echo "Response SMS sent."
  fi

  # Delete all SMS messages in storage
  echo -e "AT+CMGD=1,4\r" > $MODEM
  sleep 1
  echo "All messages deleted."

  # Wait 25 seconds before the next check
  sleep 25
done

To ensure that the timeout command is available on your OpenWRT router, you might need to install it if it’s not already available. You can install it with the following commands:

opkg update
opkg install coreutils-timeout

I hope this script helps anyone looking to automate SMS handling on their routers. If you have any questions or suggestions for improvement, feel free to share!