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
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.