communicate two Arduino via bluetooth

Hello,

My goal is to send collected data via FSR sensor between two board arduino via bluetooth.
I want to know if the created code can be correct or not. I couldn't test it because I haven't materials.
Can one help me and inform me if this code works? Results can be displayed in the serial monitor?
Really, I want your help this day (I have a deadline). Thank you in advance.

I created this code:

//sender_ code

int pin = 0; // the FSR and 10K pulldown are connected to a0
int data;
void setup() {
Serial.begin(9600);
}
void loop() {
data = analogRead(pin);
Serial.print("Pressure data= ");
Serial.println(data); // the raw analog reading
delay(1000);
}

//receiver_code

char inData;
String data;

void setup() {
Serial.begin(9600);
/// put your setup code here, to run once:

}
void loop() {
while(Serial.available()) {
inData = Serial.read();

Serial.print("Received data= ");
data += inData;

}
Serial.println (data);
delay(1000);
}

  data += inData;

That's going to fill RAM pretty quickly.
  delay(1000);And that's going to make reception very slow, and increase the chances you'll overflow the receive buffer.

Please remember to use code tags when posting code

Thank you for your reply.

I want to ask you if the proposed code can achieve the bluetooth communication and send data between sender and receiver. Results can be displayed in the monitor of sender and receiver? I didn't know if this code is correct and functinal or not.
Please could you inform me if this code works or not.

Thank you in advance.

I want to explain more.
If I have two arduino board Due and I want to establish the communication bluetooth(HC-05) between this two board and send data. Could I have ,for example, this result (attached) at the serial monitor of sender and the received data in the serial monitor of the receiver?

Thank you in advance.

1. I hope that your setup is as follows (Fig-1) containing a pressure sensor, two Arduinos (UNO and NANO), two BT Module (HC05), and two Serial Monitors (SM1 and SM2).
uhc5-2x.png
Figure-1: Connection of UNO and NANO using SUART Ports

2. You know that the Serial Monitors are connected to the UNOs via their respective Hardware UART (UART) Ports. Because the Arduino has only one UART Port, you must use a Software UART Port (SUART) to connect the BT Module.

3. A SUART Port can be created by including the following codes in the sketch.

#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3);   //SRX = 2, STX = 3
mySUART.begin(9600);

4. All available functions/methods of UART Port are also applicable for the SUART Port except that the void serialEvent() handler which is exclusively reserved for UART Port.

5. In UART/SUART Protocol, data exchange takes place one character at a time over a frame. The frame is composed of a Start Bit, 8-bit ASCII Code of the character, optional parity Bit, and the STOP Bit..
185-00.png
Figure-2: A UART frame

6. When a valid character arrives at the Receiver Section, it immediately (after validity check) enters into a 64-byte wide (with last byte always as null-byte) unseen FIFO type buffer.

7. The presence of a character in the FIFO buffer of Step-6 can be checked by executing the if(mySUART.available()>0) structure.

8. The topmost character of the FIFO buffer of Step-6 can be brought into a user defined variable by executing the instruction byte x = mySUART.read(); method.

Now based on the above information, try to revise your original sketch/write some meaningful codes, test them and post the results in this thread.

uhc5-2x.png

185-00.png

Hi,
Thank you for your reply.
My question is about the successful bluetooth communication between two arduino.
I just want to know if the proposed code can establish the bluetooth communication between two arduino or not without using the SoftwareSerial.h library.

Thank you in advance.

You may revise the codes of your original sketch in the light of the following codes.
Sender Codes

//sender_ code
#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3); //SRX, STX

int pin = 0;
int data;
char dataBuffer[20] = "";

void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
}

void loop()
{
  data = analogRead(pin);//A0 
  Serial.print("Pressure data = ");//
  Serial.println(data, HEX);
  itoa(data, dataBuffer, 16);
  mySUART.print(dataBuffer);
  mySUART.println();
  delay(1000);
}

Receiver Codes

//sender_ code
#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3); //SRX, STX

int data;
char dataBuffer[20] = "";
int i = 0;


void setup()
{
  Serial.begin(9600);
  mySUART.begin(9600);
}

void loop()
{
  if (mySUART.available() > 0)
  {
    Serial.write((char)mySUART.read());//
  }

}

sm-10S.png

sm-11R.png

sm-10S.png

sm-11R.png

Thank you for you reply.
This code is it available for arduino Due and bluetooth H-05?

Mamouti:
Thank you for you reply.
This code is it available for arduino Due and bluetooth H-05?

I have tested these codes for UNO and NANO. They should work well with DUE. If you are using DUE, you don't need to use SUART Ports as a DUE has as many as 4 UART Ports.

"don't need to use SUART Ports as a DUE has as many as 4 UART Ports"
That means It is not necessary to use the SoftwareSerial.h library and SoftwareSerial declarations in the code?

I must delete the indicated part from code? The code still working?
Please help me, I'm beginner on arduino.

Thank you very much.

If you have DUE+DUE, try the codes of my post using direct connections between UART1 and UART1 and then try using BT HC5.

Yes, i have the communication between two Due.
So, if I used my proposed code at the begining , it will be a succesful communication and data transmission?
I haven't materials to do the experience but I need a functional and correct code.
Could you help me to have the correct structure of the sender Due and the receiver Due?

Thank you.

Set your goal first.

You have an analog type pressure sensor at the Sender DUE-1. Do you want to send the calibrated or raw pressure signal to Receiver (DUE-2) using BT HC5? DUE2 will receive the data, and it will display the pressure signal on the Serial Monitor of DUE2.

Build the hardware slowly, add the software components slowly and then come up with a working project. In this Forum it is very difficult to talk/write with out experimental evidence. You need to collect your all hardware components.