DS18B20 and HC-05 code problem

I make a home automation system with arduino and i am using ds18b20 temperature sensor. I see degree from serial monitor but i cant control other parts with HC-05 bluetooth modul. I think it’s some code problem but i couldn’t solve the problem. Please help me :frowning: These are my codes;

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

#include <Servo.h>
Servo myservo;
int pos = 0;

#include <OneWire.h>

int DS18S20_Pin = 2;

OneWire ds(DS18S20_Pin);

#define Lamp1 4
#define Lamp2 5
#define Lamp3 6
#define Lamp4 7

char val;
String statusLamp1,statusLamp2,statusLamp3,statusLamp4;

void setup() {
Serial.begin(9600);
pinMode(Lamp1,OUTPUT);
pinMode(Lamp2,OUTPUT);
pinMode(Lamp3,OUTPUT);
pinMode(Lamp4,OUTPUT);
myservo.attach(3);
mySerial.begin(9600);
}

void loop() {
float temperature = getTemp();
Serial.print("Sicaklik: ");
Serial.println(temperature);
delay(1000);
}
float getTemp(){

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1);

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) {
data = ds.read();

  • }*
  • ds.reset_search();*
  • byte MSB = data[1];*
  • byte LSB = data[0];*
  • float tempRead = ((MSB << 8) | LSB);*
  • float TemperatureSum = tempRead / 16;*
  • return TemperatureSum;*

if( mySerial.available() >0 ) {

  • val = mySerial.read();*

  • Serial.println(val);*

  • {*

  • while (Serial.available() > 0){*
    }

  • if( val == ‘a’ ) {*

  • digitalWrite(Lamp1,HIGH); statusLamp1=“4”; }*

  • else if( val == ‘b’ ) {*

  • digitalWrite(Lamp2,HIGH); statusLamp2=“5”; }*

  • else if( val == ‘c’ ) {*

  • digitalWrite(Lamp3,HIGH); statusLamp3=“6”; }*

  • else if( val == ‘d’ ) {*

  • digitalWrite(Lamp4,HIGH); statusLamp4=“7”; }*

  • else if( val == ‘e’ ) {*

  • digitalWrite(Lamp1,HIGH); statusLamp1=“4”;*

  • digitalWrite(Lamp2,HIGH); statusLamp2=“5”;*

  • digitalWrite(Lamp3,HIGH); statusLamp3=“6”;*

  • digitalWrite(Lamp4,HIGH); statusLamp4=“7”;*

  • }*

  • else if( val == ‘f’ ) {*

  • for(pos = 0; pos < 90; pos += 1) {*

  • myservo.write(pos);*

  • delay(5);*

  • }*

  • }*
    //Lamp is off

  • else if( val == ‘A’ ) {*

  • digitalWrite(Lamp1,LOW); statusLamp1=“4”; }*

  • else if( val == ‘B’ ) {*

  • digitalWrite(Lamp2,LOW); statusLamp2=“5”; }*

  • else if( val == ‘C’ ) {*

  • digitalWrite(Lamp3,LOW); statusLamp3=“6”; }*

  • else if( val == ‘D’ ) {*

  • digitalWrite(Lamp4,LOW); statusLamp4=“7”; }*

  • else if( val == ‘E’ ) {*

  • digitalWrite(Lamp1,LOW); statusLamp1=“4”;*

  • digitalWrite(Lamp2,LOW); statusLamp2=“5”;*

  • digitalWrite(Lamp3,LOW); statusLamp3=“6”;*

  • digitalWrite(Lamp4,LOW); statusLamp4=“7”;*

  • }*

  • else if( val == ‘F’ ) {*

  • for(pos = 90; pos>=0; pos-=1);*

  • myservo.write(pos);*

  • delay(5);*
    }else if( val == ‘G’ )

  • {*

  • delay(500); *

  • mySerial.println(statusLamp1+statusLamp2+statusLamp3+statusLamp4+“J”); //delay(500); *

  • val=’ ';*

  • }*

  • }*
    }}

It might be just a matter of improper wiring. Have you tried just using bluetooth by itself?

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

while (Serial.available() > 0){
}

If you ever send serial data to the Arduino, it will hang here forever. What on earth were you thinking?

Yes i tried just bluetooth, without any problem it work. Just they didnt work together. I will send info from bluetooth for lamps and servo motor. The fan will be active when the temperature rises above 25 degrees.

Look at the getTemp() function. Scroll down until the find the return; statement. NOTHING after that statement will be executed. Why would you even think of putting code to get data from the bluetooth port in a function called getTemp()?

Why are you storing status as a number in a String? Could you possibly be more wasteful of your limited memory? Why not an int?

Why is half your code in italics? Why did you skip the "How to Use This Forum" thread? Do you think you're too cool for the instructions?

I didnt fully understand what I needed to do. I want to see the degree from serial monitor and the control by bluetooth module to the other parts.

Ok. I would start by answering some of the questions that have been posed. And reading the How To thread so you can fix that mess of italics.

How can i using DS18B20 and HC-05 module together?

You're going backwards. We already know that's your question. Now go fix the code in the OP so it's not in italics and answer the questions you've been asked. Or don't, but don't expect much help if you refuse to help us to help you.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

#include <Servo.h>
Servo myservo; 
int pos = 0; 

#include <OneWire.h>

int DS18S20_Pin = 12; 

OneWire ds(DS18S20_Pin);

#define Lamp1 4
#define Lamp2 5
#define Lamp3 6
#define Lamp4 7

char val;
String statusLamp1,statusLamp2,statusLamp3,statusLamp4;

void setup() {
    Serial.begin(9600);
  pinMode(Lamp1,OUTPUT);
  pinMode(Lamp2,OUTPUT);
  pinMode(Lamp3,OUTPUT);
  pinMode(Lamp4,OUTPUT);
      myservo.attach(3); 
      mySerial.begin(9600);
}

void loop() {
      float temperature = getTemp();
  Serial.print("Degree: ");
  Serial.println(temperature);
  delay(1000);
}
  float getTemp(){

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      ds.reset_search();
      return -1000;
  }
    ds.reset();
  ds.select(addr);
  ds.write(0x44,1);

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  for (int i = 0; i < 9; i++) {
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB);
  float TemperatureSum = tempRead / 16;
  return TemperatureSum;
  
if( mySerial.available() >0 ) {
    val = mySerial.read();
    Serial.println(val);
    { 
    while (Serial.available() > 0)

  if( val == 'a' ) {
    digitalWrite(Lamp1,HIGH); statusLamp1="4"; }
  else if( val == 'b' ) {
    digitalWrite(Lamp2,HIGH); statusLamp2="5"; }
  else if( val == 'c' ) {
    digitalWrite(Lamp3,HIGH); statusLamp3="6"; }
  else if( val == 'd' ) {
    digitalWrite(Lamp4,HIGH); statusLamp4="7"; }
  else if( val == 'e' ) {
    digitalWrite(Lamp1,HIGH); statusLamp1="4";
    digitalWrite(Lamp2,HIGH); statusLamp2="5";
    digitalWrite(Lamp3,HIGH); statusLamp3="6";
    digitalWrite(Lamp4,HIGH); statusLamp4="7";
    } 
    else if( val == 'f' ) {
        for(pos = 0; pos < 90; pos += 1) { 
          myservo.write(pos); 
          delay(5); 
        }
      }
 //Lamp is off
  else if( val == 'A' ) {
    digitalWrite(Lamp1,LOW); statusLamp1="4"; }
  else if( val == 'B' ) {
    digitalWrite(Lamp2,LOW); statusLamp2="5"; }
  else if( val == 'C' ) {
    digitalWrite(Lamp3,LOW); statusLamp3="6"; }
  else if( val == 'D' ) {
    digitalWrite(Lamp4,LOW); statusLamp4="7"; }
  else if( val == 'E' ) {
    digitalWrite(Lamp1,LOW); statusLamp1="4";
    digitalWrite(Lamp2,LOW); statusLamp2="5";
    digitalWrite(Lamp3,LOW); statusLamp3="6";
    digitalWrite(Lamp4,LOW); statusLamp4="7";
    } 
    else if( val == 'F' ) {
        for(pos = 90; pos>=0; pos-=1); 
          myservo.write(pos); 
          delay(5); 
}else if( val == 'G' )
  {
    delay(500);      
    
    mySerial.println(statusLamp1+statusLamp2+statusLamp3+statusLamp4+"J"); //delay(500);  
    
    val=' ';
  }
    }
}}

Your getTemp function still has code after the return. Return ends the function. Anything after that will never ever run. It might as well not be there. Move the return statement to the end of the function is the obvious answer there.

You have other bugs in there. A lot actually. So now it's time for you to answer the questions you've been asked so we can work on those.

I changed codes now i can see on serial monitor what i send from bluetooth but again doesn’t work parts. These are new codes;

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

#include <Servo.h>
Servo myservo; 
int pos = 0; 

#include <OneWire.h>

int DS18S20_Pin = 12; 

OneWire ds(DS18S20_Pin);

#define Lamp1 4
#define Lamp2 5
#define Lamp3 6
#define Lamp4 7

char val;
String statusLamp1,statusLamp2,statusLamp3,statusLamp4;

void setup() {
    Serial.begin(9600);
  pinMode(Lamp1,OUTPUT);
  pinMode(Lamp2,OUTPUT);
  pinMode(Lamp3,OUTPUT);
  pinMode(Lamp4,OUTPUT);
      myservo.attach(3); 
      mySerial.begin(9600);
}

void loop() {
      float temperature = getTemp();
  Serial.print("Degree: ");
  Serial.println(temperature);
  delay(1000);
}
  float getTemp(){

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();

  }
    ds.reset();
  ds.select(addr);
  ds.write(0x44,1);

  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  for (int i = 0; i < 9; i++) {
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB);
  float TemperatureSum = tempRead / 16;
  
if( mySerial.available() >0 ) {
    val = mySerial.read();
    Serial.println(val);
    { 
    while (Serial.available() > 0)

  if( val == 'a' ) {
    digitalWrite(Lamp1,HIGH); statusLamp1="4"; }
  else if( val == 'b' ) {
    digitalWrite(Lamp2,HIGH); statusLamp2="5"; }
  else if( val == 'c' ) {
    digitalWrite(Lamp3,HIGH); statusLamp3="6"; }
  else if( val == 'd' ) {
    digitalWrite(Lamp4,HIGH); statusLamp4="7"; }
  else if( val == 'e' ) {
    digitalWrite(Lamp1,HIGH); statusLamp1="4";
    digitalWrite(Lamp2,HIGH); statusLamp2="5";
    digitalWrite(Lamp3,HIGH); statusLamp3="6";
    digitalWrite(Lamp4,HIGH); statusLamp4="7";
    } 
    else if( val == 'f' ) {
        for(pos = 0; pos < 90; pos += 1) { 
          myservo.write(pos); 
          delay(5); 
        }
      }
 //Lamp is off
  else if( val == 'A' ) {
    digitalWrite(Lamp1,LOW); statusLamp1="4"; }
  else if( val == 'B' ) {
    digitalWrite(Lamp2,LOW); statusLamp2="5"; }
  else if( val == 'C' ) {
    digitalWrite(Lamp3,LOW); statusLamp3="6"; }
  else if( val == 'D' ) {
    digitalWrite(Lamp4,LOW); statusLamp4="7"; }
  else if( val == 'E' ) {
    digitalWrite(Lamp1,LOW); statusLamp1="4";
    digitalWrite(Lamp2,LOW); statusLamp2="5";
    digitalWrite(Lamp3,LOW); statusLamp3="6";
    digitalWrite(Lamp4,LOW); statusLamp4="7";
    } 
    else if( val == 'F' ) {
        for(pos = 90; pos>=0; pos-=1); 
          myservo.write(pos); 
          delay(5); 
}else if( val == 'G' )
  {
    delay(500);      
    
    mySerial.println(statusLamp1+statusLamp2+statusLamp3+statusLamp4+"J"); //delay(500);  
    
    val=' ';
  }
          return TemperatureSum;

    }
}}

but again doesn't work

That could mean anything. Why don't you give us something to work with. What doesn't work? What happens? How does that compare with what you expected?

Now i succeed send info from bluetooth module, but lamps or servo motor not work with bluetooth info.

Theee you go with "not works" again. Please do be specific.