Huawei E173 3G (1/2): Huawei E173 Setup, Send and Receive SMS with Raspberry Pi 

Overview

Huawei E173 3G modem is a widely used GSM/UMTS USB wireless modem. It has a connection for an external antenna that can be useful in places with low mobile phone coverage.

In this series of posts, there will be two tutorials on setting up Huawei E173 on Raspberry Pi. This post is the first in the series and basically, we'll learn how to set up Huawei E173 to Raspberry Pi, send and receive SMS using Huawei E173 with Raspberry Pi.

 

Hardware

  • Raspberry Pi 3B+ (Compatible on Raspberry Pi Zero and Zero W, Raspberry Pi B+, Raspberry Pi 2 B, Raspberry Pi 3 BRaspberry Pi 3 B+, Raspberry Pi 3 A+)

  • Huawei E173 3G HSDPA USB Modem Dongle Stick With Sim Card 
  • 
    
    

     

    Software

    • RPI terminal
    • Internet Browser (Example Chrome)

     

    Application Discussion

    The Huawei HiLink E173 Cellular Modem is an ultra-slim device with a MicroSD card slot up to 32GB. This modem is a widely used GSM/UMTS USB wireless modem. It has a connection for an external antenna that can be useful in places with low mobile phone coverage. 

     

    In this application, we will use the Huawei E173 as a USB modem to Raspberry Pi. So you can send or receive messages on your raspberry pi, or make an automated text. There's a multiple application of this, you can use the Raspberry Pi as monitoring of sensors, automate send and receive of SMS.

     

    Setup Hardware

    Mount the Huawei E173 to Raspberry Pi like this.

     

    Setup Software

    Setup Huawei E173 to Raspberry Pi:

    • The first thing to do is to determine if the Huawei E173 is recognized by the Raspberry Pi. To do that open the Raspberry Pi terminal, then input the command below. This command will list all USB devices connected.
    $ lsusb
    • Next, look for your device in the list. It looks like this.
    Bus 001 Device 008: ID 12d1:1436 Huawei Technologies Co., Ltd. Broadband stick
    • Then install the required packages to run the Huawei E173 on the Raspberry Pi, input these following commands:
    $ sudo apt update
    $ sudo apt upgrade
    $ sudo apt install ppp usb-modeswitch usb-modeswitch-data
    $ sudo reboot
    • Check where's the Huawei E173 is mounted by inputting this command:
    $ dmesg | grep ttyUSB
    • If you're successful you will see this in your terminal.
    [   79.750375] usb 1-1.1.2: GSM modem (1-port) converter now attached to ttyUSB0
    [   79.751900] usb 1-1.1.2: GSM modem (1-port) converter now attached to ttyUSB1
    [   79.753044] usb 1-1.1.2: GSM modem (1-port) converter now attached to ttyUSB2
    • Take note of the first line ttyUSB which is the ttyUSB0 we will be using later. If you see it, proceed on to installing the SMS Software. If you don't see those in the terminal, try to disconnect and reconnect the device again. And input the command again, if not we will manually input it.
    • For unsuccessful installation (Skip this if you see the Huawei E173 mounted to ttyUSB):
    • First input this command,
    $ sudo nano /etc/usb_modeswitch.d/12d1:1446
    • Then copy these and paste them on your terminal editor.
    # Huawei E173 (3.se)
    
    TargetVendor=  0x12d1
    TargetProduct= 0x1446
    
    MessageContent="55534243123456780000000000000011062000000100000000000000000000"
    NoDriverLoading=1
    • To save it press the CTRL+X key, then press thekey, and press ENTER key to save.
    • Then run the following command again:
    $ dmesg | grep ttyUSB
    • Now you can see it, then we will proceed to install the SMS Software.

     

    Install SMS Software:

    • Install the Gammu package by inputting this command.
    $ sudo apt install gammu
    • Configure Gammu:
    $ sudo gammu-config
    • Follow the pictures as instructed below, Use the arrow keys to change selection.
    • If a configuration file exists, Select Yes (For new Proceed to next):

    • If a configuration file exists, Select Yes (For new Proceed to next):

    • Set the settings as indicated, for the port may vary where your Huawei E173 ttyUSB is mounted, after setting it up select save.

    • Select Yes to overwrite the configuration file.

    • Now the Configuration is saved.

    Notice:

    • Note sometimes the Huawei E173 mount designation ttyUSB will change just input this command if you encountered some issues while running it. 
    $ dmesg | grep ttyUSB
    • You will notice multiple lines, get the latest mounted ttyUSB and replace it on the Gammu Configuration as indicated sample below.
    [   79.750375] usb 1-1.1.2: GSM modem (1-port) converter now attached to ttyUSB0
    [   79.751900] usb 1-1.1.2: GSM modem (1-port) converter now attached to ttyUSB1
    [   79.753044] usb 1-1.1.2: GSM modem (1-port) converter now attached to ttyUSB2
    [21484.945781] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0
    [21485.021496] option1 ttyUSB1: GSM modem (1-port) converter now disconnected from ttyUSB1
    [21485.021989] option1 ttyUSB2: GSM modem (1-port) converter now disconnected from ttyUSB2
    [21495.781824] usb 1-1.3: GSM modem (1-port) converter now attached to ttyUSB0 				(!!!!!GET THIS!!!!)
    [21495.871603] usb 1-1.3: GSM modem (1-port) converter now attached to ttyUSB1
    [21495.931574] usb 1-1.3: GSM modem (1-port) converter now attached to ttyUSB2
    • Now we will identify our dongle by input this command:
    $ sudo gammu --identify
    • You can see the Gammu identify result like this.
    Device               : /dev/ttyUSB0
    Manufacturer         : Huawei
    Model                : E173 (E173)
    Firmware             : 11.126.85.00.209
    IMEI                 : 867142017684772
    SIM IMSI             : 515035300710162
    • Now you can send an SMS through the terminal by inputting this command. Replace ********** with the designated mobile number, you may also need to format it with the country code.
     $ echo "Createlabz test" | sudo gammu sendsms TEXT **********
    • After a few seconds, you will receive a text message to your designated mobile number with the message Createlabz test.

    Send SMS via Python:

    • Now we will send SMS using Huawei E173 USB modem using python-gammu API, open terminal, and input this command.
    $ sudo apt-get install python-gammu
    $ cd ~
    $ sudo git clone https://github.com/createlabz/createlabz-public-raspberry-pi
    $ cd /createlabz-public-raspberry-pi/usb_modem_dongle_huwaei_e173/gammu/send
    • For sending a short message run the send-sms.py python file by inputting this command. Replace ********** with the designated mobile number. For the code, you can check it in this Github link.
    
    

    $ sudo python send-sms.py **********

    • After a few seconds, you will receive a text message to your designated mobile number with the message python-gammu testing message. If you want to change the message, input this command.
    $ sudo nano send-sms.py
    • Next, look for this lines of code.
    c17:
    message = {
        'Text': 'python-gammu testing message',
        'SMSC': {'Location': 1},
        'Number': sys.argv[1],
    }
    • And change the enclosed python-gammu testing message data, to the message you desire. After changing it press the CTRL+X key, then press the Y key, and press ENTER key to save.
    • For sending a long message run the send-long-sms.py python file by inputting this command. Replace ********** with the designated mobile number. For the code, you can check it in this Github link.
    $ sudo python send-long-sms.py **********
    • After a few seconds, you will receive a text message to your designated mobile number with the message with a text Very long python-gammu testing message. If you want to change the message, input this command.
    $ sudo nano send-long-sms.py
    • Next, look for this line of code.
    'Very long python-gammu testing message '
    'sent from example python script. '
    'Very long python-gammu testing message '
    'sent from example python script. '
    'Very long python-gammu testing message '
    'sent from example python script. '
    • Change the enclosed messages to the message you want. After that press the CTRL+X key, then press the key, and press ENTER key to save.
    • Now you can send SMS both in the direct command line, and python code. Next, we will receive and reply SMS using a script.

     

    Receive and Reply SMS with a bash Script:

    • Already created a receive-sms.sh bash script code in the Createlabz library cloned earlier. Input this command to permit the script code to be executable.
    $ cd ~/createlabz-public-raspberry-pi/usb_modem_dongle_huwaei_e173/gammu/receive/
    $ sudo chmod u+rwx receive-sms.sh
    • The code will reply back the message you send with this format "Message Send (Message you send)", to replace it input this command.
    $ sudo nano receive-sms.sh
    • Then look for this code
    reply="Message Send $message"
    • Change the "Message Send $message" to your desired reply, the $message is the message you send to this sim card. After that press the CTRL+X key, then press the key, and press ENTER key to save.
    • Proceed to install Gammu SMSD Package, input this command.
    $ sudo apt install gammu-smsd
    • Next, update the Gammu SMSD configuration file, input this command.
    $ sudo nano /etc/gammu-smsdrc
    • Set the settings as indicated below, change the port and connection, for the port may differ where your USB Dongle is mounted that we take note of earlier. Add RunOnReceive variable below the [smsd] configuration and set as indicated below. 
    # Configuration file for Gammu SMS Daemon
    
    # Gammu library configuration, see gammurc(5)
    [gammu]
    # Please configure this!
    port = /dev/ttyUSB0
    connection = at19200
    # Debugging
    #logformat = textall
    
    # SMSD configuration, see gammu-smsdrc(5)
    [smsd]
    RunOnReceive = /home/pi/createlabz-public-raspberry-pi/usb_modem_dongle_huwaei_e173/gammu/receive/receive-sms.sh
    service = files
    logfile = syslog
    # Increase for debugging information
    debuglevel = 0
    
    # 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/
    • After that press the CTRL+X key, then press the key, and press ENTER key to save.
    • Now temporarily start Gammu SMSD with this command.
    $ sudo gammu-smsd
    • As you notice it won't return back to your command prompt, this is because the Gammu SMSD programming is running in the background. Try to send a message to your Huawei E173 Sim Card number, and it will reply to a message in a couple of seconds or the message you desire if you change the reply message. If you didn't receive any message then check the port of the gammu configuration file if it's the same with the mounted ttyUSB by inputting the command we use earlier. 
    • If you receive it, view the message you send, by pressing CTRL+C to cancel the running of the Gammu SMSD, then input this command to view the received SMS logs. 
    $ cd ~/createlabz-public-raspberry-pi/usb_modem_dongle_huwaei_e173/gammu/receive
    $ cat log_sms.txt
    • Now you can see the date, time, number, and message you received.

    Receive Bash Script Code Start at Boot

    • If you want the bash script to start at boot, we need to modify the gammu-smsd.service file, to do that input this command.
    $ cd /lib/systemd/system
    $ sudo nano gammu-smsd.service
    • Now look for this line of code:
    ExecStart=/usr/bin/gammu-smsd --pid=/var/run/gammu-smsd.pid --daemon
    • And replace the line of code with this.
    ExecStart=/usr/bin/gammu-smsd --config=/etc/gammu-smsdrc --pid=/var/run/gammu-smsd.pid --daemon
    • After that press the CTRL+X key, then press the key, and press ENTER key to save.
    • Now reload the system daemons, to reload gammu-smsd.service,
    $ sudo systemctl --system daemon-reload
    • Make sure the gammu-smsd.service is enabled.
    $ sudo systemctl enable gammu-smsd.service
    • To start the gammu-smsd.service immediately.
    $ sudo systemctl start gammu-smsd.service
    • Now check the status of the gammu-smsd.service if it's running.
    $ sudo systemctl status gammu-smsd.service | grep running
    • If it runs correctly you should see it like this.
     Active: active (running) since Thu 2021-03-04 08:43:32 GMT; 28min ago
    • Now all done!
    • For debugging, you should receive a reply if you send SMS even after restarting the Raspberry Pi if you didn't receive it, make sure to check the mounted ttyUSB and change port with the corresponding ttyUSB, then reload the gammu-smsd.service and input this command to restart the gammu-smsd.service.
    $ sudo systemctl --system daemon-reload
    $ sudo systemctl restart gammu-smsd.service

     

    Conclusion:

    Now you can send and receive SMS, you may explore other features like making automated text with sensor data, or creating an alarm that sends a message to you like if you forgot to close the door, and many more just integrate it with the codes above and you have a project.

     

    References:

    https://escapologybb.com/send-sms-from-raspberry-pi/

    https://escapologybb.com/receive-sms-raspberry-pi/

    https://docs.gammu.org/python/examples.html#sending-a-message

     

     

    Huawei e173Huawei e173 3g hsdpa usb modem dongle stickHuawei e173 raspberry piHuawei e173 receive smsHuawei e173 send smsHuawei e173 setupUsb modem dongle stick

    Leave a comment

    All comments are moderated before being published