cordovarduino
Want a module for your Arduino board that provides:
- Power
- High-res Touch Interface
- Storage
- AND connectivity? (WiFi + 3G + Bluetooth)
Hey, why not just use your Android phone/tablet?
This Cordova/Phonegap plugin allows two-way serial communication using USB On-The-Go (OTG) from your Android device to your Arduino board or other USB-powered serial IO device.
And that means that ability to give your Arduino project a mobile app (web-view) interface as well as powering it using the rechargeable battery on your phone!
Install it
From the root folder of your cordova project, run :
cordova plugin add cordovarduino
How to use it
Your first need to understand how to create and upload a simple Cordova Project. Here is some info on how to get started with Cordova on Android, and here is a simple Cordova plugin you can use to get familiar with the plugin system.
The plugin API for this behaves as follows:
Because you're polite, first request the permission to use the serial port to the system:
serial
You can now open the serial port:
serial
opts
is a JSON object with the following properties:
- baudRate: defaults to 9600
- dataBits: defaults to 8
- stopBits: defaults to 1
- parity: defaults to 0
- dtr: defaults to false (it may be needed to be true for some arduino)
- rts: defaults to false (it may be needed to be true for some modules, including the monkeyboard dab module)
- sleepOnPause: defaults to true. If false, the the OTG port will remain open when the app goes to the background (or the screen turns off). Otherwise, the port with automatically close, and resume once the app is brought back to foreground.
You're now able to read and write:
serial
data
is the string representation to be written to the serial port.
buffer
is a JavaScript ArrayBuffer containing the data that was just read.
Apart from using serial.write
, you can also use serial.writeHex
to have an easy way to work with RS232 protocol driven hardware from your javascript by using hex-strings.
In a nutshell, serial.writeHex('ff')
would write just a single byte where serial.write('ff')
would let java write 2 bytes to the serial port.
Apart from that, serial.writeHex
works the same way as serial.write
does.
Register a callback that will be invoked when the driver reads incoming data from your serial device. The success callback function will recieve an ArrayBuffer filled with the data read from serial:
serial;
And finally close the port:
serial
A Simple Example
A callback-ish example.
var { ;}; serial;
A Complete Example
Here is your index.html
:
Hello World Potentiometer value Value ... ... On Off
Here is the index.js
file:
var app = { document; } { var potText = document; var delta = document; var on = document; var off = document; var open = false; var str = ''; var lastRead = ; var { ; }; // request permission first serial; on { console; if open serial; }; off { if open serial; } }; app;
And here is your Arduino project .ino
file, with a potentiometer on A0 and a led on 13:
unsigned long previousMillis;int interval = 50; void void
Your Device is not (yet) known?
Thanks to usb-serial-for-android library, you can communicate with CDC, FTDI, Arduino and other devices.
Your device might not be listed over at https://github.com/mik3y/usb-serial-for-android . If you know your devices VID (Vendor ID) and PID (Product ID) you could however try
serial
You can also choose the driver to use. Options are:
CdcAcmSerialDriver
Ch34xSerialDriver
Cp21xxSerialDriver
FtdiSerialDriver
ProlificSerialDriver
It defaults to CdcAcmSerialDriver
if empty or not one of these (please feel free to add a PR to support more).
serial
You can find your devices VID and PID on linux or android using "lsusb" (returning VID:PID in hex) or by looking at your dmesg log.
Change log
2015.10: Ed. Lafargue: Implemented "sleepOnPause" flag in the 'open' options to prevent closing the OTG port when app goes to background.
2014.08: Zevero: Option to find device by VID and PID, that let you use "unrecognized" devices.
2014.07: Hendrik Maus: Implemented writeHex for working with RS232 protocol, i.e. javascript can now pass "ff", java turns it into a 1 byte array and writes to the serial port - naturally, java, and the existing write method here, would create a 2 byte array from the input string.
2014.04: Derek K: Implemented registerReadCallback for evented reading and Android onPause/onResume
2014.03: Ed. Lafargue: Implemented read(). The success callback returns a Javascript ArrayBuffer which is the best way to handle binary data in Javascript. It is straightforward to convert this to a string if required - a utility function could be implemented in this plugin.
2013.11: Xavier Seignard: First implementation