Tuesday, 20 August 2013

Send SMS using 3G USB dongle on Linux

Before doing anything else, please make sure that your device is detected on  your Linux machine. You can check this by issuing the command "dmesg". After you plug-in the 3G card, the last few lines of the "dmesg" command should show messages related to this. For eg. on my machine I see the following messages.

[19447.425468] option 2-1.3:1.0: GSM modem (1-port) converter detected
[19447.426391] usb 2-1.3: GSM modem (1-port) converter now attached to ttyUSB0
[19447.426423] option 2-1.3:1.2: GSM modem (1-port) converter detected
[19447.426848] usb 2-1.3: GSM modem (1-port) converter now attached to ttyUSB1
[19447.426867] option 2-1.3:1.3: GSM modem (1-port) converter detected
[19447.427332] usb 2-1.3: GSM modem (1-port) converter now attached to ttyUSB2
 

  Also, please make sure that  usb_modeswitch is installed. usb_modeswitch is needed to make sure that your device is detected as a networking device instead of a storage device.

Script:

The following script will help you to send an SMS using a 3G dongle on a Linux box.

#!/bin/sh
chat TIMEOUT 1 "" "AT+CMGF=1" "OK" > /dev/ttyUSB2  < /dev/ttyUSB2
chat TIMEOUT 1 "" "AT+CMGS=\"$1\"" "OK" > /dev/ttyUSB2 < /dev/ttyUSB2
chat TIMEOUT 1 "" "$2" "OK" > /dev/ttyUSB2  < /dev/ttyUSB2
chat TIMEOUT 1 "" "^Z" "OK" > /dev/ttyUSB2 < /dev/ttyUSB2
chat TIMEOUT 1 "" "AT+CMGF=0" "OK" > /dev/ttyUSB2  < /dev/ttyUSB2


How to use:

Please note that you need to execute the script with root permissions.

1. Copy the above script into a file. Assume the name of the file is sms.sh.
2. Add execution permissions to the script ie "chmod +x sms.sh"
3. To send the SMS, execute the script with the mobile number as the first argument and the text message as the second argument.  If your message consists of multiple words, use quotes.

For eg. The following command sends the message "hello boss" to the mobile number +919876543210.

$ ./sms.sh +919876543210 "hello boss"

Personally I have tested this on OpenWRT based Ralink board and it worked well for me.

1 comment: