So I am receiving data over bluetooth which is in the form of ascii characters. So 255 comes in as 50,53,53
So what I need to do is convert the intergers into strings then concatenate the strings together then convert the new string back into a int.
So myRecievednumbers is an array and in the code below I want to convert the first 3 numbers into a string then print to the serial monitor. But I get the error no instance of overloaded function "HardwareSerial::print" matches the argument list
Not sure what I need to do to print to the monitor. Any help appreciated
Well, first of all you should show us also how myRecievednumbers[] array is defined. Then you're talking about "integers" but I suspect they're a "byte" or "char" array.
If you're receiving the data via BT depends on how you get them from the BT serial port. They could be "byte" but, as it seems you're receiving a string representing an integer (byte?) value, they can be "char". It's always better posting the whole code and not just some snippets.
BTW, if we're talking about receiving a string you should store the data in a "char myRecievednumbers[4];", I assume your data is a byte value so it comes in up to three decimal digits (ending with a CR+LF pair, or '\r', '\n'), then adding a '\0' string terminating character (so that's why I used [4] as the buffer dimension).
In this case, you can convert that string by calling the "atoi()" function (converting a "char array" into "int"):
int value = atoi(myRecievednumbers);
If the data you need to receive is a byte (value between 0 and 255) consider sending it as a single byte and not as a string, but this can be discussed later as soon as you'll better explain your environment and application.
Ok so I have included the full code. I am sending the data over bluetooth from a app made with flutter using the bluetooth classic package. A Flutter plugin to connect to Bluetooth Classic devices, mainly designed to work with serial communication. I've not had much luck getting other bluetooth packages to send any data so I'll stick with it. Looking at the docs the only option is to send a messgae as a string. See here > API
// 11:31 11/04/2024
#include <Arduino.h>
#include <BluetoothSerial.h>
#include <Preferences.h>
#include <fastled_config.h>
#include <iostream>
using namespace std;
int myRecievednumbers[300];
int counter = 0;
bool receiving_bluetooth = false;
// init Class:
BluetoothSerial ESP_BT;
int incoming;
void printNumbers(){
Serial.println("numbers incoming before conversion !!");
Serial.println();
for (int i = 0 ; i < 100; i++){
Serial.print(myRecievednumbers[i]);
}
int c = 0;
Serial.println("numbers AFTER conversion !!");
// take 3 numbers - convert to string so 2, 5 ,5 will be 255
for (int k = 0 ; k < 100 ; k++){
if ((k % 3 == 0) && (k > 0)){
c++;
std:: string d0 = to_string (c);
std:: string d1 = to_string (myRecievednumbers[k-3]-48);
std:: string d2 = to_string (myRecievednumbers[k-2]-48);
std:: string d3 = to_string (myRecievednumbers[k-1]-48);
string digit = "> " + d0 + " : " + d1 + d2 + d3 ;
String cSTR = String(digit.c_str());
Serial.print( cSTR);
}
}
}
void setup() {
Serial.begin(115200);
delay(250);
ESP_BT.begin("ESP32_Test1"); //Name of your Bluetooth interface -> will show up on your phone
}
void loop() {
delay(20); // WITHOUT THIS LINE MORE LIKELY TO GET INCORRECT DATA FROM BLUETOOTH
// -------------------- Receive Bluetooth signal ----------------------
if (ESP_BT.available()) {
receiving_bluetooth = true;
incoming = ESP_BT.read(); //Read what we receive
myRecievednumbers[counter] = incoming;
counter++;
}
else {
if (receiving_bluetooth) {
printNumbers();
Serial.println(counter);
receiving_bluetooth = false;
counter = 0;
}
}
}
regardless, you capture the read into an integer array, myReceivedNumbers [] and the values are ASCII.
the common question is what terminates the string to know you've read a complete string? you may be able to use readBytesUntil(). I believe it's compatible with BluetoothSerial
(and doesn't ESP_BT.begin () take a bit-rate as an argument)
# include <BluetoothSerial.h>
void loop ()
{
if (ESP_BT.available ()) {
char buf [90];
int n = ESP_BT.readBytesUntil ('\n', buf, sizeof(buf)-1);
buf [n] = '\0'; // terminate string with nul
Serial.println (buf);
}
}
void setup ()
{
Serial.begin (115200);
delay (250);
ESP_BT.begin (115200);
}
I have never had to do with Flutter (for Android, sometimes I used App Inventor) but first of all you need to make a better code indentation because now it's bad and it could cause you to miss some important structure information. Open your IDE, then press Ctrl-T and it automatically does the job for you (but then you'd keep it clean). Then, I suggest you to remove all that unnecessary empty lines to make the code more "compact" and readable. To separate code blocks (e.g. functions) use either one single empty line or a comment line.
Next thing is the board: what is exactly the board are you using? I suppose an ESP32, but if you specify the exact model could help us a bit more...
Then, for a simple Bluetooth string receiver I see too many things you added here...
I don't know what Flutter write() function (and don't have an ESP32 at hand to test with) actually sends: if it's a string (meaning a variable length characters sequence) it should send a terminating character, maybe a "null" (byte 0 or '\0') or a "end line" character or sequence (like '\n' or both '\r' '\n').
I suggest you to investigate the data sent via Flutter, just write a simple sketch to receive single bytes and print them out on Serial as hex values and post here the output.