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 guide 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 helps the author with some commission. So please don’t take it otherwise. Thanks.
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.
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:
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.

And the result is on your phone! you’ll get a SMS if there is no error.
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.
Also let me know what you want to get as the next article, comment below!
Read more:
Connect to Raspberry Pi from your Laptop/Desktop using VNC Viewer
How to reduce noise from DC motor
Read ThingSpeak Channel using ESP8266 and Arduino
Caller ID detection using Arduino
0 Comments