Gelu Popa
Gelu Popa

Reputation: 11

Sending data from one arduino to another via bluetooth hc05 modules

I have to do a project for Uni using 2 arduino uno, 2 HC 05 bluetooth module and 2 sensors for each arduino. I have set a module to be the slave, and the master connects to the slave only.

When I am trying to send the data from slave to be read by master, it keeps reading 0.

I am using Arduino IDE, I have no errors and everything runs.

This is the code I have so far.

Also, I want to send data from 2 sensors from the slave to the master, but I do not know how to do that.

Slave code

#include <SoftwareSerial.h>
#define TILT 7 // tilt sensor pin
#define LDR 8 //light intensity sensor pin
#define rxPin 10
#define txPin 11
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
int sentBytes;                                         ////// SLAVE CODE
byte data[2];
void setup() { 
  Serial.begin(9600);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(TILT, INPUT);
  pinMode(LDR, INPUT);
  Serial.begin(9600);
  nodeCommunication.begin(9600);
  delay(2000);
}
void loop() {

  //int LDRval = analogRead(LDR); // light intensity sensor (candela)
  //int Tilt_Sensed = digitalRead(TILT);
  data[0] = analogRead(LDR);
  data[1] = digitalRead(TILT);

  if(nodeCommunication.available()){
    sentBytes = nodeCommunication.write(data[0]);
    Serial.println("I reached this if");
  }

  Serial.println(analogRead(LDR));
  Serial.println(sentBytes);


  delay(2000);
}

Master Code

#include "dht.h"
#include <SoftwareSerial.h>
#define dht_apin A0 // Analog Pin sensor is connected to A0

dht DHT; // DHT.humidity // DHT.
#define rxPin 10
#define txPin 11

SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);
byte data = 1;
                                                          ///// MASTER CODE
void setup(){

  Serial.begin(9600);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  nodeCommunication.begin(9600);
  delay(2000);//Wait before accessing Sensor

}//end "setup()"

void loop(){
 //Start of Program 
    DHT.read11(dht_apin); // reading the data from the sensor

    if(nodeCommunication.available()){
      data = nodeCommunication.read();
      Serial.println("I reached this if"); 
    }
    Serial.print(data); Serial.println(" candela;");
    Serial.print(DHT.temperature); Serial.println(" C");
    Serial.print(DHT.humidity); Serial.println(" % humidity");

    delay(2000);//Wait 2 seconds before accessing sensor again.

//Fastest should be once every two seconds.

}// end loop() 

Please tell me what I am doing wrong. Thank you so much!

Upvotes: 0

Views: 1840

Answers (1)

Blobtech
Blobtech

Reputation: 11

byte data = 1; - should be just byte data;

Have you checked the TX/RX connections? The TX from the master has to connect to the RX of the slave and visa versa. Als GND has to be connected to eachother.

After that, let the master (the transmitter) just Serial.write (in your case nodeCommunication.write() ) to the slave (the receiver).

So something like this:

Master:

#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
byte data = 0x01; //Hex value for ''1''
SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);

void setup()
{
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
nodeCommunication.begin(9600); 
}

void loop()
{
nodeCommunication.write(data); //Sends byte data to slave
delay(1000);
}

Slave:

    #include <SoftwareSerial.h>
    #define rxPin 10
    #define txPin 11
    byte incoming
    SoftwareSerial nodeCommunication = SoftwareSerial(rxPin, txPin);

    void setup()
    {
    Serial.begin(9600);
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    nodeCommunication.begin(9600); 
    }

    void loop()
   {
    if(nodeCommunication.available > 0) //Only if data is available
    {
    byte incoming = nodeCommunication.read(); //read byte from master
    Serial.println("Incoming = ");
    Serial.println(incoming);
    }
    else
    {
    Serial.println("No data available....");
    }
   }

After your Serial communication is established, implementing the DHT value will be the next step.

Upvotes: 1

Related Questions