BLuetooth data transfer

Hi all,
I hope that someone can spot the problem with what should be a simple application.

I have a ProMini (3.3v 8Mhz) connected to an accelerometer and HC-05 bluetooth module, with the intention of sending motion data to an Android phone. The phone app was written using MIT App Inventor.

Every few seconds, the Android app sends an 'S' (actually sends 83) to the promini, which then replies with a single item of data, which is a floating point number. Weirdly, I am getting the 'previous' result...

To try and figure it out, I've written simple programs without all of the accelerometer code (which works fine), just sending a random integer, and see this:

Value to be sent by ProMini Value received by Android
56 nothing
43 56
92 43
12 92

etc....

I find that, if the Android app waits for around 1000mS after sending the 'S', before checking for incoming data, it works correctly. I know that Arduino serial is a bit slow, but.....
As the accelerometer is using the normal serial rx/tx pins, I use the SoftwareSerial library for the bluetooth.

Oddly, if I change the arduino code and upload, the first poll returns the last value seen in the previous session, suggesting that the number is lurking in a buffer somewhere...In the above example, I would receive '12' in the first poll of the new session. Sometimes I receive 2 values together.
Also - when I leave it running for some time, I see that there are several 'no replies', apparently at random.
I've tried alternative methods such as BTserial.readString, to no avail.

Here is the arduino code - I can post the blocks for the MIT AI app if it's of use?
Thanks in advance for any clues offered...

//     BT test
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11); // RX | TX, go to TX, RX respectively of bt device hc-05

String msgToSend="22";
char incomingMsg ;
 
void setup(void) 
{
   Serial.begin(9600);  
 
 Serial.println("hello");
   BTserial.begin(9600);
  
}  // end of setup




void loop(void) 
{

 msgToSend="x";
while (BTserial.available() > 0){
incomingMsg = BTserial.read();    //read a byte sent by android
 Serial.println (incomingMsg);   
        
   }

  if (incomingMsg  ==   'S') {  // send data to android via BT
msgToSend= String(random(100));
Serial.println (msgToSend);  
 BTserial.print (msgToSend);
 
  incomingMsg="X";
 
}

}//   end of main loop
    incomingMsg = "X";

You are putting 2 characters, the X and the terminating 0 of the string into a single character variable so writing on memory that you should not be

Try

    incomingMsg = 'X';

That was very quick! Thanks, will try it..

That was very quick!

That is what Alerts are for :slight_smile:

Thanks, but it is still sending the 'previous' value. The incorrect use of "X" was a leftover from when I was sending a string, rather than a byte - but changing to 'X' hasn't changed things.

What is the exact format of what you are sending ?
How many bytes ?
Does it by any chance have a trailing carriage return and/or linefeed ?

Hi Delavel.

Have you tried using a Bluetooth Serial Terminal app on the phone rather than your own app? That might help determine if the problem is with the app or the sketch.

Please post your App inventor blocks.

John.

Hi, thanks again...

I am sending a single byte of 83 - I had been sending the text "S", but had read that sending strings isn't a good idea - but none of this made a difference. There is no trailing return, line feed or whatever.

I've just tried an Android terminal (ArduTooth) which sends the "S", the ProMini recognises this and replies (I can tell from the PC serial monitor window), but ArduTooth shows no reply. (I had to change the arduino code back to dealing with strings.)

I tried another Bluetooth Terminal app, got the same results.

I've attached the AI blocks.
It's a puzzle..

cheers
Dave

I've had no trouble doing simple things like you're attempting and more complicated comms b/w Arduino & phone using MIT App Inventor.

First, I think it's essential to master using a serial terminal app. A simple pass thru sketch is teh starting point. You have to be able to achieve two way comms b/w the Arduino IDE and the terminal app.

Second, I'm surprised to see you enabling the clock in your blocks. I would do this w/o any clock. Just send the data when a button is pressed and display any received data. No clock needed. The clock could well be the thing causing the problem as it introduces a timing dimension to the tasks.

Third, I just Send Text in the app, i'e', send 'S' rather than 1ByteNumber.

You'll get there.

John.

Thanks, John. I've written several far more complex applications using MITAI, Bluetooth etc, but am struggling with this, which should be dead easy... I was hoping that someone might spot one of those stupid errors that sometimes creep in.

The reason for using the clock, as described in my original post, is that I found that unless I introduce a delay between the android sending the 'S', and checking for data returned, I was getting the 'previous' value. If I remove the delay - send the 'S' and then immediately read the incoming byte, it does appear to work - but when checked, I'm seeing that previous value.

The reason for sending a byte of 83, rather than sending a string 'S', was that I'd read that strings on the arduino can be problematic - I was dubious about that, but there was a bit of straw-clutching going on by then!

I am puzzled abut the lack of reply to the android terminal, and will have a fiddle with that.

Try this experiment:

Serial.println('S');

aarg:
Try this experiment:

Serial.println('S');

thanks but I'm not sure what that would achieve - the promini is sending characters to the PC via Serial just fine, it's the sending of characters to the phone with SoftwareSerial that's being odd.

I cannot seem to get any data received in the Android Bluetooth terminal, will be looking into that next

I think you're having a classic problem, trying to handle async events as if they are synchronous. You need to implement handshaking symbols in the stream between the Android and Arduino app. You are getting junk back because you're not doing that.

OK, now I am really puzzled... am I correct in thinking that the below code should send a series of numbers to an android terminal app? I am seeing nothing...

[code]


//     BT test
#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11); // RX | TX, go to TX, RX respectively of bt device hc-05
String msgToSend = "22";
String incomingMsg = "X";  // stores message from phone



void setup(void)
{
  Serial.begin(9600);
  Serial.println("hello");
  BTserial.begin(9600);
}  // end of setup




void loop(void)
{
  msgToSend = String(random(100));
  Serial.print ("sending: ");
  Serial.println (msgToSend);
  BTserial.print (msgToSend);
  delay (500);
  
}//   end of main loop

[/code]

am I correct in thinking that the below code should send a series of numbers to an android terminal app? I am seeing nothing...

Yes. When I use this code with Kai Morich's Bluetooth Serial Terminal I see the numbers sent to the phone that are shown on the Serial monitor.

cattledog:
Yes. When I use this code with Kai Morich's Bluetooth Serial Terminal I see the numbers sent to the phone that are shown on the Serial monitor.

thanks for that - I had tried 2 other android bluetooth terminal apps with no success, but this one works as it should. I had been starting to wonder if my phone was somehow to blame...

Just in case the ProMini was being weird, I set up an UNO with another HC-05, this worked fine.

I did try with a Windows Bluetooth Serial monitor (you have to assign a COM port manually) and found that it worked, although a little odd, in that after sending 6 or 7 values, it would pause, continue for 6 or 7, and so on.

Delavel:
I am seeing nothing...

#include <SoftwareSerial.h>
SoftwareSerial BTserial(10, 11); // RX | TX, go to TX, RX respectively of bt device hc-05

void setup(void)
{
  Serial.begin(9600);
BTserial.begin(9600);
delay(1000);
void loop()
{
Serial.println("hello");
BTserial.println("hello");
}

You are just trying to make things more complicated than they really are, and there was good advice in reply #10. You may have been having grief with Pro Mini because you were running HC-05 of 3.3v.

Nick_Pyner:

#include <SoftwareSerial.h>

SoftwareSerial BTserial(10, 11); // RX | TX, go to TX, RX respectively of bt device hc-05

void setup(void)
{
  Serial.begin(9600);
BTserial.begin(9600);
delay(1000);
void loop()
{
Serial.println("hello");
BTserial.println("hello");
}




You are just trying to make things more complicated than they really are, and there was good advice in reply #10. You may have been having grief with Pro Mini because you were running HC-05 of 3.3v.

nope - the HC-05, although I found that it does work from the 3.3v supplied from the promini, it is now supplied directly by the 4.8v powering the promini. That's one of the first things that I changed when this issue cropped up. Am still fiddling, as time allows..