3G USB dongle
Packages we will require:
- eject - for converting the device from a CD-ROM into a modem
- ppp - for pppd and chat applications
- gammu and gammu-smsd - for receiving SMS text messages and running commands
- postfix - for sending email messages
sudo apt-get update
sudo apt-get install -y eject ppp gammu gammu-smsd postfix
Configure postfix as an Internet mail server.
Convert from CD-ROM to modem
The '3' 3G USB dongle I'm connecting to my Raspberry Pi is a ZTE MF627. It has two modes, when it's originally plugged in it appears as a USB CD-ROM, for automatically installing software drivers, and has to be ejected before it turns into a network device. When the device is a CD-ROM it's ID is
19d2:2000, when it's a network device the ID is
19d2:0031.
USB Modeswitch or UDEV
If you have a modern Rasprian then USB Modeswitch will be installed and it will automatically eject the CD-ROM device for you. Otherwise you can run the eject command from udev, like this.
Create:
/etc/udev/rules.d/10-ZTE_MF627.rules
with this contents:
SUBSYSTEM=="block", KERNEL=="sr[0-9]*", SUBSYSTEMS=="usb", ATTRS{idVendor}=="19d2", ATTRS{idProduct}=="2000", RUN+="/bin/sh -c '/bin/sleep 12; /usr/bin/eject %k; /vib/sleep 1; /usr/bin/eject %k;'&", OPTIONS+="last_rule"
Now when the dongle is inserted the udev daemon spots the device and sends the eject command. You can check the device ID with lsusb.
The device will appear as three tty ports when its in modem mode.
- ttyUSB0
- ttyUSB1 used for sending sms and seeing the status of the device
- ttyUSB2 used for ppp connections
We can test the device now with chat:
chat -s -v '' ATZ OK AT+CSQ OK >/dev/ttyUSB1 </dev/ttyUSB1
in the output you'll see the current signal strength.
Once you have the device switched to be a modem you can disable the CD-ROM.
chat -s -v '' ATZ OK AT+ZCDRUN=8 OK >/dev/ttyUSB1 </dev/ttyUSB1
The device will now only appear as the serial modem.
Run pppd
First we'll create a pppd config file and it's associated modem chat file.
Create:
/etc/ppp/peers/giffgaff
with this contents:
/dev/ttyUSB2
460800
user giffgaff
password password
defaultroute
connect "/usr/sbin/chat -v -f /etc/ppp/peers/giffgaff.chat"
ipparam giffgaff
hide-password
lock
maxfail 0
mtu 1492
noauth
noipdefault
persist
usepeerdns
Create:
/etc/ppp/peers/giffgaff.chat
with this contents:
ABORT BUSY ABORT 'NO CARRIER'
ABORT VOICE ABORT 'NO DIALTONE'
ABORT 'NO DIAL TONE' ABORT 'NO ANSWER'
ABORT DELAYED
'' ATZ
OK ATQ0V1E1S0=0&C1&D2+FCLASS=0
OK AT+CGDCONT=1,"ip","giffgaff.com"
OK-AT-OK ATDT*99***1#
CONNECT \d\c
Create:
/etc/ppp/ip-up.d/default_route
with this contents:
#!/bin/sh -e
# Called when a new interface comes up
/sbin/route del default
Make it runnable
chmod a+x /etc/ppp/ip-up.d/default_route
and test with
pon giffgaff
Run gammu
This will run gammu from udev when the device is added, and stop it when the device is removed.
Create:
/usr/local/bin/ZTE_MF627
with this contents:
#!/bin/sh -e
logger "$0" "$ACTION"
case $ACTION in
add )
sleep 2
service gammu-smsd stop
sleep 2
pon giffgaff
sleep 10
service gammu-smsd start
;;
remove )
service gammu-smsd stop
poff giffgaff
;;
esac
KERNEL=="ttyUSB0", SUBSYSTEM=="tty", ATTRS{idVendor}=="19d2", ATTRS{idProduct}=="0031", RUN+="/usr/local/bin/ZTE_MF627", OPTIONS+="last_rule"