Control devices on your Rasp Pi network with text messages

Red Alert

© Lead Image © Valery Kachaev, 123RF.com

© Lead Image © Valery Kachaev, 123RF.com

Article from Issue 211/2018
Author(s):

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).

Figure 1: Control devices with SMS.

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.

Figure 2: Android Node-RED accessed from a PC.
Figure 3: Installing features in Node-RED.

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
Figure 4: SMS test setup.

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.

Figure 5: SMS scanning logic.

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.

Figure 6: Raspberry Pi TCP-to-GPIO logic.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
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.

Learn More

News