Skip to content

Instantly share code, notes, and snippets.

@nikolaybotev
Forked from artizirk/sendsms.sh
Last active May 14, 2021 03:36
Show Gist options
  • Save nikolaybotev/f09c2ca042bdaaabe07f434a6bc1e59f to your computer and use it in GitHub Desktop.
Save nikolaybotev/f09c2ca042bdaaabe07f434a6bc1e59f to your computer and use it in GitHub Desktop.
Send SMS using AT commands over the TTY of a Cellular modem. Supports Unicode international characters and emojis.
#!/bin/sh -e
# Usage: sendsms +375555555 "some text i want to send"
TELFNUMB="$1"
SMSTEXT="$2"
MODEM="${3:-/dev/ttyUSB2}"
TIMEOUT="${4:-1}"
# Text encoding
to_ucs2() {
python3 -c "import sys; print(sys.argv[1].encode('utf_16_be').hex())" "$1"
}
# Chat with modem
exec < $MODEM > $MODEM
# Configure modem
chat TIMEOUT $TIMEOUT "" "AT" "OK"
chat TIMEOUT $TIMEOUT "" "AT+CMGF=1" "OK"
chat TIMEOUT $TIMEOUT "" "AT+CSCS=\"UCS2\"" "OK"
chat TIMEOUT $TIMEOUT "" "AT+CSMP=17,167,0,25" "OK"
# Send message
chat TIMEOUT $TIMEOUT "" "AT+CMGS=\"$(to_ucs2 ${TELFNUMB})\"" ">"
chat TIMEOUT $TIMEOUT "" "$(to_ucs2 "${SMSTEXT}")^Z" "OK"
@nikolaybotev
Copy link
Author

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