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
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.