Build an SMS Forwarder with Raspberry PI Zero W and Waveshare SIM7000E hat

In this guide, I will explain how to receive SMS messages and forward them to your Telegram account using Raspberry PI Zero W and Wireshare GSM hat.

Python will be used to read SMS messages and forward them to Telegram Bot API. Messages will be listened to by Gammu SMS service and it will trigger the Python script when an SMS message received.

Setup

First of all, we have to enable communication between GSM hat and Raspberry PI Zero, run Raspberry Pi Software Configuration Tool (raspi-config);

$ sudo raspi-config

Then, follow following actions;

Go to Interface Options
Select and enter P6 Serial Port
Select No at this screen.
Lastly, select Yes at this screen.

Exit configuration tool. Now we need to enable UART on raspberry pi. Shutdown and eject SD card to open in another computer. Open /boot/config.txt file, find the below statement and uncomment it to enable the UART. You can directly append it at the end of the file as well.

enable_uart=1

Now, reboot raspberry pi device and open the console to install Gammu SMS Deamon and Python PiP package installer.

$ sudo apt-get update
$ sudo apt-get install python-pip gammu-smsd

Install Telegram Bot API python library.

$ sudo pip install python-telegram-bot==12.3.0

Note: Version 12.3.0 is the latest version that is compatible with Python 2.7, if you want to use it with Python 3+ version, you can install the latest available version.

Now you need to create a bot in Telegram and obtain a token. Afterwards, start a chat with that bot in your Telegram account and send a test message to your bot. This will help us to identify your chat ID.

Open following URL with you Telegram Bot token and copy the chat ID in the response;

https://api.telegram.org/bot[YOUR-BOT-TOKEN]/getUpdates
This is your chat ID.

Save following script in /home/pi/forward-telegram.py;

#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import telegram

numparts = int(os.environ['DECODED_PARTS'])

text = ''
# Are there any decoded parts?
if numparts == 0:
    text = os.environ['SMS_1_TEXT']
# Get all text parts
else:
    for i in range(1, numparts + 1):
        varname = 'DECODED_%d_TEXT' % i
        if varname in os.environ:
            text = text + os.environ[varname]

# Log
print('Number %s have sent text: %s' % (os.environ['SMS_1_NUMBER'], text))

#Send by Telegram
bot = telegram.Bot(token='[YOUR-BOT-TOKEN]')
bot.send_message(chat_id=[YOUR CHAT ID], text=os.environ['SMS_1_NUMBER'].strip() + " | "+ text)

Make python file executable

$ chmod +x /home/pi/forward-telegram.py

Update Gammu SMS Deamon’s configuration to use GSM hat. Waveshare uses /dev/ttyS0 for communication.

$ sudo nano /etc/gammu-smsdrc

Find following lines in the configuration file and update with correct values, for me these values are following;

port = /dev/ttyS0
connection = at115200

Also, we need to specify the script path to be ran by Gammu so add following line below [smsd] section;

RunOnReceive = /home/pi/forward-telegram.py

You can find my complete configuration file here.

Restart Gammu SMS service.

$ sudo systemctl restart gammu-smsd.service

That’s all! You should now able to get your SMS messages to your Telegram account.

You can monitor Gammu SMS service for any problem with the following command;

$ sudo systemctl status gammu-smsd.service

9 Comment(s):

  1. Adri says:

    Sorry for bothering. What font are you using on the terminal?

    1. mtrcn says:

      Hi, I’m using OpenSSH in Windows Terminal with default fonts.

  2. Jan says:

    Nice article!
    Just question: Why do you need to edit config.txt with enable_uart=1 outside the raspberry? Does it get overwritten during shutdown?
    Also enable_uart=1 is already enabled in my latest fresh install (2021-03-04-raspios-buster-armhf-lite.zip)

    1. mtrcn says:

      I had experienced that in some installations, it was not enabled by default. Thus, I wrote it down in any case. Good call.

  3. Quest says:

    Hi Mete, For me it doesn’t work to power the SIM7000E via the RPi Zero pins. What steps are required to enable this?

    1. mete says:

      For me it also didn’t work with my first RPi Zero but I bought another one with a better solder header and then it solved the power issue 🙂

  4. call2vn says:

    Can we conmect 4-5 SIM7000E modules to one rasberry Pi for receive sms from 4-5 SIM cards?

  5. rebroad says:

    “you’ll need to create a bot within telegram” – erm… how?

    1. mete says:

      you can create a telegram bot by following these instructions; https://core.telegram.org/bots#how-do-i-create-a-bot

Leave a Reply

Your email address will not be published. Required fields are marked *