Skip to content

Instantly share code, notes, and snippets.

@m0rff
Last active November 28, 2022 12:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • 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:])
@mooztaches
Copy link

hi, i have some problem when i execute the file, it say me :

buf += ch
TypeError: Can't convert 'bytes' object to str implicitly

i try to make something like buf= buf + ch.decode() or buf += ch.decode() but the program is stopping at "Connected to /dev/serial0"

i know i make something wrong, but i'm just a beginner in Python code... hope someone help me, thanks!

@kanchanbartwal
Copy link

i am stucking at the same line, this is because in getline() it never get any "\r" and continous the while loop for long time.

@Andermutu
Copy link

i am stucking at the same line, this is because in getline() it never get any "\r" and continous the while loop for long time.

I think this code is outdated, however it is not difficult to port it to Python3.7, I have already done it.

@oNddleo
Copy link

oNddleo commented Jul 22, 2019

Thank you! But this code is outdated for python 3.7, i can fixed for my code

@Andermutu
Copy link

Thank you! But this code is outdated for python 3.7, i can fixed for my code

Yes, it can be easily fixed!

@sebstud
Copy link

sebstud commented Jul 27, 2019

Halo, I am also interested to use this code in newest pfsense 2.5 with python 3.6. Can you fix it?

@Andermutu
Copy link

If anyone is interested in a working Python 3.7 code send me an email: andermutu@gmail.com

@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