Bluetooth in action!

Garin Ichsan Nugraha
Garin ESP32 DEVKIT
Published in
3 min readMar 2, 2020

Hello! we met again in our experiment before the mid exam begin. Here we’ll try to operate the built in bluetooth.

Here is our schematics that we’’ll use, you can try it your own.

First, we’ll use DHT11 Sensor and integrate it with Bluetooth so you’ll see the value in your smartphone’s screen, you can code the ESP32 like this :

#include "BluetoothSerial.h"
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
#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);
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32ihhza"); //Bluetooth device name
Serial.println(F("DHTxx test!"));
Serial.println("The device started, now you can pair it with bluetooth!");
dht.begin();
}
void loop() {
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
SerialBT.print(F("Humidity: "));
SerialBT.print(h);
SerialBT.print(" % \n");
SerialBT.print(F("Temperature: "));
SerialBT.print(t);
SerialBT.print((F("°C ")));
SerialBT.print((f));
SerialBT.print((F("°F")));
SerialBT.print("\n");
SerialBT.print((("Heat index: ")));
SerialBT.print((hic));
SerialBT.print((F("°C ")));
SerialBT.print((hif));
SerialBT.print((F("°F")));
SerialBT.print("\n");
SerialBT.print("\n");
/*if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
} */
delay(20);
}

Install Serial Bluetooth Terminal in your smartphone, just install it from Google Play Store if you are an Android user. Open the menu and choose your device to connect. We would use “ESP32Ihhza” as our bluetooth’s name.

If there is no problem, there will be something like this in your smartphone and serial monitor:

With the same schematic or you can just take off the DHT11, you can try this code bellow to try a cool stuff!

#include "BluetoothSerial.h"
#include <LiquidCrystal_I2C.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
LiquidCrystal_I2C lcd(0x3f, lcdColumns, lcdRows);BluetoothSerial SerialBT;String message = "";
char incomingChar;
void setup() {
Serial.begin(115200);
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
// initialize Bluetooth
SerialBT.begin("ESP32nya Ihza"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {lcd.setCursor(0, 0);
lcd.print("!send something!");

if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\0'){
message += String(incomingChar);
}
else{
message = "";
}
Serial.write(incomingChar);
}
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (message != ""){
// clears the display to print new message
lcd.clear();
// set cursor to first column, first row
lcd.setCursor(0, 1);
// print message
lcd.print(message);
}
}

Don’t forget to change the default settings in your Smartphone’s Serial Bluetooth Terminal Settings > Send > Newline to NULL.

Run the code and try to send something from your smartphone, there will be something interesting like this one we have.

have fun!

See ya in our next project!!

--

--