In this article, we are going to learn how you can use a GSM module with your Raspberry Pi to send SMS or do whatever you need to do. Here, we’ll use a GSMHat for raspberry pi which will be connected through USB with the Pi. So let’s start!

Disclaimer: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act copying and claiming it as yours. Most of the articles published here are kept open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guidance feel free to comment below, the author will try to help you. Also, there can be affiliation links in the article. Which will not affect you anyway, but allows the author with some commission. So please don’t take it otherwise. Thanks.

Table of Contents

Advertisements

GSM/GPRS/GNSS HAT:

GSM/GPRS/GNSS Hat is specially designed for Raspberry Pi. You can use a USB port to connect or simply use the GPIO pins as well.

Send SMS from Raspberry Pi

The module is developed by Web share. You can visit their site and learn about the details of this module. Of course, you can use other modules, but I had this to test the project, so I used it.

Setup your Pi:

We need to set up our Pi to run the code. But we need some information before that. When you are connecting the USB port, you need to find the port number.

type ‘ls -m /dev/tty*‘ on cmd pad and Pi will return you a list like this:

Send SMS from Raspberry Pi

Now, remove your USB port and do this again. There will be a change in return as you disconnected your USB. And from comparing these two replies, you can easily find your port.

In my case, it was /dev/ttyUSB0

Now, open python and start writing this code:

Python Code to send SMS:

import serial
import time 

ser = serial.Serial('/dev/ttyUSB0', 9600, timeout = 1)


def getResponse(cmd,reply,timeout):    
    cmd = cmd + '\r'
    print('CPU'+':'+cmd)
    ser.write(bytes(cmd,'utf-8'))
    time.sleep(0.01)
    for i in range (0,timeout):
        in_msg=ser.readline()
        in_msg = in_msg.decode('utf-8').strip()
        time.sleep(0.01)
        ans=in_msg.find(reply)
        if(ans!=-1):
            i=timeout             
    print('GSM'+':'+in_msg)       
    time.sleep(0.5)
    
def gsmPrint(cmd,timeout):
    cmd = cmd + '\r'
    print('CPU'+':'+cmd)
    ser.write(bytes(cmd,'utf-8'))
    time.sleep(0.01)
    for i in range (0,timeout):
        in_msg=ser.readline()
        in_msg = in_msg.decode('utf-8').strip()
        time.sleep(0.01)        
        if(len(in_msg)>0): #someting on reply
            i=timeout
    print('GSM'+':'+in_msg)       
    time.sleep(0.5)
    
def gsmEndcommand():
    ser.write(bytes(chr(26),'utf-8'))
    time.sleep(1)
    in_msg=ser.readline()
    in_msg = in_msg.decode('utf-8').strip()
    print('GSM'+':'+in_msg)
    time.sleep(1)
      
    
    
getResponse('AT','OK',2)
getResponse('AT+CMGF=1','OK',2)
getResponse('AT+CMGS=\"xxxxxxxxxxxxx\"','OK',2) #put your number replacing 'xxxxxxxxxxxxx'
gsmPrint('Testing self pythopn lybrary to send SMS. _Mithun',2)
gsmEndcommand()

Now, run your code and the Pi will send SMS to your number and the Shell will show you something like this.

Send SMS from Raspberry Pi

And the result is on your phone! you’ll get a SMS if there is no error.

Send SMS from Raspberry Pi

End:

Now, you can easily modify texts or numbers as per your requirement to Send SMS from Raspberry Pi. And remember, this code is just one cycle. To run it in infinity, simply put the code in a while loop. And you know how to do that in python. I hope, this article was helpful for you. If so, kindly share it. Thanks.

For Professional Designs or Help:

Loading

Also let me know what you want to get as the next article, comment below!

Read more:


MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

1 Comment

EDUARDO · 03/07/2022 at 3:42 pm

thank you for your great selfless contribution

Leave a Reply

Avatar placeholder

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

%d bloggers like this: