Control Arduino from Android using Node-RED and Johnny-Five
Introduction
Android mobile phones have all the perks you would want for your Arduino board (screen, real time clock, storage, LTE/GSM, sensors, WiFi, Bluetooth, Cameras, UPS, charging circuits, microphones, etc...). Combining the processing power of an old phone with the verstaile IO ports offered by the Arduino circuits is the ultimate ecological and cheap way to have the perfect IoT device.
In another article I explained how to connect an IoT board with an Android mobile phone and then program it using JavaScript with Johnny-Five. This tutorial explains how to control the board from the Android system using Node-RED.
Node-RED is visual programming tool for wiring together hardware devices, APIs and online services. It provides a browser-based editor that makes it easy to program and deploy sketches.
Here we will explain how to install and configure Node-RED in an Android device, then we will configure Johnny-Five library to access the board using a TCP connection. The Arduino or similar board can be directly connected to the Android device using USB OTG or connected to another device using Ethernet ports, Wifi shields or Blutetooth.
If you want to connect the board in the same device using the USB-OTG port you will just need a TCP bridge application. I added bellow some free and paid options.
Please note that this solution:
- Does not rely on any cloud service;
- Does not rely on WebUSB;
- Does not require Ethernet, WiFi or Bluetooth shields on Arduino;
Release notes
- This tutorial was updated and retested on Termux 0.117 (F-Droid version) with NodeJS 14.15.4, Node-RED 2.0.5 and node-red-contrib-gpio 0.51.0
Requirements
Android apps
- Termux
- ServerBridgeX or UART-TCP/IP Bridge if the board is connected via USB-OTG to the Android device.
PC apps
- Bitvise SSH client (optional to access the Android Termux terminal via SSH)
- Arduino IDE to program the Arduino board with the Firmata sketch
- COMbyTCP to use the PC as server (optional)
Termux packages
- nodejs (If Node-RED fails, try installing nodejs-lts package instead)
- python
- clang
- make
- openssh (optional to operate the device from a PC)
Installation
- Install the Firmata sketch in your Arduino/IoT board. Detailed instructions can be found in the Johnny-Five platform pages.
- Install the Termux packages and then Node-Red:
pkg upgrade pkg install nodejs pkg install pyhton pkg install make pkg install clang npm i -g --unsafe-perm node-red
- Run node-red for the first time so it can create the configuration files:
node-red
- Once it started, you can kill node-red with the Crtl+C command.
- Install Johnny-Five and the Mock-Require library. We need the last one to bypass the hardware serial ports that are not available in Termux and could crash your Node-RED server.
cd ~/.node-red npm install node-red-contrib-gpio npm install mock-require
Note: The previous version of this tutorial was using node-red-contrib-johnny5 package instead of node-red-contrib-gpio. If you are having problems, try rolling back to the old package.
- Disable the hardware serial ports in Johnny-Five.
Edit the file ~/.node-red/settings.js and add the following lines before the module.exports:
var mock = require('mock-require'); mock('@serialport/bindings', '@serialport/binding-mock'); mock('serialport', '@serialport/stream');
- Start the USB Serial to TCP application (see below) in your Desktop computer or in the Android device. Don't forget to set the baud rate to 57600 bps.
- Restart node-red:
node-red
- Open your browser from the Android device or from another computer in the same network and go to: xxx.xxx.xxx.xxx:1880 where the xxx represent the IP of the device.
- Import the test flow, find the board configuration menu and set the IP address and the port to your TCP-USB bridge (default is 127.0.0.1:8000).
- Install Firmata sketch in your Arduino board, connect it via USB to the mobile phone.
- Start your TCP-USB bridge app from Android.
- Deploy the sketch in Node-Red and see if it works....
FAQ
-
What is the default baud rate for Firmata?The default value is 57600bps.
-
Do I need to root my Android device in order to access the Arduino board using USB? No. Just use a USB-Serial to TCP bridge app and you are fine.
-
I can't connect to a TCP server broadcasting in my own WiFi network Please make sure that your Android is not running a VPN or a Firewall.
-
Running out of disk space in the Android device After the installation you can remove the python, clang and make packages.
Troubleshooting
Try running only Johnny-Five from the Android device according to the instructions in this article. Most of the problems are related with the hardware connection to the TCP bridge.