基于Raspberry Pi的短信网关设计,


口味

通过 Raspberry Pi 配合无线网卡的短信收发功能,实现短信自动收、发功能。并且能够存储短信,并将短信转发到饭否等微博平台(或者直接发email)。

用料

食材:

Raspberry Pi 一块 支持短信功能的 3G 网卡一条(本例采用华为 E173,可根据季节、个人偏好选择其他的,但不保证一定能用) SIM 卡若干 USB 无线网卡(可选)

佐料:

Debian 炊具 Gammu 软件 MySQL 数据库

步骤

配置硬件:将 3G 网卡插入 USB 口,使用 lsusb 命令可以看到当前 USB 口的设备信息。ls /dev/ttyUSB* 命令可以看到有 ttyUSB0 ttyUSB1 ttyUSB2 三个设备。 安装软件:使用 sudo apt-get install gammu,安装 gammu,该工具用以发送接受短信等操作。 配置 gammu :gammu-config,配置每一项内容。我的配置:
Port: /dev/ttyUSB2 (这个不行就换个试试)
Connection: at19200
Model: 空
Synchronize times: yes
Log file: 空
Log format: nothing
Use locking: 空
Gammu localisation: 空
保存后,测试是否成功: sudo gammu --identify 这里会输出设备和 SMS 卡的基本信息。说明成功了。 可以尝试用 echo "test" | sudo gammu sendsms TEXT 186XXXXXXXX 来测试发消息,这里只能发英文,因为没设置编码(后面会讲到)。 现在基本的短信发送功能实现了。开始配置smsd(sms deamon)以实现自动接受短信功能。sudo apt-get install gammu-smsd 安装 gammu-smsd,然后编辑 sudo vim /etc/gammu-smsdrc。下面是我的配置文件,可以供参考。配置文件一目了然,就不仔细介绍了。需要说明的一点是:这里后端存储短信使用的是 MySQL 数据库,还可以使用文本等等(自己去看文档)。所以需要建立表:http://wammu.eu/docs/manual/smsd/mysql.html RTFM看文档。
# Gammu library configuration, see gammurc(5)
[gammu]
# Please configure this!
port = /dev/ttyUSB2
connection = at
# Debugging
#logformat = textall

# SMSD configuration, see gammu-smsdrc(5)
[smsd]
RunOnReceive = /home/pi/smsutil.py
service = sql
driver = native_mysql
logfile = /var/log/gammu-smsd
host = localhost
pc = localhost
user = root
password = chungechunyemen
database = smsd
# Increase for debugging information
debuglevel = 0
ReceiveFrequency = 60
PIN = 1234
启动 gammu-smsd:gammu-smsd --config /etc/gammu-smsdrc --pid /var/run/gammu-smsd.pid --daemon --user gammu --group gammu 到这里应该可以用了。在数据库的 inbox 表中可以看到短信内容等。

使用 Python 发短信

import gammu
import sys
sm = gammu.StateMachine() # Read ~/.gammurc
sm.ReadConfig()
try:
    sm.Init()
    netinfo = sm.GetNetworkInfo()
    print 'Network name: %s' % netinfo['NetworkName']
    print 'Network code: %s' % netinfo['NetworkCode']
    print 'LAC: %s' % netinfo['LAC']
    print 'CID: %s' % netinfo['CID']

    # Prepare message data
    # We tell that we want to use first SMSC number stored in phone

    phone_number = raw_input('Enter Phone Number:\n').decode('utf-8')
    message = raw_input('Enter text:\n').decode('utf-8')

    message = {
        'Text': message,
        'SMSC': {'Location': 1},
        'Number': phone_number,
        'Coding': 'Unicode_No_Compression',
    }
    # Actually send the message
    sm.SendSMS(message)
except Exception,e:
    print e

通过指定 Coding 为 Unicode_No_Compression 可以发送 70 个字符长的 unicode,即可以发送中文了。使用 sudo 执行该代码,按照提示输入手机号和短信即可以发送。

发送到微博

自己弄吧,无非就是申请个 API,然后 OAuth,再通过脚本得到待处理的 MySQL 数据库中的数据进行发送。先不写了,好累。。

参见

http://wammu.eu/docs/manual http://giovanni.wordpress.com/2012/02/09/how-to-sendreceive-sms-with-gammu/ http://pierky.wordpress.com/2011/10/28/zabbix-send-sms-using-gammu-smsd/ http://www.techytalk.info/send-receive-sms-using-gsm-modem-phone-ubuntu/

via

文章标题:基于Raspberry Pi的短信网关设计 - 树莓派实验室 固定链接:https://shumeipai.nxez.com/2014/02/18/raspberry-pi-based-sms-gateway-design.html

相关内容