Building an Independent SMS & Call Gateway: The Complete Raspberry Pi, Gammu, and MySQL Guide
In an era dominated by cloud APIs like Twilio, there is still immense value in owning your communication infrastructure. Whether for building a resilient offline alerting system, creating a private 2FA channel, or simply cutting recurring API costs, sometimes you need direct access to the cellular network.
At Comquest, we believe in engineering robust solutions that don't rely on external dependencies when critical paths are involved. In this comprehensive guide, we will walk through transforming a humble Raspberry Pi into a fully-fledged cellular gateway capable of receiving SMS messages and logging incoming call data directly into a MySQL database, managed via phpMyAdmin.
Prerequisites Checklist
Ensure you have the following before beginning. This guide assumes a headless setup accessed via SSH.
- **Hardware:** Raspberry Pi (3B+ or 4 recommended) with a reliable 2.5A+ power supply.
- **Modem:** A USB GSM Modem (e.g., Huawei E220, or a SIM800/900 module connected via USB TTL).
- **Connectivity:** An active, standard-sized SIM card with SMS capabilities disabled PIN lock.
- **OS:** Raspberry Pi OS (Debian Bullseye or Bookworm), preferably Lite version.
Step 1 System Preparation and Hardware Recognition
Before installing software, ensure your Pi is updated and that the kernel recognizes your USB modem. Connect your modem (with the SIM inserted) to a USB port on the Pi.
Update your package repositories:
sudo apt update && sudo apt upgrade -y
Verify the modem is detected. Run `lsusb` and look for your device manufacturer (e.g., Huawei, QinHeng, etc.). Then, check the system logs to find the device path assignment.
# List USB devices
lsusb
# Check kernel messages for device attachment
dmesg | grep tty
ttyUSB0, ttyUSB1, or ttyACM0. Write this down; it is crucial for later configuration.
Step 2 Installing the LAMP Stack (Linux, Apache, MySQL/MariaDB, PHP)
We need a database to store incoming messages and a web interface to view them easily. We will use MariaDB (a robust MySQL fork) and phpMyAdmin.
2.1 Install Apache and PHP dependencies
sudo apt install apache2 php php-mysql libapache2-mod-php -y
2.2 Install and Secure MariaDB
sudo apt install mariadb-server -y
# Once installed, secure the installation.
# Follow the prompts: Set root password, remove anonymous users, disallow root login remotely.
sudo mysql_secure_installation
2.3 Install phpMyAdmin
During installation, select apache2 by pressing SPACE, then Enter. Select "Yes" to configure the database with dbconfig-common and set a strong password for the phpMyAdmin user.
sudo apt install phpmyadmin -y
To ensure Apache can find the phpMyAdmin interface, create a symbolic link and restart Apache:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
sudo systemctl restart apache2
Verification: You should now be able to access `http://your-pi-ip-address/phpmyadmin` in your browser. Log in as `root` with the password you set during the secure installation step.
Step 3 Installing and Configuring Gammu Core
Gammu is the command-line utility that talks to the cellular modem. We need to install it and verify it can communicate with your specific hardware.
sudo apt install gammu gammu-smsd -y
Now, configure Gammu to identify your modem. Run the configuration utility:
sudo gammu-config
This opens a text-based UI. Set the following based on the device path you found in Step 1:
- Port: The device path (e.g.,
/dev/ttyUSB0or/dev/ttyUSB2). - Connection: For most generic USB dongles, try
at115200. For Huawei specific dongles, tryhuawei.
Save the config and exit.
Testing the Connection
Run the identify command. If successful, it will return the modem's manufacturer, model, and IMEI. If it times out, try a different `/dev/tty` port or connection type in the config.
sudo gammu identify
Step 4 Setting up the Gammu Database
Instead of storing SMS messages in text files, we will configure Gammu's daemon (`gammu-smsd`) to use MySQL. First, we must create a dedicated user and import the required database schema.
4.1 Create User and Database
Log into MySQL as root:
sudo mysql -u root -p
Run the following SQL commands (replace `'your_strong_password'` with a real password):
CREATE DATABASE gammu_db;
CREATE USER 'gammu_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON gammu_db.* TO 'gammu_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
4.2 Import the Schema
Gammu provides a pre-built SQL schema. Locate it and import it into your new database:
# Locate the schema file (path may vary slightly by distro version)
ls /usr/share/doc/gammu-smsd/examples/sql/
# Import it (assuming it's named mysql.sql.gz)
zcat /usr/share/doc/gammu-smsd/examples/sql/mysql.sql.gz | mysql -u gammu_user -p gammu_db
Step 5 Configuring and Starting the SMS Daemon
This is the final piece of the puzzle. We configure the `gammu-smsd` service to run in the background, monitor the modem, and write data to MySQL.
Edit the SMSD configuration file:
sudo nano /etc/gammu-smsdrc
Replace the contents with the following configuration. Be sure to update the database password and your device port.
[gammu]
# Use the same port and connection that worked in Step 3
device = /dev/ttyUSB0
connection = at115200
[smsd]
# Tell Gammu to use the SQL backend
service = sql
driver = native_mysql
logfile = /var/log/gammu-smsd.log
# Increase debug level if things aren't working (0-255)
debuglevel = 1
# Database connection details set in Step 4
user = gammu_user
password = your_strong_password
pc = localhost
database = gammu_db
# Automatically check security status of PIN
pin = ''
# Ensure incoming calls are logged
checksecurity = 0
hangupcalls = 1
Start the Service
Enable the service to start on boot and start it immediately:
sudo systemctl enable gammu-smsd
sudo systemctl start gammu-smsd
Check the status to ensure it is running without errors:
sudo systemctl status gammu-smsd
# OR check the live logs
tail -f /var/log/gammu-smsd.log
Verification and Conclusion
Your gateway is now active. To verify functionality, send an SMS message to the SIM card number inserted in your Raspberry Pi.
- Open phpMyAdmin in your browser (`http://your-pi-ip/phpmyadmin`).
- Select the
gammu_dbdatabase on the left sidebar. - Click on the
inboxtable. You should see your message appear shortly after sending. - Try calling the number. The Pi will hang up (due to
hangupcalls = 1), but data will appear in thecallstable.
Need industrial-grade IoT solutions?
While a Raspberry Pi is excellent for prototyping, high-volume environments require specialized hardware. Contact Comquest engineering for scalable data ingestion solutions.