I want my Raspberry Pi Zero W to receive SMS on a spare SIM card / contract / number and send the contents to my mail account. Use case: some web services require your mobile number for registration but I do not like to give them my main number.
Update: Decided to try to use my OpenWRT instead of the Raspberry.
Update2: Fixed some issues with Modem switching from ttyUSB2 to ttyUSB3 and back.
Update3: Added ssmtp example for gmail.
Update4: Run in docker on Unraid server.
The Surf-Stick is in network mode by default, in this mode no serial devices ttyUSB
are offered
(Other Surf-Sticks offer the modem directly by default).
Sending/Receiving SMS could be handled using the web interface (as an API), but I would prefer to directly access the modem.
The device can be put in modem mode and then gammu
can be used to access it.
Unfortunately, receiving SMS did not work at first, while sending was working well.
Luckily, while experimenting I realized that changing the Preferred Message Storage solved that.
Connect Surf-Stick to your PC (due to headless RPi)
Disconnect and Disable automatic connection
192.168.0.1
Switch form network to modem (factory) mode:
http://192.168.0.1/goform/goform_process?goformId=MODE_SWITCH&switchCmd=FACTORY
Test if stick is in modem mode (on Linux)
dmesg
should list multiple GSM modem (1-port) converter now attached to ttyUSB0-2
ls -la /dev/ttyUSB*
should list 3 deviceslsusb
should list ID 19d2:0016 ZTE WCDMA Technologies MSM
Change Preferred Message Storage (as i was not able to receive sms but send them)
systemctl stop ModemManager.service
socat - /dev/ttyUSB2,crnl
AT+CPMS="SM","SM","SM"
Insert SD, Boot RPi, connect via ssh
Setup the modem software
sudo apt-get install usb-modeswitch usb-modeswitch-data
sudo apt-get install gammu
sudo adduser --system gammu --disabled-login --no-create-home
sudo adduser gammu dialout
/etc/gammurc
[gammu]
device = /dev/ttyUSB2
name = ZTE MF823 LTE USB-SurfStick
connection = at
use_locking = no
#use_locking = yes # disabled due to locking issues.
LogFile = /var/log/gammu
#LogFormat = textalldate
LogFormat = errorsdate
Test gammu:
gammu identify
gammu sendsms TEXT 0123456789 -text "Test Message"
gammu getallsms
Setup modem service:
Installation: sudo apt-get install gammu-smsd
Configuration:
/etc/gammu-smsdrc
[gammu]
device = /dev/ttyUSB2
name = ZTE MF823 LTE USB-SurfStick
connection = at
use_locking = yes
LogFile = /var/log/gammu
#LogFormat = textalldate
LogFormat = errorsdate
[smsd]
service = files
LogFile = /var/log/gammu
#DebugLevel = 4
DebugLevel = 0
CheckSecurity = 0
CheckBattery = 0
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/
RunOnReceive = /usr/local/bin/smsin.sh
(Re)Start gammu-smsd service:
systemctl restart gammu-smsd
Setup mail (uses external SMTP)
apt-get install ssmtp mailutils
/etc/ssmtp/ssmtp.conf
root=system@mydomain.tld
mailhub=mail.mydomain.tld:587
FromLineOverride=YES
AuthUser=system@mydomain.tld
AuthPass=SOMEPASSWORD
UseTLS=Yes
UseSTARTTLS=Yes
root=<user>@gmail.com
mailhub=smtp.gmail.com:587
FromLineOverride=YES
AuthUser=<user>
AuthPass=<application-password-without-spaces>
AuthMethod=LOGIN
UseTLS=Yes
UseSTARTTLS=Yes
rewriteDomain=gmail.com
Setup SMS forwarding to mail:
/usr/local/bin/smsin.sh
#!/bin/sh
for i in $(seq $SMS_MESSAGES) ; do
eval printf \"To: sms2mail@0in.de\\nFrom: hardworkinglittlerobot@gmail.com\\nSubject: SMS Service: New SMS\\n\\nSMS Service: New SMS\\nSender: \${SMS_${i}_NUMBER}\\n----\\n\${SMS_${i}_TEXT}\" \
| /usr/sbin/ssmtp sms2mail@0in.de
logger -t smsd "SMS forwarded by mail."
done
chmod +x /usr/local/bin/smsin.sh
The issues with ttyUSB lead me back to a RPi configuration...
Adjustments
opkg install kmod-usb-serial usbutils
/etc/rc.local
echo "19d2 0016" > /sys/bus/usb-serial/drivers/generic/new_id
Setup the SMS software
opkg install gammu
/etc/config/gammu
config daemon default
option enabled 1
option device /dev/ttyUSB2
option connection at
option logfile syslog
option onreceive /etc/smsin/smsin.sh
option onerror /etc/smsin/onfailure.sh
option send False
/etc/init.d/gammu
- Add following line after the line with "checkbattery" in it: echo -e "CheckSecurity = 0" >> $conffile
echo -e "ReceiveFrequency = 60" >> $conffile
echo -e "StatusFrequency = 60" >> $conffile
echo -e "CommTimeout = 60" >> $conffile
echo -e "SendTimeout = 60" >> $conffile
echo -e "LoopSleep = 10" >> $conffile
#echo -e "ResetFrequency = 3600" >> $conffile
Setup Mail and SMS forwarding to mail:
opkg install mailsend
/etc/smsin/smsin.sh
#!/bin/ash
for i in $(seq $SMS_MESSAGES) ; do
eval printf \"Sender: \${SMS_${i}_NUMBER}\\n\\n\${SMS_${i}_TEXT}\" | \
mailsend \
-smtp smtp.mydomain.tld \
-port 587 \
-starttls \
-auth-login \
-user system@mydomain.tld \
-pass SOMEPASSWORD \
-t mymail@mydomain.tld \
-f system@mydomain.tld \
-sub "SMS Service: New SMS"
logger -t smsd "SMS forwarded by mail."
rm /var/sms/inbox/$1
done
The following did not help, still searching for a solution...
Had some issues where the modem switched from ttyUSB2 to ttyUSB3 and thereby gammu loosing connection
Someone had the same problem and I was able to use the solution posted there: (de) https://forum-raspberrypi.de/forum/thread/46635-warum-disconnected-gsm-modem-zte-mit-gammu-gesteuert-alle-2-stunden/
However, I had to adapt the solution, as there is no udev functionality for OpenWRT. Sadly, it is just a hack, as the PRODUCT/VENDOR code is the same for all ttyUSB0,1,2/3 that are created by the stick, so I just hope that either 2 or 3 is the one I want to access....
/etc/hotplug.d/tty/10-lte-modem
SYMLINK="lte-modem"
case "${DEVICENAME}" in
ttyUSB2|ttyUSB3)
if [ "${ACTION}" = "add" ]; then
logger -t hotplug "<tty> Added LTE Modem: ${DEVICENAME}"
ln -s /dev/${DEVICENAME} /dev/${SYMLINK}
fi
if [ "${ACTION}" = "remove" ]; then
logger -t hotplug "<tty> Removed LTE Modem: ${DEVICENAME}"
rm /dev/${SYMLINK}
fi
;;
esac
/etc/config/gammu
[...]
option device /dev/lte-modem
[...]
/etc/init.d/cron start
/etc/init.d/cron enable
/etc/smsin/cron.sh
#!/bin/ash
/etc/init.d/gammu restart
crontab -e
0 * * * * /etc/smsin/cron.sh
- create folder in Unraid `..../appdata/sms2mail`
- put `gammurc`, `gammu-smsrc`, `ssmtp.conf`, and `smsin.sh` in folder.
- create `Dockerfile`
~~~
FROM alpine:3.18
#RUN apk add gammu
RUN apk add gammu-smsd
RUN apk add ssmtp
COPY ./gammurc /etc/gammurc
COPY ./gammu-smsrc /etc/gammu-smsdrc
RUN mkdir -p /var/spool/gammu/inbox/
RUN mkdir -p /var/spool/gammu/outbox/
RUN mkdir -p /var/spool/gammu/sent/
RUN mkdir -p /var/spool/gammu/error/
COPY ./ssmtp.conf /etc/ssmtp/ssmtp.conf
COPY ./smsin.sh /usr/local/bin/smsin.sh
CMD gammu-smsd
~~~
- Build the image: `docker build . --tag sms2mail`
- optionally test image: `docker run --rm sms2mail` (stop)
- Install necessary plugin in Unraid UI: `USB Manager Serial Options addon` to get `/dev/ttyUSB<x>` devices.
- Use the Unraid UI to create container:
Note: The container needs to run in priviledged mode to access the `/dev/ttyUSB<x>` devices
- Go to "Add Container", enter Name "sms2mail", Repository "sms2mail", choose Network type (only requires outgoing connection for ssmtp), and switch "Privileged" to "On".
Raspberry Pi - Receive/Send SMS (gammu)
Switch ZTE Surfstick to Modem Mode (instead of Network Mode which offers no serial USB interface)
SMS / CPMS Info
ZTE AT Commands
ZTE use SMS using calls to the URL interface
ZTE + OpenWRT
Gammu + OpenWRT