Skip to content

Instantly share code, notes, and snippets.

@m0rff
Last active November 28, 2022 12:45
Show Gist options
  • Save m0rff/63bf812cf94dcbf8558e4907517bfd9a to your computer and use it in GitHub Desktop.
Save m0rff/63bf812cf94dcbf8558e4907517bfd9a to your computer and use it in GitHub Desktop.
Sending a SMS with Python and pyserial via an USB 3G Modem on pfSense/FreeBSD
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
#
# Sending a SMS with Python and pyserial via an USB 3G Modem on pfSense/FreeBSD
# tested with: Huawei Technologies Co., Ltd. E620 USB Modem
# and a Telekom SIM card
import serial
from curses import ascii
import time
import sys
import getopt
baudrate = 9600
device = "/dev/cuaU0.0"
simpin = 1234
def getline(ser):
buf = ""
while True:
ch = ser.read(1)
if ch == '\r':
break
buf += ch
return buf
def send_sms(message, phonenumber):
# Initialize serial connection to 3G USB Modem
modem = serial.Serial(device, baudrate, timeout=5)
print "Connected to " + modem.name
# Check modem status
modem.write(b'AT\r')
sent_cmd = getline(modem)
response = modem.read(4)
if "OK" in response:
print "Modem Status: OK"
# check pin and enter it if needed
modem.write(b'AT+CPIN?\r')
sent_cmd = getline(modem)
line2 = getline(modem) # empty
line3 = getline(modem) # empty
response = getline(modem) # get OK
if "SIM PIN" in response:
print "Sending PIN to modem ... "
modem.write(b'AT+CPIN="%s"' % simpin)
sent_cmd = getline(modem)
response = getline(modem)
print response
elif "READY" in response:
print "PIN already entered."
# set modem to text mode
modem.write(b'AT+CMGF=1\r')
sent_cmd = getline(modem)
response = getline(modem)
if "OK" in response:
print "Modem set to text mode!"
# send sms
print "Sending sms ..."
modem.write(b'AT+CMGS="%s"\r' % phonenumber)
time.sleep(0.5)
modem.write(b'%s\r' % message)
time.sleep(0.5)
modem.write(ascii.ctrl('z'))
time.sleep(0.5)
response = modem.readlines()
if "+CMGS" in "".join(response):
print "Sucess: SMS sent!"
else:
print "Error: SMS not sent!"
else:
print "Error: Setting modem to text mode failed!"
elif "NO CARRIER" in response:
print "Error: No 3G connection!"
else:
print "Error: Something else failed!"
def main(argv):
message = ''
phonenumber = ''
usage = 'usage: send_sms.py -p +123467890 -m "your message"'
try:
opts, args = getopt.getopt(argv, "hp:m:", ["phonenumber=", "message="])
except getopt.GetoptError:
print usage
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print usage
sys.exit()
elif opt in ("-p", "--phonenumer"):
phonenumber = arg
elif opt in ("-m", "--message"):
message = arg
send_sms(message=message, phonenumber=phonenumber)
if __name__ == '__main__':
main(sys.argv[1:])
@m0rff
Copy link
Author

m0rff commented Jul 29, 2019

I don't own the hardware anymore so I can't port it to python 3.7. sorry :(

@MalteHillmann
Copy link

Line 68 should be replaced by something like this:
if "+CMGS" in "".join(response):
This checks for +CMGS anywhere in the response list. In my case +CMGS is not on a fixed position.

@m0rff
Copy link
Author

m0rff commented Dec 13, 2019

Line 68 should be replaced by something like this:
if "+CMGS" in "".join(response):
This checks for +CMGS anywhere in the response list. In my case +CMGS is not on a fixed position.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment