How to Turn Your Raspberry Pi into an SMS Gateway with RaspiSMS ?
An open-source solution for your automation projects that you can interact with via an API or web interface. ?
This tutorial guides you step-by-step through installing and configuring RaspiSMS on Raspberry Pi OS.
https://www.it-connect.fr/tuto-raspisms-raspberry-pi-passerelle-sms/How to Turn Your Raspberry Pi into an SMS Gateway with RaspiSMS?
SMS
In this tutorial, we'll see how to set up RaspiSMS on Raspberry Pi OS to send SMS messages. This will transform your Raspberry Pi into an SMS gateway that you can interact with via an API.
RaspiSMS is a free and open-source application, developed in Symfony, that allows you to send SMS messages from a web interface or using its API. The application can be used in SaaS (Software as a Service) mode or self-hosted. In this tutorial, we'll focus on the latter.
This article explains how to install and configure RaspiSMS on a Raspberry Pi. We'll also cover configuring a phone within the application and sending SMS messages using different methods.
RaspiSMS on GitHub
Table of Contents [-]
Installing RaspiSMS on Rasbian
Prerequisites
Installing RaspiSMS prerequisites
Creating the database for RaspiSMS
Installing RaspiSMS
Configuring Apache2 for RaspiSMS
Securing RaspiSMS folders
Creating the "raspisms" service
Setting up log file rotation
Configuring RaspiSMS
Detecting and configuring the 3G key
Modifying administrator account information
Creating the 3G key
Sending SMS messages with RaspiSMS
Using the web interface
Using the command-line API
Conclusion
Installing RaspiSMS on Rasbian
Prerequisites
To set up RaspiSMS, you need hardware such as:
A Raspberry Pi 4 or 5 with Raspberry Pi OS installed (for this This tutorial uses a Raspberry Pi 4 and the operating system is based on Debian Bookworm (Debian 12). An SD card with a minimum capacity of 32 or 64 GB
A 3G dongle
A SIM card (for this tutorial, a SIM card with a business plan was used)
If you want to install an operating system on a Raspberry Pi but don't know how, please refer to the following IT-Connect tutorial:
Preparing an SD card for your Raspberry Pi with Raspberry Pi Imager
Once you have all these components, you must have at least configured the following on your Raspberry Pi:
A user with "sudo" privileges (normally created during the OS installation on the Raspberry Pi)
A static IP address (using the "nmtui" command, for example)
SSH access to facilitate data entry Commands
Installing RaspiSMS Prerequisites
Connect to the Raspberry Pi via SSH using the default account created during installation or another account with at least "sudo" privileges, then update the sources:
Note: The commands are executed directly as a user named pi; files will be stored in this user's current directory. Be sure to specify the desired paths.
sudo apt-get update
Next, install the prerequisite packages for the rest of the installation.
sudo apt-get install ca-certificates apt-transport-https software-properties-common wget curl lsb-release gnupg2 git -y
Install MariaDB, Apache, and Gammu for the 3G connection.
sudo apt install apache2 mariadb-server gammu gammu-smsd python3-gammu -y
Next, install PHP 8.4 on the Raspberry Pi, as this component is essential for the RaspiSMS administration interface to function.
curl -sSL
https://packages.sury.org/php/README.txt | sudo bash -x
sudo apt-get update
sudo apt install php8.4 php8.4-common php8.4-cli php8.4-mysql php8.4-mysqli php8.4-curl php8.4-mbstring php8.4-xml -y
Creating the database for RaspiSMS
You installed MariaDB using the previous commands. Before creating the database, secure MariaDB with this basic script included in the package; it is used, among other things, to set a root password.
sudo mysql_secure_installation
Once MariaDB is secured, connect to the MariaDB instance with the "root" account (you should have set a password in the previous step).
sudo mysql -u root -p
The commands below allow you to create the raspisms database, as well as the raspismsuser user with their password. Next, assign database privileges to the user. I recommend customizing the password to P@ssword!.
CREATE DATABASE raspisms CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER 'raspismsuser'@'localhost' IDENTIFIED BY 'P@ssword!';
GRANT ALL PRIVILEGES ON raspisms.* TO 'raspismsuser'@'localhost';
FLUSH PRIVILEGES;
QUIT; Installing RaspiSMS
There are two solutions for installing RaspiSMS:
From the APT package manager
From the project's GitHub repository
Since the first solution is still based on PHP 7.4 (obsolete since November 28, 2022), the installation will be done using the second solution.
Start by cloning the project's GitHub repository into the /usr/share/raspisms directory.
sudo git clone
https://github.com/RaspbianFrance/raspisms.git /usr/share/raspisms
Go to the directory containing the RaspiSMS data. The following commands will be executed from this directory.
cd /usr/share/raspisms
Create a copy of all files with the .dist extension (the copy has the original extension of the template).
sudo cp phinx.yml.dist phinx.yml
sudo cp env.php.dist env.php
sudo cp env.prod.php.dist env.prod.php
For each of the files you copied, you must change all the %VALUE_NAME% values ??for the application to work. To begin, edit the file named phinx.yml.
sudo nano phinx.yml
The phinx.yml file defines the information related to the RaspiSMS database. You need to change the following fields in the file with the following values:
migrations: '/usr/share/raspisms/db/migrations'
seeds: '/usr/share/raspisms/db/seeds'
host: 'localhost'
name: 'raspisms'
user: 'raspismsuser'
pass: 'P@ssword !'
Note: If you have values ??different from those given, remember to enter your values.
Ezoic
In the end, your phinx.yml file should look like this:
In the second file, you need to define a field named APP_SECRET. This field secures navigation within the RaspiSMS application. To have a robust value in this field, generate a string with the following command:
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
Note: Copy the string into a text editor for later use.
Once the key/string is generated, edit the env.php file.
sudo nano env.php
The env.php file serves as the environment file for the RaspiSMS application. All application and source code parameters for RaspiSMS are defined here to ensure the application functions correctly. In this file, you must change the following fields:
'ENV' => 'prod'
'APP_SECRET' => 'your_generated_string'
'ENABLE_URL_SHORTENER' => false
Note: We disable the URL shortening parameter because we will not be using URL shorteners in RaspiSMS.
In the end, your env.php file should look like this:
Finally, edit the last file named env.prod.php.
sudo nano env.prod.php
The file is divided into 5 parts:
Web page access URL
Database connection information
Mail server settings
Using URL shorteners
Using Redis
For the web page access section, you must configure the line like this:
'STATIC_HTTP_URL' => 'http://IP/raspisms'
For the database connection section, you must enter the same information as in the phinx.yml file.
The mail server section has not been configured for this tutorial, but you can configure it to, for example, receive emails to change your password if it has been forgotten.
For the URL_SHORTENER section, as with the env.php file, we will not use it. To disable the parameter, leave the fields empty.
Finally, no Redis server has been configured on the Raspberry Pi. To disable Redis, set the USE_REDIS_QUEUES field to false and leave the other fields blank.
In this file, you must modify all the fields. In the end, your file should look like this:
Install the dependencies necessary for the RaspiSMS application to function.
sudo php composer.phar self-update
# (Ignore the lines in yellow)
sudo php composer.phar install
For the second command, when asked "Continue as root/super user [yes]?", answer "yes".
Once the dependencies are installed, you must import the database structure into the one you created previously.
sudo php vendor/bin/phinx migrate
Finally, create your user to log in to the web interface later. Here, we create the account admin@example.com with the password changeme.
php console.php controllers/internals/Console.php create_update_user --email="admin@example.com" --password="changeme" --admin="true"
At this point, you have finished installing the RaspiSMS application. However, to access the application, you still need to complete the following steps:
Configure Apache2
Set permissions on the RaspiSMS application folders and files
Create the "raspisms" service
Set up a log file rotation for RaspiSMS.
This is what we will do in the next section.
Configuring Apache2 for RaspiSMS
RaspiSMS already provides the configuration file for A in its GitHub repository
Apache. Copy this file to the correct folder.
sudo cp confs/apache2/raspisms.conf /etc/apache2/sites-available/
Enable the virtual host for RaspiSMS.
sudo a2ensite enable raspisms
Enable the Apache rewrite module.
sudo a2enmod rewrite
Restart the Apache2 service for the two changes to take effect.
sudo systemctl restart apache2
Securing the RaspiSMS folders
First, create a user named raspisms. This account will not have a password, as it will only be used by the root account.
sudo useradd -M raspisms
Grant ownership of the /usr/share/raspisms folder to the raspisms user and the www-data group.
sudo find "/usr/share/raspisms" -type f -exec chown "raspisms:www-data" {} ;
sudo find "/usr/share/raspisms" -type d -exec chown "raspisms:www-data" {} ;
sudo find "/usr/share/raspisms" -type d -exec chown "raspisms:www-data" {} ;
Create a folder for RaspiSMS logs.
sudo mkdir /var/log/raspisms
Apply the correct permissions to each folder and certain files.
sudo find "/usr/share/raspisms" -type f -exec chmod 664 {} ;
sudo find "/usr/share/raspisms" -type d -exec chmod 775 {} ;
sudo find "/var/log/raspisms" -type d -exec chmod 775 {} ;
sudo chmod -f 751 scripts
sudo find "console.php" -exec chmod 754 {} ;
sudo find "bin" -exec chmod 754 {} ;
sudo find "vendor/bin" -exec chmod 754 {} ;
sudo chmod -f 640 env.*php phinx.yml
Creating the "raspisms" service
As with Apache, RaspiSMS provides the file to create the raspisms service. Copy this file to the correct directory.
sudo cp confs/systemd/raspisms.service /etc/systemd/system/
Apply the correct permissions to this file.
`sudo chmod 644 "/etc/systemd/system/raspisms.service"`
Enable automatic startup of the raspisms service.
`sudo systemctl enable raspisms`
Start the raspisms service.
`sudo systemctl start raspisms`
Setting up log file rotation
To prevent the RaspiSMS log file from taking up too much space, we will configure RaspiSMS log rotation using the `logrotate` service.
Start by creating a file in the `logrotate.d` directory.
Open the file `/etc/logrotate.d/raspisms` using `sudo nano`. Add the following lines to configure the log file rotation to rotate weekly, up to a maximum of 8 files:
`/var/log/raspisms/daemon.log` {
`weekly`
`missingok`
`rotate 8`
`compress`
`notifempty`
`create 700` `root root`
`su root root`
}
`/home/pi/gammu.log` {
`weekly`
`missingok`
`rotate 8`
`compress`
`notifempty`
`create 700` `pi pi`
`su pi pi`
}
Force a restart of `logrotate` to rotate the RaspiSMS logs.
`sudo /usr/sbin/logrotate -f /etc/logrotate.d/raspisms -v`
Force a restart of `logrotate` to rotate all logs.
sudo /usr/sbin/logrotate -f /etc/logrotate.conf -v
Raspberry SMS Configuration
3G Dongle Detection and Configuration
To detect the 3G dongle and configure SIM card information such as the PIN and PUK codes, we will use the Gammu software.
Connect your 3G dongle to the Raspberry Pi with the SIM card inserted.
First, we will verify that the Raspberry Pi detects the 3G dongle by running the following command:
gammu-detect
You should get a similar result:
Use the gammu-config command to begin configuring the 3G dongle in Gammu, then enter the information as shown in the image below:
gammu-config
Click Save to create the configuration file.
Edit the configuration file created previously to complete the 3G dongle configuration.
sudo nano /home/pi/.gammurc
Match the configuration to the one below:
[gammu]
port = /dev/ttyUSB0
model =
connection = at
synchronizetime = yes
logfile = /home/pi/gammu.log
logformat = textall
use_locking = yes
gammucoding = utf8
gammuloc = en_GB.utf8
Note: You must leave the "gammuloc" field with the value "en_GB.utf8", as in the example. The source code is configured to expect an English word when configuring the SIM card, which is why this parameter is forced in the Gammu configuration file to use English encoding and UTF-8.
Edit or create the file named gammu-smsdrc to configure the SIM card information.
sudo nano /etc/gammu-smsdrc
Modify the file to match the following configuration:
Note: Remember to enter your SIM card information for the PIN and PUK fields. In this tutorial, the SIM card did not have a configured PIN.
# Configuration file for Gammu SMS Daemon
# Gammu library configuration, see gammurc(5)
[gammu]
# Please configure this!
port = /dev/ttyUSB0
connection = at
# Debugging
logformat = textall
# SMSD configuration, see gammu-smsdrc(5)
[smsd]
service = files
#pin =
puk = 12345678
logfile = syslog
# Increase for debugging information
debuglevel = 4
# Paths where messages are stored
inboxpath = /var/spool/gammu/inbox
/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/
Finally, restart the RaspiSMS service for the changes to take effect.
sudo systemctl restart raspisms
Modifying Administrator Account Information
Log in to the graphical interface using the credentials obtained during the RaspiSMS installation.
In the upper right corner of the page, click on "admin@example.fr" and then on "Profile".
Enter the email address you wish to use, then click on "Update Data". Do the same for the password.
Creating the 3G Key
Go to the RaspiSMS graphical interface.
In the right-hand menu, expand the "Phones" menu and then click on "Phones". Next, click the "Add" button.
Enter the following information on the page:
Phone name: for example, the key model
Phone type: Gammu
Configuration file: /etc/gammu-smsdrc
SIM card PIN: enter the one from the gammu-smsdrc file
Then click "Save phone".
Sending SMS Messages with RaspiSMS
Now that we have installed RaspiSMS, configured Gammu, and created a phone number in RaspiSMS, we can send SMS messages.
It is possible to send SMS messages with RaspiSMS in two different ways:
Either via the web interface (graphical interface)
Or via the command line using the API provided by the application
Using the web interface
Go to the RaspiSMS graphical interface.
Open the "SMS" menu and then click on "New SMS". Next, click on the green "Create a new SMS" button.
To send an SMS, fill in at least the following fields:
SMS Text
Target Number (enter the number in this format: 0123456789)
Target Contact (enter the number in this format: 0123456789)
Number to use: select the phone number
Click "Save SMS" to send the SMS.
To check if your SMS has been sent, click "Sent SMS" in the "SMS" section.
Using the Command-Line API
To send SMS messages via the command line, RaspiSMS provides an API during installation. To use the API, you need a key, which is unique to each account.
To retrieve the API key, log in to RaspiSMS and then go to your profile (as you would to change your password and email address).
In the "My Data" section, click "Click to display API key". Copy the key to a text editor for later use.
Using the API on Linux
On a Linux server, you can use the API with the curl command.
The following command allows you to send an SMS to a given number (as if using the graphical interface):
Note: Remember to include your API key and a valid phone number. The phone number must begin with the country code (+33 for France).
curl -X POST
http://192.168.1.101/raspisms/api/scheduled/ -H 'X-Api-Key: c6b3ca0803727a24228f0e7bb99d0b16' -d "text=test RaspiSMS" -d 'numbers=%2B33123456789'
Return to the RaspiSMS graphical interface and log in, if you haven't already. Open the "SMS" menu and click on "Sent SMS".
The SMS sent via the API is now displayed. If the SMS status is "Unknown", then your SMS was successfully sent to its recipient.
Using the API on Windows
To send an SMS with Windows, the steps are the same as on Linux. Instead of using curl, we use the PowerShell command Invoke-WebRequest.
Here is an example of the command under Windows:
Invoke-WebRequest -Uri "http://192.168.65.3/raspisms/api/scheduled/" -Method Post -Headers @{"X-Api-Key"=" c6b3ca0803727a24228f0e7bb99d0b16"} -Body @{"text"="test";"'numbers"="%2B33123456789"}
Note: Remember to include your API key and a valid phone number. The phone number must begin with the country code (+33 for France).
Conclusion
By following this tutorial, you are able to install, configure, and send SMS messages with RaspiSMS.