Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send and receive SMS from python using usb modem? [closed]

I'm a beginner in python and i'm looking for a library to send and receive SMS through a Huawei modem. I tried gammu, pysms and pygsm but failed to get them to work. Could you give me code examples with those libraries?

like image 296
Bilel_a Avatar asked Apr 09 '14 22:04

Bilel_a


1 Answers

You can try this code, it works for me , just plug your USB dongle and get it's device node path(in linux use lsusb and ls -lha /dev/tty*) and replace /dev/ttyACM0 with that path.Then you should able to send sms, This code works for me with Huawei USB modem.

#!/usr/bin/env python
"""
sms.py - Used to send txt messages.
"""
import serial
import time

class TextMessage:
    def __init__(self, recipient="0123456789", message="TextMessage.content not set."):
        self.recipient = recipient
        self.content = message

    def setRecipient(self, number):
        self.recipient = number

    def setContent(self, message):
        self.content = message

    def connectPhone(self):
        self.ser = serial.Serial('/dev/ttyACM0', 460800, timeout=5)
        time.sleep(1)

    def sendMessage(self):
        self.ser.write('ATZ\r')
        time.sleep(1)
        self.ser.write('AT+CMGF=1\r')
        time.sleep(1)
        self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
        time.sleep(1)
        self.ser.write(self.content + "\r")
        time.sleep(1)
        self.ser.write(chr(26))
        time.sleep(1)

    def disconnectPhone(self):
        self.ser.close()

for more details try this link (archive of that dead link as of 2014-08-25 )

like image 183
TMKasun Avatar answered Oct 03 '22 10:10

TMKasun