SMS Gateway
来自Jack's Lab
目录 |
1 设备
- ZTE MF668 (USB 3G modem)
- Linux x86 server
2 模式切换
默认情形下,内核将 ZTE MF668 识别为 SCSI CD-ROM 设备(因为其内部有一个 MicroSD 控制器,可插MicroSD卡):
$ dmesg|grep scsi [ 3.769916] scsi0 : usb-storage 1-1.3:1.0 [ 4.773973] scsi 0:0:0:0: CD-ROM ZTE USB SCSI CD-ROM 2.31 PQ: 0 ANSI: 2 [ 6.514819] sr0: scsi-1 drive [ 6.530712] sr 0:0:0:0: Attached scsi CD-ROM sr0
因此需要 usb-modeswitch 将其切换为 modem 模式:
root@raspberrypi:/# apt-get install usb-modeswitch root@raspberrypi:/# lsusb Bus 001 Device 002: ID 0424:9512 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 004: ID 19d2:2000 ZTE WCDMA Technologies MSM MF627/MF628/MF628+/MF636+ HSDPA/HSUPA root@raspberrypi:/# cat /etc/usb_modeswitch.d/19d2\:2000 # ZTE devices DefaultVendor=0x19d2 DefaultProduct=0x2000 TargetVendor=0x19d2 TargetProduct=0x0017 MessageContent="5553424312345678000000000000061e000000000000000000000000000000" MessageContent2="5553424312345679000000000000061b000000020000000000000000000000" MessageContent3="55534243123456702000000080000c85010101180101010101000000000000" NeedResponse=1 root@raspberrypi:/# usb_modeswitch -I -c /etc/usb_modeswitch.d/19d2\:2000 Looking for target devices ... No devices in target mode or class found Looking for default devices ... found matching product ID adding device Found device in default mode, class or configuration (1) Accessing device 004 on bus 001 ... Getting the current device configuration ... OK, got current device configuration (1) Using first interface: 0x00 Using endpoints 0x01 (out) and 0x81 (in) USB description data (for identification) ------------------------- Manufacturer: ZTE,Incorporated Product: ZTE WCDMA Technologies MSM Serial No.: P680A1ZTED010000 ------------------------- Looking for active driver ... No driver found. Either detached before or never attached Setting up communication with interface 0 Using endpoint 0x01 for message sending ... Trying to send message 1 to endpoint 0x01 ... OK, message successfully sent Reading the response to message 1 (CSW) ... OK, response successfully read (13 bytes). Trying to send message 2 to endpoint 0x01 ... OK, message successfully sent Reading the response to message 2 (CSW) ... OK, response successfully read (13 bytes). Trying to send message 3 to endpoint 0x01 ... OK, message successfully sent Reading the response to message 3 (CSW) ... OK, response successfully read (0 bytes). Resetting response endpoint 0x81 Resetting message endpoint 0x01 -> Run lsusb to note any changes. Bye.
3 Send/Receive SMS
4 Open Source Tools
4.1 gammu
$ apt-get install gammu gammu-smsd
配置 gammu
$ gammu-config Port: /dev/ttyUSB1 Connection: at115200
生成的配置文件默认位于 /root/.gammurc
亦可 /etc/gammurc
测试:
$ gammu getallsms #显示sim卡上所有 SMS Location 100006, folder "收件箱", phone memory, Inbox folder SMS message SMSC number : "+8613010374500" 标记为已发送 : 2013年04月15日 星期一 13时27分16秒 +0800 Coding : Unicode (no compression) Remote number : "+85264504220" Status : 已读 您的 Google 验证代码为 002226 1 SMS parts in 1 SMS sequences
发送 SMS:
$ gammu sendsms TEXT 15801234567 -textutf8 "testing"
中英文混合 SMS:
$ gammu sendsms TEXT 15801234567 -unicode -text "testing 测试"
4.2 gammu-smsd
配置:
SH-NAS:/var# cat /etc/gammu-smsdrc # Configuration file for Gammu SMS Daemon # Gammu library configuration, see gammurc(5) [gammu] # Please configure this! port = /dev/ttyUSB1 connection = at115200 # Debugging logformat = textall # SMSD configuration, see gammu-smsdrc(5) [smsd] service = sql Driver = native_mysql logfile = syslog User = smsd Password = smsd PC = localhost Database = smsd # Increase for debugging information debuglevel = 0
创建数据库和表:
mysql> create database smsd; Query OK, 1 row affected (0.00 sec) mysql> GRANT USAGE ON *.* TO smsd@localhost IDENTIFIED BY 'YOUR_PASSWORD'; Query OK, 0 rows affected (0.01 sec) mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON smsd.* to smsd@localhost; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye $ cp /usr/share/doc/gammu-smsd/examples/mysql.sql.gz /tmp/ $ gunzip /tmp/mysql.sql.gz $ mysql -u USER_NAME -pUSER_PASSWORD YOUR_DB_NAME < /tmp/mysql.sql
运行:
$ gammu-smsd --config /etc/gammu-smsdrc --pid /var/run/gammu-smsd.pid --daemon
守护进程情形下,发送 SMS:
$ gammu-smsd-inject TEXT 13912345678 -unicode -text "哈哈,猫猫"
5 SIM900A GSM 模块
pi@raspberrypi ~ $ gammu identify 设备 : /dev/ttyAMA0 制造商 : SIMCOM_Ltd 型号 : unknown (SIMCOM_SIM900A) 固件 : Revision:1137B07SIM900A32_ST_NENGRUI IMEI : 35xxxx030199023 SIM IMSI : 460011000xxx196