ESP32 Chapter 6: ESP32 Bluetooth Classic

“It is better to fail in originality than to succeed in imitation”

Bluetooth Classic

#include "BluetoothSerial.h"#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11 // DHT 11
#include <LiquidCrystal_I2C.h>
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run make menuconfig to and enable it
#endif
BluetoothSerial SerialBT;DHT dht(DHTPIN, DHTTYPE);
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
void setup() {
Serial.begin(115200);
SerialBT.begin("Uji_Coba_ESP32");
lcd.println("The device started, now you can pair it with bluetooth!");
Serial.println(F("DHT11 test!"));
dht.begin();

lcd.begin(16,2);
lcd.init();
lcd.backlight();
}
void loop() {
lcd.clear();
String message;
if (SerialBT.available()) {
lcd.clear();
while(SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
}
}
lcd.setCursor(0,0);
lcd.print(message);
Serial.println(message);
delay(5000);
lcd.clear();
lcd.clear();
delay(20);
delay(2000);

float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
lcd.println(F("Failed to read from DHT sensor!"));
return;
}
SerialBT.print(F("Humidity: "));
SerialBT.print(h);
SerialBT.print(F("%"));
delay(1000);
SerialBT.print('\n');
SerialBT.print(F("Suhu: "));
SerialBT.print(t);
SerialBT.print(F(" C"));
SerialBT.print('\n');
delay(1000);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store