Send and receive SMS using GSM modem or phone on Ubuntu

Huawei e220I currently use mobile broadband as my primary Internet connection and this worked great on my Linux PCs for a while. The GSM modem I'm using every day is good old Huawei e220, but there is one aspect of this great hardware device I haven't used on my Linux PCs, and that is sending/receiving SMS messages. In this article I will show you how to send and receive SMS messages using GSM modem or phone on Ubuntu Linux PC.

Lets begin. We don't really want to enter our administration password every time we want to send SMS message, so first thing we must do is to add our selves to the dialout user group. All members of this group have permission to use modem-like devices on Ubuntu system. This is how we do it:

sudo adduser $USER dialout

Now we can install great Linux application Gammu and its GTK frontend Wammu, again here's how:

sudo apt-get install gammu wammu

Before configuring our modem we must know a few things about it. First thing you should do is go to the Gammu phone database and find your GSM device. In this how to I will use my Huawei e220, with Gammu phone database page here. On this page I've found first data we need to configure Gammu, the connection parametar that is at19200 in my case. You should obtain the connection parameter for your GSM device on its Gammu phone database page.

Also you must find your GSM device entries inside /dev directory. You will recognize this entries because they usually start with "tty" prefix. On my hardware this device entry is named ttyUSB0 and ttyUSB1. You can also check dmesg this way:

dmesg|grep tty

Here's output of this command on my hardware:

marko@marko-pc:~$ dmesg|grep tty
[    0.000000] console [tty0] enabled
[    8.103849] usb 2-2: GSM modem (1-port) converter now attached to ttyUSB0
[    8.103998] usb 2-2: GSM modem (1-port) converter now attached to ttyUSB1

Now you can use gammu-config to configure your hardware or you can write Gammu configuration file manually. Here's how to do it using gammu-config:

gammu-config

Here's how you will setup your hardware with port ttyUSB0 and connection parameter at19200 I'm using for my Huawei e220 GSM modem:

gammu-config

After you set configuration options you can save changes and you will get Gammu configuration file .gammurc file inside you home directory. You can also save this file for future use once you are sure that configuration it contains is correct.

Important notice: Network manager has tendency to lock your GSM device once you start your GSM Internet connection. The device stays locked even after you disconnect so you must make sure that right click on Network manager -> Enable mobile broadband option is unchecked before you use Gammu. If this option is enabled you will get permission denied errors from Gammu.

After we got that right I will show you how to check your SIM card messages the CLI way and how to start the GTK Gammu GUI called Wammu. To view all messages on your SIM card using CLI you can use something like this:

gammu getallsms

For more about CLI usage you can check Linux manual pages usin man gammu command. To send and receive SMS messages using you GSM device you can also use GUI frontend to Gammu called Wamu. You will find this program by navigating to the Applications -> Accessories -> Wammu. Then you can go to Phone -> Connect. If the connection to the modem is successfully established you can use Retrieve menu to get messages or contacts from your SIM card and Create menu to send new SMS messages. If connection couldn't be established then you need to double check port and connection parameter you have used to configure Gammu. Cheers!

DevGenii

A quality focused Magento specialized web development agency. Get in touch!

38 thoughts on “Send and receive SMS using GSM modem or phone on Ubuntu

  1. Kelvin

    You are a genius man! Thenx Buddie.
    By the way how can I make and receive calls using my huawei modem and on linux Karmick?

    Reply
    1. Marko Author

      Thanks for your words. I don’t think you can place voice calls using GSM modem because most if not all operators disable voice feature on mobile broadband SIM cards. This is why you don’t have software to make and receive calls.

      Reply
  2. BullFrog

    You said that “Network manager -> Enable mobile broadband option should be unchecked before use Gammu”, is that mean I cannot use Internet connection and SMS simulteanously?

    Reply
    1. Marko Author

      Yes from my experience that is the case. The thing is that Network Manager locks your modem so Gammu cannot access it while Mobile Broadband is enabled.

      Reply
  3. Moez

    Hi Marko,
    I am currently working on my Project. It is about to use GSM modem or mobile phones that supports AT-Commands to send and receive SMS’s from people. I just want to inquire about following things:-

    1. Which GSM modem will be perfect for the above description and will use AT-commands.
    2. Can we change the program of GSM modem depending on our requirements.
    3. Which mobile will support sending and receiving SMS and the ability to connect to the PC.
    4. If we use a program to send and receive SMS, can we apply this code/program on our GSM modem or phone to change our requirements.

    Please, please, please reply me aas I am waiting for your response.
    Also please give me your id. I want to talk to you in detail as I am very much confused.

    Thanks

    Reply
  4. Jose

     Hi Marko

    On Windows I have a Huawei gsm stick and a program Mobile Partner, which allows me  to send/receive SMS while surfing (e.g while downloading a file). Is that possible with gammu/pppd or any other combination in Linux ?

    Reply
    1. Marko

      Hi Jose, with Network manager you can’t do that because it instructs pppd to lock device after your connection is first dialed. I haven’t tested this use case but theoretically by using pppd without network manager you could omit “lock” param in your so called peers file where you specify connection options and then your serial device wouldn’t be locked. 

      Here’s my guide on dialing without Network Manager on Debian based distros: http://www.techytalk.info/manage-gsm-mobile-broadband-connections-without-network-manager-ubuntu-mint-debian/

      I wrote another one for RHEL based distros if you’re RHEL/Fedora person. All of this would actually be very interesting to test.

      Reply
  5. Sagar

    Hi Marko,
                  We are working on a project which is a blend of image processing and communication.We have done with the image processing part and now we are facing problems in transmitting text file as sms using gsm modem.We plan to have two way communication between a machine and a programmable board(pandaboard) both running on Linux platform, using the established gsm network.So, our difficulty lies in transmitting a txt file consisting of symbols which is the part of our image processing program.                    Queries are like:1)How to include the path of the output file(text) in the program?2)How to configure a gsm modem in our main program for this purpose?3)How we can include AT commands in our program that will call gsm modem to send the specified file?

    Reply
    1. Marko

       Hi,
      I would take a look at minicom utility i wrote about here. It can send AT commands directly to your hardware. It is open source so you shouldn’t have difficulties getting the code you need from Minicom source code.

      Reply
      1. Sagar

        I am very thankful to you for reply.  I am testing sending sms through wavecom modem.

        I tested with minicom, its responding correctly..

        commands are
        AT
        OK

        AT+CMGS=”mobilenum” (then press Enter key)
        >”message” (then press ctrl+z)

        its sending sms properly.

        Same thing i want to implement using a code.

        now i want to send AT COMMANDS to wavecom modem using C code.
        So,  how should I proceed? 
        If you can give me rough idea regarding the flow…(In this how to include Enter key and Ctrl+z keys.)

        Reply
        1. Marko

          It’s basic serial communication you should google a bit but here’s how to initialize modem by sending AT and testing is OK receved (my modem is on /dev/ttyUSB2 serial port). The same way you can send whatever and test is OK received:

          /*
           ============================================================================
           Name        : serialcom.c
           ============================================================================
           */
           
          #include <stdio.h>
          #include <stdlib.h>
          #include <termios.h>
          #include <stdio.h>
          #include <unistd.h>
          #include <fcntl.h>
          #include <sys/signal.h>
          #include <sys/types.h>
           
          /* 0 = MODEM sent OK, -1 = MODEM bad */
          int init_modem(int fd){   /* I - Serial port file */
          	char buffer[255];  /* Input buffer */
          	char *bufptr;      /* Current char in buffer */
          	int  nbytes;       /* Number of bytes read */
          	int  tries;        /* Number of tries so far */
           
          	for (tries = 0; tries &lt; 3; tries ++){
          		/* send an AT command followed by a CR */
          		if (write(fd, &quot;ATr&quot;, 3)  0){
          			bufptr += nbytes;
           
          			if (bufptr[-1] == 'n' || bufptr[-1] == 'r')
          				break;
          		}
           
          		/* nul terminate the string and see if we got an OK response */
          		*bufptr = '';
           
          		if (strncmp(buffer, "rnOK", 2) == 0)
          			return (0);
          	}
           
          	return (-1);
          }
           
          int main(void) {
              int            fd;
              struct termios options;
           
              /* open the port */
              fd = open("/dev/ttyUSB2", O_RDWR | O_NOCTTY | O_NDELAY);
              fcntl(fd, F_SETFL, 0);
           
              /* get the current options */
              tcgetattr(fd, &amp;options);
           
              /* set raw input, 1 second timeout */
              options.c_cflag     |= (CLOCAL | CREAD);
              options.c_lflag     &amp;= ~(ICANON | ECHO | ECHOE | ISIG);
              options.c_oflag     &amp;= ~OPOST;
              options.c_cc[VMIN]  = 0;
              options.c_cc[VTIME] = 10;
           
              /* set the options */
              tcsetattr(fd, TCSANOW, &amp;options);
              if(init_modem(fd) == 0){
          	   printf("Sucess");
          	   return EXIT_SUCCESS;
             } else{
          	   printf("Failure");
          	   return EXIT_FAILURE;
             }
          }

          As you can see modem actually sends rn (new line) and then OK, you should take that in consideration for every AT command. Good luck!

          Reply
          1. Sagar

            Thanks once again….
            Above program does initialization or what??
            My job is to operate modem to send SMS from text file automatically. I don’t want to instruct it every time by typing AT commands. The only thing I want to do is to include set of AT commands required in my C code only with path of text file from which I have to get message body and send it to predetermined mobile no which also can be defined itself in the code. So this is my difficulty. If you can reply to this with appropriate flow…..

            Reply
            1. Marko Author

              This code initializes your modem by sending AT command, when modem replies with OK this means it is ready to receive your commands. AT commands are called “AT commands” because AT is initial command. If you are using C programing language you will need to go down to AT commands because I don’t believe someone made abstraction library for this. Also set of supported AT commands isn’t identical to all modem hardware manufacturers so I don’t believe it is possible to make such library. There is no easier way to do what you need and this isn’t difficult at all especially if we’re talking about C programming language.

              Reply
              1. Sagar

                Thanks for your reply..
                But how to set serial port parameters such as baud rate, parity and stop bits in above program? our modem works on 115200 baud rate,8 bits,no parity,1 stop bits.

                Reply
              2. Sagar

                Hi Marko,
                I have done with SMS sending part. Thanks for your support. But now problem is I’ve to copy the received SMS to external memory such as memory card for further processing. I am using Wavecom modem. I have tried AT+CMGR but that just read the SMS, but want data to be copied to another location.
                I look forward for your help….

                Reply
        2. ki

          hello sir,

          I want the gsm, to automatically read the data for every 30 minutes and it should send it to me. could you please send me the code.

          thankyou.

          Reply
  6. kaushik

    hi Marko, Thanx for all the valuable suggestions.
    I am using a ZTE MF110 modem and i could connect to my device and retrieve sms from it. However once the device is claimed by network manager during data usage i cannot connect to it.
    I wish there was a way so that i could receive sms from the device even when i was connected to the internet so that i do not have to disconnect everytime to check for sms.

    Reply
    1. Marko Author

      Yes this would be great. Unfortunately network manager locks serial device preventing other software to use it. Problem is that we’re using two separate programs, it would be great if NM implements SMS capabilities. I currently run without NM, this way I can instruct pppd not to lock my modem device when establishing connection and SMS tools like gammu works this way.

      Reply
  7. wasim

    Hi,

    i m using Audiotel Industrial GSM BASE modem, by using SMSLIST application i can send and receive messages, but i want to use this modem with some flow meters from which i want to send temperature and pressure values through sms. My Question is, how do i configure this modem to send sms without using any SMSList application.

    This modem receives pressure/temperature value from flow meter and sms to a specific number.

    i can access my modem through hyper terminal.

    Reply
  8. sarah

    Hello,
    I am very new to all this and dont understand even quite basic things. I got this far and because it looks so different to what you’ve got, I thought I had better check with you. ALso, I dont seem to have NM! at least I searched my computer for it and it didn’t come up. thanks

    sarah@sarah-ThinkPad-T43 ~ $ dmesg|grep tty
    [    0.000000] console [tty0] enabled
    [    0.296241] 00:0a: ttyS0 at I/O 0x3f8 (irq = 4) is a NS16550A

    Reply
    1. Marko Author

      Hi,
      you must own GSM modem with SIM card supporting SMS for this to work. Also your GSM modem must be plugged into you USB port. Try using /dev/ttyS0 where I have used /dev/ttyUSB0 and good luck!

      Reply
  9. sameer

    Hi, Marko

    I tried to send sms vi terminal as above you explained.

    I get some error like bellow, even after closing Internet connection,

    error: ” No response in specified timeout. Probably phone not connected”.

    could you help me out my device is ALCATEL. Thank you

     

     

    Reply
    1. Marko Author

      Hello,
      did you select correct port as directed by the dmesg|grep tty command? I was using /dev/ttyUSB0 in the article but yours might be different.

      Reply
  10. Aleksey

    Thank you very much for your professional POST! I have installed in my lenovo s10-3t modem huawei em770 and it’s works!

    Reply
  11. Gabriele

    gabriele@IGABC:~$ gammu getallsms
    Can not open specified file.

    ——————-

    When I open Wammu and try to connect I get “You don’t have permission for /dev/ttyUSB0/ device! Maybe you need to be member of dialout group.”

    gabriele@IGABC:~$ sudo adduser $USER dialout
    The user `gabriele’ is already a member of `dialout’.

    🙂

    thanks

    Reply
  12. clement

    i got lost on how to configure my modem, do i have to copy and pest the codes in terminal or i have to modify some thing on it?

    ( [gammu]

    port = DEVICE NAME

    connection = at115200 )

    Reply
  13. Alan Feather

    Thank you for this, I found it very useful.

    The only comment I would make is that although I was using an E220 like you, I had to use:
    connection = at115200
    instead of:
    connection = at19200
    or Gammu wouldn’t connect to my modem.

    I also had to reboot before I was recognised as a member of the dialout group although I’m not sure why.

    Reply
  14. Sorath Asnani

    Hello guys!
    I am using Raspberry Pi which supports Linux based OS “Rasbian”. I have to send SMS and MMS to two different numbers using GSM.
    I have some queries regarding that. Can anyone help me out?
    1. Which GSM modem can be used with Raspberry Pi to send SMS and MMS?
    2. Can C++ be used to write code for sending SMS/MMS?
    Any useful link or any documentation for the above requirement will be appreciated.
    Thanks.

    Reply
    1. AK

      HI,

      Still searching for solution or found the solution.

      I need to know more about MMS part. If possible can you share your capabilities. So, that I can judge if it meets my requirement.

      I am ready to pay you for your code.

      Reply
  15. Arif

    hello ,

    i want get message in gammu , but if its more then 160 characters then its splits in two seperate message,
    so , anyone have suggestion for this issue

    thanks

    Reply
  16. Jayson

    Hi, is that possible in windows 7 OS? I’ll try that in linux, its working properly. But, when i tried to windows, it so hard to implement. why is that so?

    Reply

Leave a Reply

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