How to Send SMS with Raspberry Pi

Send SMS with Raspberry Pi

It’s 2020 and sending SMS is now an old technology. But such technology remains to be a cheap and indispensable way to communicate over long distances. In this article, I will share how I send SMS with the Raspberry Pi.

I have been using a SIM800L EVB board to send SMS using a Raspberry Pi. The reason for choosing this board is its built-in voltage regulator which means it can be powered by a 5V source, similar to the Pi. This is in contrast with the smaller SIM800L board which needs a 3.7V power source.

Here are some other things you need to know about this board:

  1. It needs a 5V power source that can provide at least 2A of current
  2. The antenna must be attached for it to connect to the network
  3. If when power is provided and the board’s LED blinks then turns off, the current is not enough
  4. The board is registered to the network and ready to send and receive SMS if the red LED blinks every three seconds.

Here's how to connect the SIM800L EVB board to the Raspberry Pi:

Send SMS with Raspberry Pi

Enabling Hardware Serial

The SIM800L EVB board uses asynchronous serial to communicate with a microcontroller. Thus, the serial functions of the Raspberry Pi must be turned on. The following steps turn on the hardware serial of the Raspberry Pi running on Raspbian Buster (Raspbian 10)

Using the terminal, run sudo raspi-config

Then select 5 Interfacing Options

Next, select P6 Serial

When you press enter on this option, you will be asked two questions: 1) if you want to enable shell access via terminal and 2) if you want to enable the hardware for serial. If you’re accessing your Raspberry pi via terminal through Putty, you need to answer no to the first question and yes to the second question. Doing this will bring you to this screen:

The Raspberry Pi will ask you to reboot to apply changes. After rebooting, you are ready to use the hardware serial port.

Writing the Python Code

Now it’s time to code! Open an empty file using nano and name it sendSMS.py

sudo nano sendSMS.py

Then, copy-paste this code: (copy this then right-click on the Raspberry Pi terminal screen for Putty users)

import serial
import time

receiverNum = "+639271234567"
sim800l = serial.Serial(
port='/dev/serial0',
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)

sms = "Hello World"
time.sleep(1)
sim800l.write('AT+CMGF=1\n')
print sim800l.read(24)
time.sleep(1)
cmd1 = "AT+CMGS=\" "+str(receiverNum)+"\"\n"
sim800l.write(cmd1)
print sim800l.read(24)
time.sleep(1)
sim800l.write(str(sms))
sim800l.write(chr(26))
print sim800l.read(24)

Press ctrl+x then y to save and exit from Nano.

Run the code by issuing the command: sudo python sendSMS.py

The phone whose number is given as receiverNum in the code should receive a “Hello World” SMS.

Leave a Reply

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