openwrt使用smstools3通过USBmodem收SMS转发到即时通信的笔记(已完成,20230908更新)
需要看实操的请直接跳到第5点,前面都是废话
1、安装包:kmod-usb-serial kmod-usb-serial-option usb-modeswitch usbutils smstools
2、USB口的打开、关闭(纯粹参考)
https://openwrt.org/docs/guide-user/hardware/usb.overview
On:
echo 1 > /sys/class/gpio/gpioN/value
Off:
echo 0 > /sys/class/gpio/gpioN/value
Get current state:
cat /sys/class/gpio/gpioN/value
3、参考文档:(参考的太多了)
https://blog.csdn.net/d9394952/article/details/87868844
https://blog.csdn.net/d9394952/article/details/94197066
http://smstools3.kekekasvi.com/index.php?p=configure <----这个是smstools3的官网所在,主要看这个就好
https://lte.pw/lede-openwrt-smstools3-push-sms-to-telegram.html
https://qmsg.zendee.cn/api
https://blog.csdn.net/wh_luosangnanka5/article/details/7779194
https://ozeki-sms-gateway.com/p_136-how-to-receive-an-sms-message-with-a-modem-using-at-commands.html
https://openwrt.org/docs/guide-user/advanced/howto.send.sms
https://www.smssolutions.net/tutorials/gsm/sendsmsat/
https://openwrt.org/docs/guide-user/advanced/sms-from-shell
https://blog.longwin.com.tw/2017/12/bash-shell-curl-send-urlencode-args-2017/
https://blog.csdn.net/baidu_35692846/article/details/107516123
https://blog.csdn.net/thinkerman26/article/details/89026245
https://www.jianshu.com/p/4bd9102a6a61
4、其它参考脚本:(纯粹参考)
#!/bin/bash ID= #chat_id TOKEN= #token LOOP=3 DATE=$(date '+%Y/%m/%d %H:%M') if [ "$1" == "RECEIVED" ]; then FROM=$(sed -n '1p' < "$2" | awk -F ': ' '{printf $2}') BODY=$(sed -e '1,/^$/ d' < "$2" | iconv -f UNICODEBIG -t UTF-8) CONTENT="新短信%20$DATE%0a来自:%20$FROM%0a$BODY" echo -e "$DATE\n$FROM\n$BODY\n" >> /root/Messenger/sms.log fi toQmsg(){ curl -d "qq=$ID&msg=$CONTENT" https://qmsg.zendee.cn/send/${TOKEN} --retry 3 --retry-delay 5 } toTG(){ curl -d "chat_id=$ID&text=$CONTENT" https://api.telegram.org/bot${TOKEN}/sendMessage --retry 3 --retry-delay 5 } forward(){ #toQmsg #toTG if [ $? -ne 0 -a $LOOP -gt 0 ]; then echo "failure at $(date '+%Y/%m/%d %H:%M')" ((LOOP--)) sleep 60 forward fi } forward
安装1的包
opkg update opkg install kmod-usb-serial kmod-usb-serial-option usb-modeswitch usbutils smstools iconv
查看USBmodem的dev参数,我这里用的是12d1:1506
lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 003: ID 12d1:1506 Huawei Technologies Co., Ltd. Modem/Networkcard Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub lsusb -t /: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci-mtk/1p, 5000M /: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci-mtk/2p, 480M |__ Port 1: Dev 3, If 0, Class=Vendor Specific Class, Driver=usbserial_generic, 480M |__ Port 1: Dev 3, If 1, Class=Vendor Specific Class, Driver=, 480M |__ Port 1: Dev 3, If 2, Class=Vendor Specific Class, Driver=usbserial_generic, 480M |__ Port 1: Dev 3, If 3, Class=Vendor Specific Class, Driver=usbserial_generic, 480M |__ Port 1: Dev 3, If 4, Class=Mass Storage, Driver=usbserial_generic, 480M
创建usb-serial文件(2个),用于指定usbmodem的工作模式
cd /etc/usb_modeswitch.d/ vi 12d1:1506 TargetVendor=0x12d1 TargetProduct=0x1506 SierraMode=1 cd /etc/modules.d/ vi usb-serial usbserial vendor=0x12d1 product=0x1506
修改/etc/smsd.conf
# Description: Main configuration file for the smsd
#
devices = GSM1
incoming = /var/spool/sms/incoming
outgoing = /var/spool/sms/outgoing
checked = /var/spool/sms/checked
failed = /var/spool/sms/failed
sent = /var/spool/sms/sent
receive_before_send = no
autosplit = 3
logfile = 1
loglevel = 7
eventhandler = /root/pushsms.sh
#incoming_utf8 = yes
# Uncomment (and edit) this section to allow smsd to start: # [GSM1]
# 将短信保存在上网卡中(更快,不支持的话请使用 SM 模式)
init = AT+CPMS="ME","ME","ME"
# 将短信保存在 USIM 卡中
# init = AT+CPMS="SM","SM","SM"
device = /dev/ttyUSB0
incoming = yes
# 如果 USIM 卡有设置 pin 码需要设置这行来解锁
# pin = 0000
# 波特率,一般都是 115200,不对的话请自行修改
baudrate = 115200
# 如果检测得到信息但是无法读取需要修改这里的读取模式
check_memory_method = 2
report_device_details = yes
signal_quality_ber_ignore = yes
check_network = no
创建文件/root/pushsms.sh
#!/bin/sh echo $1 $2>> /dev/shm/sms.log if [ "$1" == "RECEIVED" ]; then FROM=$(grep "From:" $2) TEXT=$(sed -e '1,/^$/ d' < "$2" | iconv -f UNICODEBIG -t UTF-8) curl --data-urlencode "chat_id=<your id>" --data-urlencode "text=$TEXT%0a$FROM" -X POST https://api.telegram.org/bot<token>/sendMessage fi
启动smstools3
/etc/init.d/smstoolse start
然后就可以通过即时通信工作接收SMS短信了
20230831补充:
发现短信只是重启后能读一次,后面好像就不管用了,不知道是华为的问题还是SMSTOOLS的问题,所以暂时的解决办法:
opkg install usbreset
然后定时执行:
usbreset 12d1:1506
20230908补充:
查看日志发现:/dev/ttyUSB0会莫明消失,导致smstools3报I/O error错
所以加个脚本reset_usb.sh:
#!/bin/sh usbreset `lsusb | grep -v "root hub" | awk '{print $6}'`
再往smsd.conf文件里添加一行,作用是smstools3发现有错时,直接就复位USB口:
alarmhandler = /root/reset_usb.sh