MyEngineeringStuffs
Published © GPL3+

Master and Slave two way Bluetooth Data Communication

This project demonstrates the two-way communication between two Bluetooth HC-05 modules.

IntermediateFull instructions provided8 hours1,353
Master and Slave two way Bluetooth Data Communication

Things used in this project

Story

Read more

Schematics

Two way Bluetooth communication

HC-05 Bluetooth communication

Master Circuit Diagram of the Two way Bluetooth Communication Project

This is the second part of the Master circuit diagram

Code

Slave source code

Arduino
This is the full source code of the project Two way Bluetooth Communication
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int ledpin = A0;
int potpin = A1;
char data[40];
int i=0,pot=0;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  pinMode(ledpin,OUTPUT);
  lcd.begin();
  lcd.setCursor(0,0);
  lcd.print("**MES TUTORIAL**");
  lcd.setCursor(0,1);
  lcd.print("      SLAVE     ");
  delay(5000);
  lcd.clear();
}

void loop() { // run over and over
  while(Serial.available()>0){
      for(i=0;i<40;i++){       
        data[i] = Serial.read();
        //Serial.print(i);
        //Serial.println(data[i]);
        delay(5);
       if(data[i]=='S'){
         if(data[i+3]=='T'){ 
            if(data[i+4]=='1'){
             digitalWrite(ledpin,LOW);
             }
            else{
            digitalWrite(ledpin,HIGH);
            }
         }
         }
      }
       if(data[0]=='H' && data[3]=='I'){
        lcd.setCursor(0,0); 
        lcd.print("H:");
        for(int j=4;j<10;j++){
        lcd.print(data[j]);
        }
       }
       if(data[12]=='T' && data[15]=='P'){
        lcd.setCursor(0,1); 
        lcd.print("T:");
        for(int j=16;j<21;j++){
        lcd.print(data[j]);
        }
        lcd.setCursor(7,1); 
        lcd.print((char)223);
        lcd.print("C");
       }
       if(data[23]=='L' && data[26]=='H'){
        lcd.setCursor(10,0); 
        lcd.print("L :");
        for(int j=27;j<31;j++){
        lcd.print(data[j]);
        }
         }
 }
 pot = analogRead(potpin);
 int potVal = map(pot, 0, 1023, 0, 255);
 lcd.setCursor(10,1); 
 lcd.print("VR:");
 printDigits2(potVal);
 Serial.write(potVal);
 //Serial.println();
 delay(200); 
}
//this function adds a "0" to the beginning of the number,
//so that 5 minutes is displayed as "00:05:00", rather than "00:5 :00"
void printDigits2(int digits)  
{
  if(digits < 100) 
  {
    lcd.print("0");
    lcd.print(digits);
  }
  else
  {
    lcd.print(digits);
  }
}

Master Source code

Arduino
This is the full source code of the master side.
#include <dht.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
LiquidCrystal_I2C lcd(0x3F, 16, 2);
dht DHT;
#define DHT11_PIN A0
int swtch = A2;
int light = A1; 
int a=0;
int pot = 20;
int data;
void setup(){
  pinMode(swtch, INPUT);
  digitalWrite(swtch,HIGH);
  myservo.attach(9);
  lcd.begin();
  Serial.begin(9600);
  lcd.begin();
  lcd.setCursor(0,0);
  lcd.print("**MES TUTORIAL**");
  lcd.setCursor(0,1);
  lcd.print("     MASTER     ");
  delay(3000);
  lcd.clear();
}

void loop(){
  data = analogRead(light);
  DHT.read11(DHT11_PIN);
 if(Serial.available()>0){
        pot = Serial.read();
        delay(5);   
 }
 if(pot <= 180){
      myservo.write(pot); // sets the servo position according to the scaled value
      delay(15);   
 }
  lcd.setCursor(10,1); 
  lcd.print("VR:");  
  printDigits2(pot);

  
  lcd.setCursor(0,0); 
  lcd.print("H:");
  lcd.print(DHT.humidity);
  Serial.print("HUMI");
  Serial.print(DHT.humidity);
  Serial.println("%");
  lcd.setCursor(7,0); 
  lcd.print("%");

  
  lcd.setCursor(0,1);
  lcd.print("T:");
  lcd.print(DHT.temperature);
  Serial.print("TEMP");
  Serial.println(DHT.temperature);

  
  lcd.setCursor(7,1); 
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(10,0); 
  lcd.print("L :");
  printDigits3(data);
  Serial.print("LIGH");
  sendDigits2(data);
  
  if(digitalRead(swtch) == LOW){
    Serial.println("STAT0");
  }
  else{ 
    Serial.println("STAT1");
  }
  delay(200);
}
//this void function is to send serial data. if data is 984 it will send 0984
void sendDigits2(int digits)  
{
   if(digits < 100) 
  {
    Serial.print("00");
    Serial.println(digits);
  }else if(digits < 1000) 
  {
    Serial.print("0");
    Serial.println(digits);
  }
  else
  {
    Serial.println(digits);
  }
}
void printDigits2(int digits)
{
  if(digits < 100) 
  {
    lcd.print("0");
    lcd.print(digits);
  }else
  {
    lcd.print(digits);
  }
}
//this function adds a "0" to the beginning of the number,
//so that 5 minutes is displayed as "00:05:00", rather than "00:5 :00"
void printDigits3(int digits)  
{
  if(digits < 100) 
  {
    lcd.print("00");
    lcd.print(digits);
  } else if(digits < 1000) 
  {
    lcd.print("0");
    lcd.print(digits);
  }
  else
  {
    lcd.print(digits);
  }
}

Credits

MyEngineeringStuffs

MyEngineeringStuffs

13 projects β€’ 71 followers
I 😍 to help people, students are always FIRST, and YesπŸ‘‰https://paypal.me/pawanbehera1 you can buy me a COFFEE if U😍 my WORK, NAMASTE πŸ™

Comments