Control devices on your Rasp Pi network with text messages
Red Alert
We show you how to control devices connected to Rasp Pi GPIO pins with text messages from an Android phone.
In places where Internet connections are not possible or too expensive, SMS text messaging can be a simple approach for monitoring and controlling your remote systems (Figure 1). Many of the mobile providers offer Internet of Things (IoT) packages for low data throughput, whereas you'd be looking at spending $1 to $5 per month for 1-5MB of data. From the hardware standpoint, many options come in either a Raspberry Pi HAT or a USB form factor (e.g., digital modem modules).
For prototyping, an Arduino phone and Node-RED [1] is a great way to jump-start your project.
Node-RED on Android
Node-RED is a graphical programming tool for IoT projects. Node-RED is installed in base Raspberry Pi images, but it can also be loaded on Linux, Windows, Mac OS, and Android systems. To load Node-RED on Android [2], you first need to load Termux [3], an Android terminal emulator app available at Google Play. After Termux is loaded, enter the following commands to install and run Node-RED:
apt update apt upgrade apt install coreutils nano nodejs npm i -g --unsafe-perm node-red node-red
Node-RED starts a graphical web interface (Figure 2) accessed by http://<phone_ip_address>:1880. Extra features can be added and removed from Node-RED by selecting Manage palette from the hamburger menu on the right (Figure 3). For this project, I needed terminux-api for texting support and bigtimer for scheduling.
For a basic setup (Figure 4), I used:
- 1 Android phone with Termux and Node-RED
- 1 Android phone for texting
- 1 Raspberry Pi running Node-RED
- 1 NO (normally open) PowerSwitch Tail II [4] connected to a light
Scanning for Text Messages
To create a simple text message project on the Android Node-RED system, I used the following components:
- A Big Timer node defines how often the SMS inbox is scanned.
- An SMS Inbox node reads in text messages. To get the last message; select a buffer limit of 1.
- A Get SMS Action function node is used with JavaScript to check for valid text messages (Listing 1).
- A tcp out node sends a TCP message to another Node-RED system. Remember to configure the Raspberry Pi's IP address and define a port number.
Listing 1
Get SMS Action Function
Figure 5 shows a basic logic setup to scan SMS periodically and pass the action by TCP. Without any configuration, the second Big Timer output node offers a cycle time of one scan per minute. The logic in Listing 1 interprets the latest text message and sends the required logic to the Raspberry Pi General Purpose I/O (GPIO) pins. The example logic uses the text message LIGHTS ON or LIGHTS OFF to control the output on GPIO pin 12.
The first time the code runs, you are prompted on the phone with Allow Termux: API to send and view SMS messages?. After you acknowledge this message, your Node-RED logic will run cleanly.
Reading TCP Inputs
Android phones cannot be connected directly to I/O devices, so I used TCP connections to pass commands between the Arduino phone and Raspberry Pi. Node-RED is preinstalled on most Raspberry Pi images, so no added installation is required.
On the Raspberry Pi Node-RED side (Figure 6), only a couple of nodes are required: a tcp in node, to read the incoming communications, and an rpi gpio out node, to set Raspberry Pi GPIO pins. The tcp in node should be configured as the same port (8888 in this case) as the tcp out node. Also for this example, the output payloads (in right pane, not shown) should be set to single and String.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.