Skip to content

Instantly share code, notes, and snippets.

@ethaniel
Last active April 9, 2024 03:44
Show Gist options
  • 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

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