Control Arduino from Android using Node-RED and Johnny-Five

Control Arduino from Android using Node-RED and Johnny-Five

By Daniel
Sat, 01/04/2020 - Updated 6 months ago
0 comments

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;

Node-Red + Johnny-Five in Android Diagram

Requirements

Android apps

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-lts (the latest version does not work with Node-RED)
  • python
  • clang
  • make
  • openssh (optional to operate the device from a PC)

Installation

  1. Install the Firmata sketch in your Arduino/IoT board. Detailed instructions can be found in the Johnny-Five platform pages.
  2. Install the Termux packages and then Node-Red:
    pkg upgrade
    pkg install nodejs-lts
    pkg install pyhton
    pkg install make
    pkg install clang
    npm i -g --unsafe-perm node-red

     

  3. Run node-red for the first time so it can create the configuration files. Just type node-red from the command line.
  4. Once it started, you can kill node-red with the Crtl+C command.
  5. 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:
    cd ~/.node-red
    npm install node-red-contrib-johnny5
    npm install mock-require

     

  6. 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');

     

  7. 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.
  8. Restart node-red.
  9. Open your browser in the Android device or from another computer in the same network: xxx.xxx.xxx.xxx:1880 where the xxx represent the IP of the device.
  10. Import the test flow, find the board configuration menu and set the IP address and the port to point to your TCP bridge.
  11. Deploy the skech 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.