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:
#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:
#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