Arduino Forum

Using Arduino => Programming Questions => Topic started by: meanduck on Dec 20, 2016, 10:07 pm

Title: trying to get reading from Bluetooth adapter
Post by: meanduck on Dec 20, 2016, 10:07 pm
Hi everyone,

Im trying to get readings from my bluetooth adapter (HC06) on my Arduino Micro.
ive got a very simple code but im only getting "-1" om my serial monitor. does someone has a clue why this is happening?

Code: [Select]
#include <SoftwareSerial.h>

int bluetoothTx = 7;
int bluetoothRx = 8;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{

  //Setup usb serial connection to computer
  Serial.begin(9600);
  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  Serial.println(bluetooth.read());
}
Title: Re: trying to get reading from Bluetooth adapter
Post by: meanduck on Dec 20, 2016, 10:16 pm
PS. i've got an adroid app that should send out values from 1000-3255
Title: Re: trying to get reading from Bluetooth adapter
Post by: Nick_Pyner on Dec 21, 2016, 03:15 am
PS. i've got an adroid app that should send out values from 1000-3255
That sounds like just the sort of thing everybody needs.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino
Title: Re: trying to get reading from Bluetooth adapter
Post by: Dan95 on Dec 21, 2016, 03:34 am
Have a look at this example:

https://www.arduino.cc/en/Tutorial/SoftwareSerialExample
 (https://www.arduino.cc/en/Tutorial/SoftwareSerialExample)

-1 is the return value from read() function when there is nothing in the port to read.
Title: Re: trying to get reading from Bluetooth adapter
Post by: PaulS on Dec 21, 2016, 12:12 pm
Quote
-1 is the return value from read() function when there is nothing in the port to read.
Testing that there is data to read, BEFORE reading, will get rid of the pesky -1.
Title: Re: trying to get reading from Bluetooth adapter
Post by: meanduck on Feb 01, 2017, 12:28 pm
Hi Guys!

so ive managed to get some data.

if I send a 1bytenumber with my android app (appinventor2.0) it works great. ill get a number from 0-255 (more won't fit in a single byte so thats an issue).
but when i try to send it as a 2bytenumber, 4bytenumber or text I get 3 lines on my serial monitor instead of one single line with one number.
for example:
3
7
6
what I want:
376

does anybody know why this is happening and how i can fix it?

Thnaks in advance :)

Tymen

Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX


void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
pinMode(13, OUTPUT);



  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}


void loop() //
{
while(mySerial.available()){
    delay(10);
    char text = mySerial.read();
    Serial.println(text);
  }
Title: Re: trying to get reading from Bluetooth adapter
Post by: PaulS on Feb 01, 2017, 12:39 pm
Quote
does anybody know why this is happening
Your Android code and you Arduino code make this happen. You seem to have forgotten to post either one.

Quote
and how i can fix it?
A 10 lb. sledgehammer.
Title: Re: trying to get reading from Bluetooth adapter
Post by: PaulS on Feb 01, 2017, 12:42 pm
Since you did post the code, after I commented...

You clearly expect to read the data as text. So, clearly the Android is sending the value as text.

You might want to take a peak at the manual for parseInt() (https://www.arduino.cc/en/Reference/StreamParseInt).
Title: Re: trying to get reading from Bluetooth adapter
Post by: Whandall on Feb 01, 2017, 12:51 pm
Why don't you use the free hardware Serial on the Micro?
Title: Re: trying to get reading from Bluetooth adapter
Post by: meanduck on Feb 01, 2017, 01:42 pm
@ paul : here u go!
(http://i68.tinypic.com/2ivgzz8.jpg)

@ Paul : I also found out that Serial only take 1 byte at a time. so that why its acting so wierd on 2/4/text.
so i figured i need to use the buffer, but i havent figured out how. first time im using this function.


@Whandall: each time i want to upload a new code i have to unplug and plug the BT module. its a pain... so im using the software serial instead.
Title: Re: trying to get reading from Bluetooth adapter
Post by: PaulS on Feb 01, 2017, 01:44 pm
Quote
first time im using this function.
Using which function?

Serial Input Basics - Updated (http://forum.arduino.cc/index.php?topic=396450.0)
Title: Re: trying to get reading from Bluetooth adapter
Post by: Whandall on Feb 01, 2017, 01:46 pm
@Whandall: each time i want to upload a new code i have to unplug and plug the BT module. its a pain... so im using the software serial instead.
You don't have to do that on a Micro, it has a seperate hardware Serial.

Serial goes to upload and USB,
Serial1 goes to the free rx and tx pins.

Quote
In addition, some pins have specialized functions:

    Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Micro, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.
https://www.arduino.cc/en/Main/ArduinoBoardMicro (https://www.arduino.cc/en/Main/ArduinoBoardMicro)
Title: Re: trying to get reading from Bluetooth adapter
Post by: meanduck on Feb 01, 2017, 02:21 pm
Using which function?

Serial Input Basics - Updated (http://forum.arduino.cc/index.php?topic=396450.0)
buffering your serial input so i cant get multiple bytes of data in to one var. I keep getting multiple lines for one push of data. and i do not know how to get this in to one variable.

Title: Re: trying to get reading from Bluetooth adapter
Post by: meanduck on Feb 01, 2017, 02:27 pm
i tried this tutorial (https://www.youtube.com/watch?v=X5u2qCzcPn8&t=1s), but i still get this on my Serial monitor:
P
I
N
O
N

1
3


______
i created a button in AppInventor that sends "PINON 13" as TEXT.


Title: Re: trying to get reading from Bluetooth adapter
Post by: PaulS on Feb 01, 2017, 02:33 pm
Quote
i tried this tutorial
I'm not watching a movie to see some code. You already watched it, and stole, or typed, the code. Post it here.

You CAN collect data in an array, as Robin2's tutorial (that I posted a link to) shows.
Title: Re: trying to get reading from Bluetooth adapter
Post by: meanduck on Feb 01, 2017, 02:45 pm
i think i got the solution.

ive made a string var and put the serial data in there. but i also change "mySerial.read" to "mySerial.readString"

and now i get readble and expected values.

thanks for helping though!