0

I created a simple class with a simple method to send an sms to a number:

class SIM800L:
    def __init__(self, port):
        self.port = port
        self.com = serial.Serial(self.port, baudrate = 9600, timeout = 0.5)


    def send_serial_command(self, msg):

        self.com.write(msg.encode())
        print(self.com.readlines())
        

    def send_sms(self, msg, number):

            #set to text mode
            self.send_serial_command("AT+CMGF=1\r\n") 

            #set the phone number
            self.send_serial_command(f'AT+CMGS="{number}"\r\n') 

            #sms body
            self.send_serial_command(msg) 

            #sending char 26 is like hitting 'send'
            self.send_serial_command(chr(26)) 

            #wait just incase
            time.sleep(1) 

This works just fine with a single call of the method send_sms() and also when I send an sms in a loop to the same number. However as soon as I replace 1 phone number with a list and do the following it all goes wrong:

sim800l = SIM800L("/dev/ttyUSB0")
numbers = ["+44XXXXXXXXXX", "+44VVVVVVVVVV"]

for n in numbers:
    sim800l.send_sms("Hello" n)

It sends just fine to the first number, then when the method is called for the second number it feels like it interpites the at commands as part of the msg and saves in some "buffer" (I am likely not understanding whats going on correctly so depicting the way I see it) and then when the loop goes back to the first one it sends it just fine again. The reason I say it saves it in a "buffer" is that if I set it to send to only the second number, then the next sms arrving to that number will have those AT commands appended to the start of the msg.

I suspect that I am missing some command at the end of my send_sms() method but could not find any answers on the internet.

Things I've tried:

  • Adding \r\n to the number
  • Increasing time out to 5s
  • Closing and re-opening serial connection between calls of the method

Any ideas?

Thank you in advance!

Eugene Levinson
  • 172
  • 2
  • 13
  • Looks like you need to add line terminator like \r\n to the number too – DisappointedByUnaccountableMod Oct 26 '21 at 17:49
  • I tried that but didn't fix the issue. (sorry forgot to mention it in the post) – Eugene Levinson Oct 26 '21 at 17:51
  • Please add anything else you’ve tried but haven’t mentioned into your question, so readers don’t waste their time suggesting things you’ve already tried – DisappointedByUnaccountableMod Oct 26 '21 at 17:52
  • 2
    1) The AT command terminator is `\r` alone, without `\n`. 2) It is not ok sending N commands without waiting for the response of the previous one. You should implement a receive waiting either for `\r\nOK\r\n` or `\r\nERROR\r\n` before sending the following command (SMS sending can take even tens of seconds, depending on the network). 3) Is a comma missing in `sim800l.send_sms("Hello" n)`? It should be `sim800l.send_sms("Hello", n)` IMHO – Roberto Caboni Nov 03 '21 at 08:32

0 Answers0