Using a Bluetooth Thermal Printer with ESP32 and DHT 11 Sensor

https://ogbugbu-technologies.com.ng/

This tutorial will cover how to use a Bluetooth thermal printer with  an ESP32 and a DHT11 sensor. The Bluetooth thermal printer is used to print temperature and humidity data obtained from the DHT11 sensor. Using the ESP32 as a microcontroller, we can control the thermal printer and DHT11 sensor and transmit data via Bluetooth.

Components Used

Below is a list of components used in this project along with purchase links at the Golite Store:

  1. ESP32 Development Board
  2. Printer Thermal Bluetooth
  3. Sensor DHT11
  4. Push Button

System Scheme

Fritzing Scheme

Pinout Configuration

  • ESP32 -> Printer Thermal Bluetooth
    • VIN -> VCC
    • GND -> GND
    • TX2 -> RX
    • RX2 -> TX
  • ESP32 -> Sensor DHT11
    • VIN -> VCC
    • GND -> GND
    • D4 -> Data
  • ESP32 -> Push Button
    • GND -> S1
    • D18 -> S2

A brief explanation of the system schematic: The ESP32 is connected to a Bluetooth thermal printer via a UART serial connection, and also to a DHT11 sensor to read temperature and humidity data.

Program Arduino/ESP32

#include "Adafruit_Thermal.h"
#include "BluetoothSerial.h"
#include "DHT.h"

#define button 18
#define DHTPIN 4
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

BluetoothSerial SerialBT;
Adafruit_Thermal printer(&SerialBT);

uint8_t address[6] = {0x66, 0x32, 0x75, 0x31, 0x23, 0x41};
String name = "RPP02N";
char *pin = "0000";
String data;

void setup() {
  Serial.begin(115200);
  dht.begin();
  pinMode(button, INPUT_PULLUP);
  pinMode(2, OUTPUT);
  SerialBT.setPin(pin);
  SerialBT.begin("ESP32test", true);
  Serial.println("The device started in master mode, make sure remote BT device is on!");
  if (SerialBT.connect(address)) {
    Serial.println("Connected Successfully!");
  } else {
    while (!SerialBT.connected(10000)) {
      Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
      digitalWrite(2, 1);
      delay(500);
      digitalWrite(2, 0);
      delay(500);
    }
  }
  if (SerialBT.disconnect()) {
    Serial.println("Disconnected Successfully!");
  }
  SerialBT.connect();
  printer.begin();
  printer.feed(1);
  printer.setDefault();
  digitalWrite(2, 1);
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  float hif = dht.computeHeatIndex(f, h);
  float hic = dht.computeHeatIndex(t, h, false);
  if (!digitalRead(button)) {
    Serial.println("PRINT START");
    // Print the data to the thermal printer
    printer.justify('C');
    printer.println("IDRUS PROJECT x GoLite Store");
    printer.println("-----------------");
    printer.print("Temperature: ");
    printer.print(t);
    printer.println(" C");
    printer.print("Humidity: ");
    printer.print(h);
    printer.println("%");
    printer.print("Heat Index: ");
    printer.print(hic);
    printer.println(" C");
    printer.feed(2);
  }
  delay(100); // Adjust delay as needed
}

Author: Ogbugbu Technologies

Leave a Reply

Your email address will not be published. Required fields are marked *