Skip to content

Instantly share code, notes, and snippets.

@ethaniel
Last active April 9, 2024 03:44
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ethaniel/d7f9c3192041c64c89d2c5b49527d0e2 to your computer and use it in GitHub Desktop.
Save ethaniel/d7f9c3192041c64c89d2c5b49527d0e2 to your computer and use it in GitHub Desktop.
HOWTO: Receive SMS via 4G/LTE Huawei stick on Raspberry Pi 4 and forward them via Telegram

HOWTO: Receive SMS via 4G/LTE Huawei stick on Raspberry Pi 4 and forward them via Telegram

I live abroad and have only 1 sim card slot in my phone. It holds the SIM card of the country that I am in right now. But I also have another SIM card from my home country which receives my banking SMS codes. I can't afford to lose the "home" SIM card, so I decided to keep it in my house and forward the SMS messages to my main phone and computer via Telegram (just like Whatsapp, but so much better).

I also made a choice to use a 4G/LTE stick instead of 3G, because the 3G signal in my area is getting worse in worse due to operators upgrading their equipment.

Prerequisites

  1. Raspberry Pi 4
  2. Huawei E8372 (but can be any similar)

Step 1 - Get the Huawei card to work properly with Raspberry

Since I'm using a 4G/LTE Huawei card, there is an issue with it being originally booted in a "Hilux" mode which does not allow me to read incoming SMS messages.

To fix this, I need a special app call usb_modeswitch:

sudo apt update
sudo apt install usb_modeswitch

⚠️⚠️⚠️ usb_modeswitch -e should show you at least version 2.5.2. If it doesn't, something is wrong. Old versions of usb_modeswitch don't switch modes on this modem properly, so make sure that you have the latest version

Now you only need to set HuaweiAltModeGlobal=1 in /etc/usb_modeswitch.conf:

sudo sed -i "s/HuaweiAltModeGlobal=.*/HuaweiAltModeGlobal=1/" /etc/usb_modeswitch.conf

Now plug in the Huawei stick into the Raspberry, wait 10-15 seconds and the dmesg command should show you something like this:

[ 5053.514075] option 1-1.2:1.0: GSM modem (1-port) converter detected
[ 5053.514424] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB0
[ 5053.514962] option 1-1.2:1.1: GSM modem (1-port) converter detected
[ 5053.517558] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB2
[ 5053.518068] option 1-1.2:1.2: GSM modem (1-port) converter detected
[ 5053.518991] usb 1-1.2: GSM modem (1-port) converter now attached to ttyUSB3

If you don't see the ttyUSB0 above, then something is wrong. Do not proceed further.

If everything is OK, then you can add a udev rule which will attach the Huawei stick to /dev/sms. It is needed for the next step. Put the following code into /etc/udev/999-sms-gateway.rules:

SUBSYSTEM=="tty", ATTRS{idVendor}=="12d1", ATTRS{idProduct}=="155e", ENV{ID_USB_INTERFACE_NUM}=="00", SYMLINK+="sms", RUN+="/usr/bin/killall -SIGHUP gammu-smsd"

Important: 12d1 and 155e parts come from lsusb (Bus 001 Device 017: ID 12d1:155e Huawei Technologies Co., Ltd.). If you're using another card, then the 155e part might be different. So switch accordingly.

Unplug the Huawei card and plug it back in. After 10-15 seconds ls -l /dev/sms should start showing you this lrwxrwxrwx 1 root root 7 Apr 12 15:15 /dev/sms -> ttyUSB0 (or similar).

Step 2 - Receive SMS messages via Gammu

We will need a special program called Gammu. It will communicate with the Huawei stick and process the incoming SMS messages.

sudo apt install gammu

Put the following configuration into /etc/gammu-smsdrc:

# Configuration file for Gammu SMS Daemon

# Gammu library configuration, see gammurc(5)
[gammu]
port = /dev/sms
connection = at

# SMSD configuration, see gammu-smsdrc(5)
[smsd]
service = files
logfile = /var/log/gammu.log
CheckSecurity = 0
MultipartTimeout=20
# Increase for debugging information
debuglevel=0
HangupCalls=1

RunOnReceive=/gammu.sh

# Paths where messages are stored
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error

Restart gammu with the new settings: sudo /etc/init.d/gammu-smsd restart Verify that gammu is working via: tail -f /var/log/gammu.log

Step 3 - forward SMS messages via Telegram

I won't go into the details of registering a bot via Telegram. It's pretty simple - send a message to @BotFather, register the robot and you will receive the authorization Token that you need to input into the script below.

Then you contact @get_id_bot to get your "Chat ID" (it's your hidden Telegram user id). Put it into the code below as well.

Send any message to your bot from your Telegram account. This will allow your bot to send you messages back (bots can't message random users first, this is done to fight spam).

Put the following text into /gammu.sh (this is what actually sends the message via Telegram):

#!/bin/bash

TOKEN="1733*****:AA****" # you get this from the @BotFather after creating your bot
CHAT_ID="9341******" # you get this from @get_id_bot

# no need to change anything below.

URL="https://api.telegram.org/bot${TOKEN}/sendMessage"

NL=$'\n'
declare -i i MSGS
MSGS=${SMS_MESSAGES}
CONTENT="${SMS_1_NUMBER}${NL}"
i=1
while [ $i -le $MSGS ]; do
    declare "PART"="SMS_${i}_TEXT"
    CONTENT="${CONTENT}${!PART}"
    i=$(($i+1))
done
CONTENT=${CONTENT//&/%26}
CONTENT=${CONTENT//</%3C}
CONTENT=${CONTENT//>/%3E}

eval "curl -s -X POST $URL -d chat_id=\"${CHAT_ID}\" -d text=\"${CONTENT}\""

Don't forget to chmod 755 /gammu.sh

Step 5 - restart Huawei stick without unplugging

I've noticed that sometimes my Huawei stick freezes and needs to be restarted. So I wrote a little script which restarts it every 15 minutes. Put it into /reset.sh:

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'


datas=$(lsusb | grep -i hua | awk '/Bus/ {print $6}' | tr ":" "\n")
counter=0
for line in $datas
do
        counter=$((counter+1))
        if [ $counter = 1 ]
                then
                        VENDOR=$(echo "$line")
        fi

        if [ $counter = 2 ]
                then
                       PRODUCT=$(echo "$line")
        fi
done


for DIR in $(find /sys/bus/usb/devices/ -maxdepth 1 -type l); do
  if [[ -f $DIR/idVendor && -f $DIR/idProduct &&
        $(cat $DIR/idVendor) == $VENDOR && $(cat $DIR/idProduct) == $PRODUCT ]]; then
    echo found $DIR
    echo 0 > $DIR/authorized
    sleep 1.5
    echo 1 > $DIR/authorized
  fi
done

Don't forget to chmod 755 /reset.sh and add the following to sudo crontab -e -u root:

*/15 * * * * /reset.sh >/dev/null 2>&1

Links:

  1. https://sms.ru/
@rifqifebrianmrf
Copy link

Hello, i am a little bit confused to apply this steps into my raspberry pi 4. I have done the steps until I download the latest usb-modeswitch-data package, however, I don't see the ttyUSB0 in dmesg. what did i do wrong? can you help me to configure it out? thank you

@ethaniel
Copy link
Author

Hello, i am a little bit confused to apply this steps into my raspberry pi 4. I have done the steps until I download the latest usb-modeswitch-data package, however, I don't see the ttyUSB0 in dmesg. what did i do wrong? can you help me to configure it out? thank you

What does usb_modeswitch -e show? Did you run ./gen-rules.tcl?

@vthielen
Copy link

Hi, I am trying to get the Huawei E8372 to work as a regular modem that accepts AT commands.
I downloaded and installed usb_modeswitch 2.6.0, but the modem keeps switching to Hilink mode (and I need direct access to the IP address that the phone company provides). I don't get any /dev/ttyUSB devices.
I have the impression that it switches by itself to this mode (even if I disable modeswitch in the /etc/usb_modeswitch.conf file), and doesn't stay in 'mass_storage' mode where it can be changed to the mode that I need by usb_modeswitch.
What is the firmware version of your modem? Maybe Huawei changed something there

When I plug it in the following appears in /var/log/syslog:

Apr 13 15:25:40 Test11 systemd[1]: dev-sda1.device: Job dev-sda1.device/start timed out.
Apr 13 15:25:40 Test11 systemd[1]: Timed out waiting for device dev-sda1.device.
Apr 13 15:25:40 Test11 systemd[1]: Dependency failed for /mnt/log.
Apr 13 15:25:40 Test11 systemd[1]: mnt-log.mount: Job mnt-log.mount/start failed with result 'dependency'.
Apr 13 15:25:40 Test11 systemd[1]: dev-sda1.device: Job dev-sda1.device/start failed with result 'timeout'.
Apr 13 15:25:48 Test11 kernel: [ 388.830010] usb 1-1.4: new high-speed USB device number 9 using dwc_otg
Apr 13 15:25:48 Test11 kernel: [ 388.961754] usb 1-1.4: New USB device found, idVendor=12d1, idProduct=1f01
Apr 13 15:25:48 Test11 kernel: [ 388.961782] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Apr 13 15:25:48 Test11 kernel: [ 388.961793] usb 1-1.4: Product: HUAWEI_MOBILE
Apr 13 15:25:48 Test11 kernel: [ 388.961802] usb 1-1.4: Manufacturer: HUAWEI_MOBILE
Apr 13 15:25:48 Test11 kernel: [ 388.961811] usb 1-1.4: SerialNumber: 0123456789ABCDEF
Apr 13 15:25:48 Test11 kernel: [ 388.969087] usb-storage 1-1.4:1.0: USB Mass Storage device detected
Apr 13 15:25:48 Test11 kernel: [ 388.986379] scsi host0: usb-storage 1-1.4:1.0
Apr 13 15:25:48 Test11 mtp-probe: checking bus 1, device 9: "/sys/devices/platform/soc/20980000.usb/usb1/1-1/1-1.4"
Apr 13 15:25:48 Test11 mtp-probe: bus: 1, device: 9 was not an MTP device
Apr 13 15:25:48 Test11 systemd[1]: Starting USB_ModeSwitch_1-1.4:1.0...
Apr 13 15:25:49 Test11 kernel: [ 390.609975] usb 1-1.4: reset high-speed USB device number 9 using dwc_otg
Apr 13 15:25:50 Test11 kernel: [ 390.740458] usb 1-1.4: device firmware changed
Apr 13 15:25:50 Test11 kernel: [ 390.741099] usb 1-1.4: USB disconnect, device number 9
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: couldn't open "/sys/bus/usb/devices/1-1.4/devnum": no such file or directory
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: while executing
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: "open $dir/$attr r"
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: invoked from within
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: "if [file exists $dir/$attr] {
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: #011#011set rc [open $dir/$attr r]
Apr 13 15:25:50 Test11 kernel: [ 390.890122] usb 1-1.4: new high-speed USB device number 10 using dwc_otg
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: #011#011set usb($attr) [string trim [read -nonewline $rc]]
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: #011#011close $rc
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: #011} else {
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: #011#011set usb($attr)..."
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: (procedure "ReadUSBAttrs" line 13)
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: invoked from within
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: "ReadUSBAttrs $devdir"
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: (procedure "Main" line 125)
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: invoked from within
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: "Main $argv $argc"
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: (file "/usr/sbin/usb_modeswitch_dispatcher" line 1031)
Apr 13 15:25:50 Test11 systemd[1]: usb_modeswitch@1-1.4:1.0.service: Main process exited, code=exited, status=1/FAILURE
Apr 13 15:25:50 Test11 systemd[1]: Failed to start USB_ModeSwitch_1-1.4:1.0.
Apr 13 15:25:50 Test11 kernel: [ 391.021787] usb 1-1.4: New USB device found, idVendor=12d1, idProduct=14db
Apr 13 15:25:50 Test11 kernel: [ 391.021850] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=0
Apr 13 15:25:50 Test11 kernel: [ 391.021863] usb 1-1.4: Product: HUAWEI_MOBILE
Apr 13 15:25:50 Test11 kernel: [ 391.021872] usb 1-1.4: Manufacturer: HUAWEI_MOBILE
Apr 13 15:25:50 Test11 systemd[1]: usb_modeswitch@1-1.4:1.0.service: Unit entered failed state.
Apr 13 15:25:50 Test11 systemd[1]: usb_modeswitch@1-1.4:1.0.service: Failed with result 'exit-code'.
Apr 13 15:25:50 Test11 kernel: [ 391.105147] cdc_ether 1-1.4:1.0 eth1: register 'cdc_ether' at usb-20980000.usb-1.4, CDC Ethernet Device, 00:1e:10:1f:00:00
Apr 13 15:25:50 Test11 mtp-probe: checking bus 1, device 10: "/sys/devices/platform/soc/20980000.usb/usb1/1-1/1-1.4"
Apr 13 15:25:50 Test11 mtp-probe: bus: 1, device: 10 was not an MTP device
Apr 13 15:25:50 Test11 dhcpcd[417]: eth1: waiting for carrier
Apr 13 15:25:50 Test11 dhcpcd[417]: eth1: carrier acquired
Apr 13 15:25:50 Test11 dhcpcd[417]: eth1: IAID 10:1f:00:00
Apr 13 15:25:50 Test11 dhcpcd[417]: eth1: adding address fe80::21e:10ff:fe1f:0
Apr 13 15:25:50 Test11 dhcpcd[417]: eth1: carrier lost
Apr 13 15:25:50 Test11 dhcpcd[417]: eth1: deleting address fe80::21e:10ff:fe1f:0
Apr 13 15:25:58 Test11 dhcpcd[417]: eth1: carrier acquired
Apr 13 15:25:58 Test11 dhcpcd[417]: eth1: IAID 10:1f:00:00
Apr 13 15:25:58 Test11 dhcpcd[417]: eth1: adding address fe80::21e:10ff:fe1f:0
Apr 13 15:25:59 Test11 dhcpcd[417]: eth1: rebinding lease of 192.168.8.101
Apr 13 15:25:59 Test11 dhcpcd[417]: eth1: soliciting an IPv6 router
Apr 13 15:26:00 Test11 avahi-daemon[260]: Joining mDNS multicast group on interface eth1.IPv6 with address fe80::21e:10ff:fe1f:0.
Apr 13 15:26:00 Test11 avahi-daemon[260]: New relevant interface eth1.IPv6 for mDNS.
Apr 13 15:26:00 Test11 avahi-daemon[260]: Registering new address record for fe80::21e:10ff:fe1f:0 on eth1.*.
Apr 13 15:26:01 Test11 ntpd[479]: Listen normally on 8 eth1 [fe80::21e:10ff:fe1f:0%4]:123
Apr 13 15:26:04 Test11 dhcpcd[417]: eth1: Router Advertisement from fe80::ba27:c5ff:feb5:f2db
Apr 13 15:26:04 Test11 dhcpcd[417]: eth1: requesting DHCPv6 information
Apr 13 15:26:04 Test11 dhcpcd[417]: eth1: probing for an IPv4LL address
Apr 13 15:26:04 Test11 dhcpcd[417]: eth1: DHCP lease expired
Apr 13 15:26:04 Test11 dhcpcd[417]: eth1: soliciting a DHCP lease
Apr 13 15:26:04 Test11 dhcpcd[417]: eth1: offered 192.168.8.101 from 192.168.8.1
Apr 13 15:26:05 Test11 dhcpcd[417]: eth1: probing address 192.168.8.101/24
Apr 13 15:26:09 Test11 dhcpcd[417]: eth1: leased 192.168.8.101 for 86400 seconds
Apr 13 15:26:09 Test11 avahi-daemon[260]: Joining mDNS multicast group on interface eth1.IPv4 with address 192.168.8.101.
Apr 13 15:26:09 Test11 avahi-daemon[260]: New relevant interface eth1.IPv4 for mDNS.
Apr 13 15:26:09 Test11 avahi-daemon[260]: Registering new address record for 192.168.8.101 on eth1.IPv4.
Apr 13 15:26:09 Test11 dhcpcd[417]: eth1: adding route to 192.168.8.0/24
Apr 13 15:26:09 Test11 dhcpcd[417]: eth1: adding default route via 192.168.8.1
Apr 13 15:26:11 Test11 kernel: [ 411.930529] Under-voltage detected! (0x00050005)
Apr 13 15:26:11 Test11 ntpd[479]: Listen normally on 9 eth1 192.168.8.101:123
Apr 13 15:26:17 Test11 kernel: [ 418.170608] Voltage normalised (0x00000000)
Apr 13 15:26:19 Test11 dhcpcd[417]: eth1: Router Advertisement from fe80::ba27:c5ff:feb5:f2db
Apr 13 15:26:19 Test11 dhcpcd[417]: eth1: requesting DHCPv6 information
Apr 13 15:26:19 Test11 kernel: [ 420.250724] Under-voltage detected! (0x00050005)
Apr 13 15:26:25 Test11 kernel: [ 426.490814] Voltage normalised (0x00000000)
Apr 13 15:26:32 Test11 kernel: [ 432.730964] Under-voltage detected! (0x00050005)

@rifqifebrianmrf
Copy link

Hello, i am a little bit confused to apply this steps into my raspberry pi 4. I have done the steps until I download the latest usb-modeswitch-data package, however, I don't see the ttyUSB0 in dmesg. what did i do wrong? can you help me to configure it out? thank you

What does usb_modeswitch -e show? Did you run ./gen-rules.tcl?

it shows version 2.5.2:

  • usb_modeswitch: handle USB devices with multiple modes
  • Version 2.5.2 (C) Josua Dietze 2017
  • Based on libusb1/libusbx

yes i ran the ./gen-rules.tcl

@ethaniel
Copy link
Author

ethaniel commented Apr 13, 2021

Apr 13 15:25:48 Test11 systemd[1]: Starting USB_ModeSwitch_1-1.4:1.0...
Apr 13 15:25:49 Test11 kernel: [ 390.609975] usb 1-1.4: reset high-speed USB device number 9 using dwc_otg
Apr 13 15:25:50 Test11 kernel: [ 390.740458] usb 1-1.4: device firmware changed
Apr 13 15:25:50 Test11 kernel: [ 390.741099] usb 1-1.4: USB disconnect, device number 9
Apr 13 15:25:50 Test11 usb_modeswitch_dispatcher[1640]: couldn't open "/sys/bus/usb/devices/1-1.4/devnum": no such file or directory

@vthielen according to these lines in your log, usb_modeswitch does not manage to make the changes in time. Something else is sending the commands to the usb stick (perhaps an older version of usb_modeswitch?)

@ethaniel
Copy link
Author

Hello, i am a little bit confused to apply this steps into my raspberry pi 4. I have done the steps until I download the latest usb-modeswitch-data package, however, I don't see the ttyUSB0 in dmesg. what did i do wrong? can you help me to configure it out? thank you

What does usb_modeswitch -e show? Did you run ./gen-rules.tcl?

it shows version 2.5.2:

  • usb_modeswitch: handle USB devices with multiple modes
  • Version 2.5.2 (C) Josua Dietze 2017
  • Based on libusb1/libusbx

yes i ran the ./gen-rules.tcl

@rifqifebrianmrf can you please do sudo sed -i "s/HuaweiAltModeGlobal=.*/HuaweiAltModeGlobal=1/" /etc/usb_modeswitch.conf and unplug/plug in your USB stick?

@rifqifebrianmrf
Copy link

Hello, i am a little bit confused to apply this steps into my raspberry pi 4. I have done the steps until I download the latest usb-modeswitch-data package, however, I don't see the ttyUSB0 in dmesg. what did i do wrong? can you help me to configure it out? thank you

What does usb_modeswitch -e show? Did you run ./gen-rules.tcl?

it shows version 2.5.2:

  • usb_modeswitch: handle USB devices with multiple modes
  • Version 2.5.2 (C) Josua Dietze 2017
  • Based on libusb1/libusbx

yes i ran the ./gen-rules.tcl

@rifqifebrianmrf can you please do sudo sed -i "s/HuaweiAltModeGlobal=.*/HuaweiAltModeGlobal=1/" /etc/usb_modeswitch.conf and unplug/plug in your USB stick?

I have done it, but now my raspberry pi won't connect to the usb modem (the network does not appear so i cannot connect to raspi through ssh)

@L1so
Copy link

L1so commented Aug 27, 2021

Hello, I managed to send sms using Gammu, but I cant do so while connected to modem's internet, can I do both tasks?

@iformas
Copy link

iformas commented Oct 27, 2022

Hi
sudo apt install usb_modeswitch should be "sudo apt install usb-modeswitch"

I installed a fresh Raspbian x64.. y updated and upraded apt-get and I can't locate gammu package

apt-get install gammu
E: Unable to locate package gammu

@hogliux
Copy link

hogliux commented Jan 11, 2023

I've put together a few notes on modifications/tweaks I needed to do to get this running on my rpi:

https://gist.github.com/hogliux/e7c7f356c59f8711708de8d0ee3c20cd

I hope this is useful to someone.

@Terabyte9
Copy link

create the file 999-sms-gateway.rules in /etc/udev/rules/.d and not in /etc/udev/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment