How to read Arduino input on Android phone via Bluetooth

1,887 views
Skip to first unread message

Elias Fares

unread,
Jan 11, 2016, 7:10:40 PM1/11/16
to DroidScript
Hello,

Please bare with me as I am somewhat new to this but I seem to have gotten the hang of DroidScript and some of the things it can do (thanks to the samples and docs).
I need help with the bluetooth serial and Arduino setup. I am making an app in JavaScript and here is what I have:
  • Android phone (4.1.2)
  • HC-05 Bluetooth module
  • Arduino Leonardo
  • 8 Flex/Bend sensors
I would like to use the input from the flex sensors (going into pins A0-A7) to control my app and I'm not exactly sure how to get the readings via the Bluetooth serial?
What sketch should I have uploaded onto my Arduino board? What is the syntax to get the readings from those pins?

Any help or links to tutorials would be much appreciated.

Thanks in advance.

luke x3

unread,
Jan 11, 2016, 8:17:09 PM1/11/16
to DroidScript
little script based on an am3x accelerometer and a leostick
ReadAccel2Mouse.ino

Elias Fares

unread,
Jan 12, 2016, 3:18:00 PM1/12/16
to DroidScript
Sorry that's not exactly what I'm looking for.

To be clear with what my problem is:
I am looking at the Bluetooth Serial example from the DroidScript Samples and I am trying to figure out where/how do I receive and read data input?
When I run the sample program nothing pops up.

I want to read in data from pin A0.

Dave Smart

unread,
Jan 13, 2016, 12:44:32 PM1/13/16
to DroidScript
Try to find a Bluetooth serial example Arduino project that uses the HC-05 and get that working with one of the free Android serial terminal Apps first.  Then you can try to get DroidScript talking to it after that (i.e. Break the problem into two halves)

Its worth checking on instructables.com to see if anyone has documented a similar project.

luke x3

unread,
Jan 13, 2016, 8:58:16 PM1/13/16
to DroidScript
Ah it's okay, my answer kind of only related to this part (somewhat indirectly)
> What sketch should I have uploaded onto my Arduino board? What is the syntax to get the readings from those pins?

I take it you've got the flex readings on the arduino working (probably the easiest bit) > you can use the serial monitor in the arduino IDE when connected to a PC by usb for that part

the sample at http://wiki.droidscript.me.uk/doku.php?id=built_in:bluetoothserial seems a little misleading, i wouldn't really expect this bit

bt.Write( "digitalWrite(LED1,1);\n" );
to work without some code on the other side, unless the HC-05 directly supports it?

Steve Garman

unread,
Jan 14, 2016, 3:01:05 AM1/14/16
to DroidScript
Luke,
the Bluetooth serial sample is not specific to the Arduino.

The code works as-is when connected to an Espruino with an HC-05 module attached.

Elias Fares

unread,
Jan 14, 2016, 11:33:11 AM1/14/16
to DroidScript
Thanks for the responses guys. Much appreciated, I will continue to look into this.

2 things:
1) So if I get an Espruino board then I should be able to read data using analogRead(A0) and bt.SetOnReceive() ?
2) Since I am using an Arduino board I guess I have to find another way to read data being sent from the HC-05 Bluetooth module right?

I will look into instructables.com and at Arduino sketches that are used to send data via Bluetooth. Maybe I can just send the values as strings and then use JavaScript to pick apart the string and read the values?
Thanks again!

Elias Fares

unread,
Jan 14, 2016, 2:56:52 PM1/14/16
to DroidScript
I found this tutorial and follows it:

Using that sketch, DroidScript was able to read (and display) data from the Arduino via BlueTooth.
Thanks for the suggestion! :)

The sketch I uploaded was:
// This program sends data via Bluetooth

#include <SoftwareSerial.h> // Import the Arduino serial library
SoftwareSerial mySerial(0, 1); // RX, TX

// Declare variables
int A0val;

// Setup code here, runs once
void setup() {
 
// Initialize serial communications at 9600 bps
  mySerial
.begin(9600);
  mySerial
.println("Bluetooth On...");
  pinMode
(A0, INPUT);
}

// Main code, loops forever
void loop() {
 
// Constantly output analog values
  A0val
= analogRead(A0);
  mySerial
.println(A0val);

 
// Wait 2 milliseconds before the next loop
  delay
(2);
}

luke x3

unread,
Jan 15, 2016, 1:54:51 AM1/15/16
to DroidScript
Nice to see that you got it working, and with very little code

I think the easiest way for sending multiple values might be to send it over from the ardu in JSON string format.. You'll just have to set it up manually in the mySerial.println() string

luke x3

unread,
Jan 15, 2016, 2:25:32 AM1/15/16
to DroidScript
posting twice in a row =) just did i little search on this, Json arrays are pretty lean actually..

the format for an array is just square brackets and comma, so you can send as println("[1,2,3,4,5,6,7,8]") and objx = JSON.parse("[1,2,3,4,5,6,7,8]") gives objx[0] = 1, objx[7] = 8, etc

@steve yeah i kinda expected that, wasn't quite sure which one

Elias Fares

unread,
Jan 15, 2016, 1:27:50 PM1/15/16
to DroidScript
Thanks.

I'm trying to play around with it and having a hard time sending the array of data from Arduino IDE.
I'm testing it with 4 analog pins set up and I've tried:
int pinA[ ] = { analogRead(A0), analogRead(A1), analogRead(A2), analogRead(A3) };
Serial.println(pinA[0,1,2,3]);

but it doesn't work. Only the last array element gets displayed: pinA[3];
I'll look more into JSON parsing and try to find tutorials.

luke x3

unread,
Jan 16, 2016, 12:22:32 AM1/16/16
to DroidScript

Here, i'm pretty sure this should work (hopefully.. )
Examples of multiple concats on one line are a bit scarce

String jsonStr = "[" + String(pinA[0]) + "," + String(pinA[1]) + "," + String(pinA[2]) + "," + String(pinA[3]) + "]"; //a chance of working
Serial.println(jsonStr);

caveats
https://www.arduino.cc/en/Tutorial/StringAdditionOperator
http://stackoverflow.com/questions/5534290/arduino-serial-print-optimization
https://www.arduino.cc/en/Tutorial/StringConstructors

seems it may need to be written as something like

String
jsonStr = "[";
jsonStr
+= String(pinA[0] + ",");
jsonStr += String(pinA[1] + ",");
jsonStr += String(pinA[2] + ",");
jsonStr
+= String(pinA[3] + "]");


Elias Fares

unread,
Jan 18, 2016, 6:00:22 PM1/18/16
to DroidScript
Hey Luke,

That's awesome! It worked like a charm. BUT using JSON.parse caused some issues. What I did was this:
In Arduino IDE
String data = String(analogRead(A0))+","+String(analogRead(A1))+","+String(analogRead(A2))+","+String(analogRead(A3)); // Removed [ ] brackets
mySerial
.println(data);
In DriodScript IDE
function bt_OnReceive(data) {
    A
= data.split(",");
    console
.log(A[0]);
}

Using JSON.parse(data) kept giving me the error: Unexpected token ,,
I started debugging and made sure I was using json.parse correctly. I was able to make A= JSON.parse("[1,2,3,4]") give A[0]=1 but when I put it inside the bt_OnRecieve(data) it just didn't seem to work.
I did console.log(data) before I parsed the data and saw that only part of the String was being received "3,4]" was only logged so "[1,2," was missing when JSON.parse ran. I guess that is what caused the "Unexpected token ,," error message.

This error kept occurring consistently but never when I sent a string (without [ ]) and used the split function.

luke x3

unread,
Jan 25, 2016, 12:39:21 AM1/25/16
to DroidScript
Ah cool =)
About the only thing i can think of (user-side) that might have caused the errors, i've heard that sending data too fast over a slower baud rate can flood the buffers... but it's only two extra chars so I wouldn't think it'd be that.

Kind of curious to hear how you will use all that input in your app, if your still going with the 8 x 10bit analog readings... I'd imagine you could just about run a robotic arm off that ;D

Elias Fares

unread,
Jan 25, 2016, 11:59:42 AM1/25/16
to DroidScript
I am doing my Masters and my thesis is on the study of Flexible Displays.

So I will be using flex sensors as inputs to an application that I am building using DroidScript. I intend to create a flexible bezel for my Android phone with bend sensors along the bezel. I want to simulate bending the edges of the phone to interact with the phone and its applications (ex: to play a game using bend inputs).

luke x3

unread,
Jan 25, 2016, 9:08:08 PM1/25/16
to DroidScript
Aha! That does sound pretty interesting.. So if all goes well we could all have flexible-edged phones to play with sometime in the future? =D

I wonder if the error you described earlier is indicative of too low a baud rate... just doing some quick reading
> Information can be sent 1 bit at a time in a scheme called serial communications, or in groups of bits (usually 8 or 16 at a time) in a scheme called parallel communications.

So if it is running at 9600 baud & 1 bit per signal (bluetooth _serial_), you get a maximum 1200 bytes / string characters per second.. I think it might be worth a try raising the baud rate or slowing down the arduino to 20 - 50 (at most) sensor value broadcasts, as without the brackets you/data.split might not notice errors in the data... I'm not sure if javascript will always flag you for reading past the end of an array either, and pure numbers with comma delimit and line break is quite probably overwrite-ambiguous

Another way to check using data.split would be to add an incremental counter to the broadcast modulo 100, and check that each reading is 1+(the last reading) mod 100

luke x3

unread,
Jan 25, 2016, 9:18:32 PM1/25/16
to DroidScript
I guess even 2 or 3+the last reading is okay, that just means it's dropping whole sets of readings... but if it starts varying wildly then that is real trouble (it's probably out of sync)

Dave Smart

unread,
Jan 26, 2016, 4:05:36 AM1/26/16
to DroidScript
Hi Elias,

Are you using the bt.SetSplitMode() method?

I think you will need to do this, so that DroidScript knows how much data to collect before firing the callback to you.  If you don't use that then you will need to create your own FIFO buffer to watch for each chunk of data coming from the HC-05.

So if you decide to mark the end of each of your chunks of data using a semi colon (for example) you would do this:-

bt.SetSplitMode( "End", ":" ); 


Elias Fares

unread,
Feb 15, 2016, 9:13:01 AM2/15/16
to DroidScript
Hi Dave,

Yes, I am using the bt.SetSplitMode() method (it was in the bluetooth sample) but I was not sure what it was for till now.
Thanks for sharing that info :)

Eli

Andy Barnhart

unread,
Feb 15, 2016, 8:03:48 PM2/15/16
to DroidScript
The earlier math is off. 9600 baud is 960 characters per second - start bit, stop bit and 8 bits of data so you pretty much divide by 10. When I send transmissions of data between things I control at both ends, I usually pack and unpack them myself with very specific protocols. It is much faster to split a string on a specific single delimiter than to parse it according to a standard that allows for a lot of variations. The modulo arithmetic suggested is the old school trick though it was usually done mod 8 with 3 bits and an ACK/NAK response bit for 4 and then a pair of those for NS (number sent) and NR (number received). I used to write firmware back in the 80s and still dabble with devices like Arduinos sometimes.
Reply all
Reply to author
Forward
0 new messages