In Digital Marketing Strategy, email and SMS are the important tools to improve your customer services. And Raspberry Pi is a good platform to help us to implement sending email or SMS.
In this weekend, I try to send SMS from Raspberry Pi with USB 3G, and i was successful. I used a Raspberry Pi 2 model B, and a USB 3G (I am in Vietnam, so I bought a Viettel USB 3G).
At the first step, plug USB 3G in Raspberry Pi, and restart Pi.
From the terminal, type lsusb
command:
1 2 3 4 5 6 |
pi@raspberrypi ~/Projects/sms $ lsusb Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. Bus 001 Device 006: ID 19d2:0108 ZTE WCDMA Technologies MSM Bus 001 Device 004: ID 062a:4101 Creative Labs |
And using dmesg command, find ttyUSB:
1 2 3 4 5 |
i@raspberrypi ~/Projects/sms $ dmesg|grep ttyUSB [ 18.738390] usb 1-1.3: GSM modem (1-port) converter now attached to ttyUSB0 [ 18.739859] usb 1-1.3: GSM modem (1-port) converter now attached to ttyUSB1 [ 18.740816] usb 1-1.3: GSM modem (1-port) converter now attached to ttyUSB2 [ 18.741821] usb 1-1.3: GSM modem (1-port) converter now attached to ttyUSB3 |
If you see the output above, it means that USB 3G connection is correct.
Now, we need to install a software to help us connect to serial port ttyUSB. That software is picocom:
1 |
sudo apt-get install picocom |
After that, try to connect ttyUSB1 with command:
1 |
picocom /dev/ttyUSB1 -b 115200 -l |
If you see the output has “terminal ready” and you try to type “AT” and receive the response “OK”. It means that your connection is prefect. Now, you type “AT+CMFG=1” to change to “text mode”
1 2 3 |
AT+CMGF? displays current mode AT+CMGF=1 sets text mode AT+CMGF=0 sets PDU mode |
And try to send a SMS message with command:
1 |
AT+CMGS="phonenumber"<CR>the message to send<Ctrl+z> |
Finally, press Ctrl + a + x to exit picocom terminal.
If you send SMS successfully with picocom. And now, we write a Python script to send SMS message. You can see the source code below. It is so simple.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import serial def send_text(number, text, path='/dev/ttyUSB1'): ser = serial.Serial(path, timeout=1) # set text mode ser.write('AT+CMGF=%d\r' % 1) # set number ser.write('AT+CMGS="%s"\r' % number) # send message ser.write('%s\x1a' % text) print ser.readlines() ser.close() send_text('098xxxxxx', 'hey how are you?') |
To build a high performance SMS sender, we should apply Redis Pub/Sub to push all messages need to send to Queue.
Good luck!