Add new comment

Control Arduino via USB OTG from Android

Introduction

Johnny-Five is an outstanding library that allows the remote control of Arduino or other IoT boards using Node.js scripts running in virtually in any system.

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, GPS, charging circuits, microphones, etc...). Combining the processing power of an old phone with the IO ports offered by the Arduino circuits is the ultimate ecological and cheap way to have the perfect IoT device.

This script allows Johnny-Five to run from a Termux terminal communicating with other Android apps or external devices by using the TCP protocol without root, without WifFi shield and without Bluetooth shield. The reason for the TCP protocol is that Termux still does not implement USB-OTG to Serial drivers, and the vanilla Johnny-Five would hang right out the box.

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;
Diagram

Release notes

This tool was retested on Termux 0.117 (F-Droid version) with NodeJS 14.15.4 and Johnny-Five 2.1.0

Requirements

Android apps

PC apps

Termux packages

  • nodejs (or nodejs-lts if not working)
  • python
  • clang
  • make
  • openssh (optional to operate the device from the Desktop computer)

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 using the commands:
    pkg install nodejs
    pkg install python
    pkg install clang
    pkg install make
    pkg install openssh
  3. Create a new folder in your device and extract the files index.js and package.json from the downloaded package. You can also install it directly from npm usign the command
    npm install johnny-five-android-termux

     

  4. Run the following command  from the same folder where the previous files were placed. If you installed via npm, this is not necessary.
    npm install
  5. Edit the index.js file and configure for your TCP-Serial Server host and port. Note that inside this file you have a section to include your Johnny-Five code. The default script is the famous blinking led.
  6. Test the script running node index.js from the same directory. If the TCP bridge is working, the script will be able to connect. If the Arduino board (with the Firmata firmware) is connected via USB to the phone and the USB-TCP bridge software is working, the led will start to blink.

Node-Red integration

Node-Red, the ultimate IoT interface can be run from Termux and the following tutorial in my web page explains how to make use of Johnny-Five library in this framework.

FAQ

  • What is the default baud rate for Firmata? The default value is 57600bps.

  • I only get the message "Connected to server" and nothing else Please check the baud rate of the server. It should be set for 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.

  • Node is exiting quietly without errors or messages Try updating your version of node. You can also try installing node-lts package on Termux.

Troubleshooting

  1. Install Node.js LTS version in your Laptop/Desktop with the additional compiler tools and follow the instructions from the installation step 3 to the end.
  2. Run the TCP server (like the COMbyTCP) in the same computer and try to connect the board to the local installation script. If this does not work, you might have a problem with the Firmata installation in the Arduino board.
  3. If the connection works, keep the TCP server running and try to connect from the mobile device to the computer. If this does not work you have a connectivity problem with the Android device.
  4. Test the TCP connection from Termux using the telnet command followed by the host IP adress and then the port.
    Example: telnet 192.168.0.11 5011 .

Get on GitHub

This content is also on GitHub.

Credits

Based on the code snippets from Luis Montes.

Files
Version
1.0.6 (August 2021)
Platforms
Android, Windows, MacOS, Linux
File size
107KB
Checksum
SHA256: CDCB87C6A974295906F2056D6E069CAD98EED9F77D5E97BA53EAC1F6B961B9F6

Post Categories

The content of this field is kept private and will not be shown publicly.
CAPTCHA
12 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Protected by Spam Master