1
votes

I am new to Arduino and I have been working on a problem that has been troubling me for a few days.

I have an Arduino Uno and an HC-05 Bluetooth module.

Basically I want to send String and Int data together through Bluetooth.

CODE

#include <SoftwareSerial.h>        
SoftwareSerial BTSerial(10, 11); // RX | TX

void setup(void) {
  // Arduino setup
  Serial.begin(9600);
  // setting the baud rate of bluetooth
  BTSerial.begin(38400); // HC-05 default speed in AT command more
}

void loop(void) {
  int num = 123;
  BTSerial.write("#"); // Works
  BTSerial.write(num); // works
  BTSerial.write(String(num) + "#");
  // Error: no matching function for call to 'SoftwareSerial::write(StringSumHelper&)'
}

Also the result string should have '#' character at last.

According to the Arduino Website, it has 2 functions.

 - Serial.write(val) 
 - Serial.write(str) 

Any help appreciated.

Thank you.

2

2 Answers

0
votes

write is for sending raw bytes. You want to use Serial.print instead.

0
votes

if you want to send String from an another device to arduino, your code sould be like this:

#include <SoftwareSerial.h>
SoftwareSerial BT(3, 4); 
String bt = "";
void setup() {
  BT.begin(9600);
  Serial.begin(9600);

}
void loop() {
  if(BT.available()){
    bt = BT.readString();
  }
  Serial.println(bt);
  while(!BT.available());
}

the above code, waits for your bluetooth module to recieve any data and reads a String from it and prints it in Serial.

In order to send Int, you can read a String and parse it to Int.