2回答

0收藏

基于Raspberry Pi的短信网关设计

Raspberry Pi Raspberry Pi 9409 人阅读 | 2 人回复 | 2013-06-17

菜谱:基于Raspberry Pi的短信网关设计

口味

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

用料

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

佐料:
Debian 炊具
Gammu 软件
MySQL 数据库
步骤

1.配置硬件:将 3G 网卡插入 USB 口,使用 lsusb 命令可以看到当前 USB 口的设备信息。ls /dev/ttyUSB* 命令可以看到有 ttyUSB0 ttyUSB1 ttyUSB2 三个设备。
2.安装软件:使用 sudo apt-get install gammu,安装 gammu,该工具用以发送接受短信等操作。
3.配置 gammu :gammu-config,配置每一项内容。我的配置:
  1. Port: /dev/ttyUSB2 (这个不行就换个试试)
  2. Connection: at19200
  3. Model: 空
  4. Synchronize times: yes
  5. Log file: 空
  6. Log format: nothing
  7. Use locking: 空
  8. Gammu localisation: 空
复制代码
1.保存后,测试是否成功: sudo gammu --identify 这里会输出设备和 SMS 卡的基本信息。说明成功了。

2.可以尝试用 echo "test" | sudo gammu sendsms TEXT 186XXXXXXXX 来测试发消息,这里只能发英文,因为没设置编码(后面会讲到)。

3.现在基本的短信发送功能实现了。开始配置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看文档。
  1. # Gammu library configuration, see gammurc(5)
  2. [gammu]
  3. # Please configure this!
  4. port = /dev/ttyUSB2
  5. connection = at
  6. # Debugging
  7. #logformat = textall

  8. # SMSD configuration, see gammu-smsdrc(5)
  9. [smsd]
  10. RunOnReceive = /home/pi/smsutil.py
  11. service = sql
  12. driver = native_mysql
  13. logfile = /var/log/gammu-smsd
  14. host = localhost
  15. pc = localhost
  16. user = root
  17. password = chungechunyemen
  18. database = smsd
  19. # Increase for debugging information
  20. debuglevel = 0
  21. ReceiveFrequency = 60
  22. PIN = 1234
复制代码
1.启动 gammu-smsd:gammu-smsd --config /etc/gammu-smsdrc --pid /var/run/gammu-smsd.pid --daemon --user gammu --group gammu

2.到这里应该可以用了。在数据库的 inbox 表中可以看到短信内容等。

使用 Python 发短信
  1. import gammu
  2. import sys
  3. # Create state machine object
  4. sm = gammu.StateMachine() # Read ~/.gammurc
  5. sm.ReadConfig()
  6. # Connect to phone
  7. try:
  8.     sm.Init()
  9.     netinfo = sm.GetNetworkInfo()
  10.     print 'Network name: %s' % netinfo['NetworkName']
  11.     print 'Network code: %s' % netinfo['NetworkCode']
  12.     print 'LAC: %s' % netinfo['LAC']
  13.     print 'CID: %s' % netinfo['CID']

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

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

  18.     message = {
  19.         'Text': message,
  20.         'SMSC': {'Location': 1},
  21.         'Number': phone_number,
  22.         'Coding': 'Unicode_No_Compression',
  23.     }
  24.     # Actually send the message
  25.     sm.SendSMS(message)
  26. except Exception,e:
  27.     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/

转自:http://wangx.in/sms-gateway-design-based-on-raspberry-pi/

分享到:
回复

使用道具 举报

回答|共 2 个

倒序浏览

沙发

ukonline2000

发表于 2013-6-17 15:16:00 | 只看该作者

树莓派群发器
博客站点:http://ukonline2000.com
网店:http://ukonline2000.taobao.com
板凳

haothree

发表于 2013-6-18 10:24:52 | 只看该作者

ukonline2000 发表于 2013-6-17 15:16
树莓派群发器

大神搞过这方面没?
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条