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!