Go Down

Topic: communicate two Arduino via bluetooth (Read 346 times) previous topic - next topic

Mamouti

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);
}


AWOL

#1
Oct 15, 2018, 12:38 pm Last Edit: Oct 15, 2018, 12:40 pm by AWOL
Code: [Select]
 data += inData;
That's going to fill RAM pretty quickly.
Code: [Select]
   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
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Mamouti

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.

Mamouti

#3
Oct 15, 2018, 01:25 pm Last Edit: Oct 15, 2018, 02:02 pm by Mamouti
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.

GolamMostafa

#4
Oct 15, 2018, 05:28 pm Last Edit: Oct 16, 2018, 05:37 am by GolamMostafa
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).

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.
Code: [Select]
#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..

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.

Mamouti

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.

GolamMostafa

#6
Oct 16, 2018, 12:04 pm Last Edit: Oct 16, 2018, 12:07 pm by GolamMostafa
You may revise the codes of your original sketch in the light of the following codes.
Sender Codes
Code: [Select]
//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
Code: [Select]
//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());//
  }

}





Mamouti

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

GolamMostafa

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.

Mamouti

"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.

GolamMostafa

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

Mamouti

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.

GolamMostafa

#12
Oct 16, 2018, 02:41 pm Last Edit: Oct 16, 2018, 02:42 pm by GolamMostafa
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.  

Go Up