HC-05 - send multiple values

hello,

I have a question about of how to send multiple data of integer with the hc-05 modul.

I'm using 2 x HC-05 Modules (1x slave and 1x master). They are connected and set up with the baudrate of 115200. The communication between both modules are working well.
Also I have a gyro (MPU-6050) and I need to send 3 values of integers which are ranged between 0-180.
So my question is how I could send those 3 data simultaneously from my HC-05 (Master) to my HC-05 (Slave) because I need to control 3x servomotors.

That means:

ServoA = gyro x-axis (--> values between 0-180)
ServoB = gyro y-axis (--> values between 0-180)
ServoC = gyro z-axis (--> values between 0-180)

I'm using the serial1.write() function because my HC-05 master module is connected with the arduino micro.
The HC-05 slave module is connected to the arduino mega 2560.

So I just need to know how I could send those 3 data as an array with the serial1.write() function.

If I send only a single integer as in the code below it is working. But I don't know how I could send all 3 integer simulatenously.

Here is a snippet of the slave and master code:

Master:
The whole code is just withdraw from the author jrowberg for the MPU-6050 Module. That's why I just post a snippet of the values I am sending from Master to Slave.

int servo1 = map(ypr[0] * 180/M_PI, -180, 180, 0, 180);
int servo2 = map(ypr[1] * 180/M_PI, -90, 90, 0, 180);
int servo3 = map(ypr[2] * 180/M_PI, -90, 90, 0, 180);
Serial1.write(servo1);

Slave:

#include <SoftwareSerial.h>
#include <Servo.h>
int readdata;
Servo myservo1;
Servo myservo2;
Servo myservo3;

void setup() 
{
  myservo1.attach(3);
  myservo2.attach(5);
  myservo3.attach(9);
  Serial.begin(115200);  // HC-05 default speed in AT command more
}

void loop() {
      if (Serial.available()) 
      {

          int readdata = Serial.read();
          myservo1.write(readdata);

      }
}

If anyone have an idea of how I could send my data simulatenously I would be very thankful if you could make an example code.

Serial.Write( ) is capable of sending a string.
Create a string separated by commas e.g. "value1,value2,value3"
You will need to convert integers to strings.

this is not right because I get the error message :

no matching function for call to 'HardwareSerial::write(String&)'

int servo1 = map(ypr[0] * 180/M_PI, -180, 180, 0, 180);
int servo2 = map(ypr[1] * 180/M_PI, -90, 90, 0, 180);
int servo3 = map(ypr[2] * 180/M_PI, -90, 90, 0, 180);
String test = String(servo1)+","+String(servo2)+","+String(servo3);
Serial1.write(test);

this is how it looks like and I get that message:

no matching function for call to 'HardwareSerial::write(String)'

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

I suggest you use the technique in the 3rd example and get the sender to match that.

...R

I tried for several hours to write a code to solve my problem but I can't get any solution.
I would be glad about an example code to my problem.
Thank you for any further replies.

Preload:
I tried for several hours to write a code to solve my problem but I can't get any solution.

Post your best attempt and tell us what it actually does and what you want it to do that is different.

If you want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

...R

what a silly time wasted answer ;D

it's okay I've got my problem solved.

That's the reason why google finds that much bullshit. They need to improve their algorithm to filter such posts like above and let them disappear when you've opened the page.

closed

Preload:
what a silly time wasted answer ;D

Not as silly as your Reply #8

Alas, we have not yet found a way to filter questions from silly people who get in a huff because they don't like answers that suggest that they might need to do some work.

...R

They need to improve their algorithm to filter such posts like above

Agreed. Kids these days need to be handed everything on a plate, nicely arranged.

Preload:
what a silly time wasted answer ;D

it's okay I've got my problem solved.

That's the reason why google finds that much bullshit. They need to improve their algorithm to filter such posts like above and let them disappear when you've opened the page.

closed

Hello. So how did you got your problem solved? Did you used a coding scheme? Or used a command to split up the data in the serial monitor to be used as a value in your master's code?

samtheham05:
Hello. So how did you got your problem solved?

Have you studied the links in Reply #5 ?

...R

Robin2:
Have you studied the links in Reply #5 ?

...R

Hi, nevermind that. I got it working using a library called ArduinoJson. I was able to combine many variables into a string and deserialize them again into individual variables each in their own data types using that library.

My problem now is that data i want to send from the slave bluetooth to the master one only works when i type out the String and press the send button in the serial moonitor of the slave to the master. The data is displayed correctly over on the master's serial monitor. But i want it that the slave would just automate the writing of the String and send it to the master, when i try to code for that it only displays signs and symbols on the master's serial monitor which is pretty much useless.. here is a snippet of my code:

finalString = String("{\"alcohol\":" + strAlcohol + ",\"lat\":" + strLat + ",\"lng\":" + strLng + "}");
   
  if(Serial.available()){
    mySerial.println(Serial.println(finalString));
  }

The supposed variables inside the "finalString" are all variables of their own and the "finalString" serves as the String that holds all those variables, to be sent to the master. I am coding this using the ArduinoJson library and that library functions in the way in how i write the String in declaring the "finalString" variable. Anybody have ideas why this code just sends useless signs and symbols? Any help is much appreciated :slight_smile:

samtheham05:
Hi, nevermind that.

Why post a question in an old Thread if the existing advice in that Thread has no relevance for your problem - just start your own Thread with a Title that summarises your problem.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

Robin2:
Why post a question in an old Thread if the existing advice in that Thread has no relevance for your problem - just start your own Thread with a Title that summarises your problem.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

Okay. I'll look into how to use cstrings now and will also make a new topic about my problem. Thanks :slight_smile: