Question about write() function

Hello

I have little experience using the write() function, so is there any way to send the \r and \n terminals with it?

Thanks!

What are the ASCII values of '\r' and '\n' ?

Answer 13 and 10. So guess what your write() ?

  write ('\r');
  write ('\n');

So I need to send a string via bluetooth that has terminals 10 and 13, using the write() function

This way I get an error:

  if (SerialBT.hasClient()) {
    SerialBT.write("page1\n\r");
    delay(10000);
    SerialBT.write("page2\n\r");
    delay(10000);
    SerialBT.write("page3\n\r");
    delay(10000);
  }

Is there any way to do this using write()?

Use print()?

I know using print() or println() does it. My question is if you can do the same thing using write()...

What problem are you trying to solve?

Definitions of the write() function from Print.h:

    virtual size_t write(uint8_t) = 0;
    size_t write(const char *str) {
      if (str == NULL) return 0;
      return write((const uint8_t *)str, strlen(str));
    }
    virtual size_t write(const uint8_t *buffer, size_t size);
    size_t write(const char *buffer, size_t size) {
      return write((const uint8_t *)buffer, size);
    }

Try "\r\n"

What error?

If you want "\r and \n terminals" in that order, try:
SerialBT.write("page1\r\n");

exit status 1
invalid conversion from 'const char*' to 'uint8_t {aka unsigned char}' [-fpermissive]

have you ever tried what was written in #3 on your own?

void setup() {
  Serial.begin(115200);
  Serial.print("text");
  Serial.write('\r');
  Serial.write('\n');
  Serial.print("anothertext");
}

void loop() {
  // put your main code here, to run repeatedly:
}

No?
describe exactly what's not working.
post your sketch. something we can put in the IDE and press compile. Not just a junk of some lines.
post the complete error message.

Are you using SofttwareSerial for "SerialBT"? It should inherit from Print and thus have "write(const char *str)". It looks like whatever your SerialBT is based on doesn't have that function. You should be able to work around that with:
SerialBT.write("page1\n\r", strlen("page1\n\r"));

Don't worry about having the string constant twice. The compiler will only keep one copy.

Yea, there's a whole lot fishy here, starting with OP motive for doing this.

Posting a complete code would help greatly. I highly suspect Pilot Error.

Here is the complete code:

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);

  if (SerialBT.hasClient()) {
    SerialBT.write("page1\n\r", strlen("page1\n\r"));
    delay(10000);
    SerialBT.write("page2\n\r", strlen("page3\n\r"));
    delay(10000);
    SerialBT.write("page3\n\r", strlen("page3\n\r"));
    delay(10000);
  }
  else {
    Serial.println("NOT C");
  }
}

Also shows error:

exit status 1
invalid conversion from 'const char*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]

Great. Now tell us why you're so fixated on using write() instead of print().

That is far from complete.
Is it for a ESP32 board ? And you are using the Espressif Bluetooth library ?
If you turn on the extra compiler output, then the compile will tell a lot more.
For example:

/sketch/sketch.ino: In function 'void loop()':
/sketch/sketch.ino:25:20: error: invalid conversion from 'const char*' to 'const uint8_t*' {aka 'const unsigned char*'} [-fpermissive]
     SerialBT.write("page1\n\r", strlen("page1\n\r"));
                    ^~~~~~~~~~~

As @johnwasser wrote in Reply #13, the Bluetooth library is not fully compatible. The const char * is missing here.

Things you can do:

  1. Fix it yourself with SerialBT.write((const uint8_t *)"page1\r\n", strlen("page1\r\n"));
  2. Make an Issue at Github and request that a const char * will also work.
  3. Use normal code, just like everyone else. For example: SerialBT.println("page1"); The .println() function writes the text and add the CarriageReturn and LineFeed.

This is bold text to get your attention, I can make it red as well: It is "\r\n" not "\n\r".

I see the reason. BluetoothSerial.h doesn't pull in the required function definitions from Print.h by including:

using Print::write;

Compare BluetoothSerial.h to HardwareSerial.h (for an AVR board) and you'll see this.
Said functions are not declared 'virtual' in Print.h so they're not inherited from Print via Stream.

1 Like

I have added the links in this quote ▲

Well spotted.
The .print() and .println() work, so a normal text string is no problem.

1 Like