/*
Arduino | coding-help
Raid - Tuesday, February 3, 2026 10:36 PM
It's not sending the Bluetooth to my phone
But if u test it out normally it worked
But then when I integrated codes together
It just refuse to send the temperature only shows the OLED temp
TO DO: fix button
*/
#include <Wire.h>
#include <HardwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
#define DHTTYPE DHT22
const int DHT_PIN = 13;
const int TXD_PIN = 25;
const int RXD_PIN = 26;
const int BTN_PIN = 25;
const int LED_PIN = 33;
const unsigned long TWO_SEC = 2000; // DHT can only be read every 2 seconds
int oldBtnState = HIGH;
unsigned long prevTime = 0;
float h = 0.0;
float t = 0.0;
float f = 0.0;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DHT dht(DHT_PIN, DHTTYPE);
//dht DHT;
HardwareSerial BT(1);
bool checkButton() {
bool wasPressed = false;
int btnState = digitalRead(BTN_PIN); // read the pin
if (btnState != oldBtnState) { // if it changed
oldBtnState = btnState; // remember the state
if (btnState == LOW) { // was just pressed
Serial.println("Button Pressed");
wasPressed = true;
} else { // was just released
Serial.println("Button Released");
}
delay(20); // short delay to debounce button
}
return wasPressed;
}
void setup() {
Serial.begin(9600);
dht.begin();
BT.begin(9600, SERIAL_8N1, RXD_PIN, TXD_PIN);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
pinMode(LED_PIN, OUTPUT);
pinMode(BTN_PIN, INPUT_PULLUP);
// splash screen
Serial.println("\nTemperature over BT test\n");
}
void loop() {
// read the state of the pushbutton
int buttonState = checkButton();
display.clearDisplay();
display.setCursor(10, 0);
if (buttonState) {
// turn LED on:
digitalWrite(LED_PIN, HIGH);
display.println("LED ON");
display.print("Temp:");
display.println(t);
display.display();
// send temp via BT
BT.print("Temperature: ");
BT.println(t);
Serial.println("Sent temp over BT");
} else {
// turn LED off:
digitalWrite(LED_PIN, LOW);
display.println(F("LED OFF"));
display.display();
}
// read DHT every 2 seconds
if (millis() - prevTime >= TWO_SEC) {
prevTime = millis();
h = dht.readHumidity(); // read humidity
t = dht.readTemperature(); // read Celsius temp
f = dht.readTemperature(true); // read Fahrenheit temp
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temp: ");
Serial.print(t, 1);
Serial.print("°C\t");
Serial.print("Humdity: ");
Serial.print(h, 0);
Serial.println("%");
}
/*
display.clearDisplay();
display.setCursor(10, 0);
// button uses INPUT_PULLUP, state goes LOW when pressed
if (buttonState == LOW) {
// turn LED on:
digitalWrite(LED_PIN, HIGH);
display.println("LED ON");
display.print("Temp:");
display.println(t);
display.display();
// send temp via BT
BT.print("Temperature: ");
BT.println(t);
Serial.println("Sent temp over BT");
} else {
// turn LED off:
digitalWrite(LED_PIN, LOW);
display.println(F("LED OFF"));
display.display();
}
*/
}