Go Down

Topic: trying to get reading from Bluetooth adapter (Read 1 time) previous topic - next topic

meanduck

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());
}

meanduck

PS. i've got an adroid app that should send out values from 1000-3255

Nick_Pyner

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

Dan95

Have a look at this example:

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


-1 is the return value from read() function when there is nothing in the port to read.

PaulS

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.

meanduck

#5
Feb 01, 2017, 12:28 pm Last Edit: Feb 01, 2017, 12:38 pm by meanduck
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);
  }

PaulS

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.

PaulS

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().

Whandall

Why don't you use the free hardware Serial on the Micro?
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

meanduck

@ paul : here u go!


@ 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.

PaulS

Quote
first time im using this function.
Using which function?

Serial Input Basics - Updated

Whandall

#11
Feb 01, 2017, 01:46 pm Last Edit: Feb 01, 2017, 01:50 pm by Whandall
@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
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

meanduck

Using which function?

Serial Input Basics - Updated
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.


meanduck

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.



PaulS

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.

Go Up