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 poses inherent risks. Adequate skills are crucial for handling it safely. Undertake tasks at your own risk; the author disclaims responsibility for misuse, harm, or errors. Website content is unique and copyrighted; refrain from unauthorized copying. Most articles are open-source for your benefit. Feel free to use the knowledge freely, and consider purchasing available resources. For assistance or guidance, comment below; the author aims to help. Some articles may contain affiliate links, having no impact on you but supporting the author with a commission. Thank you for your understanding.

Advertisements

Table of Contents

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

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 *