Using the Raspberry Pi as a SMS-gateway

From start there have been two clear aspects of the concept, one being to log the measurements remotely, the other being alerting the personale about potential hazardous conditions in the coldroom. With this concept being Ethiopian based, we found that the people who have to interact with it, have limited access to the internet which made e-mail communication highly problematic. Seeing that the Ethiopian people are using mobile services in a broad array of different use cases  such as for instans when paying for items, they pay with minutes of talk by transferring the minutes with simple call codes. This build towards implementing the user’s own phones in the overall system, having all interaction with the device go through SMS communication.
We ended up using a Huawei E3131 3G-dongle (E303 will also work), the important part being that the modem uses the HiLink software.

HiLink modem

The great thing about the HiLink technology is that the dongle mimics an ethernetcard, which makes the device virtually a plug-and-play product. On the raspberry we have to do some small tweaks, but more on that in the setup section.
HiLink uses a web based user interface for controlling the dongle, providing SMS-capabilities and other settings. We will use the API independent of the the UI and by doing URL-requests we are capable of utilizing every function of the dongle with simple PHP scripting.

HiLink API

With great help from this article by Chaddy HV, we have setup a php library with functions for the features we use. This is basically only retrieving messages by inbox and sending and deleting messages. The script can be downloaded from our github: Here.
The basics of the library is quite simple, the dongle sets up a web server at http://192.168.1.1/, by doing url request with appropriate xml-content attached we will receive a xml respons dependent on the feature requested. (The structure of the xml data to send and of the respons can be seen in the article mentioned earlier)

Setting up a HiLink dongle on raspberry pi

As we mentioned before there has to be done some small tweaks to use the dongle on the raspberry pi.
We have had this running on both model A and B running Wheesy, with both E3131 and E303, on a side note the dongles does not require any external powersource it will run plugged directly into the Pi’s usb port.

The main problem is, that the dongle is stuck in CD mode when plugged in and we want it to be in network mode. To automate the procedure of switching to network mode at startup we will need to do the following:

  1. Install sg3-utils by entering:
    apt-get install sg3-utils
  2. Create and edit the file 10-HuaweiFlashCard.rules:
    sudo nano /etc/udev/rules.d/10-HuaweiFlashCard.rules
  3. Now add the following to the content to the file:
    SUBSYSTEMS==”usb”, ATTRS{modalias}==”usb:v12D1p1F01*”, SYMLINK+=”hwcdrom”, RUN+=”/usr/bin/sg_raw /dev/hwcdrom 11 06 20 00 00 00 00 00 01 00″
  4. Edit interfaces:
    sudo nano /etc/network/interfaces
  5. Add this to the end of the interfaces file:
    allow-hotplug eth1
    iface eth1 inet dhcp
  6. Reboot the Pi

The pi should now connect to the internet through the 3G connection (or 2G), for more details on the setup see here

Using the Raspberry Pi as a SMS-gateway

What we did to use this setup as a SMS-gateway was to write a script that searches through any new messages received by the dongle for predefined keywords and do appropriate actions to instances of known keywords. The script can be seen here. The script is set up as a cron job, checking for new text messages every minute.

To setup a php cron job do the following:

  1. Retrive the position of your php installation:
    which php
    Returns some like:
    /usr/bin/php
  2. Add your script to the cron list:
    sudo crontab -e
    In the bottom of the page add:
    */1 * * * * /usr/bin/php >location of your script<

The cryptic last line, tells cron to run the php script at the location input every minute. If you want to setup another time interval, go here and see how to do cron job scheduling.

Advertisements

10 responses to “Using the Raspberry Pi as a SMS-gateway

  1. Hi emilbunk , i am glade it helped .
    i sow your script you may like to edit your function getList to function getList($boxNo,$pageNumber) and make the function a little more flexible .

    and thanks for referencing.

    • Hey Chaddy! Nice observation, i actually completely overlooked the fact that there could be multiple pages, it should be added now 🙂 – I think I will work on completing the script later on, but we have to turn in our bachelor thesis within the month, so this is basically only what we needed for the project.

      We are the ones saying thanks to you for sharing your work.

  2. awesome. Thanks for the help with installing the Huawei E3131 under Linux / Banana Pi. It works now like a charm.

  3. Hi,
    I have this error:
    PHP Fatal error: Call to undefined function curl_init() in /home/pi/scripts/php/hilink_smsGateway.php line 6 ( $ch = curl_init($URL);)

  4. My HiLink Huawei E3131 works perfectly, except for one thing…
    Its suddenly decided now that it wont display my received text messages.

    It says how many are in the inbox, but doesnt SHOW them when I click “Inbox”
    However, it shows all messages I sent when I click “Sent” and shows all drafts I leave in the “Drafts” folder.

    I can still access the internet, AND send texts, I just cant see replies.
    It does not require any updates, as it says I already have the latest software. What is the problem with it…?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s