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. Many of the mobile providers offer IoT packages for low data throughput, where you’d be looking at spending $1 to $5 per month for 1-5 MB of data. From the hardware standpoint there are many options such digital modem modules that come in either a Raspberry Pi top or a USB form factor.
If you are looking at doing some prototyping, using an Arduino phone and Node-Red is a great way to get jump started.
Node-Red on Android
Node-Red is a graphical programming system that is used for Internet of Things (IoT) projects. Node-Red is installed in the base Raspberry Pi images, but it can also be loaded on Linux, Windows, MacOS and Android systems.
To load Node-Red on Arduino, you will first need to load Termux, an Android Terminal Emulator app, that is 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 will start a Web graphical interface that is accessed by: http://the_ip_address:1880.
Extra features can be added and removed from Node-Red by selecting the “Manage Palette” menu option. For this project I needed terminux-api for the texting support, and bigtimer to scanning the text message buffer.
A Test Setup
For a basic setup I used:
- 1 Android phone with Termux and Node-Red
- 1 Raspberry Pi with Node-Red
- 1 Powerswitch Tail II connected to a light
- 1 Android phone for texting in
Scanning for Text Messages
To create a simple text message application on the Android Note-Red system, the following components are used:
- A Big Timer node is used to define how often the SMS inbox is scanned. Without doing any configuration, the second output will offer a 1/minute cycle time.
- An SMS Inbox node will read in a defined number of text messages. To get the last message select the buffer limit to be 1.
- A function node is used with some JavaScript to check for valid text messages
- A TCP out node will send a message to another Node-Red system.
A basic logic setup will be as follows:
The function node needs some logic to interpret the latest text message and send out the required logic to the Raspberry Pi GPIO pins. The example logic uses a text message of “LIGHTS ON” or “LIGHTS OFF” to control the output on GPIO pin 12.
// look for new action texts smslast = context.get('smslast')|| 0; // Do an action if there is a new SMS message if ( msg.payload[0].received != smslast) { context.set('smslast', msg.payload[0].received ) smsbody = msg.payload[0].body; if (smsbody.toUpperCase().trim() == "LIGHTS ON") { msg.payload = 1; // this is sent to the Pi via TCP return msg; } if (smsbody.toUpperCase().trim() == "LIGHTS OFF") { msg.payload = 0; // this is sent to the Pi via TCP return msg; } }
The first time that the code runs you will be prompted on the phone to “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 can not be directly connected I/O devices, so TCP connections can used to pass commands to Raspberry Pi’s which have the I/O support. Node-Red is pre-installed on most Raspberry Pi images so no added installation is required.
On the Raspberry Pi Node-Red side only a couple of nodes are required:
- A TCP in node is used to read the incoming communications. For the configuration of this node it is important to have the port (8888 in this case) match the TCP out node’s port. Also the output should be set as: single, and string.
- An RPI GPIO out node is used set Raspberry Pi General Purpose (GPIO) pins.
Texting Out from Android
If your text scheme is simple, for example one light that is turned on and off, then you could manually just output an “ON” or “OFF” message. However if you are controlling multiple lights, heaters and other devices then manually typing the text messages gets awkward.
To manage multiple commands, I wrote a simple Android app in MIT’s App Inventor. App Inventor is a free online Android development environment, that only requires a Google login for access. In about 10 minutes I was able to get a prototype going with multiple inputs. The first step is to drag and drop some buttons from the User Interface Palette onto the viewer pane. Then drag and drop a Texting component from the Social Palette onto the view. Note, the Texting component will appear as a non-visible item.
After you have a basic layout, select the Blocks button on the menu bar, and this will open a logic page. Logic is created by clicking on the objects in the Block panel, and then dragging and dropping the block onto the Viewer panel.
To have a button sent a text message the following pieces are required:
- “when Button.Click” will be called when the users touches the button object
- “set Texting1.PhoneNumber” defines the remote phone number
- “set Texting1.Message” defines the text message
- “call Texting.SendMessage” will send the message
To build the application use the Build menu item on the menu bar.
For my test project I configured four devices and eight commands.
Final Comments
I found that using Node-Red on Android to be a lot faster than I expected, however I noticed that some of the added features (like Bluetooth support) only worked on the Raspberry Pi/Linux version of the Node-Red.
For a final solution I would definitely move to dedicated SMS hardware, but I found it nice to be able to do proof of concept testing with just some basic Android phones. Also don’t forget to setup Node-Red for automatic startup on a power up.