Go Down

Topic: URGENT Bluetooth Problem Serial.write() command doesn't work (Read 171 times) previous topic - next topic

dozcakir

Hello, I am trying to make a project which has a deadline very soon. I need some answers for my problem. Whenever I try to use the HC-05 BT modules and write something to the serial monitor using Serial.write(temp); it doesn't write the variable from the DHT sensor. it displays the value '-1'. However when I use the Serial.println(temp); command the value displays correctly. though the value doesnt transfer to the other BT HC-05 module because of the -1 issue.

Here is the code:

Sender:
Code: [Select]
#include <SimpleDHT.h>
#define pinDHT11 8 //data pin
SimpleDHT11 dht11;


#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX

void setup()
{
  Serial.begin(38400);
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
  delay(1000);
}

void loop()
{
  byte temperature = 0;
  byte humidity = 0;
  dht11.read(pinDHT11, &temperature, &humidity, NULL);
  int temp = temperature;
  Serial.println(temp);
 
 
 
  delay(1000);
  BTSerial.write(temp);
  Serial.write(20);
  delay(1000);
  int val= BTSerial.read();
  int val2=Serial.read();
  Serial.println("Val 1: ");
  Serial.println(val2);
  Serial.println("Val 2: ");
  Serial.println(val);
//  BTSerial.write(humidity + 200);
//  Serial.println(humidity);
//  delay(1000);
}


Reciver:
Code: [Select]

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> //https://github.com/adafruit/Adafruit-GFX-Library

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(10, 11); // RX | TX
int temperature;
int humidity;
void setup() {
  Serial.begin(38400);
  BTSerial.begin(38400);  // HC-05 default speed in AT command more
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  // init done

  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(2000);
  display.clearDisplay();
  Serial.println("Start");
}

void loop() {
  int val2 = Serial.read();
  Serial.println("Val 1: ");
  Serial.println(val2);
  Serial.println("Val 2: ");
    int val = BTSerial.read();
  Serial.println(val);
    if (val <= 99)
    {
      temperature = val;
    }

    else
    {
      humidity = (int)val-100;
     
    }
   

   display.setTextSize(1);
  display.setTextColor(WHITE);
display.setCursor(0,0);
  display.print("Temp: ");
  display.print(temperature);
  display.println("C");
  display.print("Humid: ");
  //display.print(humidity);
  display.print("Disabled");
  display.display();
    delay(1000);
    display.clearDisplay();
}
void printTextHum(int text)
{

display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);

  display.print("Humid: ");
  display.print(text);
  display.print("%");
  display.display();
}
void printTextTemp(int text)
{

  Serial.println("Display Temp");
display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);

  display.print("Temp: ");
  display.print(text);
  display.println("C");

}


I'm uploading the codes to an Arduino UNO module. FYI, my AT commands are set. Both BT modules have 38400 baud rate. The Sender is in slave mode. The reciver is in Master mode. The Master is binded to Slave's mac address and the CMODE has been set to 0 on the Master.
I NEED HELP ASAP

Robin2

What does URGENT mean?

I don't like the idea that you might expect to have your question answered before someone who is in front of you in the queue.

I suspect you should be using Serial.print() rather than Serial.write()

Have you written a simple program to read data from the DHT module and display it on the Arduino Serial Monitor without any Bluetooth complexity? If not, do that first.

...R
Serial Input Basics - simple reliable ways to receive data.
Two or three hours spent thinking and reading documentation solves most programming problems.

dozcakir

What does URGENT mean?

I don't like the idea that you might expect to have your question answered before someone who is in front of you in the queue.

I suspect you should be using Serial.print() rather than Serial.write()

Have you written a simple program to read data from the DHT module and display it on the Arduino Serial Monitor without any Bluetooth complexity? If not, do that first.

...R
I have a deadline in 10 hours thats why I had to write urgent.

I'm familiar with DHT11 sensor and I've done multiple projects with them. Also, isin't Serial.print() command only works for visual and not data transfer? I belive print command doesn't sends data to the other bluetooth module...

Robin2

I have a deadline in 10 hours thats why I had to write urgent.
That is not our problem - why did you leave your homework until the last minute?

Quote
Also, isin't Serial.print() command only works for visual and not data transfer? I belive print command doesn't sends data to the other bluetooth module...
That does not make any sense to me.

Anything you can do with Serial using a USB cable can also be done using Serial over a Bluetooth connection. Did you study the examples in the link I gave you?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up