HC-05 Bluettoth: No BTSerial.writes ...

Hi All

My BTSerial.write doesn’t seem to do anything! Can you help?

I am setting up a very simple Arduino and HC-05 to simulate the output from a heart monitor, emitting (eventually) at 1KHz. I have wired up the HC-05 to the Arduino Uno.The App I’m using is Serial Bluetooth Terminal. The communications function well, excepting on a specific aspect.

In the loop() I have two write/print commands:
BTSerial.write(postString);
Serial.println(postString);

  1. With both commands, I get output on both the Serial Monitor and the App. That seems Okay.

  2. If I comment out the line BTSerial.write, I STILL get output on BOTH the Serial Monitor and the App. This is NOT Okay.

  3. If I comment out the line Serial.println, I get NO output on either the Serial Monitor or the App. This is NOT Okay.

This is what I was expecting:
for 2) If I comment out the line BTSerial.write, I would ONLY get output on the Serial Monitor.
for 3) If I comment out the line Serial.println, I would ONLY get output to the App.

I’m puzzled why the BTSerialwrite doesn’t print, and, why the Serial.print writes to both channels!!

Can you help?

Could it have something to do with the pins I am using - or incorrectly using?

I look forward to your replies.

Regards
EGB

==============================

Here’s an example of my Serial Output.

64900,12345.67,45678.90,34567,89
64935,12345.67,45678.90,34567,89
64970,12345.67,45678.90,34567,89
65006,12345.67,45678.90,34567,89
65041,12345.67,45678.90,34567,89
65077,12345.67,45678.90,34567,89
65112,12345.67,45678.90,34567,89
65147,12345.67,45678.90,34567,89
65182,12345.67,45678.90,34567,89
65218,12345.67,45678.90,34567,89
65253,12345.67,45678.90,34567,89
65289,12345.67,45678.90,34567,89
65324,12345.67,45678.90,34567,89
65359,12345.67,45678.90,34567,89

Here’s my sketch code:

/* Simulates the output from a heart monitor, using HC-05 for the Bluetooth stream
 *  HR-05 connected to an Arduino
 *  All default parameters
 */
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(0, 1); //TX, RX respectively

// ESSENTIAL: set pin 10 as OUTPUT/HIGH for initialization
// other pins needed for control by the SPI library):
const int chipSelectPin = 4; // 4 or 7;
const int W5500_SS = 10; // chip select

//Time
unsigned long now = millis();
String readString = "";
char postString[50] = "                                                  ";

void setup() {
  Serial.begin(9600);
  BTSerial.begin(9600);
  delay(100); 
}

void loop() {
  now = millis();
  readString = String(now);
  readString += (",12345.67,45678.90,34567,89");
  for (byte i = 0; i < readString.length(); i++) {
      postString[i] = (char)readString[i];
      postString[i+1] = '\0'; // Add a NULL after each character
  }
  BTSerial.write(postString);
  Serial.println(postString);
}

Could it have something to do with the pins I am using - or incorrectly using?

Yes, it idoes.
Your objective is incomprehensible, but the problem is clear. For reasons unknown, you apparently want to send different information to Bluetooth and Serial monitor but you have bluetooth and serial monitor on the same port, and I'm amazed you get anything.

Your items

  1. Serial print is working. BTSerial maybe, probably not, but your code is junk and not worth looking at.

  2. Serial print is working, everything it normal, commenting out BTSerial is a really good idea.

  3. No surprise. The only sensible line in the code is now not in the code.

Your main problem is using software serial on hardware pins 0,1.

If you really want to send different data to monitor and bluetooth, move software serial and bluetooth to pins 3,4. You will then get what you expect.

Otherwise, get rid if all reference to Software serial and leave things wired as they are. Note that, in this event:

  1. you need to disconnect bluetooth when uploading code.
  2. you can prove the code without bluetooth connected
  3. Arduino doesn't care what is connected to where, it just talks to the port.
  4. the data on monitor will be the same as on the app, which might not be what you want.

Nick_Pyner:
... the problem is clear.
... you apparently want to send different information to Bluetooth and Serial monitor but you have bluetooth and serial monitor on the same port, and I'm amazed you get anything.
... The only sensible line in the code is now not in the code.

Your main problem is:

  • using software serial on hardware pins 0,1.
  • If you really want to send different data to monitor and bluetooth, move software serial and bluetooth to pins 3,4. You will then get what you expect.

HeHe @Nick_Pyner. I like this: The only sensible line in the code is now not in the code. I believe it! My level of comprehension is made clear by junky code!

The hardwareSerial-idea using pins 0,1 - even though, I don't understand what I'm doing - are not working for me! The idea of continually decoupling the Bluetooth seems backward.

Okay. So, I need to:

  1. Go full Serial and use pins 3,4.
  2. Check I'm using different pins for output to Serial and Bluetooth.

Thanks
EGB

EGB:
The idea of continually decoupling the Bluetooth seems backward.

It’s unfortunate, but the better option, so get used to it.

Okay. So, I need to:

  1. Go full Serial and use pins 3,4.

Loose language. Software serial on pins 3,4. I submit Software serial is not full serial, it is a bastardised compromise which promotes bad habits, and is best avoided.

  1. Check I’m using different pins for output to Serial and Bluetooth.

I’m not sure. You have not confirmed you are sending different data to bluetooth and monitor and I don’t even know if you really need to send anything to the serial monitor at all. If this serial monitor thing turns out to be a complete red herring, remember where you heard it first.

I’m still not sure what you want to do but there is a small chance 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

Nick_Pyner:

  1. Software serial is not full serial, … it is a bastardised compromise which promotes bad habits, and is best avoided.
  2. I don’t even know if you really need to send anything to the serial monitor at all … red herring …
  3. [Read: ]
    http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
    http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

Hey there @Nick_Peyner!
About 1) I hear but I’m still struggling to get my head around the diffs between Serial(3,4, say) and Hardware(0,1).
About 2) The serial monitor is only for debugging!
About 3) Immediately after posting my last reply, I pulled up BT_2_WAY.ino. Still deeply confused by it all. My version of BT_2_WAY is posted below. I see NO 2-way BT stuff. NONE. There is 2-way with the serial-montor - that part is clear! - but NO 2 WAY with BT. No BT exchange at all! I can’t see how that would even occur!

Key changes I made to the above submitted program is are really two things:

  • SoftwareSerial BTSerial(3, 4); // Changed from (0, 1) to (3, 4)
  • And cut the volume of transmissions via BT to my app, 'cos it would get overloaded.

My objectives are to simulate a heart monitor (which I do not yet have). Once the basic simulator works, I then need to get two BT devices to connect remotely and transfer data! The data will tx from the heart monitor simulator to the receiver module.

Apologies for displaying my ignorance on many things. But that’s why I am posting. :-p . You are focusing my attention. And, doing a super job! (It’s me who’s slow! Pls be patient with me!!!)

Regards
EGB

============
My version of BT_2_WAY.ino; changed so it made more sense to me!

/* This is a simple test for two way traffic via bluetooth 
   but you can try it first using the USB cable to the serial 
   monitor without the bluetooth module connected.
*/ 
String readString;
char c;
void setup()
{
    // pinMode(0, INPUT_PULLUP);// only needed for  JY-MCUY v1.06?
    Serial.begin(9600);
    Serial.println("OK then, you first, say something.....");
    Serial.println("Go on, type something in the space above");
    Serial.println(" and hit Send or Enter,");
    Serial.println("then I will repeat it!");
    Serial.println("");
}

void loop() {
  if (Serial.available() > 0) { // Serial.available() gives #chars in input buffer
    while (Serial.available()) {
      delay(3);  
      c = Serial.read();
      readString += c; 
    }// end while
    // Serial.write(c);  
  }
  // Access a character in a String, Syntax: string.charAt(n)
  if (readString.length() >0) {  
    int i;
    int j = readString.length();
    for (i = 0; i < j; i++) {
      Serial.write(readString.charAt(i)); // prints the whole string, char-by-char
    }
    Serial.write("\n");  // write() writes 1char, end of line
    // Serial.write("\r");  // /r is optional
    // Serial.println("");
  }
  readString="";  
}

EGB:
About 1) I hear but I’m still struggling to get my head around the diffs between Serial(3,4, say) and Hardware(0,1).

1. Inside the ATmega328P Microcontroller of the UNO Board, there is an electronics-based ‘Serial Data Communication Port’ known as UART Port. Please, take a little bit time and patience to scan the following diagram which has described the connection of the UART Port with the Computer.

Figure-1:

2. In Fig-1, we observe that the UART Port is connected with Computer. The TX and RX line of the port is also available at the DPin (digital pin) connector as 1 (TX) and 0(RX).

3. Because you are not aware about the internal connection of the UART Port, you have thought that the TX/RX lines of this port are free and accordingly you have connected your Bluetooth Module with DPin-1 and DPin-0.

4. Now you see that there are two devices connected with the same UART Port; this is no good. Therefore, you have been suggested to move your BT Module to another UART Port?

5. But we have no more UART Port inside the MCU; so, how can we have another UART Port?

6. The clever Software Engineers have created for us UART Ports using ‘Software Instructions’; these ports are known as Software UART Ports (SUART). In Fig-1, we have shown a SUART Port with SRX pin at DPin-3 and STX Pin at DPin-4.

7. When we include the following codes in our sketch, a SUART is automatically created:

#include<SoftwareSerial.h>
SoftwareSerial BTserial(3, 4);    //software name of SUART is BTserial with SRX at DPin-3 and RTX pin at DPIN-4
BTserial.begin(9600);

8. Now you can connect your BT Module with the newly created SUART Port and operate it safely. There is absolutely no conflict with any other devices.

9. All the functions/methods of the UART Port are equally applicable for the SUART Port except the following which is exclusively reserved for hardware UART Port:

void serialEvent{}

10. The following simple codes could be executed to check the functional link among your Android Smartphone, BT Module and Serial Monitor.

Sending A from Smartphone will turn ON the L (built-in LED of UNO); sending B will turn OFF L.

#include <SoftwareSerial.h>
SoftwareSerial BTserial(3, 4); // RX | TX
#define ledPin 13

void setup()
{
  Serial.begin(9600);
  BTserial.begin(9600);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop()
{
  if (BTserial.available() > 0)
  {
    char c = BTserial.read();
    if (c == 'A')
    {
      digitalWrite(ledPin, HIGH);
    }
    if (c == 'B')
    {
      digitalWrite(ledPin, LOW);
    }
  }
}

Edit: BT Module schematic is added in Fig-1.

@GolamMostafa!

The above list of explanations and the Figure, really helps.

Please, take a little bit time and patience to scan the ... diagram ...

I now see why the USB is inhibited when uploading, if DPin-1 and DPin-0 are connected to the BT.

I see also why both serial(monitor) and BTSerial were getting confused, as both outputs/inputs were connected to the same DPins, 0 and 1.

I also see how the Serial STX and SRX connects to DPin-4 and DPin-3.

Much appreciated.

Now further Questions (there is still plenty of confusion!):

  1. Why connect a BTSerial, which potentially transmit high volumes of data, to software DPins 3/4, when they could be transmitted over DPins 0/1?
  2. The GUIDE_2BT.pdf by @Nick_Pyner, shows - and argues in favour of, using DPins 0/1 for BTSerial. But then how do you obtain Serial Output to a monitor, for example, for debugging? Or just as a progress guide?

Look forward to your repliies

Regards
EGB

EGB:

  1. Why connect a BTSerial, which potentially transmit high volumes of data, to software DPins 3/4, when they could be transmitted over DPins 0/1?

Very good question. The answer is: don’t.
Having said that, a data stream 9600 is usually good enough for most mortals, hence, I suppose the default. Some serial peripherals come at higher speeds and are problematical or impossible to use on Software serial. The place where speed really shows is downloading recorded data, which is likely to be important to you. If you want to use software serial, don’t bother reading my guide, it is only about hardware serial.

then how do you obtain Serial Output to a monitor, for example, for debugging? Or just as a progress guide?

As stated in the guide, you can have monitor connected along with bluetooth, but only to see what is going on. You cannot send from monitor. If you want to debug, you can do that with bluetooth disconnected. Then you can send from monitor. See section 8 point 2! The philosophy is: get the code right, and then plug in Bluetooth - once. Further, as clearly stated on para 1, P1, if the code is already right, just plug in bluetooth and you are off to the races. I heartily recommend you read that overview again, paying particular attention to the bold type, twice. It really can’t get much simpler and, in that circumstance, I won’t bother asking why anybody would ever consider using software serial.

EGB:
Hey there @Nick_Peyner!
About 1) I hear but I’m still struggling to get my head around the diffs between Serial(3,4, say) and Hardware(0,1).

Software serial is a bastard compromise using an additional library. Hardware serial uses no extra library. Further details are in the Appendix section 5.

About 2) The serial monitor is only for debugging!

That’s fine. That is it’s job. No need for it after, debugging is done.
I now understand that you are not sending different data to monitor and Bluetooth, it just looked like you were.

About 3) Immediately after posting my last reply, I pulled up BT_2_WAY.ino. Still deeply confused by it all.

Perhaps I am not clear enough. BT_2WAY.ino is for two-way with a phone via Bluetooth, which I understand is your intention. If you want two way with the serial monitor, use the USB cable. I might also point out that BT-2WAY.ino also works with a proper terminal on a PC. See item 6 of the appendix.

My objectives are to simulate a heart monitor (which I do not yet have). Once the basic simulator works, I then need to get two BT devices to connect remotely and transfer data! The data will tx from the heart monitor simulator to the receiver module.

You are on the right tram. We all learn. Once you get one bluetooth going, check here for two. You may also need http://forum.arduino.cc/index.php?topic=396450.0.