Sending Integer values via HC-06 UART

Hi everyone.
Been a while since I asked for advice here but I need a little poke in the right direction.

My Setup

Sender: ESP32 with attached HC-SR04 and HC-06 at Hardware Serial1

Receiber: ESP32 with i2c display and HC-06.

HC’s are paired and work fine at 38400baud.
The HC-SR04 works fine as well, using a sketch of the net.

Sending Hex values like

Serial0.write(10,HEX);

works like a champ. I can receive the value and display it in HEX and DEC on the display.

Now the Distance calculated is an Integer, so I cant use the “Write” function.
If I just use
Serial0.print(distance) the receiver throws me useless numbers.

I searched the function highByte() and lowByte().

int distance;
byte outgoingByte[20];

outgoingByte[0]=highByte(distance);
outgoingByte[1]=lowByte(distance);

Serial0.write(outgoingByte,HEX);

this kind of works. I do receive reasonable Data on the other side but the values switch back and forth, jumping to 0 in beween.

I checked how many bytes are arriving with this:

while (Serial0.available()) 
     {

     incomingByte[Serialindex] = Serial0.read();
     Serial.println(incomingByte[Serialindex],HEX);
      
      
      Serial.print("Current SerialIndex: ");
      Serial.println(Serialindex);
      Serialindex++; 
      NewSerialData=true; 

     }

and Serialindex goes up to 16 every time.
If I send a Serial.write(9,HEX) is only gave me a Serialindex of 1.

Long story short:
Anyone know how I could send Data from one Arduino to another? I am trying to make a 16Bit sender for various Sensors - never would have thought that be so hard.

Questions:
1 Why does Serial.print give me strange values? Is it because this sends ASCII coded values?
2 Any way I could easily use Serial.write so send unsigned 16Bit Integer values?

If anyone is interessted - here is the complete code of both sides:

SENDER:

// defines pins numbers
const int trigPin = 2;
const int echoPin = 5;

// defines variables
long duration;
int distance;
HardwareSerial Serial0(2);
byte outgoingByte[1];  



void setup() {
 
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(38400); // Starts the serial communication
Serial0.begin(38400, SERIAL_8N2);
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

outgoingByte[0]=lowByte(distance);
//outgoingByte[1]=lowByte(distance);
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance,HEX);
Serial.print("High Byte: ");
Serial.println(outgoingByte[0],HEX);
//Serial.print("Low Byte: ");
//Serial.println(outgoingByte[1],HEX);

Serial0.write(outgoingByte,HEX);
delay(500);
}

RECEIVER:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>


//Variables for serial com
byte incomingByte[20];   // for incoming serial data
int Serialindex=0;            // index for incoming serial data
bool NewSerialData = false;

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display
HardwareSerial Serial0(2);



void setup()
{
  Serial.begin(38400);
  Serial0.begin(38400); 


  
  lcd.begin();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(1,0);
  lcd.print("BT Receiver");
  lcd.setCursor(1,1);
  lcd.print("SETUP COMPLETE");
  delay(1000);
  lcd.clear();
}


void loop()
{

while (Serial0.available()) 
     {
     Serial.println("NEW DATA");
     incomingByte[Serialindex] = Serial0.read();

     Serial.println(incomingByte[Serialindex],HEX);
      
      NewSerialData=true; 
     Serial.print("Byte 0: ");
      Serial.println(incomingByte[0],HEX);
      Serial.print("Current SerialIndex: ");
      Serial.println(Serialindex);
      Serialindex++; 

     }

if (NewSerialData)
{

lcd.clear();
lcd.setCursor(0,0);
lcd.print("High Byte: ");
lcd.print(incomingByte[0],HEX);
//lcd.setCursor(0,1);
//lcd.print("Low Byte: ");
//lcd.print(incomingByte[1],HEX);
NewSerialData=false;
Serial.println("Written To Display");


}
Serialindex=0;
}

Thanks all for your time!

Hello,

  1. Yes, print will send the number as a string of ASCII characters

2)Serial0.write(outgoingByte,HEX);That is wrong, if the first parameter is a pointer to an array then the second parameter is obviously the amount of bytes to send from this array (in your case, 2)

And the ESP32 already have integrated BT and BLE, why need another module ?

@guix Thanks for your reply, it did help me!
To adress your question: I need to quick - couple the BT modules to have different MCUs work together. To me, this was the easiest solution. I will though look into using the on board BT.

I may be out of luck but here is my issue:

Connecting the UARTs of the two ESPs via cable makes everything work fine. Data is transmitted and received nicely.

Sadly, it does look different when I use the paired HC05s.

The HC’s are paired at 115200Baud with 0 stop bits and 0 parity. I really need to look into what the last two are about.

Now, when i debug the receiving ESP I get this:

1 
______________________
2345678 
______________________
1 
______________________
2345678 
______________________
1 
______________________
2345678 
______________________
12 
______________________
345678 
______________________
1 
______________________
2345678 
______________________
1 
______________________
2345678 
______________________
1 
______________________
2345678 
______________________
1 
______________________
2345678 
______________________
1 
______________________
2345678 
________

instead of

______________________
12345678 
______________________
12345678 
______________________
12345678

as when I have them connected via cable.

The delay between each send is 100ms at the moment and I added a delay(1) to the receiving loop as indicated in a thread here.

Any tips on how to ensure a message is received correctly?
Is the configuration of the HCs wring (StopBit/Parity)?

Thanks for any infos…

Just again, here are the codes:

SENDER:

#include <Wire.h>
#include <Adafruit_ADS1015.h>


 Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads;     /* Use thi for the 12-bit version */


HardwareSerial Serial0(2);


void setup(void) 
{
  Serial.begin(115200);
  Serial0.begin(115200); 
 

   ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV

  ads.begin();
}

void loop(void) 
{
  int16_t adc0;

  adc0 = ads.readADC_SingleEnded(0);

  Serial.println("AIN0: "); Serial.print(adc0); Serial.print("Scaled: "); Serial.println(adc0*0.000125);
//  byte tx[]={highByte(adc0),lowByte(adc0),0x03,0x05,0xAA,0xFF,0xF1,0x01}; // this is the real sensor Data
  
  byte tx[]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
  Serial0.write(tx,8);
  Serial0.flush();
  
  delay(100);
}

RECEIVER:

//Variables for serial com
byte incomingByte[9];   // for incoming serial data
int Serialindex=0;            // index for incoming serial data
bool NewSerialData = false;
int Raw_1;
int tx;


HardwareSerial Serial0(2);

void setup()
{
  Serial.begin(115200);
  Serial0.begin(115200); 
}

void loop()
{

while (Serial0.available()) 
     {
 
      incomingByte[Serialindex] = Serial0.read();
      Serial.print(incomingByte[Serialindex],HEX);  
      NewSerialData=true; 
      Serialindex++; 
      delay(1); // THIS HELPS A BIT
     }
if (NewSerialData )
{
Serial.println(" ");
Serial.println("______________________");
Serial0.flush();

Raw_1=(incomingByte[0]*256 +incomingByte[1] );
tx = map(Raw_1, 352, 26208,0, 255);
//    Serial.print("add 1,0,");
//    Serial.print(tx);
//    Serial.write(0xff);
//    Serial.write(0xff);
//    Serial.write(0xff);
NewSerialData=false;
}
Serialindex=0;
}

The processor is fast and serial is slow. Do not set new data = true until all the 8 bytes are received.

while (Serial0.available())
     {
 
      incomingByte[Serialindex] = Serial0.read();
      Serial.print(incomingByte[Serialindex],HEX); 
      //NewSerialData=true;
      Serialindex++;
      if(Serialindex ==9) NewSerialData = true;
      delay(1); // THIS HELPS A BIT
     }